Monday, February 13, 2012

DIGITAL SQUARE WAVE GENERATOR

I call this a digital square wave generator because it does not rely on a potentiometer and a resistor-capacitor circuit to generate square waves. Instead it uses a FREESCALE MC9S08QD4 MCU timer module (in edge aligned PWM mode) to output square waves from 2Hz to 20KHz at 50% duty cycle in four overlapping frequency ranges.
A rotary encoder is used to adjust the frequency output. The encoder is a BOURNS PE12LS (2-bit quadrature output), with 24 pulses per rotation and also has an illuminated shaft.
The encoder is used in conjunction with the key board interrupt to increase or decrease the value of the timer (CH0) value register (TPMC0V) from a value of 15625 cycles (minimum frequency) to 100 cycles (maximum frequency). If the value of the timer modulo registers (TPMMOD) is kept at twice the value of the timer value register (TPMC0V), a constant 50% duty cycle is created. The encoder increases or decreases the frequency output in either 100 cycles per detent (COARSE ADJ) or 1 cycle per detent (FINE ADJ).
The slight drawback to this method is that there is some loss in resolution when setting a high frequency value due to the smaller ratio between the adjustment value and the timer value register.
The four different frequency bands and also the increment value (coarse or fine) are selected using a single pushbutton in conjunction with another interrupt routine. The frequency value is maintained when switching from coarse to fine adjustment. When the frequency band is changed, the output starts at the minimum value.
OPERATION OF FREQUENCY BAND SELECT INTERRUPT (PSEUDOCODE)

START (DEFAULT)
2 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
2 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
8 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
8 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
32 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
32 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
128 Hz (RANGE) / COARSE ADJUST VALUE (100) - “FINE ADJ” LED OFF
PRESS/RELEASE “FREQ RANGE” BUTTON
128 Hz (RANGE) / FINE ADJUST VALUE (1) - “FINE ADJ” LED ON

PRESS/RELEASE “FREQ RANGE” BUTTON
RETURN TO START

Below is the timer register values used to determine the frequency output.
The MCU bus speed is fixed at 4MHz. The timer module further divides the bus speed by 64, 16, 4 or 1 to determine the timer rate. To maintain a constant 50% duty cycle, the TPMMOD register must be maintained at twice the value of the TPMCOV register.  

TPMSC = 0x0E, RANGE 2Hz TO 310Hz (DEFAULT)
4MHz / 64 (divide bus by 64) = 62500
62500 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 2Hz minimum
62500 / 200 (TPMMOD) with a 100 (TPMC0V) = 310Hz maximum

TPMSC = 0x0C, RANGE 8Hz TO 1250Hz
4MHz / 16 (divide bus by 16) = 250000
250000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 8Hz minimum
250000 / 200 (TPMMOD) with a 100 (TPMC0V) = 1250Hz maximum

TPMSC = 0x0A, RANGE 32Hz TO 5000Hz
4MHz / 4 (divide bus by 4) = 1000000
1000000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 32Hz minimum
1000000 / 200 (TPMMOD) with a 100 (TPMC0V) = 5000Hz maximum

TPMSC = 0x08, RANGE 128Hz TO 20000Hz
4MHz / 1 (divide bus by 1) = 4000000
4000000 / 31250 (TPMMOD) with a 15625 (TPMC0V) = 128Hz minimum
4000000 / 200 (TPMMOD) with a 100 (TPMC0V) = 20000Hz maximum

This board can be easily reprogrammed for other uses. The three pin header that is located on the right edge has GND, +5VDC and SIGNAL outputs that are standard servo connections. Other possibilities include an adjustable PWM output to control a motor or other pulse/burst waveforms triggered by an interrupt using the on board push button.


DIGITAL SQUARE WAVE GENERATOR SCHEMATIC (PDF)

CLICK ON FOR LARGER IMAGE


DIGITAL SQUARE WAVE GENERATOR PCB (PDF)

CLICK ON FOR LARGER IMAGE


FINISHED PCB


Monday, January 30, 2012

MC9S08QD4 MCU TIMER MODULE

MCU OVERVIEW

