好酷屋

利用单片机的定时器和数码管,设计一个电子时钟

好酷屋

发布于2023-03-09
标签:==更多>

好酷屋教程网小编为您收集和整理了利用单片机的定时器和数码管,设计一个电子时钟的相关教程:给你个电路和程序,//前几天帮一个网友写,单片机定时时钟在调节功能,可以参考一下我程序/****************************************************6位数码

给你个电路和程序,//前几天帮一个网友写,单片机定时时钟在调节功能,可以参考一下我程序

/***************************************************
* 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 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_Outk = 0;
unsigned char value_shio = 8;
unsigned char value_feno = 40;
unsigned char value_miao = 0;
unsigned char code value_tab0[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; //共阳极unsigned char code value_tab1[]={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10}; //共阳极带点

/*
* 延时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 = 0x01;
TH0 = 0x4C;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/* * 按键开关【启动和停止】子函数
**/
void Scan_keyoutCmd(void)
{
if(KEY0_01 == 0)
{
Delay_1ms(10);
if(KEY0_01 == 0)
{
TR0 = ~ TR0;
}
while(KEY0_01 == 0);
}
if(KEY0_02 == 0) {
Delay_1ms(10);
if(KEY0_02 == 0)
{
value_miao--;
if(value_miao == -1)
{
value_feno--;
value_miao = 59;
if(value_feno == -1)
{
value_shio--;
value_feno = 59;
if(value_shio == -1)
{
value_shio = 23;
}
}
}
while(KEY0_02 == 0);
}
}
if(KEY0_03 == 0) {
Delay_1ms(10);
if(KEY0_03 == 0)
{
value_miao++;
if(value_miao == 60)
{
value_miao = 0;
value_feno++;
if(value_feno == 60)
{
value_feno = 0;
value_shio++;
if(value_shio == 24)
{
value_shio = 0;
}
}
}
while(KEY0_03 == 0); //检测松手
}
}
}
/* * 函数主体
**/
int main(void)
{
P1 = P2 = P3 = 0x00;
Tero_Init();

while(1)
{
Scan_keyoutCmd();
Disp_layledketCmd(); //执行应运程序
}
}
/* * 定时器中断服务程序
**/
void Tmero()interrupt 1
{
TH0 = 0x4C;
TL0 = 0x00;
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;
}
}
} }
}

以上就是好酷屋教程网小编为您收集和整理的数码管,定时器,单片机相关内容,如果对您有帮助,请帮忙分享这篇文章^_^

本文来源: https://www.haoku5.com/youxi/6409bca9402607fa1205320b.html

相关推荐

    热门专题