308 8. ATMEL AVR OPERATING PARAMETERS AND INTERFACING
There are 16 different LEDs to drive for the wind speed indicator. Rather than use 16 microcontroller
pins, the binary value of the LED for illumination is provided to the 74154 4-to-16 decoder. The
decoder provides a one cold” output as determined by the binary code provided by PORTC[5:2]
of the ATmega328 (Analog In [5:2] on the Arduino UNO R3). For example, when A
16
is provided
to the 74154 input, output /Y 10 is asserted low, while all other outputs remain at logic high. The
74154 is from the standard TTL family. It has sufficient current sink capability (I
OL
= 16 mA)to
meet the current requirements of an LED (V
f
= 1.5 VDC, I
f
= 15 mA).
8.13.4 UML ACTIVITY DIAGRAMS
The UML activity diagram for the main program is provided in Figure 8.35. After initializing the
subsystems, the program enters a continuous loop where temperature and wind direction is sensed
and displayed on the LCD and the LED display. The system then enters a delay to set how often
the temperature and wind direction parameters are updated. We have you construct the individual
UML activity diagrams for each function as an end of chapter exercise.
8.13.5 MICROCONTROLLER CODE
//include file***************************************************************
#include <iom328pv.h>
//function prototypes********************************************************
void initialize_ports(void);
void initialize_ADC(void);
void temperature_to_LCD(unsigned int ADCValue);
unsigned int readADC(unsigned char);
void LCD_init(void);
void putChar(unsigned char);
void putcommand(unsigned char);
void display_data(void);
void convert_wind_direction(unsigned int);
void delay(unsigned int number_of_6_55ms_interrupts);
void init_timer0_ovf_interrupt(void);
void timer0_interrupt_isr(void);
//interrupt handler definition
#pragma interrupt_handler timer0_interrupt_isr:17
//door profile data
//Global variables******************************************************
unsigned int temperature, wind_direction;
8.13. EXTENDED EXAMPLE 5: WEATHER STATION 309
weather vane
Vcc = 5 V
GND-1
VDD-2
Vo - 3
RS-4
R/W-5
E-6
DB0-7
DB1-8
DB2-9
DB3-10
DB4-11
DB5-12
DB6-13
DB7-14
Vcc
10K
AND671GST
data
enable
RS:command/data
8
data
LM34
Vcc = 5 V
75
1uF
temperature sensor
S
E
W
LED0
LED4
LED8
LED12
N
74154
4:16
decoder
G1
G2
(1)Y0
(2)Y1
(3)Y2
(17)Y15
LED0
Vcc = 5 VDC
+
LED15
LED0
LED1
LED2
Vcc = 5 VDC
+
LED15
A(23)
B(22)
C(21)
D(20)
Vcc = 5 VDC
(24)
(12)
220
220
Figure 8.34: Circuit diagram for weather station.
310 8. ATMEL AVR OPERATING PARAMETERS AND INTERFACING
include files
global variables
function prototypes
initialize ADC
initialize LCD
while(1)
convert temp
convert wind direction
display temp &
wind direction on LCD
display wind direction
on LED
delay(desired_update_time)
Figure 8.35: Weather station UML activity diagram.
8.13. EXTENDED EXAMPLE 5: WEATHER STATION 311
unsigned int binary_weighted_voltage_low, binary_weighted_voltage_high;
unsigned char dir_tx_data;
void main(void)
{
initialize_ports();
initialize_ADC();
LCD_init();
init_timer0_ovf_interrupt();
while(1)
{
//temperature data: read -> display -> transmit
temperature = readADC(0x00); //Read Temp - LM34
temperature_to_LCD (temperature); //Convert and display temp on LCD
//wind direction data: read -> display -> transmit
wind_direction = readADC(0x01); //Read wind direction
convert_wind_direction(wind_direction); //Convert wind direction
//delay 15 minutes
delay(13740):
}
}
//***********************************************************************
void initialize_ports()
{
DDRB = 0x03; //PORTB[1:0] as outputs
DDRC = 0x3c; //PORTC[5:2] as outputs
DDRD = 0xFF; //PORTD[7:0] as outputs
}
//***********************************************************************
void initialize_ADC()
{
ADMUX = 0; //select channel 0
312 8. ATMEL AVR OPERATING PARAMETERS AND INTERFACING
//enable ADC and set module enable ADC
ADCSRA = 0xC3; //set module prescalar to 8
while(!(ADCSRA & 0x10)); //Wait until conversion is ready
ADCSRA |= 0x10; //Clear conversion ready flag
}
//***********************************************************************
unsigned int readADC(unsigned char channel)
{
unsigned int binary_weighted_voltage, binary_weighted_voltage_low;
unsigned int binary_weighted_voltage_high;//weighted binary voltage
ADMUX = channel; //Select channel
ADCSRA |= 0x43; //Start conversion
//Set ADC module prescalar
//to 8 critical for
//accurate ADC results
while (!(ADCSRA & 0x10)); //Check if conversion is ready
ADCSRA |= 0x10; //Clear conv rdy flag set the bit
binary_weighted_voltage_low = ADCL; //Read 8 low bits first (important)
//Read 2 high bits, multiply by 256
binary_weighted_voltage_high = ((unsigned int)(ADCH << 8));
binary_weighted_voltage=binary_weighted_voltage_low+binary_weighted_voltage_high;
return binary_weighted_voltage; //ADCH:ADCL
}
//***********************************************************************
//LCD_Init: initialization for an LCD connected in the following manner:
//LCD: AND671GST 1x16 character display
//LCD configured as two 8 character lines in a 1x16 array
//LCD data: bus PORTD[7:0]
//LCD RS: PORTB[1]
//LCD E: PORTB[0]
//***********************************************************************
void LCD_init(void)
{
delay(1);

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.