當前位置:編程學習大全網 - 編程語言 - 求壹個關於用C語言編寫的電子時鐘的程序,內容要有鬧鐘,可以調整時間

求壹個關於用C語言編寫的電子時鐘的程序,內容要有鬧鐘,可以調整時間

#include<reg52.h>

#include<absacc.h>

#include<intrins.h>

#define unit unsigned int

#define uchar unsigned char

//#define HZ 12

sbit key0=P0^0; // 分鐘調整

sbit key1=P0^1; // 小時調整

sbit P2_0=P2^7; //秒 指示燈

sbit MN_RXD=P3^6;

sbit MN_TXD=P3^7;

uchar data CLOCK[4]={0,0,0,12};//存放時鐘時間(百分秒,秒,分,和時位)

//數碼管顯示表0-f 滅

uchar code TABLE[]={0xBE,0x06,0xEA,0x6E,0x56,0x7C,0xFC,0x0E,0xFE,0x7E,0x00};

//**********************************

//模擬串口發送壹個字節數據 函數

//**********************************

void SendData(unsigned char senddata)

{

unsigned char i;

for(i=0;i<8;i++)

{

if((senddata&0x01)==0)

MN_RXD=0;

else

MN_RXD=1;

_nop_();

MN_TXD=0;

_nop_();

MN_TXD=1;

senddata=senddata>>1;

}

}

//**********************************

//顯示程序函數

//**********************************

void display(void)

{

// unsigned int n;

uchar temp;

temp=CLOCK[1]; temp=temp%10; SendData(TABLE[temp]);

temp=CLOCK[1]; temp=temp/10; SendData(TABLE[temp]);

temp=CLOCK[2]; temp=temp%10; SendData(TABLE[temp]);

temp=CLOCK[2]; temp=temp/10; SendData(TABLE[temp]);

temp=CLOCK[3]; temp=temp%10; SendData(TABLE[temp]);

temp=CLOCK[3]; temp=temp/10; SendData(TABLE[temp]);

/*

for(n=0;n<5000;n++);

for(n=0;n<6;n++)

{

SendData(TABLE[10]);

}

*/

}

//**********************************

//按鍵控制函數

//**********************************

void keycan()

{

unsigned int n;

EA=0;

if(key0==0) // 分鐘調整

{

for(n=0;n<10000;n++); //延時去抖動

while(key0==0);

CLOCK[2]=CLOCK[2]+1;

if(CLOCK[2]==60) //到壹時

{

CLOCK[2]=0;

}

display();

}

if(key1==0) // 小時調整

{

for(n=0;n<10000;n++); //延時去抖動

while(key1==0);

CLOCK[3]=CLOCK[3]+1;

if(CLOCK[3]==24)

{

CLOCK[3]=0;

}

display();

}

EA=1;

}

//**********************************

//T0中斷服務函數

//**********************************

void time0() interrupt 1 //using 1

{

TH0=0xD8; TL0=0xF0; //重置初值

// TH0=0xB1; TL0=0xE0;

//時鐘處理

CLOCK[0]=CLOCK[0]+1;

}

//**********************************

//主函數

//**********************************

void main()

{

EA=1;

ET0=1;

TMOD=0x01; //T0方式1定時

TH0=0xD8; TL0=0xF0; //D8F0 定時10ms

// TH0=0xB1; TL0=0xE0; //定時 20ms

TR0=1;

for(;;)

{

if(CLOCK[0]==100) //到壹秒 10ms*100

{

CLOCK[0]=0;

P2_0=~P2_0;

CLOCK[1]=CLOCK[1]+1;

if(CLOCK[1]==60) //到壹分

{

CLOCK[1]=0;

CLOCK[2]=CLOCK[2]+1;

if(CLOCK[2]==60) //到壹時

{

CLOCK[2]=0;

CLOCK[3]=CLOCK[3]+1;

if(CLOCK[3]==24)

{

CLOCK[3]=0;

}

}

}

display();

}

keycan();

}

}

  • 上一篇:微信服務號怎麽開發
  • 下一篇:廣數980Tdb宏程序怎麽車90乘8的梯形螺紋
  • copyright 2024編程學習大全網