|
程序清單
/*-----------------------------------------------
名稱:DS1302時(shí)鐘數(shù)碼管顯示
內(nèi)容:DS1302實(shí)時(shí)時(shí)鐘數(shù)碼管顯示,只顯示時(shí)間。并通過4個(gè)按鍵加減小時(shí)、分鐘。
------------------------------------------------*/
#include<reg52.h> //包含頭文件,一般情況不需要改動(dòng),頭文件包含特殊功能寄存器的定義
#include "ds1302.h"
#define KeyPort P3 //定義按鍵端口
#define DataPort P0 //定義數(shù)據(jù)端口 程序中遇到DataPort 則用P0 替換
sbit LATCH1=P2^2;//定義鎖存使能端口 段鎖存
sbit LATCH2=P2^3;// 位鎖存
bit ReadTimeFlag;//定義讀時(shí)間標(biāo)志
unsigned char code dofly_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 顯示段碼值0~9
unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//分別對(duì)應(yīng)相應(yīng)的數(shù)碼管點(diǎn)亮,即位碼
unsigned char TempData[8]; //存儲(chǔ)顯示值的全局變量
void DelayUs2x(unsigned char t);//us級(jí)延時(shí)函數(shù)聲明
void DelayMs(unsigned char t); //ms級(jí)延時(shí)
void Display(unsigned char FirstBit,unsigned char Num);//數(shù)碼管顯示函數(shù)
unsigned char KeyScan(void);//鍵盤掃描
void Init_Timer0(void);//定時(shí)器初始化
/*------------------------------------------------
主函數(shù)
------------------------------------------------*/
void main (void)
{
unsigned char num;
nit_Timer0();
Ds1302_Init();
while (1) //主循環(huán)
{
num=KeyScan();
switch(num)
{
case 1:time_buf1[4]++;if(time_buf1[4]==24)time_buf1[4]=0;
Ds1302_Write_Time();break; //正常時(shí)間 小時(shí) 加1
case 2:time_buf1[4]--;if(time_buf1[4]==255)time_buf1[4]=23;
Ds1302_Write_Time();break; //正常時(shí)間 小時(shí)減1
case 3:time_buf1[5]++;if(time_buf1[5]==60)time_buf1[5]=0;
Ds1302_Write_Time();break;//分加1
case 4:time_buf1[5]--;if(time_buf1[5]==255)time_buf1[5]=59;
Ds1302_Write_Time();break; //分減1
default:break;
}
f(ReadTimeFlag==1)
{
ReadTimeFlag=0;
Ds1302_Read_Time();
//數(shù)據(jù)的轉(zhuǎn)換,因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
TempData[0]=dofly_DuanMa[time_buf1[4]/10]; //時(shí)
TempData[1]=dofly_DuanMa[time_buf1[4]%10];
TempData[2]=0x40; //加入"-"
TempData[3]=dofly_DuanMa[time_buf1[5]/10]; //分
TempData[4]=dofly_DuanMa[time_buf1[5]%10];
TempData[5]=0x40;
TempData[6]=dofly_DuanMa[time_buf1[6]/10]; //秒
TempData[7]=dofly_DuanMa[time_buf1[6]%10];
}
}
}
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時(shí)1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void Display(unsigned char FirstBit,unsigned char Num)
{
static unsigned char i=0;
DataPort=0; //清空數(shù)據(jù),防止有交替重影
LATCH1=1; //段鎖存
LATCH1=0;
DataPort=dofly_WeiMa[i+FirstBit]; //取位碼
LATCH2=1; //位鎖存
LATCH2=0;
DataPort=TempData[ i]; //取顯示數(shù)據(jù),段碼
LATCH1=1; //段鎖存
LATCH1=0;
i++;
if(i==Num)
i=0;
}
/*------------------------------------------------
定時(shí)器初始化子程序
------------------------------------------------*/
void Init_Timer0(void)
{
TMOD |= 0x01; //使用模式1,16位定時(shí)器,使用"|"符號(hào)可以在使用多個(gè)定時(shí)器時(shí)不受影響
//TH0=0x00; //給定初值
//TL0=0x00;
EA=1; //總中斷打開
ET0=1; //定時(shí)器中斷打開
TR0=1; //定時(shí)器開關(guān)打開
}
/*------------------------------------------------
定時(shí)器中斷子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
static unsigned int num;
TH0=(65536-2000)/256; //重新賦值 2ms
TL0=(65536-2000)%256;
Display(0,8); // 調(diào)用數(shù)碼管掃描
num++;
if(num==50) //大致100ms
{
num=0;
ReadTimeFlag=1; //讀標(biāo)志位置1
}
}
/*------------------------------------------------
按鍵掃描函數(shù),返回掃描鍵值
------------------------------------------------*/
unsigned char KeyScan(void)
{
unsigned char keyvalue;
if(KeyPort!=0xff)
{
DelayMs(10);
if(KeyPort!=0xff)
{
keyvalue=KeyPort;
while(KeyPort!=0xff);
switch(keyvalue)
{
case 0xfe:return 1;break;
case 0xfd:return 2;break;
case 0xfb:return 3;break;
case 0xf7:return 4;break;
case 0xef:return 5;break;
case 0xdf:return 6;break;
case 0xbf:return 7;break;
case 0x7f:return 8;break;
default:return 0;break;
}
}
}
return 0;
}
|
|