- #include <mega32.h>
- #include <delay.h>
- // Alphanumeric LCD functions
- #include <alcd.h>
- #include <stdio.h>
- #include <delay.h>
- #include <stdbool.h>
- int t1,t2 ;
- enum main_state{
- select,
- temp,
- set}state;
- int tempc = 0;
- char str[17];
- bool b1,b2,b3;
- int bu_delay = 500;
- int L,H;
- /////////////////
- #define bu1 PIND.0
- #define bu2 PIND.1
- #define bu3 PIND.2
- // Declare your global variables here
- unsigned int adc_data;
- // Voltage Reference: AVCC pin
- #define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))
- // ADC interrupt service routine
- interrupt [ADC_INT] void adc_isr(void)
- {
- // Read the AD conversion result
- adc_data=ADCW;
- }
- // Read the AD conversion result
- // with noise canceling
- unsigned int read_adc(unsigned char adc_input)
- {
- ADMUX=adc_input | ADC_VREF_TYPE;
- // Delay needed for the stabilization of the ADC input voltage
- delay_us(10);
- #asm
- in r30,mcucr
- cbr r30,__sm_mask
- sbr r30,__se_bit | __sm_adc_noise_red
- out mcucr,r30
- sleep
- cbr r30,__se_bit
- out mcucr,r30
- #endasm
- return adc_data;
- }
- void init();
- void main(void)
- {
- init();
- DDRC = 0x03;
- DDRD = 0x00;
- PORTD = 0x07;
- while (1)
- {
- // Place your code here
- //////////////////////////////timing
- delay_ms(1);
- t1++;
- t2++;
- /////////////////////////////timing
- ////////////////////////////get temp live and check temp
- tempc = read_adc(0) * 0.488 ;
- if(tempc > H){PORTC.0=1;}else{PORTC.0=0;}
- if(tempc < L){PORTC.1=1;}else{PORTC.1=0;}
- ////////////////////////////get temp live
- lcd_gotoxy(0,0);
- ///////////////////////////button
- if (t1 > bu_delay) {
- if (bu1 == 0) {
- b1 = 1;
- bu_delay = (bu_delay > 10) ? bu_delay-5 : 10;
- t1 = 0;
- }
- else if (bu2 == 0) {
- b2 = 1;
- bu_delay = (bu_delay > 10) ? bu_delay-5 : 10;
- t1 = 0;
- }
- else {
- bu_delay = 50;
- }
- }
- if(t2 > 100){if (bu3 == 0) { b3 = 1; t2 = 0;}}
- ///////////////////////////button
- //////////////////////////display
- switch(state){
- case select:
- lcd_puts("1.Temp");
- lcd_gotoxy(0,1);
- lcd_puts("2.set");
- if(b1){state = temp;lcd_clear();b1=0;}
- else if(b2){state = set;lcd_clear();b2=0;}
- break;
- case temp:
- lcd_puts(str);
- lcd_gotoxy(0,1);
- lcd_puts(str);
- if(b3){state = select;lcd_clear();b3=0;}
- break;
- case set:
- lcd_puts(str);
- if(b1){L = (L < 99) ? L+1 : 0;;b1=0;}
- else if(b2){H = (H < 99) ? H+1 : 0;b2=0;}
- else if(b3){state = select;lcd_clear();b3=0;}
- break;
- }
- }
- }
- void init(){
- // ADC initialization
- // ADC Clock frequency: 62.500 kHz
- // ADC Voltage Reference: AVCC pin
- // ADC Auto Trigger Source: ADC Stopped
- ADMUX=ADC_VREF_TYPE;
- ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
- SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
- // Alphanumeric LCD initialization
- // Connections are specified in the
- // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
- // RS - PORTB Bit 1
- // RD - PORTB Bit 2
- // EN - PORTB Bit 3
- // D4 - PORTB Bit 4
- // D5 - PORTB Bit 5
- // D6 - PORTB Bit 6
- // D7 - PORTB Bit 7
- // Characters/line: 16
- lcd_init(16);
- // Global enable interrupts
- #asm("sei")
- }
Recent Pastes