#include<reg51.h>
#include"lcd.h"
#include"ds1302.h"
typedef unsigned int u16; //?????????????????????
typedef unsigned char u8;
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^2;
sbit K4=P3^3;
sbit beep=P1^5;
u8 start=0;
void Int0Configuration();
void LcdDisplay();
unsigned char SetState,SetPlace;
void Delay10ms(void); //??? 0us
/*******************************************************************************
* ?????? : main
* ???????? : ??????
* ???? : ??
* ??? : ??
*******************************************************************************/
void delay(u16 y)
{
while(y--);
}
void sound()
{
u8 y=100;
while(y--)
{
beep=~beep;
delay(100);
}
}
void keypros()
{
if(K1==0&&K2==0) //???
{
delay(1000);
if(K1==0&&K2==0)
{
start=1;
}
while(!(K1&&K2));
}
if(K4==0&&K1==0) //??
{
delay(1000);
if(K4==0&&K1==0)
{
start=0;
}
while(!K4&&K1);
}
}
void main()
{
unsigned char i;
unsigned char U;
Int0Configuration();
LcdInit();
Ds1302Init();
while(1)
{
if(SetState==0)
{
Ds1302ReadTime();
}
else
{
if(K1==0) //?????K1?????
{
Delay10ms(); //????????
if(K1==0)
{
SetPlace++;
if(SetPlace>=7)
SetPlace=0;
}
while((i<50)&&(K1==0)) //???????????
{
Delay10ms();
i++;
}
i=0;
}
if(K2==0) //?????K2?????
{
Delay10ms(); //????????
if(K2==0)
{
TIME[SetPlace]++;
if((TIME[SetPlace]&0x0f)>9) //????BCD??
{
TIME[SetPlace]=TIME[SetPlace]+6;
}
if((TIME[SetPlace]>=0x60)&&(SetPlace<2)) //????????59
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x24)&&(SetPlace==2)) //С?????23
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x32)&&(SetPlace==3)) //??????31
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x13)&&(SetPlace==4)) //??????12
{
TIME[SetPlace]=1;
}
if((TIME[SetPlace]>=0x7)&&(SetPlace==5)) //??????7
{
TIME[SetPlace]=1;
}
// if(SetPlace==5) //??????12
// {
// TIME[SetPlace]=;
// }
}
while((i<50)&&(K2==0)) //???????????
{
Delay10ms();
i++;
}
i=0;
}
if(K4==0) //?????K2?????
{
Delay10ms(); //????????
if(K4==0)
{
TIME[SetPlace]--;
if((TIME[SetPlace]&0x0f)>9) //????BCD??
{
TIME[SetPlace]=TIME[SetPlace]+6;
}
if((TIME[SetPlace]==0x00)&&(SetPlace<2)) //????????59
{
TIME[SetPlace]=60;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==2)) //С?????23
{
TIME[SetPlace]=24;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==3)) //??????31
{
TIME[SetPlace]=31;
}
if((TIME[SetPlace]==0x01)&&(SetPlace==4)) //??????12
{
TIME[SetPlace]=12;
}
if((TIME[SetPlace]==0x1)&&(SetPlace==5)) //??????7
{
TIME[SetPlace]=7;
}
}
while((U<50)&&(K4==0)) //???????????
{
Delay10ms();
U++;
}
U=0;
}
}
LcdDisplay();
keypros();
if(start==1)
{
sound();
}
else
{
beep=1;
}
if((TIME[1]==0)&&(TIME[2]==0))
{
while(1)
{
sound();
}
}
}
/* if(TIME[1]==0&&TIME[2]==0)
{
while(1)
{
sound();
}
}*/
}
/*******************************************************************************
* ?????? : LcdDisplay()
* ???????? : ???????
* ???? : ??
* ??? : ??
*******************************************************************************/
void LcdDisplay()
{
LcdWriteCom(0x80+0X40);
LcdWriteData('0'+TIME[2]/16); //?
LcdWriteData('0'+(TIME[2]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[1]/16); //??
LcdWriteData('0'+(TIME[1]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[0]/16); //??
LcdWriteData('0'+(TIME[0]&0x0f));
LcdWriteCom(0x80);
LcdWriteData('2');
LcdWriteData('0');
LcdWriteData('0'+TIME[6]/16); //??
LcdWriteData('0'+(TIME[6]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[4]/16); //??
LcdWriteData('0'+(TIME[4]&0x0f));
LcdWriteData('-');
LcdWriteData('0'+TIME[3]/16); //??
LcdWriteData('0'+(TIME[3]&0x0f));
LcdWriteCom(0x8D);
LcdWriteData('0'+(TIME[5]&0x07)); //????
}
/*******************************************************************************
* ?????? : Int0Configuration()
* ???????? : ???????ж?0
* ???? : ??
* ??? : ??
*******************************************************************************/
void Int0Configuration()
{
//????INT0
IT0=1;//????????????????????
EX0=1;//??INT0???ж??????
EA=1;//?????ж?
}
/*******************************************************************************
* ?????? : Int0()
* ???????? : ???ж?0 ?ж????
* ???? : ??
* ??? : ??
*******************************************************************************/
void Int0() interrupt 0
{
Delay10ms();
if(K3==0)
{
SetState=~SetState;
SetPlace=0;
Ds1302Init();
}
}
/*******************************************************************************
* ?????? : Delay10ms
* ???????? : ????????????10ms
* ???? : ??
* ??? : ??
*******************************************************************************/
void Delay10ms(void) //??? 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
下面是
#include"lcd.h"
/*******************************************************************************
* ?? ?? ?? : Lcd1602_Delay1ms
* ???????? : ????????????1ms
* ?? ?? : c
* ?? ?? : ??
* ? ?? : ?ú???????12MHZ?????£?12???????????????
*******************************************************************************/
void Lcd1602_Delay1ms(uint c) //??? 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*******************************************************************************
* ?? ?? ?? : LcdWriteCom
* ???????? : ??LCDд?????????????
* ?? ?? : com
* ?? ?? : ??
*******************************************************************************/
#ifndef LCD1602_4PINS //????ж??????LCD1602_4PINS?
void LcdWriteCom(uchar com) //д??????
{
LCD1602_E = 0; //???
LCD1602_RS = 0; //?????????
LCD1602_RW = 0; //???д??
LCD1602_DATAPINS = com; //????????
Lcd1602_Delay1ms(1); //??????????
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5); //???????
LCD1602_E = 0;
}
#else
void LcdWriteCom(uchar com) //д??????
{
LCD1602_E = 0; //???????
LCD1602_RS = 0; //???д??????
LCD1602_RW = 0; //???д??
LCD1602_DATAPINS = com; //????4λ?????????P0??????λ????????????λ?????
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
// Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //???????λ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* ?? ?? ?? : LcdWriteData
* ???????? : ??LCDд?????????????
* ?? ?? : dat
* ?? ?? : ??
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdWriteData(uchar dat) //д??????
{
LCD1602_E = 0; //???????
LCD1602_RS = 1; //???????????
LCD1602_RW = 0; //???д??
LCD1602_DATAPINS = dat; //д??????
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5); //???????
LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat) //д??????
{
LCD1602_E = 0; //???????
LCD1602_RS = 1; //???д??????
LCD1602_RW = 0; //???д??
LCD1602_DATAPINS = dat; //????4λ?????????P0??????λ????????????λ?????
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = dat << 4; //д?????λ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //д?????
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* ?? ?? ?? : LcdInit()
* ???????? : ?????LCD??
* ?? ?? : ??
* ?? ?? : ??
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdInit() //LCD??????????
{
LcdWriteCom(0x38); //?????
LcdWriteCom(0x0c); //?????????????
LcdWriteCom(0x06); //д???????1
LcdWriteCom(0x01); //????
LcdWriteCom(0x80); //??????????????
}
#else
void LcdInit() //LCD??????????
{
LcdWriteCom(0x32); //??8λ??????4λ????
LcdWriteCom(0x28); //????λ???μ?????
LcdWriteCom(0x0c); //?????????????
LcdWriteCom(0x06); //д???????1
LcdWriteCom(0x01); //????
LcdWriteCom(0x80); //??????????????
}
#endif
再是ds1302时钟芯片的程序
#include"ds1302.h"
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
uchar TIME[7] = {0, 0, 0x12, 0x20, 0x10, 0x02, 0x18};
void Ds1302Write(uchar addr, uchar dat)
{
uchar n;
RST = 0;
_nop_();
SCLK = 0;
_nop_();
RST = 1;
_nop_();
for (n=0; n<8; n++)
{
DSIO = addr & 0x01;
addr >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
for (n=0; n<8; n++)
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;
_nop_();
}
uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
_nop_();
SCLK = 0;
_nop_();
RST = 1;
_nop_();
for(n=0; n<8; n++)
{
DSIO = addr & 0x01;
addr >>= 1;
SCLK = 1;//???????????????DS1302???????
_nop_();
SCLK = 0;//DS1302????????????????
_nop_();
}
_nop_();
for(n=0; n<8; n++)//???8λ????
{
dat1 = DSIO;//?????λ???????
dat = (dat>>1) | (dat1<<7);
SCLK = 1;
_nop_();
SCLK = 0;//DS1302????????????????
_nop_();
}
RST = 0;
_nop_(); //?????DS1302??λ????????,??????
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
/*******************************************************************************
* ?? ?? ?? : Ds1302Init
* ???????? : ?????DS1302.
* ?? ?? : ??
* ?? ?? : ??
*******************************************************************************/
void Ds1302Init()
{
uchar n;
Ds1302Write(0x8E,0X00); //???д????????????д????????
for (n=0; n<7; n++)//д??7??????????????????????????
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //??д????????
}
/*******************************************************************************
* ?? ?? ?? : Ds1302ReadTime
* ???????? : ?????????
* ?? ?? : ??
* ?? ?? : ??
*******************************************************************************/
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//???7??????????????????????????
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}
|