MC_Module 4
MC_Module 4
MC_Module 4
Basics of Serial Communication – ATMEGA32 connection to RS232 - AVR serial port programming in
C- LCD interfacing - Keyboard interfacing - ADC interfacing – DAC interfacing - Sensor interfacing
SERIAL COMMUNICATION
Serial communication is used for transferring data between two systems located at distances
of hundreds of feet to millions of miles apart.
For serial data communication to work, the byte of data must be converted to serial bits
Using a parallel-in-serial-out shift register; then it can be transmitted over a single data line.
For long-distance data transfers, serial data communication requires a modem to
modulate (convert from Os and 1s to audio tones) and demodulate (convert from audio
tones to Os and 1s).
Serial data communication uses two methods:
Asynchronous
Synchronous.
The synchronous method transfers a block of data (characters) at a time, whereas the
asynchronous method transfers a single byte at a time.
Special IC chips are made by many manufacturers for serial data communications.
These chips are commonly referred to as UART(universal asynchronous receiver-
transmitter) and USART(universal synchronous asynchronous receiver-transmitter).
The AVR chip has a built-in USART.
Data transmission methods are:
Duplex – Data can be both transmitted and received.
Simplex – only sends data.
Duplex transmissions can be half or full duplex.
Half duplex – Data is transmitted one way at a time.
Full duplex – Data can go both ways at the same time.
Asynchronous serial communication
Is widely used for character-oriented transmissions.
Each character is placed between start and stop bits. This is called framing.
In data framing for asynchronous communications , the data ,such as ASCII characters, are
packed between a start bit and a stop bit.
The start bit is always one bit, but the stop bit can be one or two bits.
The start bit is always a 0(low), and the stop bit (s) is 1(high).
The ATmega32 has two pins that are used specifically for transferring and receiving data
serially.
These two pins are called TX and RX and are part of the PortD group (PD0 and PD1) of the
40-pinpackage.
Pin15 of the ATmega32 is assigned to TX and pin 14 is designated as RX.
These pins are TTL compatible; therefore , they require a line driver to make them RS232
compatible.
One such line driver is the MAX232chip.
MAX232
Because the RS232 is not compatible with today's microprocessors and microcontrollers, we
need a line driver (voltage converter) to convert the RS232's signals to TTL voltage levels that
will be accept able to the AVR's TX and RX pins.
One example of such a converter is MAX232 from Maxim Corp.
The MAX232 converts from RS232 voltage levels to TTL voltage levels, and vice versa.
MAX233
To save board space, some designers use the MAX233 chip from Maxim.
The MAX233 performs the same job as the MAX232 but eliminates the need for capacitors.
However, the MAX233 chip is much more expensive than the MAX232.
LCD INTERFACING
In recent years the LCD is finding wide spread use replacing LEDs (seven segment LEDs or
other multisegment LEDs). This is due to the following reasons:
2. The ability to display numbers, characters, and graphics. This is in contrast to LEDs, which are
limited to numbers and a few characters.
3. Incorporation of a refreshing controller into the LCD, there by relieving the CPU of the task of
refreshing the LCD. In contrast, the LED must be refreshed by the CPU (or in some other way)
to keep displaying the data.
Vcc : +5 V
Vss : ground
VEE :used for controlling LCD contrast
RS (Register Select)
There are two very important registers inside the LCD.-command code register, data register
The RS pin is used for their selection as follows.
If RS=0, the instruction command code register is selected, allowing the user to send
commands such as clear display, cursor at home, and so on.
If RS= 1 the data register is selected, allowing the user to send data to be
displayed on the LCD.
R/W (Read/Write)
D0-D7
The 8-bit data pins, DO-D7,are used to send information to the LCD or read the
contents of the LCD's internal registers.
Command code are:
Code Command to LCD instruction
To send data and commands to LCDs you should do the following steps. Notice that steps 2
and 3 can be repeated many times:
1. Initialize the LCD .
To initialize the LCD for 5x 7matrix and 8-bit operation, the
following sequence of commands should be sent to the LCD: Ox38, Ox0E , and 0x01.
Next we will show how to send a command to the LCD.
After power-up you should wait about 15ms before sending initializing commands to
the LCD.
2. Send any of the commands to the LCD.
The AVR connection to the LCD for 8-bit data is shown below:
In 4-bit mode, we initialize the LCD with the command code as 33, 32 and 28 in hex.
The value $28 initializes the display for5x7 matrix
33,32 tells the LCD to go into 4-bit mode.
LCD connection using 4-bit data.
The rows are connected to an output port and the columns are connected to an input port.
Columns and rows are all connected to high(VCC).
It is the function of the microcontroller to scan the keyboard continuously to detect and
identify the key pressed.
ADC INTERFACING
A non-chip ADC eliminates the need for an external ADC connection, which leaves more
pins for other I/0 activities.
The vast majority o f the AVR chips come with ADC.
ATmega32 ADC features
The ADC peripheral of the ATmega32 has the following characteristics:
1) It is a10-bit ADC.
2) It has 8 analog input channels, 7 differential input channels, and 2 differential Input
channels with optional gain of 1Ox and 200x.
3) The converted output binary data is held by two special function registers called ADCL
(AID Result Low) and ADCH (AID Result High).
4) Because the ADCH:ADCL registers give us 16bits and the ADC data out is only I0bits wide, 6
bits of the 16 are unused. We have the option of making either the upper 6 bits or the
lower 6bits unused.
5) We have three options for Vref- V ref can be connected t o AVCC (AnalogVcc), internal
2.56Vreference, or external AREF pin.
6) The conversion t i m e is dictated by the crystal frequency connected t o the XTAL pins
(Fosc) and ADPS0:2 bits.
AVR programming in Assembly and C
In the AVR microcontroller five major re gi ste rs are associated with the ADC.
They are ADCH (high data),ADCL (low data),ADCSRA (ADC Control and Status Register),
ADMUX (ADC multiplexer selection register), and SPIOR (Special Function I/O Register).
1. ADCH 4. ADMUX
2. ADCL 5. SPIOR
3. ADCSRA
1. ADMUX register
It is ADC multiplexer selection register.
This register is used to select the reference voltage, select whether the result is left adjusted
or right adjusted and select the analog inputs.
The bits of ADMUX register is shown below:
The REFS1 and REFS0 bits of the ADMUX register can be used to select the Vref source.
The AVRs have a 10-bit ADC, which means that the result is 10 bits long and cannot be
stored in a single byte.
In AVR two 8-bitregisters are dedicated to the ADC result, but only 10 of the 16 hits are used
and 6bits are unused.
You can select the position of used bits in the bytes.
If you set the ADLAR bit in ADMUX register, the result bits will be left-justified; otherwise,
the result bits will be right justified.
To program the A/D converter of the AVR, the following steps must be taken:
1. Make the pin for the selected ADC channel an input pin.
2. Turn on the ADC module of the AVR because it is disabled upon power-on reset to save
power.
3. Select the conversion speed. We use registers ADPS2:0 to select the conversion speed.
4. Select voltage reference and ADC input channels. We use the REFS0 and REFS1 bits i n the
ADMUX register to select voltage reference and the MUX4:0 bits in ADMUX to select the
ADC input channel.
5. Activate the start conversion bit by writing a one to the ADSC bit of ADCSRA.
6. Wait for the conversion to be completed by polling the ADIF bit in the ADCSRA register.
7. After the ADIF bit has gone HIGH, read the ADCL and ADCH registers to get the digital data
output. Notice that you have to read ADCL before ADCH; otherwise, the result will not be
valid.
8. If you want to read the selected channel again, go back to step5.
• We can use interrupts instead of polling to avoid tying down the microcontroller:
Upon completion of the conversion, the ADIF (A/D interrupt flag) changes to HIGH, if
ADIE=1 it will force CPU to jump to the ADC interrupt handler.
DAC INTERFACING
The digital-to- analog converter (DAC) is a device widely used to convert digital pulses to
analog signals.
There are two methods of creating a DAC:
1. binary weighted 2. R/2R ladder.
Most of the integrated circuit DACs, including the MC1408(DAC0808),use R/2R method
because it can achieve a much higher degree of precision.
The first criterion for judging a DAC is its resolution, which is a function of the number of
binary inputs.
The common ones are 8, 1 0 , and 12 b i ts.
The number of data bit inputs decides there solution of the DAC because the number of
analog output levels is equal to 2n, where n is the number of data bit inputs.
Therefore, an 8-input DAC such as the DAC0808 provides 256 discrete voltage (or current)
levels of output.
The 12-bit DAC provides 4096 discrete voltage levels.
In the MC1408 (DAC0808), the digital inputs are converted to current (Iout), and by
connecting a resistor to the lout pin, we convert the result to voltage.
Where D0 is the LSB, D7 is the MSB for the inputs and Iref is the input current.
lref Current is generally set to 2.0 mA.
If all the inputs to the DAC are high, the maximum output current is 1.99mA.
lref Current output is isolated by connecting it to an op-amp, with Rf=5k for feedback
resistor, to avoid inaccuracy.
The A/D has 10-bit resolution with a maximum of 1024 steps, and the LM34 (orLM35)
produces 10mV for every degree of temperature change.
Now, if we use the step size of 10mV, the Vout will be 10, 240mV (10.24 V) for full scale
output.
This is not acceptable even though the maximum temperature sensed by the LM34 is 300
degrees F, and the highest output we will get for the A/D is 3000mV(3.00V).
If we use the internal 2.56V reference voltage the step size would be 2.56V/1024=2.5mV.
This makes the binary output number for the ADC four times the real temperature.
We can scale it by dividing it by 4 to get the real number for temperature.