The FREESCALE MC9S08QD4 is a low cost 8 bit microcontroller available in an 8 pin DIP or SOIC package. It can operate from a voltage range of 2.7VDC to 5.5VDC. The QD4 version has 4kb of Flash memory and 256b of Ram.
The CPU core operates at 16MHz with a bus speed of 4MHz when operating at 5VDC. The peripherals include a four channel, 10 bit ADC, two Timer modules and a 4 pin Key Board Interrupt module.
Also, there are four general purpose input / output pins, one input only pin and one output only pin with an output current level of 10mA per pin (60mA total for package). A single wire program and debug interface is used for programming the flash memory.
The Timer channels are configured as follows;
TIM1:  2-channel timer / pulse-width modulator
TIM2:  1-channel timer / pulse-width modulator

Each channel can be used for input capture, output compare, buffered edge-aligned PWM, or buffered center-aligned PWM.

INTERNAL CLOCK SOURCE CALIBRATION

In order to create precise timer waveforms it is important to trim the 32KHz internal reference clock source that is used in the MC9S08QD4. The digital oscillator (FFL) output is post divided by two resulting in a buss clock of 4MHz. The Timer is configured to divide the buss clock by 4 resulting in a main counter running at 1MHz.

The internal reference frequency can be modified by writing a value to the ICSTRM register. This register requires a signed 8 bit value. Increasing the trim value decreases the reference frequency, while decreasing the value increases the frequency.

It is possible to set the reference manually. However, this would require configuring the timer to output a calibration frequency, such as a 1KHz or 10KHz square wave and adjusting the reference while measuring the square wave with a calibrated oscilloscope or frequency counter.

A much better and easier way is to use the BDM tool (P&E MULTILINK BDM) that is used to program the flash memory. The BUSCLK is measured and corrected by a special function during flash programming and the corresponding trim value is written to flash memory. The application just needs to copy the value to the ICSTRM trim register.  Note: There is another fine trim register named ICSSC. However, I have found that just writing to the ICSTRM register works very well.

This statement defines the trim value location in flash memory.
#define NV_ICSTRM (*(const char * _FAR)0x0000FFAF)

This statement copies the trim value from flash memory to the trim register and should be located in the main C function.
ICSTRM = NV_ICSTRM;


TIMER OVERVIEW

Both the Input Capture and Output Compare modes are based on the timers main 16 bit up counter. As an up counter, the counter counts from 0x0000h to either 0xFFFFh or a 16 bit modulus value stored in the TPMMODH:TPMMODL registers.

There is an interrupt flag associated with this main counter. The timer overflow flag (TOF) is set whenever the timer overflows from 0xFFFFh to 0x0000h.

If a modulus value is used, the timer overflow flag (TOF) is set when the timer overflows from the modulus value to 0x0000h. The (TOF) can be monitored by software polling (TOIE = 0) or can generate a hardware interrupt (TOIE = 1).

Since the MCU has an 8 bit data width and the timer is a 16 bit timer, there is a built in coherency mechanism where values are transferred only after both 8 bit bytes of a 16 bit register have been written.


INPUT CAPTURE

The Input Capture function can be used to capture the time at which an external event occurs. When an active edge occurs on the active input capture channel, the timer module latches the value of the timer counter into the timer channel value registers (TPMCnVH:TMPCnVL). Rising edges, falling edges or any edge can be selected as the active input capture edge.
An input capture event sets a flag bit (CHnF) that can be used to trigger a CPU interrupt request (CHnIE = 1).  This flag can also be monitored by software polling (CHnIE = 0). 

In operation, the difference between two consecutive captures (two rising or falling edges captured) indicates the period of the external signal or with two adjacent edges captured, the pulse width. It is important to remember that if the time between two captures is longer than the modulo setting of the timer, then the number of timer rollovers must be kept track of using the timer overflow flag (TOF). This value is then added to the capture time.

Other applications for the Input Capture mode are;

1.) Event arrival-time recording: Can be used to compare the arrival time of several consecutive input capture events.

2.) Interrupt Generation: Input Capture pins can be used as an edge sensitive interrupt source.

INPUT CAPTURE PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows the input capture portion of a program that displays the pulse width input on timer 1, channel 0 (in microseconds).
Display of 1000 = 1000 microseconds or 1 millisecond
Display of 10000 = 10000 microseconds or 10 milliseconds


These two pictures show the Input Capture measuring a 5kHz square wave. A 5kHz square wave has a period of 200uS which would correspond to a pulse width of 100 uS (as indicated on oscilloscope at 50uS/div) at 50% duty cycle.







 
OUTPUT COMPARE MODE

