51单片机流水灯数码管定时器秒表计算器程序.docx
- 文档编号:7417548
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:21
- 大小:17.07KB
51单片机流水灯数码管定时器秒表计算器程序.docx
《51单片机流水灯数码管定时器秒表计算器程序.docx》由会员分享,可在线阅读,更多相关《51单片机流水灯数码管定时器秒表计算器程序.docx(21页珍藏版)》请在冰豆网上搜索。
51单片机流水灯数码管定时器秒表计算器程序
51单片机流水灯、数码管、定时器、秒表、计算器程序
流水灯#include
chardata_group[]={0x7e,0xbd,0xdb,0xe7,0xdb,0xbd,0x7e};
unsignedinta;
voiddelay(unsignedinttime){
inti,j;
for(i=time;i>0;i--){
for(j=100;j>0;j--);
}
}
voidmove_mid_side(void){
intm;
for(m=0;m<7;m++){
P1=data_group[m];
delay(500);
}
}
voidmove_right_only(void){
inta,temp;
delay(500);
temp=0x80;
for(a=7;a>=0;a--){
temp=~temp;
P1=temp;
delay(500);
temp=~temp;
temp>>=1;
}
temp=0x80;
delay(1000);
}
voidmove_left_only(void){
inta,temp;
delay(500);
temp=0x01;
for(a=7;a>=0;a--){
temp=~temp;
P1=temp;
delay(500);
temp=~temp;
temp<<=1;
}
temp=0x01;
delay(1000);
}
voidmove_right_hlod(void){
inta,temp;
temp=0x7f;
for(a=8;a>=0;a--){
P1=temp;
delay(500);
temp=temp>>1;
}
temp=~temp;
delay(1000);
}
voidmove_left_hold(void){
inta,temp;
temp=0xfe;
for(a=8;a>=0;a--){
P1=temp;
delay(500);
temp<<=1;
}
temp=~temp;
delay(1000);
}
voidmain()
{
while
(1)
{
move_left_hold();
move_mid_side();
move_right_hlod();
move_mid_side();
move_left_only();
move_right_only();
}
}
数码管#include
chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9
chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};//0xff全关,之后为第
一个数码管到第八个数码管
//chardata_wei_add[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00};
voiddelay(unsignedinttime){
inti,j;
for(i=time;i>0;i--){
for(j=0;j<100;j++);
}
}
voidshow_number_only(void)
{
inti;
P2=data_wei[1];//P2为位选
for(i=0;i<10;i++){
P0=data_duan[i];//P0为段选
delay(500);
}
}
/*******************数码管动态显示子程序***************/
voidmove_show1(void)
{
inti;
for(i=1;i<2;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show2(void)
{
inti;
for(i=1;i<3;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show3(void)
{
inti;
for(i=1;i<4;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show4(void)
{
inti;
for(i=0;i<5;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show5(void)
{
inti;
for(i=0;i<6;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show6(void)
{
inti;
for(i=0;i<7;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show7(void)
{
inti;
for(i=0;i<8;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
voidmove_show8(void)
{
inti;
for(i=0;i<9;i++){
P2=data_wei[i];
P0=data_duan[i];
delay
(1);
}
}
/********数码管动态显示,实现数码管由1~8在八个数码管上一次显示************/
voidmove_show_hold(void)
{
inttemp,i;
temp=1000;
for(i=temp;i>0;i--)
move_show1();
temp=600;
for(i=temp;i>0;i--)
move_show2();
temp=400;
for(i=temp;i>0;i--)
move_show3();
temp=200;
for(i=temp;i>0;i--)
move_show4();
temp=200;
for(i=temp;i>0;i--)
move_show5();
temp=200;
for(i=temp;i>0;i--)
move_show6();
temp=100;
for(i=temp;i>0;i--)
move_show7();
temp=100;
for(i=temp;i>0;i--)
move_show8();
}
voidshow_all(void)//将所有的位选端口打开,变化段选,显示整体变化1~9
{
inti;
P2=data_wei[9];
for(i=0;i<10;i++){
P0=data_duan[i];
delay(500);
}
}
intmain(void){
while
(1){
//show_number_only();
move_show8();
//move_show_hold();//show_all();
}
}
独立按键
#include
sbitkey1=P3^0;sbitd2=P1^0;
inttemp,number=0;
voiddelay(unsignedinttime){
inti,j;
for(i=time;i>0;i--)
{
for(j=100;j>0;j--);
}
}
intmain(void){
temp=0xfe;
while
(1){
d2=1;
key1=1;
P1=temp;
if(key1==0){
delay(100);
if(key1==0){
number++;
if(number<=7){
temp=~temp;
temp=temp<<1;
temp=~temp;
P1=temp;
}
else{
temp=0xfe;
number=0;
}
}
}
}
}
矩阵按键#include
chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,
0xc0,0x86,0x8e,};//0~9chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};
intnumber=0;
voiddelay(unsignedinttime){
inti,j;
for(i=time;i>0;i--){
for(j=0;j<100;j++);
}
}
//voidmove_show1(void)
//{
//inti;
//for(i=1;i<2;i++){//P2=data_wei[i];//P0=data_duan[i];//delay
(1);
//}
//}
//voidmove_show2(void)
//{
//inti;
//for(i=1;i<3;i++){//P2=data_wei[i];
//P0=data_duan[i];//delay
(1);
//}
//}
voidscan_key(void)
{
inttemp;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
delay
(1);
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
switch(temp){
case0xe0:
number=12;break;
case0xd0:
number=13;break;
case0xb0:
number=14;break;
case0x70:
number=15;break;
}
while(temp!
=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
delay
(1);
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
switch(temp){
case0xe0:
number=8;break;
case0xd0:
number=9;break;
case0xb0:
number=10;break;
case0x70:
number=11;break;
}
while(temp!
=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
delay
(1);
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
switch(temp){
case0xe0:
number=4;break;
case0xd0:
number=5;break;
case0xb0:
number=6;break;
case0x70:
number=7;break;
}
while(temp!
=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
delay
(1);
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
switch(temp){
case0xe0:
number=0;break;
case0xd0:
number=1;break;
case0xb0:
number=2;break;
case0x70:
number=3;break;
}
while(temp!
=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
//returnnumber;
}
intmain(void)
{
//intnum=0;
while
(1){
//num=scan_key(num);
scan_key();
P2=data_wei[1];
P0=data_duan[number];
}
}
定时器中断#include
chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9
chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};//0xff全关,之后为第
一个数码管到第八个数码管
intnumber=0,time=0;
intmain(void)
{
//TMOD=0x01;//设置工作方式定时器0
//TH0=(65536-50000)/256;//给计数器装初值//TL0=(65535-50000)%256;
//EA=1;//开总中断//ET0=1;//定时器T0中断允许//TR0=1;//启动定时器0
TMOD=0x10;//设置工作方式定时器1
TH1=(65536-50000)/256;//给计数器装初值
TL1=(65535-50000)%256;
EA=1;//开总中断
ET1=1;//定时器T1中断允许
TR1=1;//启动定时器1
while
(1){
if(number==20){
P2=data_wei[2];
P0=data_duan[4];
}
if(time==40){
P2=data_wei[0];
number=0;
time=0;
}
}
}
voidexter3()interrupt3
{
TH1=(65536-50000)/256;//给计数器装初值
TL1=(65535-50000)%256;
time++;
number++;
}
//voidexter1()interrupt1
//{
//TH0=(65536-50000)/256;//给计数器装初值//TL0=(65535-50000)%256;
//time++;
//number++;
//}
时钟#include
chardata_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0~9
chardata_wei[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00};
unsignedlongintsecond=0,minute=0,hour=0,point=0,tip=0;
unsignedlonginttime=0,sign=0,number=0,key=0,variable=0;unsignedlongintS1=0,S2=0,M1=0,M2=0,H1=0;H2=0;
voiddelay(unsignedinttime){
inti,j;
for(i=time;i>0;i--){
for(j=0;j<100;j++);
}
}
voidkey_scan(void)
{
inttemp;//应用按键检测
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
delay
(1);
temp=P1;
temp=temp&0xf0;
while(temp!
=0xf0){
switch(temp){
case0xe0:
number=1;break;
case0xd0:
number=2;break;
case0xb0:
number=3;break;
case0x70:
number=4;break;
}
while(temp!
=0xf0){
temp=P1;
temp=temp&0xf0;
}
}
}
//P1=0xfb;
//temp=P1;
//temp=temp&0xf0;//while(temp!
=0xf0){//delay
(1);
//temp=P1;
//temp=temp&0xf0;//while(temp!
=0xf0){//switch(temp){//case0xe0:
number=5;break;//case0xd0:
number=6;break;//case0xb0:
number=7;break;//case0x70:
number=8;break;//}
//while(temp!
=0xf0){//temp=P1;
//temp=temp&0xf0;//}
//}
////
//}
}
voidadjust_alarm(void){
if(number==1){//控制时钟的启动与停止
key=!
key;
if(key==1){
EA=0;
variable=time;
}
if(key==0){
EA=1;
}
number=0;
}
if(number==2){//调节时间选择标志位
tip=tip+1;
tip=tip%6;
if(tip==0){
tip=6;
}
number=0;
}
if((number==3)&&(EA==0)){
if(tip==1)
time=time-1;
if(tip==2)
time=time-10;
if(tip==3)
time=time-60;
if(tip==4)
time=time-600;
if(tip==5)
time=time-3600;
if(tip==6)
time=time-36000;
number=0;
}
if((number==4)&(EA==0)){
if(tip==1)
time=time+1;
if(tip==2)
time=time+10;
if(tip==3)
time=time+60;
if(tip==4)
time=time+600;
if(tip==5)
time=time+3600;
if(tip==6)
time=time+36000;
number=0;
}
//if((number==5)&&(EA==0)){//if(tip==1)
//variable=variable-1;//if(tip==2)
//variable=variable-10;//if(tip==3)
//variable=variable-60;//if(tip==4)
//variable=variable-600;//if(tip==5)
//variable=variable-3600;//if(tip==6)
//variable=variable-3600;//number=0;
//}
//if((number==6)&(EA==0)){//if(tip==1)
//variable=variable+1;//if(tip==2)
//variable=variable+10;//if(tip==3)
//variable=variable+60;//if(tip==4)
//variable=variable+600;//if(tip==5)
//variable=variable+3600;//if(tip==6)
//variable=variable+36000;//number=0;
//}
}
voidcompare_time(void)
{
if(time>variable){
;
}
}
voidalarm_show_s(void)
{
intsign2;
for(sign2=1;sign2<2;sign2++){//数码管动态显示
S1=second%10;
P2=data_wei[sign2];
P0=data_duan[S1];
delay
(1);
}
for(sign2=2;sign2<3;sign2++){
S2=second/10;
P2=data_wei[sign2];
P0=data_duan[S2];
delay
(1);
}
for(sign2=3;sign2<4;sign2++){
P2=data_wei[sign2];
P0=0xbf;
delay
(1);
}
for(sign2=4;sign2<5;sign2++){
M1=minute%10;
P2=data_wei[sign2];
P0=data_duan[M1];
delay
(1);
}
for(sign2=5;sign2<6;sign2++){
M2=minute/10;
P2=data_wei[sign2];
P0=data_duan[M2];
delay
(1);
}
for(sign2=6;sign2<7;sign2++){
P2=data_wei
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 流水 数码管 定时器 秒表 计算器 程序