In: Electrical Engineering
/* Generating 10 ms delay pulse on PORTB using ATmega128 Timer interrupt */ #include <avr/io.h> #include <avr/interrupt.h> /* timer0 overflow interrupt */ ISR(TIMER0_OVF_vect) { PORTB=~PORTB; /* Toggle PORTB */ TCNT0 = 0xB2; } (PIND==0x0FE&&PIND==0x0FD) {
int position=0,BJT[4]={0xFE,0xFD,0xFB,0xF7};
int number=0,segment[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
int msec=0, sec=0, min=0;
void main()
{
DDRA=0xFF;
PORTA=0XFF;
DDRB=0xFF;
PORTB=0xFF;
TCCR0=0x04;
TIMSK=0x01;
SREG=0x80;
while(1){}
}
interrupt[TIM0_OVF]void timer0_ovf_isr()
{
TCNT0=0x06;
PORTA=BJT[position];
if(position==0)number=sec%10;
if(position==1)number=sec/10;
if(position==2)number=min%10;
if(postion==3)number=min/10;
PORTB=segment[number];
position++;
if(position>3)position=0;
msec++;
if(msec==1000)
{
msec=0;
sec++;
}
if(sec==60)
{
sec=0;
min++;
}
if(min==60)min=0;
}
}
}
int main( void ) { if { DDRB=0xFF; /* Make port B as output */ sei(); TIMSK=(1<<TOIE0); /* Enable Timer0 overflow interrupts */ TCNT0 = 0xB2; /* Load TCNT0, count for 10ms*/ TCCR0 = (1<<CS02) | (1<<CS00); /* Start timer0 with /1024 prescaler*/ while(1); }