- Code:
#include<reg51.h>
#include<stdio.h>
#include<intrins.h>
// khai bao cac led de quet
sbit led1 = P1^3;
sbit led2 = P1^2;
sbit led3 = P1^1;
sbit led4 = P1^0;
sbit inc_hour = P2^0; //tang gio
sbit dec_hour = P2^1; //giam gio
sbit inc_min=P2^2; //tang phut
sbit dec_min=P2^3; //giam phut
sbit start = P2^4; //bat dau *** lui
sbit sec = P2^5; //chan can dieu khien bat/tat
sbit role = P2^6; //nhap nhay theo giay
int hour=0,min=0,s;
// khai bao ma led 7 doan ktot chung
unsigned char num[10]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//hien so khong co dau cham (led 1,3,4)
unsigned char num2[10]= {0xbF,0x86,0xdB,0xcF,0xe6,0xeD,0xfD,0x87,0xfF,0xeF}; //hien so co dau cham (led2)
// chuong trinhd delay
void delay()
{
{
TMOD=0x01;
TH0=0xFC;
TL0=0x18;
TR0=1;
while(!TF0);
TF0=0;
TR0=0;
}
}
//-------------quet led----------------
void quet()
{
led1=0;
P3=num[hour/10];// led1
delay();
led1=1;
P3=0x00;
led2=0;
P3=num2[hour%10]; //led 2
delay();
led2=1;
P3=0x00;
led3=0;
P3=num[min/10]; // led3
delay();
led3=1;
P3=0x00;
led4=0;
P3=num[min%10]; // led4
delay();
led4=1;
P3=0x00;
}
void delay2(m)
{
int i;
for (i=0;i<m;i++)
{
quet();
}
}
// chuong trinh xuat dong thoi ra 4 led
void main(void)
{
//----------hen gio----------------
inc_hour=dec_hour=0;
inc_min=dec_min=0;
role=start=hour=min=0;
while (start==0)
{
if (inc_min==1)
{
min++;
if (min==60)
{
min=0;
hour++;
}
inc_min=0;
delay2(50);
}
quet();
if (dec_min==1)
{
min--;
if (min<0)
{
min=59;
if (hour>0) hour--;
}
dec_min=0;
delay2(50);
}
if (inc_hour==1)
{
hour++;
if (hour>99) hour=0;
inc_hour=0;
delay2(50);
}
if (dec_hour==1)
{
hour--;
if (hour<0) hour=99;
dec_hour=0;
delay2(50);
}
}
while (1)
{
role=1;
for (s=0;s<60;s++)
{
sec=1;
delay2(105);
sec=0;
delay2(105);
quet();
}
min--;
if (min<0 && hour > 0)
{
min=59;
hour--;
}
if (min==0 && hour==0) break;
}
while(1)
{role=0;}
}
còn đây là mạch mô phỏng và nguyên lý:
[You must be registered and logged in to see this link.]