- //************1602液晶显示程序************
- #include<reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchara,miao,shi,fen,ri,yue,nian,week,flag1,key1n,temp;
- #define yh 0x80
- #define er 0x80+0x40
- //DS1302时钟芯片与C51之间的引脚连接定义
- sbit IO=P1^1;
- sbit SCLK=P1^0;
- sbit RST=P1^2;
- sbit ACC0=ACC^0;
- sbit ACC7=ACC^7;
- //校时按键与C51的引脚连接定义
- sbit key1=P1^5; //设置键
- sbit key2=P1^6; //加键
- sbit key3=P1^7; //减键
- //sbit buzzer=P1^3; //蜂鸣器,通过三极管9012驱动,端口低电平响
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- typedef bit BOOL ;
- extern GetTemp(void);
- extern unsigned int idata Temperature;
- extern Init18b20 ();
- //unsigned char a,b,c;
- sbit lcden=P2^7; //液晶使能位
- sbit lcdrw=P2^5; //液晶读写位
- sbit lcdrs=P2^6; //数据命令选择位
- uchar code tab1[]={"2011-07-12 "}; //年显示的固定字符
- uchar code tab2[]={"19:50:00"}; //时间显示的固定字符
-
- void w_cmd(uint date);
- void w_data(uint date);
- /************定时器1子函数***********/
- void system_Ini()
- {
- TMOD|= 0x11;
- TH1=0x8e; //11.0592M
- TL1=0x33;
- IE= 0x8A;
- TR1 = 1;
- }
- /************延时子函数***********/
- void delay(uchar x)
- {
- uchar y,z;
- for(y=x;y>0;y--)
- for(z=110;z>0;z--);
- }
- /*************** 液晶初始化和写命令及数据命令********************/
- void lcd_init()
- {
- lcden=0; //初始使能端
- lcdrw=0; // 写数据选择
- w_cmd(0x38); //设置液晶工作模式,意思:16*2行显示,5*7点阵,8位数据
- w_cmd(0x01); //清显示
- w_cmd(0x0c); //开显示不显示光标
- w_cmd(0x06); //整屏不移动,光标自动右移
- w_cmd(yh+1); //日历显示固定符号从第一行第1个位置之后开始显示
- for(a=0;a<14;a++)
- {
- w_data(tab1[a]); //向液晶屏写日历显示的固定符号部分
- delay(3);
- }
- w_cmd(er+2); //时间显示固定符号写入位置,从第2个位置后开始显示
- for(a=0;a<8;a++)
- {
- w_data(tab2[a]); //写显示时间固定符号,两个冒号
- delay(3);
- }
- }
- void w_cmd(uint date) //“写”命令子程序
- {
- lcdrs=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void w_data(uint date) //”写“数据子程序
- {
- lcdrs=1;
- P0=date;
- delay(3);
- lcden=1;
- delay(5);
- lcden=0;
- }
- /***************DS1302有关子函数********************/
- void write_byte(uchar dat)//写一个字节
- {
- ACC=dat;
- RST=1;
- for(a=8;a>0;a--)
- {
- IO=ACC0;
- SCLK=0;
- SCLK=1;
- ACC=ACC>>1;
- }
- }
- uchar read_byte() //读一个字节
- {
- RST=1;
- for(a=8;a>0;a--)
- {
- ACC7=IO;
- SCLK=1;
- SCLK=0;
- ACC=ACC>>1;
- }
- return(ACC);
- }
- //----------------------------------------
- void write_1302(uchar add,uchar dat) //向1302芯片写函数,指定写入地址,数据
- {
- RST=0;
- SCLK=0;
- RST=1;
- write_byte(add);
- write_byte(dat);
- SCLK=1;
- RST=0;
- }
- uchar read_1302(uchar add) //从1302读数据函数,指定读取数据来源地址
- {
- uchartemp;
- RST=0;
- SCLK=0;
- RST=1;
- write_byte(add);
- temp=read_byte();
- SCLK=1;
- RST=0;
- return(temp);
- }
- uchar BCD_Decimal(uchar bcd) //BCD码转十进制函数,输入BCD,返回十进制
- {
- uchar Decimal;
- Decimal=bcd>>4;
- return(Decimal=Decimal*10+(bcd&=0x0F));
- }
- //--------------------------------------
- void ds1302_init() //1302芯片初始化子函数(2010-01-07,12:00:00,week4)
- {
- RST=0;
- SCLK=0;
- write_1302(0x8e,0x00); //允许写,禁止写保护
- write_1302(0x80,0x00); //向DS1302内写秒寄存器80H写入初始秒数据00
- write_1302(0x82,0x53); //向DS1302内写分寄存器82H写入初始分数据00
- write_1302(0x84,0x19); //向DS1302内写小时寄存器84H写入初始小时数据12
- write_1302(0x8a,0x02); //向DS1302内写周寄存器8aH写入初始周数据4
- write_1302(0x86,0x12); //向DS1302内写日期寄存器86H写入初始日期数据07
- write_1302(0x88,0x07); //向DS1302内写月份寄存器88H写入初始月份数据01
- write_1302(0x8c,0x11); //向DS1302内写年份寄存器8cH写入初始年份数据10
- write_1302(0x8e,0x80); //打开写保护
- }
- /********温度显示子函数*********/
- void write_temp(uchar add,uchar dat) //向LCD写温度数据,并指定显示位置
- {
- uchargw,sw;
- gw=dat%10; //取得个位数字
- sw=dat/10; //取得十位数字
- w_cmd(er+add); //er是头文件规定的值0x80+0x40
- w_data(0x30+sw); //数字+30得到该数字的LCD1602显示码
- w_data(0x30+gw); //数字+30得到该数字的LCD1602显示码
- w_data(0xdf); //显示温度的小圆圈符号,0xdf是液晶屏字符库的该符号地址码
- w_data(0x43); //显示"C"符号,0x43是液晶屏字符库里大写C的地址码
- }
- /******** 时间显示子函数*********/
- void write_sfm(uchar add,uchar dat) //向LCD写时分秒,有显示位置加、现示数据,两个参数
- {
- uchargw,sw;
- gw=dat%10; //取得个位数字
- sw=dat/10; //取得十位数字
- w_cmd(er+add); //er是头文件规定的值0x80+0x40
- w_data(0x30+sw); //数字+30得到该数字的LCD1602显示码
- w_data(0x30+gw); //数字+30得到该数字的LCD1602显示码
-
- }
- /********年、月、日显示子函数*********/
- void write_nyr(uchar add,uchar dat) //向LCD写年月日,有显示位置加数、显示数据,两个参数
- {
- uchargw,sw;
- gw=dat%10; //取得个位数字
- sw=dat/10; //取得十位数字
- w_cmd(yh+add); //设定显示位置为第一个位置+add
- w_data(0x30+sw); //数字+30得到该数字的LCD1602显示码
- w_data(0x30+gw); //数字+30得到该数字的LCD1602显示码
-
- }
- /*******写星期子函数*********/
- void write_week(uchar week) //写星期函数
- {
- w_cmd(yh+0x0c); //星期字符的显示位置
- switch(week)
- {
- case 1:w_data('M'); //星期数为1时,显示
- w_data('O');
- w_data('N');
- break;
-
- case 2:w_data('T'); //星期数据为2时显示
- w_data('U');
- w_data('E');
- break;
-
- case 3:w_data('W'); //星期数据为3时显示
- w_data('E');
- w_data('D');
- break;
-
- case 4:w_data('T'); //星期数据为4是显示
- w_data('H');
- w_data('U');
- break;
-
- case 5:w_data('F'); //星期数据为5时显示
- w_data('R');
- w_data('I');
- break;
-
- case 6:w_data('S'); //星期数据为6时显示
- w_data('T');
- w_data('A');
- break;
-
- case 7:w_data('S'); //星期数据为7时显示
- w_data('U');
- w_data('N');
- break;
- }
- }
-
- /*******键盘扫描有关函数*********/
- void keyscan()
- {
- if(key1==0) //key1为功能键(设置键)
- {
- delay(9); //延时,用于消抖动
- if(key1==0) //延时后再次确认按键按下
- {
- // buzzer=0; //蜂鸣器短响一次
- // delay(20);
- // buzzer=1;
- while(!key1);
- key1n++;
- if(key1n==9)
- key1n=1; //设置按键共有秒、分、时、星期、日、月、年、返回,8个功能循环
- switch(key1n)
- {
-
- case1: TR0=0; //关闭定时器
- //TR1=0;
- w_cmd(er+0x09); //设置按键按动一次,秒位置显示光标
- w_cmd(0x0f); //设置光标为闪烁
- temp=(miao)/10*16+(miao)%10; //秒数据写入DS1302
- write_1302(0x8e,0x00);
- write_1302(0x80,0x80|temp);//miao
- write_1302(0x8e,0x80);
- break;
- case2: w_cmd(er+6); //按2次fen位置显示光标
-
- //write_1602com(0x0f);
- break;
- case3: w_cmd(er+3); //按动3次,shi
- //write_1602com(0x0f);
- break;
- case4: w_cmd(yh+0x0e); //按动4次,week
- //write_1602com(0x0f);
- break;
- case5: w_cmd(yh+0x0a); //按动5次,ri
- //write_1602com(0x0f);
- break;
- case6: w_cmd(yh+0x07); //按动6次,yue
- //write_1602com(0x0f);
- break;
- case7: w_cmd(yh+0x04); //按动7次,nian
- //write_1602com(0x0f);
- break;
- case8:
- w_cmd(0x0c); //按动到第8次,设置光标不闪烁
- TR0=1;//打开定时器
- temp=(miao)/10*16+(miao)%10;
- write_1302(0x8e,0x00);
- write_1302(0x80,0x00|temp);//miao数据写入DS1302
- write_1302(0x8e,0x80);
- break;
-
- }
- }
- }
- //------------------------------加键key2----------------------------
- if(key1n!=0) //当key1按下以下。再按以下键才有效(按键次数不等于零)
- {
- if(key2==0) //上调键
- {
- delay(10);
- if(key2==0)
- {
- // buzzer=0; //蜂鸣器短响一次
- // delay(20);
- // buzzer=1;
- while(!key2);
- switch(key1n)
- {
- case1:miao++; //设置键按动1次,调秒
- if(miao==60)
- miao=0;
- write_sfm(0x08,miao);
- temp=(miao)/10*16+(miao)%10;
- write_1302(0x8e,0x00);
- write_1302(0x80,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+0x09);
- //write_1602com(0x0b);
- break;
- case2:fen++;
- if(fen==60)
- fen=0;
- write_sfm(0x05,fen);
- temp=(fen)/10*16+(fen)%10;
- write_1302(0x8e,0x00);
- write_1302(0x82,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+6);
- break;
- case3:shi++;
- if(shi==24)
- shi=0;
- write_sfm(2,shi);
- temp=(shi)/10*16+(shi)%10;
- write_1302(0x8e,0x00);
- write_1302(0x84,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+3);
- break;
- case4:week++;
- if(week==8)
- week=1;
- w_cmd(yh+0x0C) ;
- write_week(week);
- temp=(week)/10*16+(week)%10;
- write_1302(0x8e,0x00);
- write_1302(0x8a,temp);
- write_1302(0x8e,0x80);
- w_cmd(yh+0x0e);
- break;
- case5:ri++;
- if(ri==32)
- ri=1;
- write_nyr(9,ri);
- temp=(ri)/10*16+(ri)%10;
- write_1302(0x8e,0x00);
- write_1302(0x86,temp);
- write_1302(0x8e,0x80);
- w_cmd(yh+10);
- break;
- case6:yue++;
- if(yue==13)
- yue=1;
- write_nyr(6,yue);
- temp=(yue)/10*16+(yue)%10;
- write_1302(0x8e,0x00);
- write_1302(0x88,temp);
- write_1302(0x8e,0x80);
- w_cmd(yh+7);
- break;
- case7:nian++;
- if(nian==100)
- nian=0;
- write_nyr(3,nian);
- temp=(nian)/10*16+(nian)%10;
- write_1302(0x8e,0x00);
- write_1302(0x8c,temp);
- write_1302(0x8e,0x80);
- w_cmd(yh+4);
- break;
-
- }
- }
- }
- //------------------减键key3,各句功能参照'加键'注释---------------
- if(key3==0)
- {
- delay(10); //调延时,消抖动
- if(key3==0)
- {
- // buzzer=0; //蜂鸣器短响一次
- // delay(20);
- // buzzer=1;
- while(!key3);
- switch(key1n)
- {
- case 1:miao--;
- if(miao==-1)
- miao=59;
- write_sfm(0x08,miao);
- temp=(miao)/10*16+(miao)%10;
- write_1302(0x8e,0x00);
- write_1302(0x80,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+0x09);
- //write_1602com(0x0b);
- break;
- case 2:fen--;
- if(fen==-1)
- fen=59;
- write_sfm(5,fen);
- temp=(fen)/10*16+(fen)%10;
- write_1302(0x8e,0x00);
- write_1302(0x82,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+6);
- break;
- case 3:shi--;
- if(shi==-1)
- shi=23;
- write_sfm(2,shi);
- temp=(shi)/10*16+(shi)%10;
- write_1302(0x8e,0x00);
- write_1302(0x84,temp);
- write_1302(0x8e,0x80);
- w_cmd(er+3);
- break;
- case 4:week--;
- if(week==0)
- week=7;
- w_cmd(yh+0x0C);
- write_week(week);
- temp=(week)/10*16+(week)%10;
- write_1302(0x8e,0x00);
- write_1302(0x8a,temp);
- write_1302(0x8e,0x80);
- w_cmd(yh+0x0e);
- break;
- case 5:ri--;
- if(ri==0)
- ri=31;
- write_nyr(9,ri);
- temp=(ri)/10*16+(ri)%10;//十进制转换成DS1302要求的DCB码
- write_1302(0x8e,0x00);//允许写,禁止写保护
- write_1302(0x86,temp);//向DS1302内写日期寄存器86H写入调整后的日期数据BCD码
- write_1302(0x8e,0x80);//打开写保护
- w_cmd(yh+10);//因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
- break;
- case 6:yue--;
- if(yue==0)
- yue=12;
- write_nyr(6,yue);
- temp=(yue)/10*16+(yue)%10;//十进制转换成DS1302要求的DCB码
- write_1302(0x8e,0x00);//允许写,禁止写保护
- write_1302(0x88,temp);//向DS1302内写月份寄存器88H写入调整后的月份数据BCD码
- write_1302(0x8e,0x80);//打开写保护
- w_cmd(yh+7);//因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
- break;
- case 7:nian--;
- if(nian==-1)
- nian=99;
- write_nyr(3,nian);
- temp=(nian)/10*16+(nian)%10;//十进制转换成DS1302要求的DCB码
- write_1302(0x8e,0x00);//允许写,禁止写保护
- write_1302(0x8c,temp);//向DS1302内写年份寄存器8cH写入调整后的年份数据BCD码
- write_1302(0x8e,0x80);//打开写保护
- w_cmd(yh+4);//因为设置液晶的模式是写入数据后,指针自动加一,所以需要光标回位
- break;
-
-
- }
- }
- }
- }
- }
- /***************"TO中断"初始********************/
- void intrupt() //"TO中断"初始
- {
- TMOD=0x01; //选择16位定时器0x01
- EA=1; //开总中断
- ET0=1; //开“T0”中断允许位
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256; //定时器装初值
- TR0=1;
- }
- /*void init() //定时器、计数器设置函数
- {
- TMOD=0x11; //指定定时/计数器的工作方式为3
- TH0=0; //定时器T0的高四位=0
- TL0=0; //定时器T0的低四位=0
- EA=1; //系统允许有开放的中断
- ET0=1; //允许T0中断
- TR0=1; //开启中断,启动定时器
- }*/
- /***************主函数********************/
- void main()
- {
- lcd_init(); //调用液晶屏初始化子函数
- ds1302_init(); //调用DS1302时钟的初始化子函数
- intrupt();
- // system_Ini();
- // led=0; //打开LCD的背光电源
- // buzzer=0; //蜂鸣器长响一次
- // delay(80);
- // buzzer=1;
- while(1) //无限循环下面的语句:
- {
-
- keyscan(); //调用键盘扫描子函数
-
- }
- }
- void timer0() interrupt 1 //取得并显示日历和时间
- {
-
- Init18b20();
- system_Ini();
- GetTemp(); //温度传感器DS18b2初始化子函数,在头文件中
- flag1=Temperature;
- // flag=ReadTemperature(); //将18b2头文件运行返回的函数结果送到变量flag中,用于显示
- //读取秒时分周日月年七个数据(DS1302的读寄存器与写寄存器不一样):
- miao = BCD_Decimal(read_1302(0x81));
- fen= BCD_Decimal(read_1302(0x83));
- shi = BCD_Decimal(read_1302(0x85));
- ri = BCD_Decimal(read_1302(0x87));
- yue= BCD_Decimal(read_1302(0x89));
- nian=BCD_Decimal(read_1302(0x8d));
- week=BCD_Decimal(read_1302(0x8b));
- //显示温度、秒、时、分数据:
-
- write_temp(12,flag1);//显示温度,从第二行第12个字符后开始显示
- write_sfm(8,miao);//秒,从第二行第8个字后开始显示(调用时分秒显示子函数)
- write_sfm(5,fen);//分,从第二行第5个字符后开始显示
- write_sfm(2,shi);//小时,从第二行第2个字符后开始显示
-
- //显示日、月、年数据:
- write_nyr(9,ri);//日期,从第二行第9个字符后开始显示
- write_nyr(6,yue);//月份,从第二行第6个字符后开始显示
- write_nyr(3,nian);//年,从第二行第3个字符后开始显示
- write_week(week);
- }
复制代码
|