谁能告诉我51单片机简单的led数码管时钟程序 24小时制的(c语言版的)

有没有人在啊,想请教一下,谁能告诉我51单片机简单的led数码管时钟程序 24小时制的(c语言版的)
最新回答
橘萝卜蹲

2024-06-26 15:56:01

#include "reg52.h"
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shi,fen,miao;
uchar time;
void delay(uint x)
{
uint y;
for(;x>0;x--)
{
for(y=0;y<124;y++);
}
}
void display(uchar shi,uchar fen,uchar miao)
{
P2=0; //位码
P0=(tab[shi/10]); //段码
delay(2);
P2=1;
P0=(tab[shi%10]);
delay(2);
P2=2; //位码
P0=0x40; //段码
delay(2);
P2=3; //位码
P0=(tab[fen/10]); //段码
delay(2);
P2=4;
P0=(tab[fen%10]);
delay(2);
P2=5; //位码
P0=0x40; //段码
delay(2);
P2=6; //位码
P0=(tab[miao/10]); //段码
delay(2);
P2=7;
P0=(tab[miao%10]);
delay(2);

}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{

if(time==20)
{
time=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}

}

}
display(shi,fen,miao);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
time++;
}

/*还有什么不明白继续追加*/
安旭薇

2024-06-26 09:26:19

好久没有用51编程了,有可能有错误,你试一下,有问题告诉我再改正。
P2.0秒的个位的数码管的共阴极
P2.1秒的十位的数码管的共阴极

P2.2分的个位的数码管的共阴极

P2.3分的十位的数码管的共阴极

P2.4时的个位的数码管的共阴极

P2.5时的十位的数码管的共阴极

#include <reg52.h>// 晶振为12MHz,
#include <stdio.h>
void IT0_Init(void);
void xianshi(void);
uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int miao=0,fen=0,shi=0;
int main(void){
IT0_Init();
Timer0_Overflow();
while(1){
if(miao>59){
fen++;
miao=0;
if(fen>59){
shi++;
fen=0;
if(shi>24)
shi=0;
xianshi();
}}}}
void IT0_Init(void)
{
TMOD=0x01;//设置T0在方式1下工作
TH0=0x3C;
TL0=0xAF;//这两个寄存器存的是计数器的计数开始的值,计算发现这两个值累加至溢出后正好是50ms
ET0=1;//使T0中断可以溢出
EA=1;//开启总中断
TF0=0;//溢出位清零
TR0=1;//开启T0
}
void xianshi(void)
{
P2=0x01;
P1=tab[(miao%10)];
P2=0x02;
P1=tab[(miao/10)];
P2=0x04;
P1=tab[(fen%10)];
P2=0x08;
P1=tab[(fen/10)];
P2=0x10;
P1=tab[(shi%10)];
P2=0x20;
P1=tab[(shi/10)];
}
void Timer0_Overflow() interrupt 1
{
static unsigned char second=20;//用20次中断来判断1秒
TH0=0x3C;
TL0=0xAF;
second--;
if (second==0)//每隔一秒的操作
{
second=20;//重新赋值每秒计数器
miao++;
}
}
猫街少女

2024-06-26 13:32:07

/**************************************************
*
* 6位数码管显示电子钟
*
* 功能: 按键加,按键减, 按键,启动和停止。
*
*
***************************************************/
#include "REG52.H"

sbit SHUG_01 = P2^0; //数码管1
sbit SHUG_02 = P2^1; //数码管2
sbit SHUG_03 = P2^2; //数码管3
sbit SHUG_04 = P2^3; //数码管4
sbit SHUG_05 = P2^4; //数码管5
sbit SHUG_06 = P2^5; //数码管6
sbit LED0_01 = P2^6; //LED灯1
sbit LED0_02 = P2^7; //LED灯1
sbit LED0_03 = P3^0; //LED灯1
sbit LED0_04 = P3^1; //LED灯1
sbit KEY0_01 = P0^0; //按键 启动或停止
sbit KEY0_02 = P0^1; //按键 加
sbit KEY0_03 = P0^2; //按键减
sbit KEY0_04 = P0^3; //按键菜单
#define LED_DATA P1 //P1数据端口
unsigned char value_temp = 0;
unsigned char value_Date = 0;
unsigned char value_Outk = 0;
unsigned char value_shio = 8;
unsigned char value_feno = 4;
unsigned char value_miao = 0;
unsigned char value_Mume = 0;
unsigned char code value_tab0[]=
{
0xC0,
0xF9,
0xA4,
0xB0,
0x99,
0x92,
0x82,
0xF8,
0x80,
0x90
}; //共阳极0-9显示断码
unsigned char code value_tab1[]=
{
0x40,
0x79,
0x24,
0x30,
0x19,
0x12,
0x02,
0x78,
0x00,
0x10
}; //共阳极带点0-9显示断码

