Mình dùng Codevisionavr vs Proteus mô phỏng.
Mạch:

Code:
#include <mega16.h>
#include <delay.h>
unsigned int L1,L2,L3,L4,i;
unsigned int x=001;
unsigned char LED[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0}; //Bang ma hien thi LED 7 ung voi cac so tu 0 - 9
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
x++;
if (x==10000) x=0;

}

// Declare your global variables here

void main(void)
{

DDRC=0xFF;
DDRD=0x1b;
PORTD.2=1;
PIND.2=1;
// Global enable interrupts
#asm("sei")

while (1)
{
// Place your code here
if (PIND.2==0) PIND.2=1;

L1=(x/1000)%10;
L2=(x/100)%10;
L3=(x/10)%10;
L4=(x/1)%10;
for (i=0;i<=7;i++)
{
PORTC=LED[L1]; PORTD.0=1; delay_ms(0); PORTD.0=0;
PORTC=LED[L2]; PORTD.1=1; delay_ms(0); PORTD.1=0;
PORTC=LED[L3]; PORTD.3=1; delay_ms(0); PORTD.3=0;
PORTC=LED[L4]; PORTD.4=1; delay_ms(0); PORTD.4=0;
}

}
}

--------------------------------------------------------------------------------
Xem bài viết cùng chuyên mục: