Třeba tam někdo najde chybu hned při prvním ohledání. Ono jak do toho člověk hledí příliš dlouho tak tu chybu ani nevidí.
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>
#include "lcd.h"
//DEsky
#define mala_dps 0
#define vetsi_dps 1
#define nejvetsi_dps 6
#define oboustrana 7
#define viko 6
#define tlacitko 3
//prednastaveni casu
volatile unsigned char hodiny=0;
volatile unsigned char minuty=2;
volatile unsigned char sekundy=30;
//globalni a jiné proměnné
unsigned char temp;
char buffer[16];
int jednou=0; //promenna pro zobrazeni upozorneni na nezavrene viko
volatile char encoder=0; //hodnota enkoderu
volatile char rozmer_dps=0;
volatile char oboustr=0;
volatile unsigned char hod=0, min=0, sek=0, pom_h=0, pom_m=0, pom_s=0;
//============================================================================
//==============================INICIALIZACE==================================
//============================================================================
void inicializace()
{
hod=hodiny; min=minuty; sek=sekundy;
//nastaveni vstupnich pinu portu D
DDRD &= ~(1 << PD0); //enkoder
DDRD &= ~(1 << PD2); //enkoder
DDRD &= ~(1 << tlacitko);
DDRD &= ~(1 << viko);
//povoleni vnitřních pull-up odporu
PORTD |= (1 << PD0);
PORTD |= (1 << PD2);
PORTD |= (1 << tlacitko);
PORTD |= (1 << viko);
//zakazani vnitrnich pull-up odporu
PORTC &= ~(1 << mala_dps);
PORTC &= ~(1 << vetsi_dps);
PORTC &= ~(1 << nejvetsi_dps);
PORTC &= ~(1 << oboustrana);
//nastaveni vystupnich pinu portu D
DDRC |= (1 << mala_dps) | (1 << vetsi_dps) | (1 << nejvetsi_dps) | (1 << oboustrana);
//nastaveni PULL-UP odporu
//PORTC |= (1 << mala_dps) | (1 << vetsi_dps) | (1 << nejvetsi_dps) | (1 << oboustrana);
//nastaveni preruseni od enkoderu
MCUCR|=(1<<ISC01)|(1<<ISC00); //Nastaveni INT0 na "rising" signal na pinu PD2
GICR |=(1<<INT0 ); //povoleni preruseni
//nastaveni preruseni od citace casovace 1 (cas)
TCCR1B |= (1 << CS12); //preddelička 256 (16us)
TIMSK |= (1 << TOIE1); //prerušeni pri pretečení TCNT1
//nastaveni preruseni od citace casovace 0 (tlacitko)
TCCR0 |= (1<<WGM01)|(1<<CS02)|(1<<CS00); //CTC mode, preddelicka 1024 (64us)
OCR0=255; //cca 16,384ms pro kontrolu tlačítka
TIMSK |= (1 << OCIE0);
//Nataveni displeje a prvni zobrazeni
lcd_init(LCD_DISP_ON); //Init LCD
lcd_clrscr();
//lcd_init(LCD_DISP_ON_CURSOR_BLINK); //blikání kurzoru
sei(); //povoleni přerušení
}
//============================================================================
//==================================PRERUSENI=================================
//============================================================================
volatile unsigned char tlac_prodleva,tlac_present;
volatile long int cas=0;
volatile unsigned int pomoc=5, pomoc_2=0;
volatile unsigned char flag=0, hotovo=0, zadej_cas=0; //stisk tlacitka
ISR (INT0_vect) //ratační enkoder (vlevo/vpravo) ======= PD0 & PD2
{
cli();
if (((PIND&0x01)==0x00)&&(encoder<9999)) encoder=encoder+1; //inc
if (((PIND&0x01)==0x01)&&(encoder > 0 )) encoder=encoder-1; //dec
sei();
}
ISR (TIMER1_OVF_vect){
TCNT1 = 3036; //preteceni kazdou 1s
if ( ((PIND & (1 << viko))==0) && (flag>=5)){
if (cas!=0){cas--;}
else {
void (*fn_ptr) (void) = (void (*) (void)) 0; //reset MCU
fn_ptr();
}
}
}
ISR(TIMER0_COMP_vect){ //přerušení od čítače časovače 0
//ošetření tlačítka a jeho zákmitů
if((PIND & (1 << tlacitko))){
if(tlac_prodleva<20) tlac_prodleva++;
if(tlac_prodleva==19) tlac_present=1;
}
else{
if(tlac_prodleva==20){ //sem se dostane pouze pri stisku tlacitka
if (flag==0){rozmer_dps=encoder;encoder=0;} //ulozeni rozmeru DPS
if (flag==1){oboustr=encoder;encoder=hodiny;} //ulozeni oboustrane DPS
if (flag==2){encoder=minuty;} //resetuje hodnotu v enkoderu
if (flag==3){encoder=sekundy;}
if (flag==4){encoder=0;}
flag++; //zvyseni pri stisku tlacitka
}
tlac_prodleva=0;
}
//nastaveni casu
if (flag==2){ //nastaveni hodin
hodiny=encoder;
if (hodiny>=24){hodiny=0;encoder=0;}
}
if (flag==3){ //nastaveni minut
minuty=encoder;
if (minuty>=60){minuty=0;encoder=0;}
}
if (flag==4){ //nastaveni sekund
sekundy=encoder;
if (sekundy>=60){sekundy=0;encoder=0;}
}
}
//============================================================================
//==================================FUNKCE====================================
//============================================================================
/****************************************************
****************fce pro zadani velikosti DPS*********
****************************************************/
void velikost_DPS(void)
{
if (flag==0){
lcd_gotoxy(0,0);
lcd_puts(" vyber rozmer ");
if(encoder==0){
lcd_gotoxy(0,1);
lcd_puts("1: 10x10 cm");
}
if(encoder==1){
lcd_gotoxy(0,1);
lcd_puts("2: 21x10 cm");
}
if(encoder==2){
lcd_gotoxy(0,1);
lcd_puts("3: 30x21 cm");
}
if(encoder>=3){
encoder=0;
}
}
}
/****************************************************
****************fce pro vyber oboustrane DPS*********
****************************************************/
void oboustrana_DPS(void)
{
if (flag==1){
lcd_gotoxy(0,0);
lcd_puts("oboustranou DPS?");
if (encoder==0){
lcd_gotoxy(0,1);
lcd_puts(" NE ");
}
if (encoder==1){
lcd_gotoxy(0,1);
lcd_puts(" ANO ");
}
if(encoder>=2){
encoder=0;
}
}
}
/****************************************************
***************fce pro nastaveni casu osvitu*********
****************************************************/
void nastaveni_casu(void)
{
//hodiny
if (flag==2){
lcd_gotoxy(0,0);
lcd_puts("nastaveni hodin ");
//nastaveni sekund probiha v preruseni
lcd_gotoxy(0,1);
sprintf(buffer," %02d:%02d.%02d ",hodiny,minuty,sekundy);
lcd_puts(buffer);
}
//minuty
if (flag==3){
lcd_gotoxy(0,0);
lcd_puts("nastaveni minut ");
//nastaveni sekund probiha v preruseni
lcd_gotoxy(0,1);
sprintf(buffer," %02d:%02d.%02d ",hodiny,minuty,sekundy);
lcd_puts(buffer);
}
//sekundy
if (flag==4){
lcd_gotoxy(0,0);
lcd_puts("nastaveni sekund");
//nastaveni sekund probiha v preruseni
lcd_gotoxy(0,1);
sprintf(buffer," %02d:%02d.%02d ",hodiny,minuty,sekundy);
lcd_puts(buffer);
//ulozani hodnoty hodiny,minuty, sekundy pro pozdejsi praci
pom_h=hodiny;
pom_m=minuty;
pom_s=sekundy;
//ulozani hodnoty hodiny,minuty, sekundy pro pozdejsi praci
hod=hodiny;
min=minuty;
sek=sekundy;
cas=( 3600 * (long)hodiny) + ( 60 * (long)minuty) + (long)sekundy; //prevod hodnot hod, min, sek na CAS
}
}
/****************************************************
*****************fce pro rozsviceni DPS**************
****************************************************/
void rosviceni_DPS(void)
{
if (oboustr==0){
if (rozmer_dps==0){
PORTC |= (1 << mala_dps);
PORTC &= ~(1 << vetsi_dps);
PORTC &= ~(1 << nejvetsi_dps);
}
if (rozmer_dps==1){
PORTC |= (1 << mala_dps);
PORTC |= (1 << vetsi_dps);
PORTC &= ~(1 << nejvetsi_dps);
}
if (rozmer_dps==2){
PORTC |= (1 << mala_dps);
PORTC |= (1 << vetsi_dps);
PORTC |= (1 << nejvetsi_dps);
}
}
else{
PORTC |= (1 << oboustrana);
if (rozmer_dps==0){
PORTC |= (1 << mala_dps);
PORTC &= ~(1 << vetsi_dps);
PORTC &= ~(1 << nejvetsi_dps);
}
if (rozmer_dps==1){
PORTC |= (1 << mala_dps);
PORTC |= (1 << vetsi_dps);
PORTC &= ~(1 << nejvetsi_dps);
}
if (rozmer_dps==2){
PORTC |= (1 << mala_dps);
PORTC |= (1 << vetsi_dps);
PORTC |= (1 << nejvetsi_dps);
}
}
}
/****************************************************
******************fce pro odpocitaveni***************
****************************************************/
void odpocet(void)
{
if (flag==5){
if ((PIND & (1 << viko))==0){
rosviceni_DPS();
sprintf(buffer,"nast: %02d:%02d.%02d ",pom_h,pom_m,pom_s);
lcd_gotoxy(0,0);
lcd_puts(buffer);
hod=cas/3600;
min=(cas-((long)hod*3600))/60;
sek=cas%60;
sprintf(buffer,"zbyva: %02d:%02d.%02d",hod,min,sek);
lcd_gotoxy(0,1);
lcd_puts(buffer);
PORTC |= (1 << mala_dps); //rozsviceni male DPSky
}
else{
PORTC &= ~(1 << mala_dps); //zhasnuti male DPSky
PORTC &= ~(1 << vetsi_dps); //zhasnuti male DPSky
PORTC &= ~(1 << nejvetsi_dps); //zhasnuti male DPSky
PORTC &= ~(1 << oboustrana); //zhasnuti male DPSky
lcd_gotoxy(0,0);
lcd_puts(" otevrene viko ");
lcd_gotoxy(0,1);
sprintf(buffer,"zbyva: %02d:%02d.%02d",hod,min,sek);
lcd_puts(buffer);
}
}
}
/****************************************************
*******************Restart MCU***********************
****************************************************/
void reboot(void) {
void (*fn_ptr) (void) = (void (*) (void)) 0;
fn_ptr();
}
/****************************************************
*************fce pro kontrolu tlacitka***************
****************************************************/
void kotrola(void)
{
if (flag>=6){reboot();}
}
//============================================================================
//=====================================MAIN===================================
//============================================================================
int main(void)
{
inicializace();
while(1){ //nekonecnik
//výběr velikosti DPSky
velikost_DPS();
//vyber oboustrane DPS
oboustrana_DPS();
//nastaveni casu osvitu
nastaveni_casu();
//nastaveni odpocitavani
odpocet();
//kontrola odpoctu (dokonceni osvitu)
kotrola();
}//while
return 1;
}//main