In Output Compare, the timer can generate timed pulses with adjustable position, polarity, duration and frequency.  The Output Compare mode uses the same 16 bit counter and channel value registers as described for the Input Capture function.  In Output Compare mode, the timer can set, clear or toggle the timer channel pin being used when the counter reaches the value located in the channel value registers (TPMCnVH:TMPCnVL).

Applications for the Output Compare function include the generation of a single pulse, a square wave or a specific time delay.

OUTPUT COMPARE PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows an output compare example that creates pulses from 1.0mS to 2.0mS with a period of 20mS that can be used to control a servo.


EDGE ALIGNED PWM

Using Pulse Width Modulation (PWM) is an excellent way to control the speed of a brush motor, the brightness of a lamp or LED and even to position a SERVO (servo control is usually referred to as PPM or Pulse Position Modulation).
Most MCU’s today have a timer module that can be programmed to provide a PWM output with little MCU overhead. The PWM output described here is a fixed period (frequency) output with an adjustable duty cycle or pulse width. By varying the pulse width to a motor, the average voltage delivered to the motor changes and so does its speed.
Edge aligned PWM mode uses the normal up counting mode of the timer counter.
The period of the PWM is based on the value in the modulus register (TPMMODH:TPMMODL). The duty cycle is determined by the value in the timer value registers (TPMCnVH:TMPCnVL).


Duty cycles from 0% to 100% are possible. A timer value of 0x0000 will result in a duty cycle of 0%. A duty cycle of 100% results when the timer value is greater than the modulus value. The PWM output can be either High-True or Low-True (inverted)   pulses.

EDGE ALIGNED PWM (PPM) PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows a PWM (PPM) servo output with a fixed period of 20mS that cycles between 1.0 milliseconds (pulse width) and ramping up to 2.0 milliseconds (pulse width)  and then ramping back down to 1.0 milliseconds (pulse width) in 50uS increments.


FREQUENCY COUNTER

A frequency counter can be created by combining the use of the Input Capture and Output Compare functions. The Output Compare function is used to create a one second time base by setting the timer modulo to 10mS and using a timer overflow flag to track the looping of this modulo 100 times (10mS x 100 = 1 second).

The Input Capture is used to track the total number of rising edges during each one second interval. An interrupt service routine is used to increase the edge count.

FREQUENCY COUNTER PROGRAM EXAMPLE (CLICK HERE FOR LINK)

DESCRIPTION
Example shows the one second time base and interrupt service routine.

These two pictures show the frequency counter with an input frequency of 1kHz.
The oscilloscope indicates a period of 1mS, (1kHz) at 200uS/div.





Conclusion: The MC9S08QD4 Timer is very versatile and can be used for numerous applications requiring precise timing and waveform generation.

Tuesday, December 20, 2011

ELECTRICITY FROM SUNLIGHT

PHOTOELECTRIC DEVICES

There are several types of devices commonly found in electronics that utilize the photoelectric effect. Some materials such as sodium or potassium have the ability to emit electrons when exposed to light. This property is called PHOTOEMISSION and is the basis for the phototube.

A second device utilizes the property of PHOTOCONDUCTIVITY and exhibits a change in resistance when exposed to light and is typically made from cadmium sulfide. Others include the phototransistor, photodiode and photo SCR.

SOLAR CELLS

The most important device, especially in today’s green movement, is called the solar cell.  A device typically made out of selenium or more likely silicon. The solar cell is considered a PHOTOVOLTAIC device and generates a voltage when exposed to light.

A solar cell starts out as an extremely pure silicon crystal that has been doped with a small amount (about 1ppm) of either Arsenic or Boron.

In a crystal of pure silicon, each silicon atom has four valence electrons and is bonded to four other silicon atoms by a shared pair of electrons (covalent bond).

Arsenic atoms have five valence electrons, four which are bound to the silicon atom. The fifth electron is fairly free to move around and this material is considered a “N” or donor type semiconductor.

Boron atoms have three valence electrons. These three electrons also bond to nearby silicon atoms. However, this leaves a shortage of one electron, leaving a positive hole in the crystal. This material is considered a “P” or acceptor type semiconductor.

When these two types of crystals (“N” type and “P” type) are joined, electrons tend to flow from “N” type (donor) to the “P” type (acceptor). However, the holes near the junction are quickly filled and the current flow ceases.

When sunlight strikes the cell, more electrons are dislodged. This creates more mobile electrons and more positive holes. Some of the free electrons and holes are produced within the depletion region (an area devoid of majority carriers) while others are generated outside of the region but also are drawn into it.