/*
* 延时1ms延时子函数
**/
void Delay_1ms(unsigned int Time)
{
unsigned int x, y;
for(x = Time; x > 0; x--)
for(y = 120; y > 0; y--);
}
/*
* 程序运行【启动和停止】子函数
**/
void Disp_layledketCmd(void)
{
/***小时***/
SHUG_01 = 1;
LED_DATA = value_tab0[value_shio / 10]; /*******小时的十位数显示***************/
Delay_1ms(5);
SHUG_01 = 0;
SHUG_02 = 1;
LED_DATA = value_tab1[value_shio % 10]; /*******小时的个位数显示***************/
Delay_1ms(5);
SHUG_02 = 0;
/***分钟***/
SHUG_03 = 1;
LED_DATA = value_tab0[value_feno / 10]; /*******分钟的十位数显示***************/
Delay_1ms(5);
SHUG_03 = 0;
SHUG_04 = 1;
LED_DATA = value_tab1[value_feno % 10]; /*******分钟的个位数显示***************/
Delay_1ms(5);
SHUG_04 = 0;
/***秒钟***/
SHUG_05 = 1;
LED_DATA = value_tab0[value_miao / 10]; /*******秒钟的十位数显示***************/
Delay_1ms(5);
SHUG_05 = 0;
SHUG_06 = 1;
LED_DATA = value_tab0[value_miao % 10]; /*******秒钟的个位数显示***************/
Delay_1ms(5);
SHUG_06 = 0;
}
/*
* 定时器初始化定时50ms
**/
void Tero_Init(void)
{
TMOD = 0x11;
TH0 = 0x3C;
TL0 = 0xB0;
TH1 = 0x4C;
TL1 = 0x00;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
EA = 1;
}
/*
* 按键开关【启动和停止】子函数
**/
void Scan_keyoutCmd(void)
{
if(KEY0_01 == 0)
{
Delay_1ms(10);
if(KEY0_01 == 0)
{
value_Mume++;
if(value_Mume == 4)
{
value_Mume = 0;
}
while(KEY0_01 == 0);
}
}

if(value_Mume == 1)
{
TR0 = 0;
LED0_02 = 0;
if(KEY0_02 == 0)
{
Delay_1ms(10);
if(KEY0_02 == 0)
{
value_shio--;
if(value_shio == -1)
{
value_shio = 23;
}
while(KEY0_02 == 0);
}
}
if(KEY0_03 == 0)
{
Delay_1ms(10);
if(KEY0_03 == 0)
{
value_shio++;
if(value_shio == 24)
{
value_shio = 0;
}
while(KEY0_03 == 0); //检测松手
}
}
}

else if(value_Mume == 2)
{
LED0_02 = 1;
LED0_03 = 0;
if(KEY0_02 == 0)
{
Delay_1ms(10);
if(KEY0_02 == 0)
{
value_feno --;
if(value_feno == -1)
{
value_feno = 59;
}
while(KEY0_02 == 0);
}
}
if(KEY0_03 == 0)
{
Delay_1ms(10);
if(KEY0_03 == 0)
{
value_feno++;
if(value_feno == 60)
{
value_feno = 0;
}
while(KEY0_03 == 0); //检测松手
}
}
}

else if(value_Mume == 3)
{
LED0_03 = 1;
LED0_04 = 0;
if(KEY0_02 == 0)
{
Delay_1ms(10);
if(KEY0_02 == 0)
{
value_miao --;
if(value_miao == -1)
{
value_miao = 59;
}
while(KEY0_02 == 0);
}
}
if(KEY0_03 == 0)
{
Delay_1ms(10);
if(KEY0_03 == 0)
{
value_miao++;
if(value_miao == 60)
{
value_miao = 0;
}
while(KEY0_03 == 0); //检测松手
}
}
}
else
{
LED0_02 = 1;
LED0_03 = 1;
LED0_04 = 1;
TR0 = 1;
}
}
/*
* 函数主体
**/
int main(void)
{
P1 = P2 = P3 = 0x00;
Tero_Init();

while(1)
{
Scan_keyoutCmd();
Disp_layledketCmd(); //执行应运程序
}
}
/*
* 定时器T0中断服务程序
**/
void Tmero()interrupt 1
{
TH0 = 0x3C;
TL0 = 0xB0;
value_temp++;
if(value_temp == 20)
{
value_temp = 0;
value_miao++;
if(value_miao == 60)
{
value_feno++;
value_miao = 0;
if(value_feno == 60)
{
value_shio++;
value_feno = 0;
if(value_shio == 24)
{
value_shio = 0;
}
}
}
}
}
/*
* 定时器T1中断服务程序
**/
void Tmer1()interrupt 3
{
TH1 = 0x4C;
TL1 = 0x00;
value_Date++;
if(value_Date == 5)
{
value_Date = 0;
LED0_01 = ~ LED0_01;
}
}