#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
unsigned char lcd_digits[10] __attribute__((progmem)) =
{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
unsigned char key;
unsigned char data[4]={2,0,0,8};
static unsigned char num=0;
static unsigned char pos=0;
static unsigned char w=0;
ISR(TIMER0_COMP_vect)
{
PORTB=(PORTB&0xf0)|1<<num;
PORTA=pgm_read_byte(lcd_digits+data[num]);
if(num==pos)
{
if(w<50) PORTB=(PORTB&0xf0)|1<<num;
if(w>50) PORTB=(PORTB&0xf0)|0<<num;
}
if(w==100)
w=0;
else
w++;
if(num==3)
num=0;
else
num++;
}
void keyboard(void)
{
char k;
static unsigned char p;
k=(PINB>>4)|0xf0;
if(k==0xff)
{
p=0;
return;
}
if(p==1)
return;
_delay_ms(10);
if(k!=((PINB>>4)|0xf0))
return;
p=1;
key=~k;
}
void menu(void)
{
switch(key)
{
case 1:
if(pos>0)
pos--;
break;
case 2:
if(pos<3)
pos++;
break;
case 4:
if(data[pos]<9)
data[pos]++;
break;
case 8:
if(data[pos]>0)
data[pos]--;
break;
}
key=0;
}
int main(void)
{
DDRA=0xff;
DDRB=0x0f;
PORTB|=0xf0;
TCCR0=0x0c;
OCR0=0x40;
TIMSK|=1<<OCIE0;
SREG|=1<<SREG_I;
while(1)
{
keyboard();
menu();
}
return 0;
}