I am trying to complete my project at university but I cannot find why my screen resets after a second. The project supposed to be stopwatch, showing minutes and seconds. I need to have 3 buttons: 1. startstop 2.set minutes 3. set seconds, but it should works.. I have no idea why it set "0000" second after changing it...
Project is wrote to ATMEGA32 processor
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000
#include <util/delay.h>
#define segment_DDR DDRA //porty dotyczace wyswietlacza
#define segment PORTA
#define digit_DDR DDRB
#define digit PORTB
#define keys_DDR DDRC //porty dotyczace klawiszy
#define keys_PULLUP PORTC
#define keys_PIN PINC
#define key1 0
#define key2 1
#define key3 2
volatile uint8_t button1=0, button2=0, button3=0, startstop=0; //deklaracja zmiennych
uint8_t seg7[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xd8, 0x80, 0x90}; //tablice
volatile uint8_t data[] = {0, 0, 0, 0};
ISR(TIMER0_OVF_vect) //timer0
{
static uint8_t i=0, dot=0, mig=0; //odswiezanie wyswietlacza
digit |= (1<<i);
if(i++==3) i=0;
segment = seg7[data[i]];
if((i==3) && (startstop==1) && (dot++==50))
{
dot=0;
mig ^= 1;
}
if (i==3)
{
segment = seg7[data[i]] & 0x7f;
segment |= (mig<<7);
}
digit &= ~(1<<i);
//button startstop
if (!(keys_PIN & (1<<key1)))
{
switch(button1)
{
case 0:
button1=1;
break;
case 1:
button1=2;
break;
}
}
else
{
switch(button1)
{
case 3:
button1=4;
break;
case 4:
button1=0;
break;
}
}
//przycisk_m
if (!(keys_PIN & (1<<key2)))
{
switch(button2)
{
case 0:
button2=1;
break;
case 1:
button2=2;
break;
}
}
else
{
switch(button2)
{
case 3:
button2=4;
break;
case 4:
button2=0;
break;
}
}
//przycisk_h
if (!(keys_PIN & (1<<key3)))
{
switch(button3)
{
case 0:
button3=1;
break;
case 1:
button3=2;
break;
}
}
else
{
switch(button3)
{
case 3:
button3=4;
break;
case 4:
button3=0;
break;
}
}
}
ISR(TIMER1_OVF_vect) //timer1
{
}
int main(void)
{
keys_PULLUP |= (1<<key1)|(1<<key2)|(1<<key3); //pullup
keys_DDR &= ~((1<<klawisz1)|(1<<klawisz2)|(1<<klawisz3));
segment_DDR = 0xff;
digit_DDR |= 0x0ff;
digit |= 0x0ff;
segment = 0xff;
TCCR0 = (1<<CS01);
TCCR1B = (1<<CS12)|(1<<WGM12);
TIMSK = (1<<TOIE0)|(1<<OCIE1A);
OCR1A = 62500;
sei();
while (1)
{
if (button1==2)
{
startstop = !startstop;
button1 = 3;
}
if(startstop!=1 && button2==2)
{
if (data[3]++==9)
{
data[3]=0;
if (data[2]++==6)
{
data[2]=0;
}
}
button2=3;
}
if(startstop!=1 && button3==2)
{
if (data[1]++==9)
{
data[1]=0;
if (data[0]++==6)
{
data[0]=0;
}
}
button3=3;
}
while(startstop==1)
{
if (data[3]++==9)
{
data[3]=0;
if (data[2]++==6)
{
data[2]=0;
if (data[1]++==9)
{
data[1]=0;
if (data[0]++==6)
{
data[0]=0;
}
}
}
}
_delay_ms(1000);
}
}
}