Dragon's Course Project

C code for AVR Microcontroller:

(ADC and Serial Communication)

#include<Mega163.h>
#include<Stdio.h>
#asm
.equ __lcd_port=0x15

#endasm
#include<Lcd.h>

unsigned int temp;
unsigned char buf[15], que[20];
int switch1 = 1;
int tick=0, qcounter=0, txcounter=0;
float tempcon;

interrupt [UART_TXC] void UART_transmit_isr (void) //Serial communication Interrupt
{
if (qcounter!=txcounter) UDR=que[txcounter++]; //pointer increament and send next char
}

void sendmsg(char *s)
{
qcounter=0; //preset queue counter
txcounter=1;
que[qcounter++]=0x0d;
que[qcounter++]=0x0a;
while(*s) que[qcounter++]=*s++; //push character into queue
UDR=que[0]; //send first character
}

interrupt[ADC_INT] void adc_isr(void) //ADC interrupt to convert LM34 Voltage to binary number
{
temp=ADCW;
temp=temp/64;
ADCSR=ADCSR|0x40;
}

unsigned int delay = 400;

interrupt[TIM0_OVF]void timer0_ovf_isr(void) //timer interrupt to control update rate
{TCNT0=0;

if(++tick==delay)
{

if (switch1==1)
{
tempcon=(float)temp/2.046;
temp=(int)tempcon;
sprintf(buf,"%d Deg F",temp);
lcd_gotoxy(0,1);
lcd_puts(buf);
}
if (switch1==0)
{
sprintf(buf,"%d Deg C",((temp*1.96)-32)/1.8);
lcd_gotoxy(0,1);
lcd_puts(buf);
}
sprintf(buf,"%d Deg F\n",temp);
sendmsg(buf);

tick=0;
}
}

 

void main(void)
{
DDRB=0xFF;
DDRD=0xA0;

//Serial port Settings
UCSRB=0x58;
UBRR=0x17;

lcd_init(16);


lcd_gotoxy(0,0);
lcd_putsf("Current Temp:"); //lcd_clear;
ADMUX=0x20;
ADCSR=0xCE;

TCCR0=0x03;
TCNT0=0x00;
TIMSK=0x01;
GIMSK=0xC0;
MCUCR=0x0A;
#asm("sei")
while(1);

}