The free electrons in the region are swept from the P-type to the N-type material and the holes are drawn in the opposite direction. This produces a small voltage across the PN junction and if a load resistance is connected, a small current will flow through the load. This current flows from the N-type material through the load and back to the P-type material.

In the typical construction of a solar cell, the “N” type silicon layer is applied to a metal backplate which becomes the negative output connection. The “P” type semiconductor is diffused into the exposed face of the “N” type layer. This is known as a P-on-N device. An ohmic contact is then formed on the “P” type layer becoming the positive terminal.

These cells are also available in an N-on-P configuration. The main difference between the two configurations is that the active area of the P-on-N device becomes positively charged when exposed to light. For the N-on-P device, the active area becomes negatively charged. Also, the N-on-P cell has a lower dark reverse leakage than the P-on-N cell.

Since the output from a single solar cell is fairly small, only about 0.4VDC, multiple cells are usually connected in various series and parallel combinations to increase both the voltage and current to useful levels.

PURE CRYSTAL OF SILICON



SILICON DOPED WITH ARSENIC HAS EXTRA ELECTRONS



SILICON DOPED WITH BORON HAS POSITIVE HOLES



ELECTRON FLOW FROM DONOR TO ACCEPTOR





REFERENCE

Hill, John; Kolb, Doris. "Chemistry for Changing Times" Ninth Edition, 2001 Prentice-Hall

Wednesday, December 7, 2011

TC74 DIGITAL TEMPERATURE SENSOR

The TC74 is a digital temperature sensor that outputs an eight bit word that corresponds to temperature. The output is compatible with the IIC serial data bus interface. Temperature accuracy is +/- 2 °C from +25 °C to +85 °C and +/- 3 °C from 0 °C to +125 °C with a resolution of 1 °C. Package options are either TO-220 or SOT-23.

All TC74 sensors can be used with a supply voltage from 2.7 VDC to 5.5 VDC. However, the TC74 is available with specific supply voltages of either 3.3 VDC or 5.0 VDC. By matching the sensor to the supply voltage, better accuracy can be obtained. Also, each voltage can be obtained with eight different address values, allowing more than one sensor on a single IIC bus.

The eight bit output is in two’s complement form. Using the two’s complement number system allows a way to express both positive and negative numbers. In two’s complement, all numbers that have a most significant bit (MSB) of 0 are positive and all numbers with a MSB of 1 are negative. Therefore, the largest positive number that can be expressed as an eight bit number is 127/D or (01111111). Negative numbers consist of values from -1 to -128 with 0 being considered a positive number.

Some Examples:

+127 °C  =  0111 1111b
  +25 °C  =  0001 1001b
    +0 °C  =  0000 0000b
     -1 °C  =  1111 1111b
   -25 °C  =  1110 0111b
   -55 °C  =  1100 1001b

In order to display the correct negative value, I convert any negative numbers back into its positive number. I do this by taking the two’s complement of the negative number. Two’s complement = one’s complement + 1. The one’s complement is performed by inverting all bits, 1’s become 0’s and 0’s become 1’s.

EXAMPLE:

-1 °C   = 1111 1111 = 0000 0000 + 1 = 1
-25 °C = 1110 0111 = 0001 1000 + 1 = 25
-55 °C = 1100 1001 = 0011 0110 + 1 = 55
TC74 SERIAL PORT OPERATION

START Condition (S)
The TC74 continuously monitors the SDA and SCLK lines for a START condition (a high-to-low transition of SDA while SCLK is high) and will not respond until this condition is met.

Address Byte
Immediately following the START condition, the master must transmit the address byte to the TC74. The 7-bit address transmitted in the serial bit stream must match for the TC74 to respond with an Acknowledge (indicating the TC74 is on the bus and ready to accept data). The 8-bit in the address byte is a Read/Write bit. This bit is a ‘1’ for a read operation or ‘0’ for a write operation.

Acknowledge (ACK)
Acknowledge (ACK) provides a positive handshake between the master and the TC74. The master releases SDA after transmitting 8 bits. The master then generates a ninth clock cycle to allow the TC74 to pull the SDA line low. This action acknowledges that the TC74 successfully received the previous 8 bits of data or address.

Data Byte
After a successful acknowledge (ACK) of the address byte, the master must transmit the data byte to be written, or clock-in the data to be read. An acknowledge (ACK) will be generated after a successful write of a data byte into the TC74.

