Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <stdio.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "lcd.h"
- short i=0;
- #define F_CPU 16000000UL
- int BoilerOn =0;
- int hours = 9;
- int minutes = 30;
- int seconds = 0;
- int Prog_Pos = 0;
- int CalcTimerH=0;
- int CalcTimerM=0;
- int CalcTimerS=0;
- int Delay2 = 0;
- int Delay2H = 0;
- int Delay2M = 0;
- int Calibrated = 0;
- void CalcTimeTimer(int StartH, int StartM, int StartS, int EndH, int EndM, int EndS)
- {
- int StartTime= (StartH*3600)+(StartM*60)+StartS;
- int EndTime= (EndH*3600)+(EndM*60)+EndS;
- int CalcTime;
- if(StartTime > EndTime)
- {
- CalcTime = (86400 - StartTime) + EndTime;
- }
- else
- {
- CalcTime = EndTime - StartTime;
- }
- CalcTimerH = CalcTime/3600;
- CalcTimerM = (CalcTime/60)%60;
- CalcTimerS = CalcTime%60;
- }
- void DisplayTimeFormatedString(int H, int M, int S, int PosX, int PosY)
- {
- char buffer[2];
- lcd_goto_xy(PosX, PosY);
- if(H<10)
- lcd_str("0");
- itoa(H,buffer,10);
- lcd_str(buffer);
- lcd_str(":");
- if(M<10)
- lcd_str("0");
- itoa(M,buffer,10);
- lcd_str(buffer);
- lcd_str(":");
- if(S<10)
- lcd_str("0");
- itoa(S,buffer,10);
- lcd_str(buffer);
- }
- void ShowTime(void)
- {
- lcd_goto_xy(1,1);
- lcd_str("Время:");
- DisplayTimeFormatedString(hours, minutes, seconds,7,1);
- }
- void CheckBoilerTime(void)
- {
- if((hours == 5 || hours == 17) && minutes == 0 && seconds == 0 && Delay2==0)//Boiler ON
- {
- PORTD |= (1<<PD6);
- PORTD &= ~(1<<PD4);
- BoilerOn = 1;
- }
- else if((hours == 7 || hours == 19) && minutes == 0 && seconds == 0 && Delay2==0)//Boiler OFF
- {
- PORTD &= ~(1<<PD6);
- PORTD |= (1<<PD4);
- BoilerOn = 0;
- }
- if(BoilerOn == 1 && Delay2==0)
- {
- lcd_goto_xy(1,5);
- lcd_str("Выкл: ");
- if(hours >= 5 || hours <= 7)
- CalcTimeTimer(hours, minutes, seconds, 7, 0, 0);
- if(hours >= 17 || hours <= 19)
- CalcTimeTimer(hours, minutes, seconds, 19, 0, 0);
- DisplayTimeFormatedString(CalcTimerH, CalcTimerM, CalcTimerS,7,4);
- }
- if(Delay2H == hours && Delay2M == minutes && seconds == 0 && Delay2==1)
- {
- PORTD &= ~(1<<PD6);
- PORTD |= (1<<PD4);
- Delay2 = 0;
- }
- if(Delay2==1)
- {
- lcd_goto_xy(1,5);
- lcd_str("Выкл: ");
- CalcTimeTimer(hours, minutes, seconds, Delay2H, Delay2M, 0);
- DisplayTimeFormatedString(CalcTimerH, CalcTimerM, CalcTimerS,7,5);
- }
- }
- void Program (void)
- {
- if(Prog_Pos == 1)
- {
- if (bit_is_clear(PIND, 3))//Pressed
- hours++;
- if(hours>23)
- hours=0;
- lcd_write(1,3, hours,"Часы: ");
- }
- else if(Prog_Pos == 2)
- {
- if (bit_is_clear(PIND, 3))//Pressed
- minutes++;
- if(minutes>59)
- minutes=0;
- lcd_write(1,3, minutes,"Мин: ");
- }
- else
- {
- Prog_Pos = 0;
- }
- }
- void Display(void)
- {
- lcd_clear();
- ShowTime();
- if(Calibrated == 1)
- {
- CheckBoilerTime();
- Program();
- }
- else
- {
- lcd_goto_xy(4,3);
- lcd_str("Время не");
- lcd_goto_xy(3,4);
- lcd_str("настроено!");
- Delay2 = 0;
- BoilerOn = 0;
- }
- }
- //========================================================================
- // Main program
- //========================================================================
- int main(void)
- {
- //////////////////////
- DDRD = 0b01110000;
- PORTD = 0b00001111;
- DDRA = 0xFF;
- PORTA = 0x00;
- PORTD |= (1<<PD5);//Red on
- DDRD &= ~(1<<PD2);
- DDRD &= ~(1<<PD3);
- PORTD |= (1<<PD2);
- PORTD |= (1<<PD3);
- TCCR1B = (1<<CS12|1<<CS10|1<<WGM12);
- OCR1A = 15625-1;
- TIMSK = 1<<OCIE1A;
- _delay_ms(100);
- lcd_init();
- _delay_ms(100);
- lcd_contrast(0x40);
- sei();
- ///////////////
- //////////////
- while(1)
- {
- if (bit_is_clear(PIND, 2))//Pressed
- {
- Prog_Pos++;
- Calibrated = 1;
- PORTD |= (1<<PD4);//Green on
- PORTD &= ~(1<<PD5);//Red off
- Display();
- }
- if (bit_is_clear(PIND, 1) && Calibrated == 1)//Pressed
- {
- if(Delay2==0)
- {
- Delay2 = 1;
- Delay2H = hours + 2;
- PORTD |= (1<<PD6);
- PORTD &= ~(1<<PD4);
- BoilerOn = 0;
- }
- else
- {
- Delay2 = 0;
- PORTD &= ~(1<<PD6);
- PORTD |= (1<<PD4);
- BoilerOn = 0;
- }
- if(Delay2H>23)
- Delay2H = Delay2H - 23;
- Delay2M = minutes;
- Display();
- }
- if(Prog_Pos != 0)
- {
- Display();
- }
- _delay_ms(200);
- }
- }
- //========================================
- ISR(TIMER1_COMPA_vect)
- {
- if(Prog_Pos == 0)
- {
- seconds++;
- if(seconds==60)
- {
- seconds=0;
- minutes++;
- }
- if(minutes==60)
- {
- minutes=0;
- hours++;
- }
- if(hours==24)
- {
- hours=0;
- }
- Display();
- }
- }
- //========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement