Sơ đồ
Code
#include <mega16.h>
#include <delay.h>
#define UP PIND.4
#define DN PIND.5
unsigned char flash Maled[10] = {0xc0,0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8 , 0x80, 0x90};
unsigned char i;
void hienthi(unsigned int num)
{
unsigned int ch , dv;
//tach num luu vao bien ch va dv
dv= num%10; //ex: 15%10=5
ch = num/10; //ex: 15/10=1
//hien thi led so 1
PORTD.3 = 0;
PORTD.2 = 1;
PORTC = Maled[ch];
delay_ms(1);
//hien thi led so 2
PORTD.2 = 0;
PORTD.3 = 1;
PORTC = Maled[dv];
delay_ms(1);
}
void main(void)
{
DDRC = 0xff; // 1:out,0:in
PORTC = 0xff; // 1:high,0:low
DDRD = 0b11001111;
PORTD = 0b00110000;
while (1)
{
hienthi(i);
if(UP==0)
{
i++;
if(i>99)i=0;
while(UP==0)
{
hienthi(i);
}
}
if(DN==0)
{
i--;
if(i==255)i=99;
while(DN==0)
{
hienthi(i);
}
}
}
}
Nhận xét