C 25
Termostat By Hightechrobo on 16th July 2025 09:02:25 PM
  1. #include <mega32.h>
  2.  
  3. #include <delay.h>
  4.  
  5. // Alphanumeric LCD functions
  6. #include <alcd.h>
  7. #include <stdio.h>
  8. #include <delay.h>
  9. #include <stdbool.h>
  10.  
  11. int t1,t2 ;
  12. enum main_state{
  13.     select,
  14.     temp,
  15.     set}state;
  16.    
  17. int tempc = 0;
  18. char str[17];
  19. bool b1,b2,b3;
  20. int bu_delay = 500;
  21.  
  22. int L,H;
  23.  
  24. /////////////////
  25. #define bu1 PIND.0
  26. #define bu2 PIND.1
  27. #define bu3 PIND.2
  28. // Declare your global variables here
  29.  
  30. unsigned int adc_data;
  31. // Voltage Reference: AVCC pin
  32. #define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))
  33.  
  34. // ADC interrupt service routine
  35. interrupt [ADC_INT] void adc_isr(void)
  36. {
  37. // Read the AD conversion result
  38. adc_data=ADCW;
  39. }
  40.  
  41. // Read the AD conversion result
  42. // with noise canceling
  43. unsigned int read_adc(unsigned char adc_input)
  44. {
  45. ADMUX=adc_input | ADC_VREF_TYPE;
  46. // Delay needed for the stabilization of the ADC input voltage
  47. delay_us(10);
  48. #asm
  49.     in   r30,mcucr
  50.     cbr  r30,__sm_mask
  51.     sbr  r30,__se_bit | __sm_adc_noise_red
  52.     out  mcucr,r30
  53.     sleep
  54.     cbr  r30,__se_bit
  55.     out  mcucr,r30
  56. #endasm
  57. return adc_data;
  58. }
  59.  
  60.  
  61.  
  62.  
  63. void init();
  64.  
  65.  
  66.  
  67.  
  68.  
  69. void main(void)
  70. {
  71. init();
  72. DDRC = 0x03;
  73. DDRD = 0x00;
  74. PORTD = 0x07;
  75.  
  76.    
  77.  
  78. while (1)
  79.       {
  80.       // Place your code here
  81.       //////////////////////////////timing
  82.       delay_ms(1);        
  83.       t1++;
  84.       t2++;
  85.       /////////////////////////////timing  
  86.       ////////////////////////////get temp live and check temp
  87.       tempc = read_adc(0) * 0.488 ;
  88.       if(tempc > H){PORTC.0=1;}else{PORTC.0=0;}
  89.       if(tempc < L){PORTC.1=1;}else{PORTC.1=0;}
  90.       ////////////////////////////get temp live
  91.       lcd_gotoxy(0,0);
  92.       ///////////////////////////button
  93.        
  94.         if (t1 > bu_delay) {
  95.             if (bu1 == 0) {
  96.             b1 = 1;
  97.             bu_delay = (bu_delay > 10) ? bu_delay-5 : 10;
  98.             t1 = 0;
  99.             }
  100.             else if (bu2 == 0) {
  101.             b2 = 1;
  102.             bu_delay = (bu_delay > 10) ? bu_delay-5 : 10;
  103.             t1 = 0;
  104.             }  
  105.             else {
  106.             bu_delay = 50;
  107.             }
  108.         }
  109.         if(t2 > 100){if (bu3 == 0) { b3 = 1; t2 = 0;}}
  110.      
  111.      
  112.       ///////////////////////////button
  113.       //////////////////////////display
  114.       switch(state){
  115.         case select:
  116.             lcd_puts("1.Temp");
  117.             lcd_gotoxy(0,1);
  118.             lcd_puts("2.set");
  119.            
  120.             if(b1){state = temp;lcd_clear();b1=0;}
  121.             else if(b2){state = set;lcd_clear();b2=0;}
  122.         break;  
  123.         case temp:
  124.             sprintf(str,"Temp = %3d%cC",tempc,223);
  125.             lcd_puts(str);
  126.             sprintf(str,"L:%02d%cC  H:%02d%cC",L,223,H,223);
  127.             lcd_gotoxy(0,1);
  128.             lcd_puts(str);  
  129.            if(b3){state = select;lcd_clear();b3=0;}
  130.         break;
  131.         case set:
  132.             sprintf(str,"L:%02d  H:%02d",L,H);
  133.             lcd_puts(str);
  134.             if(b1){L = (L < 99) ? L+1 : 0;;b1=0;}
  135.             else if(b2){H = (H < 99) ? H+1 : 0;b2=0;}
  136.             else if(b3){state = select;lcd_clear();b3=0;}
  137.         break;
  138.            
  139.         }
  140.       }
  141.      
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. void init(){
  149. // ADC initialization
  150. // ADC Clock frequency: 62.500 kHz
  151. // ADC Voltage Reference: AVCC pin
  152. // ADC Auto Trigger Source: ADC Stopped
  153. ADMUX=ADC_VREF_TYPE;
  154. ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
  155. SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
  156.  
  157. // Alphanumeric LCD initialization
  158. // Connections are specified in the
  159. // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
  160. // RS - PORTB Bit 1
  161. // RD - PORTB Bit 2
  162. // EN - PORTB Bit 3
  163. // D4 - PORTB Bit 4
  164. // D5 - PORTB Bit 5
  165. // D6 - PORTB Bit 6
  166. // D7 - PORTB Bit 7
  167. // Characters/line: 16
  168. lcd_init(16);
  169.  
  170. // Global enable interrupts
  171. #asm("sei")
  172. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.