O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.2. WEATHER STATION 331
The result is placed in the corresponding ADC12MEMx
//register.
//***********************************************************************
unsigned int readADC(unsigned char channel)
{
ADC12CTL0 = ADC12ON+ADC12SHT0_2; //Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP; //Use sampling timer
ADC12MCTL0 = ADC12SREF_0+channel;//Vr+ = AVcc and Vr-=AVss and
//Input channel select
ADC12CTL0 |= ADC12ENC; //Enable conversions
ADC12CTL0 |= ADC12SC; //Start conversion-software trigger
while(ADC12IFG == 0x0000); //Wait for conversion complete flag
}
//***********************************************************************
void InitUART_A0(void)
{
P3SEL = 0x10; //P3.4 USCI_A0 TXD
P3DIR = 0x10; //P3.4 output
UCA0CTL1 |= UCSWRST; //Put USCI state machine in reset during
//configuration
UCA0CTL1 |= UCSSEL_1; //Clock source select: CLK = ACLK
UCA0BR0 = 0x03; //Set Baud rate: 32kHz/9600=3.41
UCA0BR1 = 0x00;
UCA0CTL0 = 0x00; //Configure UART:
// -parity disabled, LSB TX first
// -8 data bits, 1 stop bit
// -UART mode, async
UCA0CTL1 &= ˜UCSWRST; //Initialize USCI state machine
}
//***********************************************************************
void UART_TX_A0(unsigned char data)
{
while (!(UCA0IFG&UCTXIFG)); //USCI_A0 TX buffer ready?
UCA0TXBUF = data; // TX data
332 10. SYSTEM LEVEL DESIGN
}
//***********************************************************************
void putChar(unsigned char character)
{
UART_TX_A0(character);
}
//***********************************************************************
//putcommand: to send command a special command character (either 0xFE or
//0x7C) must precede command.
A command may then be sent.
//***********************************************************************
void putcommand(unsigned char command)
{
UART_TX_A0(0xFE);
UART_TX_A0(command);
}
//***********************************************************************
void temperature_to_LCD(unsigned int ADCValue)
{
float voltage,temperature;
unsigned int tens, ones, tenths;
//Convert voltage to floating point
//Isolate each numerical digit for
//display on LCD
voltage = (float)ADCValue*3.3/4096.0;
temperature = voltage*100;
tens = (unsigned int)(temperature/10);
ones = (unsigned int)(temperature-(float)tens*10);
tenths = (unsigned int)(((temperature-(float)tens*10)-(float)ones)*10);
//Convert isolated digit to ASCII
putChar((unsigned char)(tens)+48);
putChar((unsigned char)(ones)+48);
putChar(’.’);
10.2. WEATHER STATION 333
putChar((unsigned char)(tenths)+48);
putChar(’F’);
}
//***********************************************************************
void convert_wind_direction(unsigned int wind_dir_int)
{
float wind_dir_float;
//convert wind direction to float
wind_dir_float = ((float)wind_dir_int)/4096.0) * 3.3;
//N - LED0
if((wind_dir_float <= 0.103)||(wind_dir_float > 3.199))
{
putcommand(0x14); //move cursor to right
putchar(’N’); //LCD displays: N
P4OUT = 0x00; //illuminate LED 0
}
//NNE - LED1
if((wind_dir_float > 0.103)||(wind_dir_float <= 0.309))
{
putcommand(0x14); //move cursor to right
putchar(’N’); //LCD displays: NNE
putchar(’N’);
putchar(’E’);
P4OUT = 0x01; //illuminate LED 1
}
//NE - LED2
if((wind_dir_float > 0.309)||(wind_dir_float <= 0.516))
{
putcommand(0x14); //move cursor to right
putchar(’N’); //LCD displays: NE
putchar(’E’);
P4OUT = 0x02; //illuminate LED 2
}
334 10. SYSTEM LEVEL DESIGN
//ENE - LED3
if((wind_dir_float > 0.516)||(wind_dir_float <= 0.722))
{
putcommand(0x14); //move cursor to right
putchar(’E’); //LCD displays: NNE
putchar(’N’);
putchar(’E’);
P4OUT = 0x03; //illuminate LED 3
}
//E - LED4
if((wind_dir_float > 0.722)||(wind_dir_float <= 0.928))
{
putcommand(0x14); //move cursor to right
putchar(’E’); //LCD displays: E
P4OUT = 0x04; //illuminate LED 4
}
//ESE - LED5
if((wind_dir_float > 0.928)||(wind_dir_float <= 1.134))
{
putcommand(0x14); //move cursor to right
putchar(’E’); //LCD displays: ESE
putchar(’S’);
putchar(’E’);
P4OUT = 0x05; //illuminate LED 5
}
//SE - LED6
if((wind_dir_float > 1.134)||(wind_dir_float <= 1.341))
{
putcommand(0x14); //move cursor to right
putchar(’S’); //LCD displays: SE
putchar(’E’);
P4OUT = 0x06; //illuminate LED 6
}
//SSE - LED7
if((wind_dir_float > 1.341)||(wind_dir_float <= 1.547))
10.2. WEATHER STATION 335
{
putcommand(0x14); //move cursor to right
putchar(’S’); //LCD displays: SSE
putchar(’S’);
putchar(’E’);
P4OUT = 0x07; //illuminate LED 7
}
//S - LED8
if((wind_dir_float > 1.547)||(wind_dir_float <= 1.753))
{
putcommand(0x14); //move cursor to right
putchar(’S’); //LCD displays: S
P4OUT = 0x08; //illuminate LED 8
}
//SSW - LED9
if((wind_dir_float > 1.753)||(wind_dir_float <= 1.959))
{
putcommand(0x14); //move cursor to right
putchar(’S’); //LCD displays: SSW
putchar(’S’);
putchar(’W’);
P4OUT = 0x09; //illuminate LED 9
}
//SW - LED10 (A)
if((wind_dir_float > 1.959)||(wind_dir_float <= 2.166))
{
putcommand(0x14); //move cursor to right
putchar(’S’); //LCD displays: SW
putchar(’W’);
P4OUT = 0x0a; //illuminate LED 10 (A)
}
//WSW - LED11 (B)
if((wind_dir_float > 2.166)||(wind_dir_float <= 2.372))
{
putcommand(0x14); //move cursor to right

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required