首先準(zhǔn)備材料吧:SEG數(shù)碼管4個共陰極(小編用的共陽極的)、10k排阻(小編沒有直接做了個)、51單片機、按鈕、30p電容、12MHz晶振、10k電阻、10uf電容、插針、74ls245(小編用三極管代替了)。
成品圖
原理圖
主程序:
#include "Key.h"
#include "led.h"
uchar timeSetBuff[8]={'0','0',LED_MODE_COUNT,'0','0',LED_MODE_COUNT,'0','0'};//時鐘數(shù)據(jù)緩沖區(qū)
uchar alarmSetBuff[8]={'0','0',LED_MODE_ALARM,'0','0',LED_MODE_ALARM,'0','0'};//鬧鐘時間數(shù)據(jù)緩沖區(qū)
uchar timeDisMod=0x24; //00100100
uchar secondTemp,minuteTemp,hourTemp;
uchar secondAlarm,minuteAlarm,hourAlarm;
uchar timer0Temp;
uchar alarmKeyCount; //鬧鐘起停鍵計數(shù)
bit alarm_is_ok; //鬧鐘起停狀態(tài)
bit time_is_ok; //時間是否到
bit interface_mod; // 顯示界面模式,0為主時間界面,1為設(shè)置界面
/********************************************************************************************
* 函數(shù)名稱:定時器0
* 功 能:定時器0初始化
* 入口參數(shù):無
* 出口參數(shù):無
*********************************************************************************************/
void initTimer0(void) //定時10ms
{
TMOD |= 0x01; //GATE=0,TR=1運行;C/T=1,counter,0,timer;01十六進制
TH0 = 0xdc;
TL0 = 0x00;
TR0 = 1; //timer0 控制位,為1時啟動timer0
ET0 = 1; //timer0 中斷使能
}
/********************************************************************************************
* 函數(shù)名稱:時鐘比較
* 功 能:比較時鐘和鬧鐘的時間緩沖區(qū),只比較時和分
* 入口參數(shù):無
* 出口參數(shù):無
*********************************************************************************************/
void timeCompare(void)
{
if((minuteTemp == minuteAlarm) && (hourTemp == hourAlarm))
{
time_is_ok = 1;
alarm_is_ok = 0;
interface_mod = 1;
}
}
/********************************************************************************************
* 函數(shù)名稱:主函數(shù)
* 功 能:調(diào)用并執(zhí)行各個子函數(shù)
* 入口參數(shù):無
* 出口參數(shù):無
*********************************************************************************************/
void main()
{
uchar keyRead;
init_led();
init_key();
initTimer0();
timer0Temp = 0x00;
secondTemp = 0x00;
minuteTemp = 0x00;
hourTemp = 0x00;
secondAlarm = 0x00;
minuteAlarm = 0x00;
hourAlarm = 0x00;
alarmKeyCount = 0x02;
alarm_is_ok = 0; //默認狀態(tài)停止
time_is_ok = 0; //默認時間未到
interface_mod = 0;
sei();
while(1)
{
if(interface_mod == 1)
ledSweepDisplay(alarmSetBuff,timeDisMod);
else
ledSweepDisplay(timeSetBuff,timeDisMod);
if(alarm_is_ok == 1)
timeCompare();
keyRead = get_key_value();
if(keyRead == KEY_ALARM)
{
if(alarm_is_ok == 1 || time_is_ok == 1)
{
alarm_is_ok = 0;
time_is_ok = 0;
interface_mod = 0;
alarmKeyCount = 0x00;
}
else
{
if(alarmKeyCount
alarmKeyCount++;
else
alarmKeyCount = 0x00;
if(alarmKeyCount == 0x01)
interface_mod = 1;
if(alarmKeyCount == 0x02)
{
alarm_is_ok = 1;
interface_mod = 0;
}
}
}
if(keyRead == KEY_SECOND)
{ if(interface_mod == 1)
{
if(secondAlarm
secondAlarm++;
else
secondAlarm = 0x00;
alarmSetBuff[S_L] = secondAlarm % 10 + '0';
alarmSetBuff[S_H] = secondAlarm / 10 + '0';
}
else
{
if(secondTemp
secondTemp += 1;
else
secondTemp = 0x00;
timeSetBuff[S_L] = secondTemp % 10 + '0';
timeSetBuff[S_H] = secondTemp / 10 + '0';
}
}
if(keyRead == KEY_MINUTE)
{ if(interface_mod == 1)
{
if(minuteAlarm
minuteAlarm++;
else
minuteAlarm=0x00;
alarmSetBuff[M_L]=minuteAlarm%10+'0';
alarmSetBuff[M_H]=minuteAlarm/10+'0';
}
else
{
if(minuteTemp
minuteTemp += 1;
else
minuteTemp = 0x00;
timeSetBuff[M_L] = minuteTemp % 10 + '0';
timeSetBuff[M_H] = minuteTemp / 10 + '0';
}
}
if(keyRead == KEY_HOUR)
{
if(interface_mod == 1)
{
if(hourAlarm
hourAlarm++;
else
hourAlarm = 0x00;
alarmSetBuff[H_L] = hourAlarm % 10 + '0';
alarmSetBuff[H_H] = hourAlarm / 10 + '0';
}
else
{
if(hourTemp
hourTemp += 1;
else
hourTemp = 0x00;
timeSetBuff[H_L] = hourTemp%10+'0';
timeSetBuff[H_H] = hourTemp/10+'0';
}
}
}
}
/*, */
/********************************************************************************************
* 函數(shù)名稱:定時器0中斷函數(shù)
* 功 能:定時器0溢出中斷入口處,定時10ms
* 入口參數(shù):無
* 出口參數(shù):無
*********************************************************************************************/
void timer0_overflow(void) interrupt 1
{
TH0 = 0xdc;
TL0 = 0x00;
if(time_is_ok == 1) //報警
WARN_TRUMPET = ~WARN_TRUMPET;
if(timer0Temp
timer0Temp++;
else //進中斷100次
{
timer0Temp = 0x00;
if(secondTemp
{
secondTemp++;
}
else
{
secondTemp = 0x00;
if(minuteTemp
minuteTemp++;
else
{
minuteTemp = 0x00;
if(hourTemp
hourTemp++;
else
hourTemp = 0x00;
}
}
}
timeSetBuff[S_L] = secondTemp%10+'0';
timeSetBuff[S_H] = secondTemp/10+'0';
timeSetBuff[M_L] = minuteTemp%10+'0';
timeSetBuff[M_H] = minuteTemp/10+'0';
timeSetBuff[H_L] = hourTemp%10+'0';
timeSetBuff[H_H] = hourTemp/10+'0';
sei();
}
-
電路圖
+關(guān)注
關(guān)注
10325文章
10718瀏覽量
528378 -
51單片機
+關(guān)注
關(guān)注
273文章
5699瀏覽量
123228 -
數(shù)碼管
+關(guān)注
關(guān)注
32文章
1876瀏覽量
90970
原文標(biāo)題:51單片機數(shù)碼管動態(tài)時鐘電路圖
文章出處:【微信號:changxuemcu,微信公眾號:暢學(xué)單片機】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論