目录
倒计时: 低级版没有闪
用单片机的时间中断器(TR0-TR1)来进行计时来接近现实时间
#include <reg52.h>
#define uc8 unsigned char
sbit k1 = P3^4;
sbit k2 = P3^5;
sbit k3 = P3^6;
sbit k4 = P3^7;
bit db;
sbit led1 = P2^4;
sbit led2 = P2^5;
sbit led3 = P2^6;
sbit led4 = P2^7;
uc8 i,sh = 0,bh = 0,m = 0,sm = 5,gf = 1,sf=0,ph = 0,count_num=0;
unsigned char code xianshi[]={0x8f,0x4f,0x2f,0x1f};//ÊýÂë¹Ü¸÷λµÄÂë±í
unsigned char code meidian[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//ÊýÂë¹Ü¸÷λµÄÂë±í(´øÉÏСµã)
unsigned char code youdian[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
unsigned int value = 0;
unsigned int value_conut = 0;
void delay(unsigned char jk){
unsigned char j,k;
for(j=jk;j>0;j--)
for(k=112;k>0;k--);
}
void delay1(unsigned int jk){
unsigned int j,k;
for(j=jk;j>0;j--)
for(k=112;k>0;k--);
}
void display1(uc8 wei,uc8 shu)//ÔÚÈÎÒâһλÏÔʾÈÎÒâµÄÊý×Ö
{
wei=wei-1;
P2 |= 0xf0;//¸øP2.4-P2.7ÖÃ1
if(wei == 2)
P0=youdian[shu];
else
P0=meidian[shu];
P2=P2&xianshi[wei];//¸øP2ÐèÒªÏÔʾµÄÄÇһλÖÃ1£¬ÆäËûÖÃ0
delay(1);
}
void display(unsigned char a,unsigned char b,unsigned char c,unsigned char d){ //Ò»´ÎÏÔʾ4¸öÊý×Ö£¬ÇÒÿλÏÔʾÊý×Ö¿É×Ô¶¨¡£
display1(4,a);
display1(3,b);
display1(2,c);
display1(1,d);
}
//void start_timer(){
// EA=1; //¿ª×ÜÖжÏ
// ET0=1; //¿ª¶¨Ê±Æ÷0ÖжÏ
// TR0=1; //´ò¿ª¶¨Ê±Æ÷
//}
//void stop_timer(){
// ET0=0; //¹Ø¶¨Ê±Æ÷0ÖжÏ
// EA=0; //¹Ø×ÜÖжÏ
// TR0=0; //¹Ø±Õ¶¨Ê±Æ÷
//}
void main()
{
TMOD = 0x01;
TH0 = 0x0Dc;
TL0 = 0x009;
EA=1; //¿ª×ÜÖжÏ
ET0=1; //¿ª¶¨Ê±Æ÷0ÖжÏ
while(1){
// 按钮判断
if(!k1 && !db) {
db = 1;
TR0 = ~TR0;
}else if(k1) {
db = 0;
}
if(TR0) {
P1 = 0xfe;
}else {
P1 = 0xff;
}
/* if(k1 == 0)
{
delay1(4);
if(k1 == 0) count_num++;
if(count_num == 1) {
start_timer();
P1 = 0xf0;
}else if(count_num == 2) {
stop_timer();
P1 = 0x0f;
}else if(count_num == 3){
count_num = 1;
start_timer();
P1 = 0xf0;
}
} */
/* if(k3 == 0){
// TMOD = 0x10;
TH1 = 0x3C;
TL1 = 0x0B0;
ET1 = 1;
TR1 = 1;
if(k3 == 0 ) {
if(value >= 60) {
stop_timer();
P1 = 0x0f;
sf=0;
gf=1;
sm=5;
m=0;
count_num = 0;
}else {
ET1 = 0;
TR1 = 0;
value = 0;
}
}
} */
if(k3 == 0){
if(value >= 280){
if(k3 == 0) {
TR0 = 0;
gf=1;
sm=5;
m=0;
count_num = 0;
value = 0;
}else {
value = 0;
}
}
}
if(gf == 0 && sm == 0 && m == 0) {
P1 = 0xff;
}
//µ¹Êý¼Æʱ
if(value < 277) {
if(i == 1)
{
if(sh == 0)
{
if(bh == 0)
{
if(m == 0)
{
if(sm == 0)
{
if(gf == 0)
{
if(sf == 0) {
P1 = 0xff;
}else {
sf--;
gf = 10;
}
}
else
{
gf--;
sm = 10;
}
}
else
{
sm--;
m = 10;
}
}
else
{
m--;
bh = 10;
}
}
else
{
bh--;
sh= 10;
}
}
else
sh--;
i = 0;
}
}
display(sf,gf,sm,m);
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x0Dc;
TL0 = 0x009;
i++;
if(k3 == 0) {
value++;
}
//add your code here!
}
倒计时: 高配版 有闪
倒计时到最后十秒进行闪烁
#include <reg52.h>
sbit led1 = P2^4;
sbit led2 = P2^5;
sbit led3 = P2^6;
sbit led4 = P2^7;
sbit k1 = P3^4;
sbit k2 = P3^5;
int lhj = 0,value = 0,i = 0, value_number = 0, smg1_light = 5, smg2_light = 2, smg3_light = 0, smg4_light = 0,flag = 0;
bit db;
unsigned char code light[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int x)
{
unsigned int i,j;
for(i = x;i > 0;i--){
for(j = 112;j > 0;j--){};
}
}
void main()
{
int value_fuzhi = 0;
TMOD = 0x01;
// TH0 = 0x0Dc;
// TL0 = 0x009;
TH0 = 0x3C;
TL0 = 0x0B0;
EA=1; //¿ª×ÜÖжÏ
ET0=1; //¿ª¶¨Ê±Æ÷0ÖжÏ
while(1){
if(!k1 && !db) {
db = 1;
TR0 = ~TR0;
}else if(k1) {
db = 0;
}
if(TR0) {
P1 = 0xfe;
}else {
P1 = 0xff;
}
if(i == 20) {
i = 0;
smg1_light--;
}
if(smg1_light < 0) {
smg2_light--;
smg1_light = 9;
}
if(smg1_light == 0 && smg2_light == 1) {
flag++;
}
// ¹Ø±ÕÏÔʾ
if(smg1_light >= 0 && smg2_light == 1 || flag && value <= 4) {
led4 = 0;
led3 = 0;
led2 = 0;
led1 = 0;
delay(1);
}
// ¿ªÆôÏÔʾ
if(smg1_light >= 0 && smg2_light == 1 || smg2_light == 0 && value > 4 && value <= 10) {
P0 = light[smg1_light];
P2 = 0x88;
delay(1);
P0 = light[smg2_light];
P2 = 0x48;
delay(1);
P0 = light[smg3_light];
P2 = 0x28;
delay(1);
P0 = light[smg4_light];
P2 = 0x18;
delay(1);
}
if(lhj >= 20) {
TR0 = 0;
smg1_light = 5, smg2_light = 2, smg3_light = 0, smg4_light = 0;
}
if(smg1_light >= 0 && smg2_light > 1 || smg2_light == 0 && smg1_light < 0) {
P0 = light[smg1_light];
P2 = 0x88;
delay(1);
P0 = light[smg2_light];
P2 = 0x48;
delay(1);
P0 = light[smg3_light];
P2 = 0x28;
delay(1);
P0 = light[smg4_light];
P2 = 0x18;
delay(1);
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x3C;
TL0 = 0x0B0;
i++;
if(flag) {
value++;
}
if(value > 10) {
value = 0;
}
if(smg1_light == 0 && smg2_light == 0 && smg3_light == 0 && smg4_light == 0 ) {
lhj++;
}
if(lhj > 20) {
lhj = 0;
}
//add your code here!
}