1、51 单片机 ds1302 时钟芯片51 单片机 ds1302 时钟芯片 #define uint unsigned int#define uchar unsigned charsbit lcdrs = P10;sbit lcdrw = P11;sbit lcden = P12;sbit key0 = P20;/功能键,选择时分秒sbit key1 = P21;/加1键sbit key2 = P22;/减1键sbit key4 = P24;sbit clk_1302 = P15; /1302芯片位定义sbit io_1302 = P16;sbit rst_1302 = P17;uchar bd
2、ata dat;sbit dat0 = dat0;sbit dat7 = dat7;uchar key0_count;/按键0被按的次数(03uchar flag;char hour,minute,second;uchar table_date = 2009-4-12 Mon;uchar table_time = 00:00:00;/* 函数申明 */void write_cmd_1602(uchar cmd;void write_data_1602(uchar dat;void write_add(uchar add,uchar dat;void init1602(;void delay(u
3、int z;uchar reverse(uchar c;void keyscan(;void init(;void RTC_initial (;void wr_1302(uchar wr_data;uchar rd_1302(void;uchar uc_R1302(uchar ucAddr;void v_W1302(uchar ucAddr, uchar ucDa;主程序 #include #include #include header.h/* 主函数 */void main(RTC_initial(;init(;/init1602(;/while(1;while(1if(flagsecon
4、d = uc_R1302(0x81;minute = uc_R1302(0x83;hour = uc_R1302(0x85;write_add(0x40+10,second;write_add(0x40+7,minute;write_add(0x40+4,hour;keyscan(;delay(1;void RTC_initial (v_W1302(0x8e,0x00; /使能v_W1302(0x80,0x80; /停止晶振v_W1302(0x90,0x00; /关闭充电v_W1302(0x8c,0x07; /设置年v_W1302(0x8a,0x06; /设置周v_W1302(0x88,0x0
5、1; /设置月v_W1302(0x86,0x13; /设置日v_W1302(0x84,0x18; /设置小时v_W1302(0x82,0x05; /设置分v_W1302(0x80,0x00; /启动晶振,设置秒v_W1302(0x8e,0x80; /使能/* 功 能: 往DS1302写入1Byte数据* 输 入: wr_data写入的数据* 返回值: 无*/void wr_1302(uchar wr_datauchar i = 0;dat = wr_data;rst_1302 = 1;clk_1302 = 0;delay(5;for(i=0;i= 1;/rst_1302 = 0; /* 功 能
6、: 从DS1302读出1Byte数据* 返回值: dat*/uchar rd_1302(voiduchar i;rst_1302 = 1;clk_1302 = 1;delay(5;for(i=0;i= 1;dat7 = io_1302;clk_1302 = 1;_nop_(;_nop_(;clk_1302 = 0;_nop_(;rst_1302 = 0;return dat;/* * 名 称: uc_R1302* 说 明: 先写地址,后读命令/数据* 功 能: 读取DS1302某地址的数据* 调 用: v_RTInputByte( , uc_RTOutputByte(* 输 入: ucAddr
7、: DS1302地址* 返回值: ucDa :读取的数据*/uchar uc_R1302(uchar adduchar temp= 0xff;wr_1302(add;temp = rd_1302(; return temp;/* * 名 称: v_W1302* 说 明: 先写地址,后写命令/数据* 功 能: 往DS1302写入数据* 调 用: v_RTInputByte( * 输 入: ucAddr: DS1302地址, ucDa: 要写的数据* 返回值: 无*/void v_W1302(uchar ucAddr, uchar ucDawr_1302(ucAddr; wr_1302(ucDa;
8、rst_1302 = 0;clk_1302 = 0; /1602液晶初始化void init1602(lcdrw = 0; /本程序只读不写,故将其置低lcden = 0; /关闭1602delay(5;write_cmd_1602(0x01; /清屏write_cmd_1602(0x38; /显示模式write_cmd_1602(0x0c; /开显示,不显示光标,不闪烁write_cmd_1602(0x06; /地址指针自动加1write_cmd_1602(0x80+1;/将光标指向第一行第二个位置delay(5;void delay(uint zuint x,y;for(x=z;x0;x-
9、for(y=110;y0;y-;/* 将发送的数据进行翻转,高位到低位,低位转到高位 */ uchar reverse(uchar c uchar chbuff=0; if (c&0x01 chbuff=chbuff|0x80; if (c&0x02 chbuff=chbuff|0x40; if (c&0x04 chbuff=chbuff|0x20; if (c&0x08 chbuff=chbuff|0x10; if (c&0x10 chbuff=chbuff|0x08; if (c&0x20 chbuff=chbuff|0x04; if (c&0x40 chbuff=chbuff|0x02;
10、 if (c&0x80 chbuff=chbuff|0x01; return chbuff; /向液晶写命令void write_cmd_1602(uchar cmdlcdrs = 0; /低表示写命令,高表示写数据delay(5;P0 = reverse(cmd;delay(5;lcden = 1; /给一个高脉冲delay(5; lcden = 0;delay(5;/向液晶写数据void write_data_1602(uchar datlcdrs = 1; /低表示写命令,高表示写数据delay(5;P0 = reverse(dat;delay(5;lcden = 1; /给一个高脉冲d
11、elay(5; lcden = 0;delay(5; /根据地址向液晶写数据void write_add(uchar add,uchar datuchar shi,ge;shi = dat/10;ge = dat%10;delay(5;write_cmd_1602(0x80+add;write_data_1602(shi+0x30;delay(5;write_data_1602(ge+0x30;/键盘扫描函数void keyscan(key4 = 0;if(0=key0 /第一个键被按下delay(5; /消抖if(0=key0key0_count +;if(4=key0_countkey0_
12、count = 0;flag = 1;while(!key0; /松手检测flag = 0;if(0=key0_countflag = 0; write_cmd_1602(0x0c; /禁止显示光标并不让其闪烁 if(1=key0_countflag = 0;write_cmd_1602(0x80+0x40+11;/将地址指针指向秒的位置write_cmd_1602(0x0f; /显示光标并闪烁delay(5; if(2=key0_countflag = 0;write_cmd_1602(0x80+0x40+8;/将地址指针指向分的位置write_cmd_1602(0x0f; /显示光标并闪烁delay(5;if(3=key0_countflag = 0;write_cmd_1602(0x80+0x40+5;/将地址指针指向小时的位置write_cmd_1602(0x0f; /显示光标并闪烁 delay(5;if(0=key1 /第二个键被按下delay(5; /消抖if(0=key1while(!key1; /松手检测flag = 0;if(key0_count=1second +;if(60=second
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1