利用动态扫描和定时器1在数码管上显示出从765432开始以1/10秒的速度往下递减 直至765398并保持此数,与此同时利用定时器0以500MS速度进行流水灯从上至下移动 ,当数码管上数减到停止时,实验板上流水灯出停止然后全部开始闪烁,3秒后(用 T0定时)流水灯全部关闭,数码管上显示出“HELLO”,到此保持住
#include <reg52.h>
#include <intrins.h> #define uchar unsigned char #define uint unsigned int /**tatle_du数组数据为0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,H,L**/ uchar code tatle_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71, 0x76,0x79,0x38,0}; uchar code tatle_we[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; sbit dula=P2^6; sbit wela=P2^7; uchar tt,t0,numwe,numdu,temp,bai,shi,ge,flag,flag1; uint shu; void display(uchar aa,uchar bb,uchar cc,uchar bai,uchar shi,uchar ge); void delay(uchar z); void inter_init(); void main() { inter_init(); while(1) { if(flag1 != 1) { display(7,6,5,bai,shi,ge); } else { display(16,17,18,18,0,19); } } } /*定时器0流水灯与闪烁*/ void inter0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; /*如果减数不到398,流水灯与减数同时运行,如果到了398开启闪烁*/ if(flag != 1) { /*流水灯*/ if(tt==10) { tt=0; delay(5); temp=_crol_(temp,1); P1=temp; } } else { /*使灯闪烁4秒*/ if(tt%4==0) { P1=~P1; } if(tt==80) { TR0=0; P1=0xff; flag1=1; } } } /*定时器1数码管显示*/ void inter1() interrupt 3 { TH1=(65536-50000)/256; TL1=(65536-50000)%256; t0++; if(t0==2) { t0=0; shu--; bai=shu/100; shi=shu%100/10; ge=shu%10; if(shu==398) { TR0=0; //关闭定时器0,关闭流水灯 P1=0xff; TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt=0; TR0=1; //开启定时器0,并填充数据。使灯闪烁 flag=1; TR1=0; //关闭定时器1保持数据显示 } } } void delay(uchar z) { uchar x,y; for(x=z;x>0;x--) { for(y=110;y>0;y--); } } void inter_init() { tt=0; t0=0; flag=0; flag1=0; shu=432; temp=0xfe; P1=temp; TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; TH1=(65536-50000)/256; TL1=(65536-50000)%256; EA=1; ET0=1; ET1=1; TR0=1; TR1=1; } void display(uchar aa,uchar bb,uchar cc,uchar bai,uchar shi,uchar ge) { dula=1; P0=tatle_du[aa]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(5); dula=1; P0=tatle_du[bb]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(5); dula=1; P0=tatle_du[cc]; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(5); dula=1; P0=tatle_du[bai]; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(5); dula=1; P0=tatle_du[shi]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(5); dula=1; P0=tatle_du[ge]; dula=0; P0=0xff; wela=1; P0=0xdf; wela=0; delay(5); }
如果有错误,欢迎各位网友指导。
另外喜欢的话,给个推荐呗,谢谢!