innt();//液晶初始化
cominnt();//chun kou chu shi hua
sprintf( comstart,"Ds1302 for LCD1602 time clok
");fcom(31);
sprintf( comstart,"innting........
");fcom(19);// jingru qi dong biao
biao1=1;P1=0xff;biao2=0;biao3=0;
while(1)//zhu xun huan
{
if(RI==1){bao=comrt();
if(bao=='o')sprintf( comstart,"com on
");
if(bao!='o')sprintf( comstart,"com off
");
fcom(11);
}
key=keyscan();//调用键盘扫miao
switch(key)
{
/* case 0x7e:biao1=0;break;//0 按下相应的键显示相对应的码值
case 0x7d:biao1=1;break;//1
case 0x7b:P0=0xa4;break;//2
case 0x77:P0=0xb0;break;//3
case 0xbe:P0=0x99;break;//4
case 0xbd:P0=0x92;break;//5
case 0xbb:P0=0x82;break;//6
case 0xb7:P0=0xf8;break;//7
case 0xde:P0=0x80;break;//8
case 0xdd:P0=0x90;break;//9*/
//case 0xdb:biao1=0;break;//a
/* case 0xd7:P0=0x83;break;//b
case 0xee:P0=0xc6;break;//c*/
case 0xee:biao1=0;break;//d queren
// case 0xeb:P0=0x86;break;//e
// case 0xe7:P0=0x8e;break;
}
if(biao1==0){
biao2=0;
wchar2(0,'S');wchar2(1,'e');wchar2(2,'t');wchar2(3,':'); //xiang LCD shang zen jia SET biao zhi
sprintf(comstart,"Rtc set mode
");fcom(15);//fa song chuan xingbiao qi
while(!biao1)//jin ru she zhi zhu xin huan
{
key=keyscan();//调用键盘扫描,
switch(key)
{
case 0xe7:biao1=1;break;//quxiao
case 0x7d:biao2=1;break;
case 0x7b:biao2=2;break;
case 0x77:biao2=3;break;
case 0xbe:biao2=4;break;//4
}
if(biao2==1){
sprintf(comstart,"Set RTC for Ds1302
");fcom(22);
xiemd(0x0f);//kai lcd guang biao
while(biao2==1)
{
P1=0xf7; key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
case 0x7b:stfen++;if(stfen>=60)stfen=0;break;//2
case 0x77:stfen--;if(stfen<=0)stfen=60;break;//3
case 0xbd:stshi++;if(stshi>=24)stshi=0;break;//5
case 0xbb:stshi--;if(stshi<=0)stshi=23;break;//6
}
l_tmpdate[1]=stfen;///
fen=l_tmpdate[1];
fenshi=fen/10;//////////fen chu li duan
fenge=fen%10;
wchar2(8,shijian1[fenge]);
wchar2(7,shijian1[fenshi]);///
l_tmpdate[2]=stshi;//
shi=l_tmpdate[2];
shishi=shi/10;/////////////shi chu li duan
shige=shi%10;
wchar2(5,shijian1[shige]);
wchar2(4,shijian1[shishi]);///
while(key==0xbd){key=keyscan();}//an jian song shou jian ce
while(key==0xbb){key=keyscan();}//an jian song shou jian ce
while(key==0x77){key=keyscan();}//an jian song shou jian ce
while(key==0x7b){key=keyscan();}//an jian song shou jian ce
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
sprintf(comstart,"Write RTC for Ds1302
");fcom(25);//fa song chuan xingbiao qi
Set_RTC();//设定完成后写入ds1302
}
if(biao2==2){
sprintf(comstart,"biao2=2
");fcom(13);//fa song chuan xingbiao qi
xiemd(0x0f);
while(biao2==2)
{
P1=0xfb;
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao tui chu
}
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
if(biao2==3){
sprintf(comstart,"biao2=3
");fcom(13);//fa song chuan xingbiao qi
xiemd(0x0f);
while(biao2==3)
{
P1=0xfd; //led zhi shi
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
}
//wchar2(0,' ');
//wchar2(1,' ');wchar2(2,' ');wchar2(3,' ');// xiemd(0x0f);
//d1=1;d2=1;
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
if(biao2==4){
sprintf(comstart,"biao2=4
");fcom(13);//fa song chuan xingbiao qi
while(biao2==4)
{
P1=0xfe; //LED zhi shi
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
}
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
xiemd(0x0c);//kuan bi guan biao
P1=0xff;//led zhi shi quan mei
}
wchar2(0,' ');wchar2(1,' ');wchar2(2,' ');wchar2(3,' ');//qing chu LCD she zhi zhuang tai xian shi
biao3=0;//cong xing sao miao nian yue ri biao ji
}
if(biao1==1){
if(biao3==0){
if(bao=='o'){sprintf(comstart,"Delay .. read in Ds1302 time
");fcom(31);}
delay(30000);
}
Read_RTC();
miao=l_tmpdate[0];
miaoshi=(miao/16)&0x07;
miaoge=miao&0X0f;
wchar2(11,shijian1[miaoge]);
delay(1000);
wchar2(10,shijian1[miaoshi]);miao=(miaoshi*10+miaoge);
delay(1000);
if(biao3==0){if(bao=='o'){sprintf(comstart,"read once rtc
");fcom(15);}
miao=1;biao3=2;}
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#include<stdio.h>
#include<intrins.h>
#define Data P0
sbit SCK=P3^6;
sbit SDA=P3^4;
sbit RST = P3^5; // DS1302复位
sbit rs=P2^4;
sbit rw=P2^5;
sbit e=P2^6;
sbit d1=P1^5;
sbit d2=P1^3;
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char l_tmpdate[7]={0,29,23,4,4,3,11};//秒分时日月周年08-05-15 12:00:00
unsigned char l_tmpdisplay[8];
//bit ReadRTC_Flag;
char data comstart[]=" ";
uchar code riqi[]=" 2011-04-12 MOD ";
uchar code shijian[]=" 00:00:00 ";
uchar code shijian1[]="0123456789";
//uchar code shijian[]=" 00-00-00 ";
/*void InitTIMER0(void) //定时器0初始化
{
TMOD|=0x01;//定时器设置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}*/
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循环8次 写入数据
{
SCK=0;
SDA=temp&0x01; //每次传输低字节
temp>>=1; //右移一位
SCK=1;
}
}
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_(); //启动
Write_Ds1302_Byte(address); //发送地址
Write_Ds1302_Byte(dat); //发送数据
RST=0; //恢复
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循环8次 读取数据
{
if(SDA)
temp|=0x80; //每次传输低字节
SCK=0;
temp>>=1; //右移一位
SCK=1;
}
RST=0;
_nop_(); //以下为DS1302复位的稳定时间
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp); //返回
}
/****************************************************************************/
void Read_RTC(void) //读取 日历
{
unsigned char i,*p;
p=read_rtc_address; //地址传递
for(i=0;i<7;i++) //分7次读取 秒分时日月周年
{
l_tmpdate=Read_Ds1302(*p);
p++;
}
}
/***********************************************************************/
void Set_RTC(void) //设定 日历
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++){ //BCD处理
tmp=l_tmpdate/10;
l_tmpdate=l_tmpdate%10;
l_tmpdate=l_tmpdate+tmp*16;
}
Write_Ds1302(0x8E,0X00);
p=write_rtc_address; //传地址
for(i=0;i<7;i++) //7次写入 秒分时日月周年
{
Write_Ds1302(*p,l_tmpdate);
p++;
}
Write_Ds1302(0x8E,0x80);
}
void cominnt(void)
{
TMOD=0x21;//设置定时器1为工作方式2
TH1=0xfd;//给定时器1赋值高8位
TL1=0xfd;//?给定时器1赋值瓦8位
TR1=1;//启动定时器1
REN=1;//
SM0=0;//设置串行数据方式
SM1=1;//设置串行数据方式
RI=0;
TI=0;
}
/*void tim(void) interrupt 1 using 1//中断,用于数码管扫描
{
static unsigned char i,num;
TH0=0xf5;
TL0=0xe0;
P0=table[l_tmpdisplay]; //查表法得到要显示数字的数码段
P2=table1[7-i];
i++;
if(i==8)
{
i=0;
num++;
if(10==num) //隔段时间读取1302的数据。时间间隔可以调整
{
ReadRTC_Flag=1; //使用标志位判断
num=0;
}
}
}*/
void delay (uint chan)//yan shi han shu
{
while(--chan);
}
//
void xiemd(uchar md)//qu ming ding 写命令 液晶
{
delay(500);
rs=0;
rw=0;
e=0;
delay(1);
e=1;
P0=md;
delay(5);
e=0;
}
//
void xiesu(uchar su)//qu shu ju 写数据 液晶
{
delay(500);
rs=1;
rw=0;
e=0;
delay(1);
e=1;
P0=su;
delay(5);
e=0;
}
//
void wchar1 (uchar age,uchar a)//xie ru ding er hang han shu (age)wei wei shu (B)shi yao xie de shu ju
{
uchar z;
z=0x80+age;
xiemd(z);
xiesu(a);
}
//
void wchar2 (uchar bge,uchar b)//xie ru ding er hang han shu (beg)wei wei shu (B)shi yao xie de shu ju
{
uchar a;
a=0xc0+bge;
xiemd(a);
xiesu(b);
}
void innt()
{
uchar c;
xiemd(0x38);//qing ping
xiemd(0x38);//qing ping
xiemd(0x38);//qing ping
xiemd(0x06);//qing ping
xiemd(0x0c);//xianshi kai guan kongzhi
xiemd(0x01);//gong neng she zhi
for(c=0;c<15;c++){
wchar2(c,shijian[c]);
wchar1(c,riqi[c]);}
}
/////////////////////////////////////////////////////////
uchar keyscan(void)//键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l;//行列值
P3=0x0f; //行线输出全为0
cord_h=P3&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
delay(100); //去抖
if(cord_h!=0x0f)
{
cord_h=P3&0x0f; //读入列线值
P3=cord_h|0xf0; //输出当前列线值
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值并返回
}
}return(0xff); //否则返回固定值0xff
}
void fcom(unsigned char h)
{
unsigned char b;
for(b=0;b<h;b++){
SBUF=comstart;
while(!TI);//判断发送是否完成
TI=0;}
}
/*void comst1 (void)
{
uchar a;
for(a=0;a<7;a++)
{
SBUF=l_tmpdate[a];
while(!TI);//判断发送是否完成
TI=0;//发送标志位清零
}
}*/
unsigned char comrt (void)
{
uchar rd;
{
rd= SBUF;
while(!RI);//判断jieshou是否完成
RI=0;//jieshou 标志位清零
return (rd);
}
}
void main()///////////////////////////////////////////主程序//////////////////////////////////////////////////
{
unsigned char bao=0, miao,miaoshi,miaoge,fen,fenshi,fenge,shi,shishi,shige,biao1,biao2,biao3,key,stfen=30,stshi=30,ri,rishi,rige,stri;//shi jian bian liang sheng ming
innt();//液晶初始化
cominnt();//chun kou chu shi hua
sprintf( comstart,"Ds1302 for LCD1602 time clok ");fcom(31);
sprintf( comstart,"innting........ ");fcom(19);// jingru qi dong biao
biao1=1;P1=0xff;biao2=0;biao3=0;
while(1)//zhu xun huan
{
if(RI==1){bao=comrt();
if(bao=='o')sprintf( comstart,"com on ");
if(bao!='o')sprintf( comstart,"com off ");
fcom(11);
}
key=keyscan();//调用键盘扫miao
switch(key)
{
/* case 0x7e:biao1=0;break;//0 按下相应的键显示相对应的码值
case 0x7d:biao1=1;break;//1
case 0x7b:P0=0xa4;break;//2
case 0x77:P0=0xb0;break;//3
case 0xbe:P0=0x99;break;//4
case 0xbd:P0=0x92;break;//5
case 0xbb:P0=0x82;break;//6
case 0xb7:P0=0xf8;break;//7
case 0xde:P0=0x80;break;//8
case 0xdd:P0=0x90;break;//9*/
//case 0xdb:biao1=0;break;//a
/* case 0xd7:P0=0x83;break;//b
case 0xee:P0=0xc6;break;//c*/
case 0xee:biao1=0;break;//d queren
// case 0xeb:P0=0x86;break;//e
// case 0xe7:P0=0x8e;break;
}
if(biao1==0){
biao2=0;
wchar2(0,'S');wchar2(1,'e');wchar2(2,'t');wchar2(3,':'); //xiang LCD shang zen jia SET biao zhi
sprintf(comstart,"Rtc set mode ");fcom(15);//fa song chuan xingbiao qi
while(!biao1)//jin ru she zhi zhu xin huan
{
key=keyscan();//调用键盘扫描,
switch(key)
{
case 0xe7:biao1=1;break;//quxiao
case 0x7d:biao2=1;break;
case 0x7b:biao2=2;break;
case 0x77:biao2=3;break;
case 0xbe:biao2=4;break;//4
}
if(biao2==1){
sprintf(comstart,"Set RTC for Ds1302 ");fcom(22);
xiemd(0x0f);//kai lcd guang biao
while(biao2==1)
{
P1=0xf7; key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
case 0x7b:stfen++;if(stfen>=60)stfen=0;break;//2
case 0x77:stfen--;if(stfen<=0)stfen=60;break;//3
case 0xbd:stshi++;if(stshi>=24)stshi=0;break;//5
case 0xbb:stshi--;if(stshi<=0)stshi=23;break;//6
}
l_tmpdate[1]=stfen;///
fen=l_tmpdate[1];
fenshi=fen/10;//////////fen chu li duan
fenge=fen%10;
wchar2(8,shijian1[fenge]);
wchar2(7,shijian1[fenshi]);///
l_tmpdate[2]=stshi;//
shi=l_tmpdate[2];
shishi=shi/10;/////////////shi chu li duan
shige=shi%10;
wchar2(5,shijian1[shige]);
wchar2(4,shijian1[shishi]);///
stri=4;
l_tmpdate[3]=stri;
ri=l_tmpdate[3];
rishi=ri/10;
rige=ri%10;
wchar1(10,shijian1[rige]);
wchar1(9,shijian1[rishi]);
while(key==0xbd){key=keyscan();}//an jian song shou jian ce
while(key==0xbb){key=keyscan();}//an jian song shou jian ce
while(key==0x77){key=keyscan();}//an jian song shou jian ce
while(key==0x7b){key=keyscan();}//an jian song shou jian ce
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
sprintf(comstart,"Write RTC for Ds1302 ");fcom(25);//fa song chuan xingbiao qi
Set_RTC();//设定完成后写入ds1302
}
if(biao2==2){
sprintf(comstart,"biao2=2 ");fcom(13);//fa song chuan xingbiao qi
xiemd(0x0f);
while(biao2==2)
{
P1=0xfb;
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao tui chu
}
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
if(biao2==3){
sprintf(comstart,"biao2=3 ");fcom(13);//fa song chuan xingbiao qi
xiemd(0x0f);
while(biao2==3)
{
P1=0xfd; //led zhi shi
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
}
//wchar2(0,' ');
//wchar2(1,' ');wchar2(2,' ');wchar2(3,' ');// xiemd(0x0f);
//d1=1;d2=1;
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
if(biao2==4){
sprintf(comstart,"biao2=4 ");fcom(13);//fa song chuan xingbiao qi
while(biao2==4)
{
P1=0xfe; //LED zhi shi
key=keyscan();//调用键盘扫描?
switch(key)
{
case 0xe7:biao2=0;break;//quxiao
}
while(key==0xe7){key=keyscan();}//an jian song shou jian ce
}
}
xiemd(0x0c);//kuan bi guan biao
P1=0xff;//led zhi shi quan mei
}
wchar2(0,' ');wchar2(1,' ');wchar2(2,' ');wchar2(3,' ');//qing chu LCD she zhi zhuang tai xian shi
biao3=0;//cong xing sao miao nian yue ri biao ji
}
if(biao1==1){
if(biao3==0){
if(bao=='o'){sprintf(comstart,"Delay .. read in Ds1302 time ");fcom(31);}
delay(30000);
}
Read_RTC();
miao=l_tmpdate[0];
miaoshi=(miao/16)&0x07;
miaoge=miao&0X0f;
wchar2(11,shijian1[miaoge]);
delay(1000);
wchar2(10,shijian1[miaoshi]);miao=(miaoshi*10+miaoge);
delay(1000);
if(biao3==0){if(bao=='o'){sprintf(comstart,"read once rtc ");fcom(15);}
miao=1;biao3=2;}
if(miao==1){
if(bao=='o'){sprintf(comstart,"dufen ");fcom(8);}
delay(10000);
fen=l_tmpdate[1];
fenshi=(fen/16)&0x07;
fenge=fen&0X0f;
wchar2(8,shijian1[fenge]);
wchar2(7,shijian1[fenshi]);
if(biao3==2){biao3=1;fen=1;}
if(fen>=1){ if(bao=='o'){sprintf(comstart,"dushi ");fcom(8);}
delay(1000);
shi=l_tmpdate[2];
shishi=(shi/16)&0x07;
shige=shi&0X0f;
wchar2(5,shijian1[shige]);
wchar2(4,shijian1[shishi]);
if(bao=='o'){sprintf(comstart,"du ri ");fcom(8);}
delay(1000);
ri=l_tmpdate[3];
rishi=(ri/16)&0x07;
rige=ri&0X0f;
wchar1(10,shijian1[rige]);
wchar1(9,shijian1[rishi]);
}
}
}
}
}
-----------------------------------------------------------------------
老师叫我们用的是pic16f690. 不用日历功能, 只要时分秒就行,再加上一个闹钟功能,因为到时候我们还需要用面包板连接按钮和扬声器还有1602A,pic16f690芯片的pickit2. 求解答啊 感激不尽啊!!
一周热门 更多>