#include //======================================================================================= // Variables //======================================================================================= int adc[10] = {0}; //Sets up an array of 10 integers and zero's the values int avg_adc = 0; volatile int bit; volatile int i,n,first,second, zeroth; float temp; float degF; float longfirst; //======================================================================================= // Initializations for testing and proper debugging //======================================================================================= void adc_Setup(); void adc_Sam10(); void Initialize(); void Writecom(); void Writedata(); void Temperature(); //======================================================================================= // ADC10 interrupt service routine //======================================================================================= #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //======================================================================================= // ADC set-up function //======================================================================================= void adc_Setup() { ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; // + Interrupt Enable ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled } //======================================================================================= // ADC sample conversion function //======================================================================================= void adc_Sam10() { ADC10CTL0 &= ~ENC; // Disable Conversion while (ADC10CTL1 & BUSY); // Wait if ADC10 busy ADC10SA = (int)adc; // Transfers data to next array ADC10CTL0 |= ENC + ADC10SC; // Enable Conversion and conversion start __bis_SR_register(CPUOFF + GIE); // Low Power Mode 0, ADC10_ISR } //======================================================================================= // Initialize // this is to just set the LCD up for operations //======================================================================================= void Initialize(void) { Writecom(0x39); //function set Writecom(0x14); //internal osc frequency Writecom(0x56); //power control Writecom(0x6D); //follower control Writecom(0x70); //contrast Writecom(0x0f); //display on Writecom(0x06); //entry mode Writecom(0x01); //clear } //======================================================================================= // Writecom // this is used to initialize the LCD so that we can write data to it to display // characters using hex values. the LCD only knows the MSP430 is writing to it when CS // is pulled low. the LCD can only be set up when the RS pin is pulled low as well. // this is also called to set the proper position of the cursor between the 2 rows. //======================================================================================= void Writecom(int d) { P1OUT &=~ BIT1; //CS pull down P1OUT &=~ BIT2; //RS pull down for(bit=1; bit<=8; bit++) //send 8 bits { if((d&0x80)==0x80) //get only the MSB P1OUT |= BIT7; //if 1, then MOSI=1 else P1OUT &=~ BIT7; //if 0, then MOSI=0 d=(d<<1); //shift data byte left P1OUT &=~ BIT5; //CL (low) P1OUT |= BIT5; //CL (high) P1OUT &=~ BIT5; //CL (low) } P1OUT |= BIT1; //CS pull up } //======================================================================================= // Writedata // this function will be used to actually write characters to the LCD. when the RS pin // is pulled high then it know that a character is being sent to be displayed. //======================================================================================= void Writedata(int d) { P1OUT &=~ BIT1; //CS pull down P1OUT |= BIT2; //RS pull up for(bit=1; bit<=8; bit++) //send 8 bits { if((d&0x80)==0x80) //get only the MSB P1OUT |= BIT7; //if 1, then MOSI=1 else P1OUT &=~ BIT7; //if 0, then SI=0 d=(d<<1); //shift data byte left P1OUT &=~ BIT5; //CL (low) P1OUT |= BIT5; //CL (high) P1OUT &=~ BIT5; //CL (low) } P1OUT |= BIT1; //CS pull up } //======================================================================================= // Temperature // Displays the Micro-controller temerature to LCD //======================================================================================= void Temperature() { volatile char str[]={'0','1','2','3','4','5','6','7','8','9'}; Writecom(0x80); // start row 1 column 1 Writedata(0x54); // T Writedata(0x65); // e Writedata(0x6D); // m Writedata(0x70); // p Writedata(0x65); // e Writedata(0x72); // r Writedata(0x61); // a Writedata(0x74); // t Writedata(0x75); // u Writedata(0x72); // r Writedata(0x65); // e Writecom(0xc0); // start row 2 column 1 zeroth = degF / 100; longfirst = degF / 10; first = degF / 10; second = (longfirst - first) * 10; Writedata(str[zeroth]); //Displays hundredth digit Writedata(str[first]); //Displays tenth digit Writedata(str[second]); //Displays ones digit Writedata(0xDF); //Degree Symbol Writedata('F'); //F } //======================================================================================= // main //======================================================================================= void main() { WDTCTL = WDTPW + WDTHOLD; //Stop WDT P1DIR |= BIT1+BIT2+BIT5+BIT7+BIT3; Initialize(); //Set up LCD adc_Setup(); while(1) { P1OUT |= BIT3; //Set backlight on adc_Sam10(); temp = ADC10MEM; degF = ((temp - 630) * 761) / 1024; //Calculate temperature in degrees Fahrenheit __delay_cycles(50000); //Delay Temperature(); //Displays current temperature of micro-controller } }