STOP Condition (P)
Communications must be terminated by a STOP condition (a low-to-high transition of SDA while SCLK is high). The STOP condition must be communicated by the master to the TC74.

The following is a C function that is used to send and receive data from the TC74.
It was written for the FREESCALE MC9S08 series 8 bit microcontrollers.

IIC REGISTER BIT DEFINITIONS

IICC:    IIC Control Register
IICD:    IIC Data I/O Register

IICC_TX:         Transmit Mode Select: 0 = Receive, 1 = Transmit
IICC_TXAK:    Transmit Acknowledge Enable
IICS_IICIF:      IIC Interrupt Flag
IICC_RSTA:     Repeat Start
IICC_MST:       Master Mode Select
IICC_RXAK:    Receive Acknowledge


IIC TC74 TEMPERATURE SENSOR DATA FUNCTION
void iic_tmpdata(void)

{
//GET TEMPERATURE DATA FROM SENSOR
//WRITE - SEND START BYTE
IICC_TX = 1;
IICC_TXAK = 0;
IICC = 0xB0;
IICD = 0x96;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//WRITE - SEND COMMAND BYTE
IICD = 0x01;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//WRITE - SEND CONFIGURATION BYTE
//$00 = normal mode, $80 = standby mode
IICD = 0x00;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//WRITE - STOP AND DELAY BEFORE READ
IICC_MST = 0;
iic_wait (50);

//READ - SEND SLAVE ADDRESS
IICC = 0xB0;
IICD = 0x96;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//READ - SEND COMMAND BYTE (TEMPERATURE)
IICD = 0x00;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//READ - REPEAT START COMMAND
IICC_RSTA = 1;
IICD = 0x97;
while (!IICS_IICIF);
IICS_IICIF = 1;
while (IICS_RXAK);

//READ - PERFORM DUMMY READ
IICC_TX = 0;
IICC_TXAK = 1;
TC74_data = IICD;
while (!IICS_IICIF);
IICS_IICIF = 1;

//READ - STOP
IICC_MST = 0;
iic_wait (50);

//READ TEMPERATURE
TC74_data = IICD;
}

CIRCUIT BOARD

A relatively simple circuit board was designed to display the temperature data from the TC74 sensor. The microcontroller was a FREESCALE MC9S08QG8 operating at 3.3 VDC. Two LTS3401 (“0.8”) common anode green LED displays were used along with a CAT4016 constant current LED driver to drive the display segments.

The CAT4016 can drive up to 16 LEDS, making it perfect for two seven segment displays along with a decimal point on each display. Current limiting for the LED’s requires only a single 4K resistor. A 4K resistor will set the LED current at 15 mA per segment. The displays are directly driven and not multiplexed. I prefer direct drive as it lowers CPU overhead and power supply bus noise.

For a negative sign, I usually prefer to use the middle (“g”) segment on an LED display. Unfortunately, doing so in this case, with only two digits available,  would limit the minimum temperature display to only -9 °C. I decided instead to use a 2mm x 7mm rectangular green LED just to the left of the tens display as a negative indicator. This allows the minimum display to be -99 °C. The negative LED was controlled by an available port pin on the MCU.

The TC74 was connected to the PCB using a 2x5 pin connector made by 3M.
This would allow using a four conductor cable to connect the sensor for remote operation.


The single sided PCB measures 3in x 3in and was homemade using a photo negative process.

TC74 PCB SCHEMATIC

CLICK ON FOR LARGER IMAGE



TC74 PCB LAYOUT
TC74 PCB LAYOUT PDF


CLICK ON FOR LARGER IMAGE


FINISHED PCB


CLICK ON FOR LARGER IMAGE
 

TEMPERATURE DISPLAY ROUTINE

The display loop routine shows the temperature in both °C and °F. The example below indicates a temperature of  -10 °C and 14 °F. The program also includes a display test that lights all the display segments and the two indicator LED's when the PCB is powered up.

An alternate program could be easily created that stores the highest maximum temperature and also the lowest minimum temperature. The PCB has a PB switch which could control an interrupt (flag) that causes the display loop to display the high / low temperature as “HI” / (temp) and “Lo” / (temp) in both °C and °F. This is something I have done with a similar TC74 design that uses an LCD display.


CLICK ON FOR LARGER IMAGE

REFERENCE: MICROCHIP TC74 DATASHEET  http://www.microchip.com/