I designed a dual thermometer with ATMega16 and LM35 for measuring the temperature of two different components. Now one component is temperature critical. I need the thermometer to sound a buzzer when the temperature reaches a threshold (say 28C).
The buzzer should beep like this:
On (5 sec), Off (15 sec) and so on.
My code is this:
(Many thanks to Mr. David Prentice, who helped me understand the basics of ADC and write the code.)
#include <avr/io.h> // special function registers #include <stdio.h> // sprintf() #define F_CPU 4000000 // you are running on 4MHz RC #include <util/delay.h> // _delay_xx() #include "lcd.h" // Fleury LCD library int readADC(char channel) { ADMUX = (3 << REFS0)|(1<<ADLAR)|(channel << MUX0); // VREF=2.56V, 8-bit, channel #0-7 is on PA0-PA7 pins _delay_us(10); // allow multiplexer to settle ADCSRA |= (1<<ADSC); // Start Conversion while (ADCSRA & (1<<ADSC)); // wait for completion return ADCH; // 8-bit result because we use ADLAR } int main(void) { char display[32], channel = 0, degree = 0xDF; // special character for degree symbol int tempC, i; ADCSRA = (1<<ADEN)|(7 << ADPS0); // Enable, div128 lcd_init(LCD_DISP_ON); while (1) { lcd_clrscr(); lcd_gotoxy(4,0); lcd_puts("The Bagho's"); _delay_ms(1100); lcd_gotoxy(0,1); lcd_puts("Dual Thermometer"); _delay_ms(5000); lcd_clrscr(); for (i=1; i<60; i++) { tempC = readADC(channel); // 10mV per C. full-scale is 2.56V i.e. 256C sprintf(display, "Channel %d %3d%cC ", channel, tempC, degree); lcd_gotoxy(0,channel); // 1st column, N'th line lcd_puts(display); // string is all printed in one go _delay_ms(500); channel++; if (channel >= 2) channel = 0; } } }
I tried a lot to get info on the internet, but it hasn't been of any help.