Advertisement
Guest User

Untitled

a guest
Jan 21st, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1.  
  2. #define F_CPU 16000000UL
  3.  
  4. #include <avr/io.h>
  5. #include <stdio.h>
  6.  
  7. #include <util/delay.h>
  8. #include <avr/interrupt.h>
  9. #include "lcd.h"
  10.  
  11. short i=0;
  12.  
  13. #define F_CPU 16000000UL
  14.  
  15. int BoilerOn =0;
  16.  
  17. int hours = 9;
  18. int minutes = 30;
  19. int seconds = 0;
  20.  
  21. int Prog_Pos = 0;
  22.  
  23. int CalcTimerH=0;
  24. int CalcTimerM=0;
  25. int CalcTimerS=0;
  26.  
  27. int Delay2 = 0;
  28.  
  29. int Delay2H = 0;
  30. int Delay2M = 0;
  31.  
  32. int Calibrated = 0;
  33.  
  34. void CalcTimeTimer(int StartH, int StartM, int StartS, int EndH, int EndM, int EndS)
  35. {
  36.     int StartTime= (StartH*3600)+(StartM*60)+StartS;
  37.     int EndTime= (EndH*3600)+(EndM*60)+EndS;
  38.     int CalcTime;
  39.    
  40.     if(StartTime > EndTime)
  41.     {
  42.         CalcTime = (86400 - StartTime) + EndTime;
  43.     }
  44.     else
  45.     {
  46.         CalcTime = EndTime - StartTime;
  47.     }
  48.     CalcTimerH = CalcTime/3600;
  49.     CalcTimerM = (CalcTime/60)%60;
  50.     CalcTimerS = CalcTime%60;
  51. }
  52.  
  53. void DisplayTimeFormatedString(int H, int M, int S, int PosX, int PosY)
  54. {
  55.     char buffer[2];
  56.    
  57.     lcd_goto_xy(PosX, PosY);
  58.    
  59.  
  60.     if(H<10)
  61.     lcd_str("0");
  62.        
  63.     itoa(H,buffer,10);
  64.     lcd_str(buffer);
  65.     lcd_str(":");
  66.    
  67.     if(M<10)
  68.     lcd_str("0");
  69.    
  70.     itoa(M,buffer,10);
  71.     lcd_str(buffer);
  72.     lcd_str(":");
  73.    
  74.     if(S<10)
  75.     lcd_str("0");
  76.    
  77.     itoa(S,buffer,10);
  78.     lcd_str(buffer);
  79. }
  80.  
  81.  
  82.  
  83. void ShowTime(void)
  84. {
  85.     lcd_goto_xy(1,1);
  86.     lcd_str("Время:");
  87.  
  88.     DisplayTimeFormatedString(hours, minutes, seconds,7,1);
  89.  
  90. }
  91.  
  92. void CheckBoilerTime(void)
  93. {
  94.     if((hours == 5 || hours == 17) && minutes == 0 && seconds == 0 && Delay2==0)//Boiler ON
  95.     {
  96.         PORTD |= (1<<PD6);
  97.         PORTD &= ~(1<<PD4);
  98.         BoilerOn = 1;
  99.        
  100.     }
  101.     else if((hours == 7 || hours == 19) && minutes == 0 && seconds == 0 && Delay2==0)//Boiler OFF
  102.     {
  103.         PORTD &= ~(1<<PD6);
  104.         PORTD |= (1<<PD4);
  105.         BoilerOn = 0;
  106.     }
  107.    
  108.     if(BoilerOn == 1 && Delay2==0)
  109.     {
  110.         lcd_goto_xy(1,5);
  111.         lcd_str("Выкл: ");
  112.        
  113.         if(hours >= 5 || hours <= 7)
  114.             CalcTimeTimer(hours, minutes, seconds, 7, 0, 0);
  115.         if(hours >= 17 || hours <= 19)
  116.             CalcTimeTimer(hours, minutes, seconds, 19, 0, 0);
  117.        
  118.         DisplayTimeFormatedString(CalcTimerH, CalcTimerM, CalcTimerS,7,4);
  119.     }
  120.    
  121.     if(Delay2H == hours && Delay2M == minutes && seconds == 0 && Delay2==1)
  122.     {
  123.         PORTD &= ~(1<<PD6);
  124.         PORTD |= (1<<PD4);
  125.         Delay2 = 0;
  126.     }
  127.  
  128.     if(Delay2==1)
  129.     {      
  130.         lcd_goto_xy(1,5);
  131.         lcd_str("Выкл: ");
  132.        
  133.         CalcTimeTimer(hours, minutes, seconds, Delay2H, Delay2M, 0);
  134.        
  135.         DisplayTimeFormatedString(CalcTimerH, CalcTimerM, CalcTimerS,7,5);
  136.     }
  137. }
  138.  
  139. void Program (void)
  140. {
  141.     if(Prog_Pos == 1)
  142.     {
  143.         if (bit_is_clear(PIND, 3))//Pressed
  144.         hours++;
  145.        
  146.         if(hours>23)
  147.         hours=0;
  148.        
  149.         lcd_write(1,3, hours,"Часы: ");
  150.     }
  151.     else if(Prog_Pos == 2)
  152.     {
  153.         if (bit_is_clear(PIND, 3))//Pressed
  154.         minutes++;
  155.        
  156.         if(minutes>59)
  157.         minutes=0;
  158.        
  159.         lcd_write(1,3, minutes,"Мин: ");
  160.     }
  161.     else
  162.     {
  163.         Prog_Pos = 0;
  164.     }
  165. }
  166.  
  167. void Display(void)
  168. {
  169.     lcd_clear();
  170.     ShowTime();
  171.     if(Calibrated == 1)
  172.     {
  173.         CheckBoilerTime();
  174.         Program();
  175.     }
  176.     else
  177.     {
  178.         lcd_goto_xy(4,3);
  179.         lcd_str("Время не");
  180.         lcd_goto_xy(3,4);
  181.         lcd_str("настроено!");
  182.         Delay2 = 0;
  183.         BoilerOn = 0;
  184.     }
  185. }
  186. //========================================================================
  187. //  Main program
  188. //========================================================================
  189. int main(void)
  190. {  
  191.     //////////////////////
  192.     DDRD  = 0b01110000;
  193.     PORTD = 0b00001111;
  194.    
  195.     DDRA = 0xFF;
  196.     PORTA = 0x00;
  197.    
  198.     PORTD |= (1<<PD5);//Red on
  199.    
  200.     DDRD &= ~(1<<PD2);
  201.     DDRD &= ~(1<<PD3);
  202.     PORTD |= (1<<PD2);
  203.     PORTD |= (1<<PD3);
  204.  
  205.    
  206.  
  207.     TCCR1B = (1<<CS12|1<<CS10|1<<WGM12);
  208.     OCR1A = 15625-1;
  209.     TIMSK = 1<<OCIE1A;
  210.  
  211.     _delay_ms(100);
  212.     lcd_init();
  213.     _delay_ms(100);
  214.     lcd_contrast(0x40);
  215.    
  216.     sei();
  217.    
  218.     ///////////////
  219.    
  220.    
  221.     //////////////
  222.    
  223.     while(1)
  224.     {      
  225.         if (bit_is_clear(PIND, 2))//Pressed
  226.         {
  227.             Prog_Pos++;
  228.             Calibrated = 1;
  229.             PORTD |= (1<<PD4);//Green on
  230.             PORTD &= ~(1<<PD5);//Red off
  231.             Display();
  232.         }
  233.        
  234.         if (bit_is_clear(PIND, 1) && Calibrated == 1)//Pressed
  235.         {
  236.             if(Delay2==0)
  237.             {
  238.                 Delay2 = 1;
  239.                 Delay2H = hours + 2;
  240.                
  241.                 PORTD |= (1<<PD6);
  242.                 PORTD &= ~(1<<PD4);
  243.                
  244.                 BoilerOn = 0;  
  245.             }
  246.             else
  247.             {
  248.                 Delay2 = 0;
  249.                
  250.                 PORTD &= ~(1<<PD6);
  251.                 PORTD |= (1<<PD4);
  252.                 BoilerOn = 0;
  253.             }
  254.                
  255.             if(Delay2H>23)
  256.                 Delay2H = Delay2H - 23;
  257.                
  258.             Delay2M = minutes;
  259.             Display();
  260.         }
  261.        
  262.         if(Prog_Pos != 0)
  263.         {
  264.             Display();
  265.         }
  266.        
  267.         _delay_ms(200);
  268.     }
  269. }
  270.  
  271.  
  272. //========================================
  273. ISR(TIMER1_COMPA_vect)
  274. {
  275.     if(Prog_Pos == 0)
  276.     {
  277.         seconds++;
  278.         if(seconds==60)
  279.         {
  280.             seconds=0;
  281.             minutes++;
  282.         }
  283.         if(minutes==60)
  284.         {
  285.             minutes=0;
  286.             hours++;
  287.         }
  288.         if(hours==24)
  289.         {
  290.             hours=0;
  291.         }
  292.  
  293.         Display();
  294.     }
  295. }
  296.  
  297.  
  298.  
  299. //========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement