带字库12864液晶显示DS1302数字时钟C源程序
基于DS1302写的12864LCD时钟程序(word文档良心出品)
以下是我写的12864LCD并行口时钟显示程序,时钟用按键可调/***************12864.h头文件******************************/#ifndef _12864_h#define _12864_h/*****包含头文件**************/#include<reg51.h>#include"string.h"#include"DS1302.h"/********定义I/0口**********/#define LCD12864_DATA P0sbit LCD12864_E = P2^7;sbit LCD12864_RW = P2^5;sbit LCD12864_RS = P2^6;sbit LCD12864_RET= P2^0;sbit LCD12864_PSB =P2^2;/******声明全局函数*********/void Delay1ms(unsigned int); //声明延时函数void BusyChk_12864(void); //声明忙检测函数void WrCom(unsigned char); //声明写入命令函数//unsigned char ReDat(void); //声明读数据函数void WrDat(unsigned char); //声明写数据函数void LcdInit(void); //12864初始化函数void SetAddress(unsigned char ,unsigned char); //设置地址转换void DisplayString(unsigned char ,unsigned char,unsigned char *p);//行显示字符void DisplayTime();void DisplayDay();void DisplayWeek();#endif/*****************************12864.c***********************************/#include"12864.h"/****************************************************************************** ** 函数名: Delay1ms* 函数功能: 延时函数,延时1ms* 输入: c* 输出 e : 无* 说名: 该函数是在12MHZ晶振下,12分频单片机的延时。
ds1302的12864液晶按键可调显示实时时钟程序(word文档良心出品)
一.绪言1.在信息显示技术中,人们发现了信息数字化的重要作用和意义。
数字化的信息更加准确,同一性,更易传输和识别。
很多信息可以直接由数字表示,从而数字化信息显示又成为信息显示的又一个重要内容。
又从数字化显示发展到字符显示,它把人类特有的语言文字用于显示,这种显示与数字显示合在一起用途更广用量更大。
在这同时,人们还希望用图形和图像进行显示,且显示的内容为五彩缤纷,并且可以实时活动和具有三维立体效果。
这些在二十世纪尾声时都已经陆续实现。
LCD的计算机器,半导体发光数码管显示(LED)的汽车计价器,商场的大屏幕广告。
这零零总总的各类显示正为你做着各种各样的服务,相信在不久的将来显示技术的发展将会为人类做出更大的贡献。
Ds1302时钟芯片现在流行的串行时钟电路很多,如DS1302、 DS1307、PCF8485等。
这些电路的接口简单、价格低廉、使用方便,被广泛地采用。
本文介绍的实时时钟电路DS1302是DALLAS公司的一种具有涓细电流充电能力的电路,主要特点是采用串行数据传输,可为掉电保护电源提供可编程的充电功能,并且可以关闭充电功能。
采用普通32.768kHz晶振。
(一)设计任务本课题采用ds1302的时钟芯片为主要的的设计来源,采用显示是1cd1602的图形点阵液晶显示。
,(二)提出方案及方案论证在设计中,我主要是考虑ds1302的时间显示问题,因为网上也有ds1302的读写和显示程序,但是至于怎样才能显示详细信息,我提出了两个方案,一个是LCD1602来显示,一个是LCD12864,其中1602是个字符型的点阵,而1CD12864是个图形的点阵,相比之下12864能够更好显示数据,而CPU都采用89C52的单片机(三)原理说明:原理其实很简单,LCD12864的各种指令先进行宏定义和ds1302的读写指令也进行宏定义,从ds1302的读出数据显示在lcd上(二)程序流程图(三)C51程序设计,重要的代码要有注释。
DS1302时钟芯片 C语言程序设计
unsigned char i,tmp; time_buf[1]=ds1302_read_byte(ds1302_year_add); //年 time_buf[2]=ds1302_read_byte(ds1302_month_add); //月 time_buf[3]=ds1302_read_byte(ds1302_date_add); //日 time_buf[4]=ds1302_read_byte(ds1302_hr_add); //时 time_buf[5]=ds1302_read_byte(ds1302_min_add); //分 time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7F;//秒 time_buf[7]=ds1302_read_byte(ds1302_day_add); //周 for(i=0;i<8;i++){ //BCD处理 tmp=time_buf[i]/16; time_buf1[i]=time_buf[i]%16; time_buf1[i]=time_buf1[i]+tmp*10; } } /*DS302初始化函数*/ void ds1302_init(void) { RST_CLR; /*RST脚置低*/ SCK_CLR; /*SCK脚置低*/ }
} else { temp &= 0x7F; } SCK_SET; SCK_CLR; } RST_CLR; /*停止DS1302总线*/ return temp; } /*向DS302写入时钟数据*/ void ds1302_write_time(void) { unsigned char i,tmp; for(i=0;i<8;i++){ //BCD处理 tmp=time_buf1[i]/10; time_buf[i]=time_buf1[i]%10; time_buf[i]=time_buf[i]+tmp*16; } ds1302_write_byte(ds1302_control_add,0x00); //关闭写保护 ds1302_write_byte(ds1302_sec_add,0x80); //暂停 ds1302_write_byte(ds1302_charger_add,0xa9); //涓流充电 ds1302_write_byte(ds1302_year_add,time_buf[1]); //年 ds1302_write_byte(ds1302_month_add,time_buf[2]); //月 ds1302_write_byte(ds1302_date_add,time_buf[3]); //日 ds1302_write_byte(ds1302_day_add,time_buf[7]); //周 ds1302_write_byte(ds1302_hr_add,time_buf[4]); //时 ds1302_write_byte(ds1302_min_add,time_buf[5]); //分 ds1302_write_byte(ds1302_sec_add,time_buf[6]); //秒 ds1302_write_byte(ds1302_day_add,time_buf[7]); //周 ds1302_write_byte(ds1302_control_add,0x80); //打开写保护 } /*从DS302读出时钟数据*/ void ds1302_read_time(void) {
DS1302时钟芯片51单片机c语言程序
#ifndef __DS1302_H__#define __DS1302_H__#define uchar unsigned char#define uint unsigned int#includesbit SCLK = P3^2;sbit IO = P2^4;sbit RST = P3^3;#define R_Second 0x81#define W_Second 0x80#define R_Minute 0x83#define W_Minute 0x82#define R_Hour 0x85#define W_Hour 0x84h#define R_Day 0x87#define W_Day 0x86_n_#define R_Month 0x89澝儙#define W_Month 0x88 #define R_Week 0x8B#define W_Week 0x渢#define R_Year 0x8D#define W_Year 0x8C?^•#define R_Control 0xu _簭_•void DS1302_Write_Byte(uchar Date;?void Write_DS1302(uchar Adr,uchar Date;uchar Read_DS1302(uchar Adr;void Init_DS1302(;'#endifW燊/************************************************************** 函數名稱 _Z_k謃?缧?_恄漀騋单字节写葅发亽輸入參數:写的字节U輸出參數 -_无M;1,?備<5%注:**************************************************************/F _void DS1302_Write_Byte(uchar Date{琿彆 uchar i;G for(i = 0;i < 8;i++{ if(Date & 0x01IO = 1;else_鞺穢齄 IO = 0;O _ SCLK = 1;3┋SCLK = 0;}诰搕謃鋃乓莗れ擾葞U/************************************************************** : uchar DS1302_Read_Byte(•函數功能:单字节读恳輸入參數蛈w{-无E轆儡:|3?o读出的数据備注:**************************************************************/ uchar DS1302_Read_Byte(!b{uchar i,Temp = 0;for(i = 0;i < 8;i++{ Temp = Temp >> 1;(_eTemp = Temp | 0x80;SCLK = 1;}return Temp;Y仞_Gv'€+枊瞒~0壇糪f?_6篢2擺Pz;函數功能:写数据輸入參數 &率郷骋確_F澼 G?_弒]u_q无蘤窓膠注巓钌鑪**************************************************************/v嗣趉{藁mSCLK = 0;RST = 1;DS1302_Write_Byte(Adr;濒 DS1302_Write_Byte(Date;i Mc}n/************************************************************** 殧泛曋ㄖ嬪i: uchar Read_DS1302(uchar Adr齙鳑扼蝊_╛嵖??>輸入參數檟_范:U?T啃禈輸出參數:读出的数据拥&__ 注:_**************************************************************/ uchar Read_DS1302(uchar Adr{uchar Temp = 0;RST = 0;4_RST = 1;DS1302_Write_Byte(Adr;Temp = DS1302_Read_Byte(;RST = 0;潵擾?_獐衉}悱qG_?帶燙ㄚ負焉/************************************************************** 函數名稱:_函數功能陕鮛€_kE_P_w?鋃DS1302o齙:无?輸出參數: U _?眶備注:@_2•蒧void Init_DS1302(癬窥Q_u瞪I _%~唼? _镈 Write_DS1302(W_Month,0x09;中_# 疴FWrite_DS1302(W_Minute,0x11;Write_DS1302(W_Second,0x23;Write_DS1302(W_Control,0x80;_Ts}<。
ds1302时钟程序详解-ds1302程序流程图(C程序)
ds1302时钟程序详解,ds1302程序流程图(C程序)ds1302时钟程序详解DS1302 的控制字如图2所示。
控制字节的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入DS1302中,位6如果为0,则表示存取日历时钟数据,为1表示存取RAM数据;位5至位1指示操作单元的地址;最低有效位(位0)如为0表示要进行写操作,为1表示进行读操作,控制字节总是从最低位开始输出。
2.3 数据输入输出(I/O)在控制指令字输入后的下一个SCLK时钟的上升沿时,数据被写入DS1302,数据输入从低位即位0开始。
同样,在紧跟8位的控制指令字后的下一个SCLK脉冲的下降沿读出DS1302的数据,读出数据时从低位0位到高位7。
2.4 DS1302的寄存器DS1302有12个寄存器,其中有7个寄存器与日历、时钟相关,存放的数据位为BCD码形式,其日历、时间寄存器及其控制字见表1。
此外,DS1302 还有年份寄存器、控制寄存器、充电寄存器、时钟突发寄存器及与RAM相关的寄存器等。
时钟突发寄存器可一次性顺序读写除充电寄存器外的所有寄存器内容。
DS1302与RAM相关的寄存器分为两类:一类是单个RAM单元,共31个,每个单元组态为一个8位的字节,其命令控制字为C0H~FDH,其中奇数为读操作,偶数为写操作;另一类为突发方式下的RAM寄存器,此方式下可一次性读写所有的RAM的31个字节,命令控制字为FEH(写)、FFH(读)。
ds1302程序流程图3.2 DS1302实时时间流程图4示出DS1302的实时时间流程。
根据此流程框图,不难采集实时时间。
下面结合流程图对DS1302的基本操作进行编程:根据本人在调试中遇到的问题,特作如下说明: DS1302 与微处理器进行数据交换时,首先由微处理器向电路发送命令字节,命令字节最高位MSB(D7)必须为逻辑1,如果D7=0,则禁止写DS1302,即写保护;D6=0,指定时钟数据,D6=1,指定RAM数据;D5~D1指定输入或输出的特定寄存器;最低位LSB(D 0)为逻辑0,指定写操作(输入),D0=1,指定读操作(输出)。
51单片机带字库12864液晶动态汉字显示C语言源程序
51单片机带字库12864液晶动态汉字显示C语言源程序2009-10-17 19:36实验芯片为:AT89S52,带字库液晶屏:12864,实现的目的:动态一个汉字一个汉字显示。
其实和显示汉字的程序一样,只是稍做一下修改即可。
C语言源程序如下:#include <stdio.h>#include <math.h>#include <reg52.h>#include <string.h>sbit RS=P2^4;sbit WRD=P2^5;sbit E=P2^6;sbit PSB=P2^1;sbit RES=P2^3;void TransferData(char data1,bit DI);void delayms(unsigned int n);void delay(unsigned int m);void lcd_mesg003(unsigned char code *addr1);unsigned char code IC_DAT[]={"我是一个中国人啊"};void initinal(void){delay(40);PSB=1; //并口工作模式delay(1);RES=0; //复位delay(1);RES=1; //复位置高delay(10);TransferData(0x30,0);delay(100);TransferData(0x30,0);delay(37);TransferData(0x08,0);delay(100);TransferData(0x10,0);delay(100);TransferData(0x0C,0);delay(100);TransferData(0x01,0);delay(10);TransferData(0x06,0);delay(100);}void main(void){while(1){initinal();//调用LCD字库初始化程序delay(100);lcd_mesg003(IC_DAT);}}void lcd_mesg003(unsigned char code *addr1){unsigned char i;//第一行TransferData(0x80,0);delay(100);for(i=0;i<16;i++){TransferData(*addr1,1);addr1++;delayms(50);}}void TransferData(char data1,bit DI) //传送数据或是命令,当DI=0时,传送命令,当DI=1时,传送数据{WRD=0;RS=DI;delay(1);P0=data1;E=1;delay(1);E=0;}void delayms(unsigned int n){unsigned int i,j;for(i=0;i<n;i++)for(j=0;j<2000;j++);}void delay(unsigned int m){unsigned int i,j;for(i=0;i<m;i++)for(j=0;j<10;j++);}原理就是在显示每个汉字的时候加了一个延时程序,当然可以用定时器来实现,这样的话,不占用CPU处理时间。
ds1302时钟程序详解,ds1302程序流程图
ds1302时钟程序详解,ds1302程序流程图(C程序)ds1302时钟程序详解DS1302 的控制字如图2所示。
控制字节的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入DS1302中,位6如果为0,则表示存取日历时钟数据,为1表示存取RAM数据;位5至位1指示操作单元的地址;最低有效位(位0)如为0表示要进行写操作,为1表示进行读操作,控制字节总是从最低位开始输出。
数据输入输出(I/O)在控制指令字输入后的下一个SCLK时钟的上升沿时,数据被写入DS1302,数据输入从低位即位0开始。
同样,在紧跟8位的控制指令字后的下一个SCLK脉冲的下降沿读出DS1302的数据,读出数据时从低位0位到高位7。
DS1302的寄存器DS1302有12个寄存器,其中有7个寄存器与日历、时钟相关,存放的数据位为BCD码形式,其日历、时间寄存器及其控制字见表1。
此外,DS1302 还有年份寄存器、控制寄存器、充电寄存器、时钟突发寄存器及与RAM相关的寄存器等。
时钟突发寄存器可一次性顺序读写除充电寄存器外的所有寄存器内容。
DS1302与RAM相关的寄存器分为两类:一类是单个RAM单元,共31个,每个单元组态为一个8位的字节,其命令控制字为C0H~FDH,其中奇数为读操作,偶数为写操作;另一类为突发方式下的RAM寄存器,此方式下可一次性读写所有的RAM的31个字节,命令控制字为FEH(写)、FFH(读)。
ds1302程序流程图DS1302实时时间流程图4示出DS1302的实时时间流程。
根据此流程框图,不难采集实时时间。
下面结合流程图对DS1302的基本操作进行编程:根据本人在调试中遇到的问题,特作如下说明:DS1302 与微处理器进行数据交换时,首先由微处理器向电路发送命令字节,命令字节最高位MSB(D7)必须为逻辑1,如果D7=0,则禁止写DS1302,即写保护;D6=0,指定时钟数据,D6=1,指定RAM数据;D5~D1指定输入或输出的特定寄存器;最低位L SB(D0)为逻辑0,指定写操作(输入), D0=1,指定读操作(输出)。
12864带字库c语言源码
#include <stdio.h>#include <math.h>#include <reg52.h>#include <string.h>unsigned char code IC_DAT[];unsigned char code pic1[];unsigned char code pic2[];unsigned char code pic3[];sbit RS =P2^4;sbit WRD=P2^5;sbit E= P2^6;sbit PSB=P2^1;sbit RES=P2^3;void TransferData(char data1,bit DI);void display(void);void display_grapic(void);void delayms(unsigned int n);void DisplayLine(unsigned char line1,unsigned char line2); void DisplayGraphic(unsigned char code *adder);void delay(unsigned int m);void lcd_mesg(unsigned char code *adder1);unsigned char code IC_DAT[]={"上海浩豚电子科技""单片机开发板系列""中文字库测试程序""恭喜发财身体健康"};unsigned char code IC_DAT2[]={"海纳百川宽容为先""欲成大业诚信为先""游弋商海济世为先""人立于世守法为先"};void initinal(void) //LCD字库初始化程序delay(40); //大于40MS的延时程序PSB=1; //设置为8BIT并口工作模式delay(1); //延时RES=0; //复位delay(1); //延时RES=1; //复位置高delay(10);TransferData(0x30,0); //Extended Function Set :8BIT设置,RE=0: basic instruction set, G=0 :graphic display OFFdelay(100); //大于100uS的延时程序TransferData(0x30,0); //Function Setdelay(37); ////大于37uS的延时程序TransferData(0x08,0); //Display on Controldelay(100); //大于100uS的延时程序TransferData(0x10,0); //Cursor Display Control光标设置delay(100); //大于100uS的延时程序TransferData(0x0C,0); //Display Control,D=1,显示开delay(100); //大于100uS的延时程序TransferData(0x01,0); //Display Cleardelay(10); //大于10mS的延时程序TransferData(0x06,0); //Enry Mode Set,光标从右向左加1位移动delay(100); //大于100uS的延时程序}void initina2(void) //LCD显示图片(扩展)初始化程序{delay(40); //大于40MS的延时程序PSB=1; //设置为8BIT并口工作模式delay(1); //延时RES=0; //复位delay(1); //延时RES=1; //复位置高delay(10);TransferData(0x36,0); //Extended Function Set RE=1: extended instructiondelay(100); //大于100uS的延时程序TransferData(0x36,0); //Extended Function Set:RE=1: extended instruction setdelay(37); ////大于37uS的延时程序TransferData(0x3E,0); //EXFUNCTION(DL=8BITS,RE=1,G=1)delay(100); //大于100uS的延时程序TransferData(0x01,0); //CLEAR SCREENdelay(100); //大于100uS的延时程序void main(void){while(1){initina2(); //调用LCD显示图片(扩展)初始化程序DisplayGraphic(pic2); //显示图片2delayms(200);DisplayGraphic(pic3); //显示图片3delayms(200);initinal(); //调用LCD字库初始化程序delay(100); //大于100uS的延时程序lcd_mesg(IC_DAT); //显示中文汉字1delayms(240);delayms(240);initinal(); //调用LCD字库初始化程序delay(100); //大于100uS的延时程序lcd_mesg(IC_DAT2); //显示中文汉字2delayms(240);delayms(240);}}void lcd_mesg(unsigned char code *adder1){unsigned char i;TransferData(0x80,0); //Set Graphic Display RAM Addressdelay(100);for(i=0;i<32;i++){TransferData(*adder1,1);adder1++;}TransferData(0x90,0); //Set Graphic Display RAM Addressdelay(100);for(i=32;i<64;i++){TransferData(*adder1,1);adder1++;}}void TransferData(char data1,bit DI) //传送数据或者命令,当DI=0是,传送命令,当DI=1,传送数据.{WRD=0;RS=DI;delay(1);P0=data1;E=1;delay(1);E=0;}void delayms(unsigned int n) //延时10×n毫秒程序{unsigned int i,j;for(i=0;i<n;i++)for(j=0;j<2000;j++);}void delay(unsigned int m) //延时程序{unsigned int i,j;for(i=0;i<m;i++)for(j=0;j<10;j++);}void DisplayGraphic(unsigned char code *adder){int i,j;//*******显示上半屏内容设置for(i=0;i<32;i++) //{TransferData((0x80 + i),0); //SET 垂直地址VERTICAL ADDTransferData(0x80,0); //SET 水平地址HORIZONTAL ADDfor(j=0;j<16;j++){TransferData(*adder,1);adder++;}}//*******显示下半屏内容设置for(i=0;i<32;i++) //{TransferData((0x80 + i),0); //SET 垂直地址VERTICAL ADDTransferData(0x88,0); //SET 水平地址HORIZONTAL ADDfor(j=0;j<16;j++){TransferData(*adder,1);adder++;}}}unsigned char code pic2[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x1F,0xFF,0x81,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x1F,0xFF,0x81,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x18,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x08,0x00,0x00,0x00,0x03, 0xC0,0x1F,0xFF,0x80,0x00,0x00,0x04,0x07,0x81,0xE0,0x10,0x10,0x03,0xC0,0x30,0x03, 0xC0,0x1F,0xFF,0x80,0x00,0x00,0x0C,0x0C,0xC3,0x30,0x08,0x20,0x0C,0xE0,0x70,0x03, 0xC0,0x18,0x01,0x87,0xFF,0xFE,0x3C,0x18,0x66,0x18,0x04,0x40,0x0C,0x60,0x70,0x03, 0xC0,0x1F,0xFF,0x87,0xFF,0xFE,0x0C,0x18,0x66,0x18,0x02,0x80,0x18,0x00,0xB0,0x03, 0xC0,0x1F,0xFF,0x80,0x06,0x00,0x0C,0x18,0x66,0x18,0x01,0x00,0x1B,0x81,0x30,0x03, 0xC0,0x01,0x98,0x00,0x06,0x00,0x0C,0x00,0xE3,0x30,0x02,0x80,0x1C,0xC1,0x30,0x03, 0xC0,0x19,0x99,0x80,0x66,0x60,0x0C,0x00,0xC1,0xE0,0x04,0x40,0x18,0x62,0x30,0x03, 0xC0,0x19,0x99,0x80,0x66,0x30,0x0C,0x01,0x83,0x30,0x08,0x20,0x18,0x66,0x30,0x03, 0xC0,0x0D,0x9B,0x00,0xC6,0x38,0x0C,0x03,0x06,0x18,0x10,0x10,0x18,0x67,0xFC,0x03,0xC0,0x0D,0x9B,0x01,0xC6,0x1C,0x0C,0x06,0x06,0x18,0x20,0x08,0x18,0x60,0x30,0x03, 0xC0,0x01,0x98,0x03,0x86,0x0C,0x0C,0x0C,0x06,0x18,0x00,0x00,0x08,0x60,0x30,0x03, 0xC0,0x7F,0xFF,0xE3,0x06,0x00,0x0C,0x18,0x03,0x30,0x00,0x00,0x0C,0xC0,0x30,0x03, 0xC0,0x7F,0xFF,0xE0,0x1E,0x00,0x0C,0x1F,0xE1,0xE0,0x00,0x00,0x07,0x80,0x30,0x03, 0xC0,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x18,0x00,0x00,0x00,0x06,0x0C,0x03, 0xC3,0xFF,0xFC,0x3F,0xF0,0x80,0x0C,0x00,0x3F,0x18,0x01,0xFF,0xFE,0x06,0x0C,0x03, 0xC3,0xFF,0xFC,0x3F,0xF1,0x80,0x0F,0xFC,0x3F,0x18,0x01,0xFF,0xFE,0x06,0x0C,0x03, 0xC3,0x0C,0x0C,0x0C,0xC3,0x00,0x0F,0xFC,0x33,0x7F,0xE1,0x86,0x06,0x06,0x0C,0x03, 0xC3,0x1F,0xCC,0x0C,0xC6,0x00,0x0C,0x00,0x33,0x7F,0xE1,0x8F,0xE6,0x06,0x0C,0x03, 0xC3,0x3F,0xCC,0x0C,0xCC,0x00,0x0C,0x00,0x36,0x30,0x01,0x9F,0xE6,0x07,0xFF,0xE3, 0xC3,0x79,0x8C,0x0C,0xC8,0x00,0xFF,0xF0,0x36,0x36,0x01,0xBC,0xC6,0x07,0xFF,0xE3, 0xC3,0x6F,0x0C,0x7F,0xF0,0xC0,0xFF,0xF0,0x36,0x66,0x01,0xB7,0x86,0x06,0x00,0x03, 0xC3,0x0F,0x0C,0x7F,0xF1,0x80,0xC0,0x30,0x36,0x7F,0xC1,0x87,0x86,0x06,0x00,0x03, 0xC3,0x79,0xEC,0x0C,0xC3,0x00,0xC0,0x30,0x33,0x7F,0xC1,0xBC,0xF6,0x06,0x00,0x03, 0xC3,0x76,0xEC,0x0C,0xC6,0x00,0xC0,0x30,0x33,0x06,0x01,0xBB,0x76,0x07,0xFF,0x03, 0xC3,0x07,0x0C,0x0C,0xCC,0x20,0xFF,0xF0,0x33,0x06,0x01,0x83,0x86,0x07,0xFF,0x03, 0xC3,0x03,0x0C,0x18,0xC8,0x60,0xFF,0xF0,0x3F,0x06,0x01,0x81,0x86,0x06,0x03,0x03, 0xC3,0x1C,0x0C,0x18,0xC0,0xC0,0x00,0x00,0x3E,0xFF,0xE1,0x8E,0x06,0x06,0x03,0x03, 0xC3,0x1F,0x0C,0x18,0xC1,0x81,0xB1,0x8C,0x30,0xFF,0xE1,0x8F,0x86,0x06,0x03,0x03, 0xC3,0x03,0x0C,0x30,0xC7,0x01,0xB1,0x8C,0x30,0x06,0x01,0x81,0x86,0x0C,0x03,0x03, 0xC3,0xFF,0xFC,0x70,0xDE,0x03,0x18,0xC6,0x30,0x06,0x01,0xFF,0xFE,0x0C,0x03,0x03, 0xC3,0xFF,0xFC,0x20,0x18,0x03,0x18,0xC6,0x30,0x06,0x01,0xFF,0xFE,0x18,0x03,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,};unsigned char code pic3[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC3,0x02,0x0E,0x1C,0x08,0xF8,0x67,0xE3,0x87,0x0C,0x08,0x38,0x70,0x23,0xE1,0x83, 0xC4,0x8E,0x11,0x22,0x18,0x80,0x90,0x24,0x48,0x92,0x38,0x44,0x88,0x62,0x02,0x43, 0xC8,0x42,0x11,0x22,0x28,0x81,0x00,0x24,0x48,0xA1,0x08,0x44,0x88,0xA2,0x04,0x03, 0xC8,0x42,0x01,0x02,0x28,0xF1,0xE0,0x44,0x48,0xA1,0x08,0x04,0x08,0xA3,0xC7,0x83, 0xC8,0x42,0x02,0x0C,0x48,0x89,0x10,0x43,0x88,0xA1,0x08,0x08,0x31,0x22,0x24,0x43, 0xC8,0x42,0x04,0x02,0x88,0x09,0x10,0x44,0x47,0xA1,0x08,0x10,0x0A,0x20,0x24,0x43, 0xC8,0x42,0x08,0x22,0xFC,0x89,0x10,0x44,0x40,0xA1,0x08,0x20,0x8B,0xF2,0x24,0x43, 0xC4,0x82,0x10,0x22,0x08,0x89,0x10,0x84,0x49,0x12,0x08,0x40,0x88,0x22,0x24,0x43, 0xC3,0x02,0x1F,0x1C,0x08,0x70,0xE0,0x83,0x86,0x0C,0x08,0x7C,0x70,0x21,0xC3,0x83, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xCF,0xC7,0x0E,0x18,0x10,0x70,0xE0,0x47,0xC3,0x3F,0x1C,0x38,0x60,0x41,0xC3,0x83, 0xC0,0x48,0x91,0x24,0x70,0x89,0x10,0xC4,0x04,0x81,0x22,0x44,0x91,0xC2,0x24,0x43, 0xC0,0x48,0x91,0x42,0x10,0x89,0x11,0x44,0x08,0x01,0x22,0x45,0x08,0x42,0x24,0x43, 0xC0,0x88,0x91,0x42,0x10,0x08,0x11,0x47,0x8F,0x02,0x22,0x45,0x08,0x40,0x20,0x43, 0xC0,0x87,0x11,0x42,0x10,0x10,0x62,0x44,0x48,0x82,0x1C,0x45,0x08,0x40,0x41,0x83, 0xC0,0x88,0x8F,0x42,0x10,0x20,0x14,0x40,0x48,0x82,0x22,0x3D,0x08,0x40,0x80,0x43, 0xC0,0x88,0x81,0x42,0x10,0x41,0x17,0xE4,0x48,0x82,0x22,0x05,0x08,0x41,0x04,0x43, 0xC1,0x08,0x92,0x24,0x10,0x81,0x10,0x44,0x48,0x84,0x22,0x48,0x90,0x42,0x04,0x43, 0xC1,0x07,0x0C,0x18,0x10,0xF8,0xE0,0x43,0x87,0x04,0x1C,0x30,0x60,0x43,0xE3,0x83, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x8F,0x86,0x7E,0x38,0x70,0xC0,0x83,0x87,0x02,0x3E,0x19,0xF8,0xE1,0xC3,0x03, 0xC1,0x88,0x09,0x02,0x44,0x89,0x23,0x84,0x48,0x86,0x20,0x24,0x09,0x12,0x24,0x83, 0xC2,0x88,0x10,0x02,0x44,0x8A,0x10,0x84,0x48,0x8A,0x20,0x40,0x09,0x12,0x28,0x43, 0xC2,0x8F,0x1E,0x04,0x44,0x8A,0x10,0x80,0x40,0x8A,0x3C,0x78,0x11,0x12,0x28,0x43, 0xC4,0x88,0x91,0x04,0x38,0x8A,0x10,0x80,0x83,0x12,0x22,0x44,0x10,0xE2,0x28,0x43, 0xC8,0x80,0x91,0x04,0x44,0x7A,0x10,0x81,0x00,0xA2,0x02,0x44,0x11,0x11,0xE8,0x43, 0xCF,0xC8,0x91,0x04,0x44,0x0A,0x10,0x82,0x08,0xBF,0x22,0x44,0x11,0x10,0x28,0x43,0xC0,0x88,0x91,0x08,0x44,0x91,0x20,0x84,0x08,0x82,0x22,0x44,0x21,0x12,0x44,0x83, 0xC0,0x87,0x0E,0x08,0x38,0x60,0xC0,0x87,0xC7,0x02,0x1C,0x38,0x20,0xE1,0x83,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC1,0x07,0x0E,0x04,0x7C,0x33,0xF1,0xC3,0x86,0x04,0x1C,0x38,0x11,0xF0,0xCF,0xC3, 0xC7,0x08,0x91,0x0C,0x40,0x48,0x12,0x24,0x49,0x1C,0x22,0x44,0x31,0x01,0x20,0x43, 0xC1,0x08,0x91,0x14,0x40,0x80,0x12,0x24,0x50,0x84,0x22,0x44,0x51,0x02,0x00,0x43, 0xC1,0x00,0x81,0x14,0x78,0xF0,0x22,0x24,0x50,0x84,0x02,0x04,0x51,0xE3,0xC0,0x83, 0xC1,0x01,0x06,0x24,0x44,0x88,0x21,0xC4,0x50,0x84,0x04,0x18,0x91,0x12,0x20,0x83, 0xC1,0x02,0x01,0x44,0x04,0x88,0x22,0x23,0xD0,0x84,0x08,0x05,0x10,0x12,0x20,0x83, 0xC1,0x04,0x11,0x7E,0x44,0x88,0x22,0x20,0x50,0x84,0x10,0x45,0xF9,0x12,0x20,0x83, 0xC1,0x08,0x11,0x04,0x44,0x88,0x42,0x24,0x89,0x04,0x20,0x44,0x11,0x12,0x21,0x03, 0xC1,0x0F,0x8E,0x04,0x38,0x70,0x41,0xC3,0x06,0x04,0x3E,0x38,0x10,0xE1,0xC1,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,};#include <reg52.h>#include <intrins.h>#include <stdio.h>#define uchar unsigned char#define Fisrt_Addr 0 //定义字符/汉字显示起始位置typedef unsigned char uint8;sbit LCD_RS=P2^2;sbit LCD_RW=P2^3;sbit LCD_E=P2^4;sbit PCB=P2^5;uchar i,j;uint8 AC_TABLE[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行汉字位置0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行汉字位置0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行汉字位置0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行汉字位置};void delay(int ms){while(ms--){for(j=0;j<120;j++);}}bit lcd_busy(){ bit result;LCD_RS=0;LCD_RW=1;LCD_E= 1;result=(bit)(P0&0x80);LCD_E=0;return result;}void lcd_wcmd(uchar cmd){while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_E=0;P0=cmd;LCD_E=1;LCD_E=0;}void lcd_wdata(uchar dat){while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_E=0;P0=dat ;LCD_E=1;LCD_E=0;}void lcd_initial(){lcd_wcmd(0x30);delay(1);lcd_wcmd(0x0C); //开显示,关光标,不闪烁delay(1);lcd_wcmd(0x06);//指定在资料写入或读取时,光标的移动方向delay(1);lcd_wcmd(0x01);delay(10); //这里要延时长一点,否侧会影响下面的操作}/*------------------清屏命令--------------------------*/void Clr_Screen(){lcd_wcmd(0x30);//考虑到前面出现0x34lcd_wcmd(0x01);delay(10);}/*------------------整屏写入数据:全亮,或者竖条-----------------------------*///先写0x34关绘图,写地址数据,0x360x绘图void Lcd_Fill(uchar disdata){ uchar x,y; //参考PDF写GDRAM的步骤,显示坐标for(y=0;y<32;y++)for(x=0;x<16;x++){ lcd_wcmd(0x34);lcd_wcmd(y+0x80); //行地址lcd_wcmd(x+0x80); //列地址lcd_wdata(disdata);lcd_wdata(disdata);lcd_wcmd(0x36);}lcd_wcmd(0x30);//考虑到前面出现0x34}/*------------------显示字符串--------------------------*/void Zi_FU(uchar code *s){while(*s!=0){ lcd_wdata(*s);s++;delay(10);}}//注意:中文显示时空格符或数字ASCII码必需两个一起用,标点符号注意中英文void intruction(){Clr_Screen() ;lcd_wcmd(0x80);//第一行(如果是地址是:80H,即LCD的第一行的第一个位置显示)Zi_FU("erwatgeawgaseg");lcd_wcmd(0x90);//第二行(如果是地址是:90H,即LCD的第二行的第一个位置显示)Zi_FU("wetwetw");lcd_wcmd(0x88);//第三行(如果是地址是:88H,即LCD的第二行的第一个位置显示)Zi_FU("yhwerqytqw");lcd_wcmd(0x98);//第四行(如果是地址是:98H,即LCD的第二行的第一个位置显示)Zi_FU("weagfawgaw?);delay(1000);}void feeling1(){lcd_wcmd(0x30);Zi_FU("fasgfwqf");delay(300);lcd_wcmd(0x90+Fisrt_Addr);Zi_FU("gwagawga");delay(300);lcd_wcmd(0x88+Fisrt_Addr);Zi_FU("nfaishdgfioa");delay(300);lcd_wcmd(0x98+Fisrt_Addr);delay(500);Zi_FU("6456464");}/*------------------显示图片---------------------------*/void Disp_Img(uchar code *bmp){uchar count=2,X=0x80;lcd_wcmd(0x34); //扩展指令lcd_wcmd(0x36); //打开绘图for(;count>0;count--){for(i=0;i<32;i++){ lcd_wcmd(0x80|i);lcd_wcmd(X); //X地址坐标,自动加1.for(j=0;j<16;j++) //16*8=128lcd_wdata(*bmp++);}X=0x88;//下半屏}}/********************************************* 函数名称:去字摸函数,取模方式是横向,顺序函数功能:**********************************************/ void ZK(uchar *zk){uchar i, j;for(j=0;j<16;j++){lcd_wcmd(0x34); //扩展指令lcd_wcmd(0x36); //打开绘图lcd_wcmd(0x80|j);lcd_wcmd(0x80) ;//X地址坐标,自动加1.for(i=0;i<2;i++){lcd_wdata(*zk++);}}}/********************************************* 函数名称: 向某一行写字符函数功能:**********************************************/ void Lcd_Write_Poss(uchar x,uchar y,uchar *ZiFu){uchar addr,i=0;if(x==1)addr=0x80;if(x==2)addr=0x90;if(x==3)addr=0x88;if(x==4)addr=0x98;addr=addr+y;lcd_wcmd(0x30);lcd_wcmd(addr);while(*ZiFu!='\0'){lcd_wdata(*ZiFu++);}}void main(){PCB=1;while(1){lcd_initial();delay(1000);Clr_Screen();feeling1();delay(3000);for(i=8;i>0;i--){lcd_wcmd(0x18); //一二行与三四行调换了,液晶分为上下两个半屏,delay(1000); //有时会乱,与上面程序有关,要加lcd_wcmd(0x30),clrscreen();}Clr_Screen(); //清屏feeling1();lcd_wcmd(0x34);lcd_wcmd(0x04); //0X04第一行和第三行反白显示,delay(3000); //0X05第二行和第四行反白显示,与PDF不符lcd_wcmd(0x04); //恢复正常delay(3000);lcd_wcmd(0x34);lcd_wcmd(0x03); //允许输入滚动地址for(i=0x40;i<0x7f;i++){lcd_wcmd(i); //垂直滚动屏幕的内容delay(500);}}}。
DS1302时钟程序(完整版),液晶显示
#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar table0[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //液晶显示数字0——9代码sbit SCLK=P1^0;sbit DATA=P1^1;sbit CE=P1^2;sbit RS=P2^0;sbit RW=P2^1;sbit EN=P2^2;sbit FB=P0^7;sbit start=P1^4;sbit stop=P1^5;uchar i;/*******************延时n微秒函数*****************/void delaynus(uchar n) //n us延时函数{// uchar i;for(i=n;i>0;i--);}/*******************写数据函数*****************/void write1302(uchar add,uchar dat){CE=0;SCLK=0;CE=1;for(i=0;i<8;i++) //写入地址{DATA=add&0x01;SCLK=1;SCLK=0;add>>=1;}for(i=0;i<8;i++) //写入数据{DATA=dat&0x01;SCLK=1;SCLK=0;dat>>=1;}SCLK=1;CE=0;}/*******************读数据函数*****************/uchar read1302(uchar add){uchar dat;CE=0;SCLK=0;CE=1;for(i=8;i>0;i--) //写入地址{DATA=add&0x01;SCLK=1;SCLK=0;add>>=1;}for(i=8;i>0;i--) //读出数据/********************为神马???**************************/{dat>>=1;if(DATA==1)dat=dat|0x80;SCLK=1;SCLK=0;}SCLK=1;CE=0;return dat;}/*******************DS1302初始化函数*****************/void init1302(){uchar flag;flag=read1302(0x81);if(flag&0x80){write1302(0x8e,0x00);write1302(0x80,0x00);write1302(0x82,(((1/10)<<4)|(1%10)));write1302(0x84,(((20/10)<<4)|(20%10)));write1302(0x86,(((19/10)<<4)|(19%10)));write1302(0x88,(((7/10)<<4)|(7%10)));write1302(0x8a,((2/10)<<4)|(2%10));write1302(0x8c,(((11/10)<<4)|(11%10)));write1302(0x90,0xa5); //充电。
12864显示可调1302时钟
sbit key1=P3^3; //自加“+”键
sbit key2=P3^4; //自减“-”键
uchar sum,keyflag=0; //调用时,分,秒,标志,调用键盘处理标志.
/*--------------以下为LCD12864相关函数--------------------------*/
uchar code TAB4[]={"室温: ℃"}; //第四行
/*----------------------DS1302相关声明-------------------------*/
char TAB_1302[]={ 0x00,0x40,0x09,0x10,0x07,0x00,0x12}; //秒:分:时:日:月:星期:年.
sck=1;
}
}
/**************************************************************
函数名称:uchar r_12864byte(void)
函数功能:读字节
***************************************************************/
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
/*------------LCD12864相关声明-----------------------------*/
#define com 0
函数名称:void c_12864busy( void )
51单片机带字库液晶12864ds1302数字时钟c源程序(无按键修改功能)
51单片机+带字库液晶12864+DS1302数字时钟C 源程序(无按键修改功 能)过两天的搜索与调试,在别人程序的基础上,不断修改,终于调试成功了 这个程序。
目前还不能修改时间与日期,只是以预定时间以始。
适用于开发板:51单片机(AT89S52 +带字库液晶12864(ST7920)+DS1302实时时钟)实现功能:简单,数字时钟 +日期(以后会不断完美)。
C 语言源程序如下:#include <reg52.h>#include <intrins.h> #define uchar unsigned char #define uint unsigned int /*DS1302 端 口设置 */sbit SCK=P3A 6;sbit SDA=P3A4;sbit RST = P3A5;bitReadRTC_Flag;/* 12864 端口定义 */ #define LCD_data P0 sbit LCD_RS = P2A4;sbit LCD_RW = P2A5; sbit LCD_EN = P2A 6;//液晶使能控制sbit PSB 二卩2八1; //并口控制 //DS1302 时钟 //DS1302 IO // DS1302 复位 //读DS1302全局变量//带字库液晶 12864数据口// 寄存器选择输入// 液晶读/写控制sbit RES=P2A3;uchar code dis1[] = {"电子设计天地"};// 液晶显示的汉字uchar code dis2[] = {"有志者,事竟成!"};uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'};unsigned char temp;#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};void lcd_pos(uchar X,uchar Y); //确定显示位置unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};〃秒分时日月周年09-10-19 16:07:00code unsigned charwrite_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年最低位读写位code unsigned charread_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};void Write_Ds1302_byte(unsigned char temp);void Write_Ds1302( unsigned char address,unsigned char dat );unsigned char Read_Ds1302 ( unsigned char address );void Read_RTC(void);//read RTCvoid Set_RTC(void); //set RTCvoid InitTIMER0(void);//inital timer0/*******************************************************************//**//* 延时函数 *//* *//******************************************************************* /void delay(unsigned int m) // 延时程序 {unsigned int i,j;for(i=0;i<m;i++)**************************************************//*检查LCD 忙状态*//*lcd_busy 为1时,忙,等待。
DS1302显示时钟的C程序
#include<reg52.h> //包含单片机寄存器的头文件#include<intrins.h> //包含_nop_()函数定义的头文件#define uint unsigned int#define uchar unsigned charunsigned char code digit[10]={"0123456789"}; //定义字符数组显示数字sbit SCLK=P1^0; //位定义1302芯片的接口,时钟输出端口定义在P1.0引脚sbit DATA=P1^1; //位定义1302芯片的接口,数据输出端定义在P1.1引脚sbit RST=P1^2; //位定义1302芯片的接口,复位端口定义在P1.2引脚/*****************************************************函数功能:延时若干微秒入口参数:n***************************************************/void delay(uint i){ unsigned char j;while(i--){for(j=110;j>0;j--){;}}}/*****************************************************函数功能:向1302写一个字节数据入口参数:x***************************************************/void Write1302(unsigned char dat){unsigned char i;SCLK=0; //拉低SCLK,为脉冲上升沿写入数据做好准备delay(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续写8个二进制位数据{DATA=dat&0x01; //取出dat的第0位数据写入1302 低位在前,高位在后delay(2); //稍微等待,使硬件做好准备SCLK=1; //上升沿写入数据delay(2); //稍微等待,使硬件做好准备SCLK=0; //重新拉低SCLK,形成脉冲dat>>=1; //将dat的各数据位右移1位,准备写入下一个数据位}}/*****************************************************函数功能:根据命令字,向1302写一个字节数据入口参数:Cmd,储存命令字;dat,储存待写的数据***************************************************/void WriteSet1302(unsigned char Cmd,unsigned char dat){RST=0; //禁止数据传递SCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输delay(2); //稍微等待,使硬件做好准备Write1302(Cmd); //写入命令字Write1302(dat); //写数据SCLK=1; //将时钟电平置于高电平状态RST=0; //禁止数据传递}/*****************************************************函数功能:从1302读一个字节数据入口参数:x***************************************************/ unsigned char Read1302(void){unsigned char i,dat;delay(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续读8个二进制位数据{ dat>>=1;if(DATA==1) //如果读出的数据是1dat|=0x80; //将1取出,写在dat的最高位SCLK=1; //将SCLK置于高电平,为下降沿读出delay(2); //稍微等待SCLK=0; //拉低SCLK,形成脉冲下降沿delay(2); //稍微等待}return dat; //将读出的数据返回}/*****************************************************函数功能:根据命令字,从1302读取一个字节数据入口参数:Cmd***************************************************/ unsigned char ReadSet1302(unsigned char Cmd){unsigned char dat;RST=0; //拉低RSTSCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输Write1302(Cmd); //写入命令字dat=Read1302(); //读出数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递return dat; //将读出的数据返回}/*****************************************************函数功能:1302进行初始化设置***************************************************/void Init_DS1302(void){unsigned char flag;flag= ReadSet1302(0x81);if(flag&0x80) { //判断时钟芯片是否关闭WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((55/10)<<4|(55%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((18/10)<<4|(18%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((6/10)<<4|(6%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((9/10)<<4|(9%10))); //根据写年寄存器命令字,写入年的初始值WriteSet1302(0x90,0xa5); //打开充电功能选择2K电阻充电方式WriteSet1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令}}//如果不想每次都初始化时间,也就是掉电后还想让时钟继续走时的话就用上面的语句/*--------------------这是每次都初始化的语句-----------------*//*WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((55/10)<<4|(55%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((18/10)<<4|(18%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((6/10)<<4|(6%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((9/10)<<4|(9%10))); //根据写年寄存器命令字,写入年的初始值WriteSet1302(0x90,0xa5); //打开充电功能选择2K电阻充电方式WriteSet1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令*//****************************************************************************** *以下是对液晶模块的操作程序******************************************************************************* /sbit RS=P2^0; //寄存器选择位,将RS位定义为P2.0引脚sbit RW=P2^1; //读写选择位,将RW位定义为P2.1引脚sbit E=P2^2; //使能信号位,将E位定义为P2.2引脚sbit BF=P0^7; //忙碌标志位,,将BF位定义为P0.7引脚/*****************************************************函数功能:判断液晶模块的忙碌状态返回值:result。
DS1302时钟芯片完整C语言应用程序_可直接调用
}
for(i=0;i<8;i++)
{
DS1302_CLK=0;
DS1302_DATA=DDATA&0X01;
DS1302_CLK=1;
DDATA=DDATA>>1; //传送数据。
}
DS1302_CLK=0;
DS1302_RST=0;
//使能端关闭。
}
/************************************************************/
uchar DS1302_Clock[8]={0x01,0x01,0x00,0x06,0x09,0x02,0x11,0}; //时钟缓存。秒、分、时、
日、月、周、年、控制寄存器。
void DS1302_Write(uchar CDATA,uchar DDATA); //写数据。
uchar DS1302_Read(uchar CDATA); //读数据。
#endif
/********DS1302_WriteClock()***********************************
函数名 :DS1302_WriteClock() 突发方式,写入所有时钟数据。
入口参数:无
出口参数:无
**************************************************************/
void DS1302_Write(uchar CDATA,uchar DDATA)
{
uchar i;
DS1302_RST=1;
//使能端有效,开始传送数据。
for(i=0;i<8;i++)
ds1302显示在12864的c语言程序
dat2 = dat % 10;
dat2 = dat2 + dat1 * 16;
return dat2;
}
//BCD码转换为数据
uchar BCD_Chg_Dat(uchar dat)
{
uchar dat1, dat2;
dat1 = dat / 16;
dat2 = dat % 16;
TSCLK = 1;//拉高时钟总线,产生上升沿数据被DS1302读走
cmd >>=1;//右移一位
}
for(i = 0; i < 8; i++)//每次写1位,写8次
{
TSCLK = 0;//拉低时钟总线
TIO = dat & 0x01; //写1位数据,从最低位开始写
TSCLK = 1;//拉高时钟总线,产生上升沿数据被DS1302读走
{
uchar i;
TRST = 0; //拉低使能端
TSCLK = 0;//拉低数据总线
TRST = 1; //拉高使能端,产生上升沿开始写数据
for(i = 0; i < 8; i++)//每次写1位,写8次
{
TSCLK = 0;//拉低时钟总线
TIO = cmd & 0x01; //写1位数据,从最低位开始写
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define MAIN_Fosc11059200UL//宏定义主时钟HZ
DS1302实时时钟芯片的C语言源程序
T_RST=0;
k=ucDa/16; //在将10进制的数转化为16进制的数。
ucDa=ucDa%16;
ucDa=ucDa+k*10;
return(ucDa);
}
void v_Set1302(uchar *pSecDa) /*设定初试时间*/
/* 时钟程序*/
/************************************/
sbit T_CLK=P3^6; /* 实时时钟的时钟线引脚*/
sbit T_IO=P3^7; /* 实时时钟的数据线*/
sbit T_RST=P3^5; /* 实时时钟的复位线引脚*/
}
void main1(void)
{ v_Get1302();
temp=ucCurtime[3]*10000+ucCurtime[2]*100+ucCurtime[1]; //temp里的值是为了显示到数码管里而设定的值.
display1();
}
void main2(void)
}
/*显示*/
void display1()
{
uchar c;
t2_10();
c=a[0]; //个位
P2=ledk[c]; //显示
gw=0;
unint a[6];//={0,0,0,0,0,0}; /*位段的数字*/
uchar code ledk[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
{
unsigned char ucDa,k;
DS1302时钟芯片C51源代码源程序
DS1302时钟芯片C51源代码源程序DS1302--- 51C语言代码,头文件与对应C代码已分离开,直接可用(自己写,绝对可用。
)其中星期几能直接由日期按公式推算出来,time.week=(time.year/100)/4-2*(time.year/100)+(time.year%100)+(time.year%100)/4+26*(tim e.month+1)/10 +time.day-1;(详情请搜:蔡乐公式)///////////////////////////////////////////////////////////////DS1302头文件:#ifndef _ds1302_h_#define _ds1302_h_#define uchar unsigned char#define uint unsigned int#define ulong unsigned long#define u32 unsigned long intsbit sdinn = P2^6;sbit sclkk = P2^5;sbit cee = P2^7;#define second_w 0x80#define second_r 0x81#define minute_w 0x82#define minute_r 0x83#define hour_w 0x84#define hour_r 0x85#define day_w 0x86#define day_r 0x87#define month_w 0x88#define month_r 0x89#define year_w 0x8c#define year_r 0x8d#define week_w 0x8a#define week_r 0x8b#define protect_w 0xc0#define protect_r 0xc1#define clock_base_w 0xc2#define clock_base_r 0xc3#define clock_state_w 0xcc#define clock_state_r 0xcd#define D_protect 0x77typedef struct{uint year;uint month;uint day;uint hour;uint minute;uint second;uint week;} Time_info;void DS1302_Writebyte(uchar dat);void time_write2DS1302(Time_info Time);void time_read_from_DS1302(Time_info *Time);uchar DS1302_Readbyte();void DS1302Write(uchar cmd, uchar dat);uchar DS1302Read(uchar cmd);void Init_DS1302(void);void display_date(unsigned char X,unsigned char Y,Time_info Read_T); void writeDate2DS1302(Time_info Time);void time_HMS2DS1302(Time_info Time);void Alarm_respond();void sendtime2computer();#endif///////////////////////////////////////////////////////////DS1302-C代码:#include "option.h"xdata uchar trans_buf;extern xdata uchar Date_Index;extern xdata uchar Time_Index;extern xdata uchar Write_date_buff[15];extern xdata uchar Alarm_buff[7];extern xdata uchar Alarm_arrive;extern xdata uchar arriveminute;extern xdata uchar Alarm_state;extern xdata Time_info Read_Time;xdata uchar tiao_buff[30];xdata uint hour,minute,second;xdata uchar old_second=250;void delay(unsigned long int i){while(i--);}void time_write2DS1302(Time_info Time)DS1302Write(0x8e,0x00);//写保护关trans_buf = ((Time.year%100/10)<<4)+((Time.year%10)&0X0F); // 写入年DS1302Write(year_w, trans_buf);trans_buf = (((Time.month%100/10)<<4)&0x10) + ((Time.month%10)&0X0F); //写入月DS1302Write(month_w, trans_buf);trans_buf = (((Time.day%100/10)<<4)&0x30) + ((Time.day%10)&0X0F);//写入日DS1302Write(day_w, trans_buf);trans_buf = (((Time.hour%100/10)<<4)&0x30)+((Time.hour%10)&0X0F);//写入时DS1302Write(hour_w, trans_buf);trans_buf = (((Time.minute%100/10)<<4)&0x70) + ((Time.minute%10)&0X0F); //写入分DS1302Write(minute_w, trans_buf);trans_buf = (((Time.second%100/10)<<4)&0x70) + ((Time.second%10)&0X0F); //写入秒DS1302Write(second_w, trans_buf);trans_buf = Time.week%7; //写入星期DS1302Write(week_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void writeDate2DS1302(Time_info Time){DS1302Write(0x8e,0x00);//写保护关trans_buf = ((Time.year%100/10)<<4)+((Time.year%10)&0X0F); // 写入年DS1302Write(year_w, trans_buf);trans_buf = (((Time.month%100/10)<<4)&0x10) +((Time.month%10)&0X0F); //写入月DS1302Write(month_w, trans_buf);trans_buf = (((Time.day%100/10)<<4)&0x30) + ((Time.day%10)&0X0F);//写入日DS1302Write(day_w, trans_buf);Time.week = (int)(Time.year/100)/4-(int)2*(Time.year/100)+(int)(Time.year%100)+(int)(Time.year%100)/4+(int)26*(Time.mo nth+1)/10+(int)Time.day-1;// printf("--week:%5d--",Time.week);Time.week %= 7;if(Time.week == 0) Time.week = 7;trans_buf = Time.week; //写入星期DS1302Write(week_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void time_HMS2DS1302(Time_info Time){DS1302Write(0x8e,0x00);//写保护关trans_buf = (((Time.hour%100/10)<<4)&0x30)+((Time.hour%10)&0X0F);//写入时DS1302Write(hour_w, trans_buf);trans_buf = (((Time.minute%100/10)<<4)&0x70) + ((Time.minute%10)&0X0F); //写入分DS1302Write(minute_w, trans_buf);trans_buf = (((Time.second%100/10)<<4)&0x70) + ((Time.second%10)&0X0F); //写入秒DS1302Write(second_w, trans_buf);DS1302Write(0x8e,0x80); //开写保护}void time_read_from_DS1302(Time_info *Time){Time->year = DS1302Read(year_r);Time->month = DS1302Read(month_r);Time->day = DS1302Read(day_r);Time->hour = DS1302Read(hour_r);Time->minute = DS1302Read(minute_r);Time->second = DS1302Read(second_r);Time->week = DS1302Read(week_r);Time->year = 2000+((Time->year>>4)*10)+(Time->year&0x0f);Time->month = ((Time->month>>4)*10) + (Time->month&0x0f);Time->day = (Time->day>>4)*10 + (Time->day&0x0f);Time->hour = (Time->hour>>4)*10 + (Time->hour&0x0f);Time->minute = ((Time->minute>>4)*10) + (Time->minute&0x0f);Time->second = (Time->second>>4)*10 + (Time->second&0x0f); }void DS1302_Writebyte(uchar dat){uchar i;sclkk = 0;delay(300);for(i = 0; i < 8; i++){sdinn = dat & 0x01;delay(90);sclkk = 1;delay(180);sclkk = 0;dat >>= 1;}}uchar DS1302_Readbyte(){uchar i, dat = 0;delay(90);for(i = 0; i < 8; i++){dat >>=1;if(sdinn)dat |= 0x80;sclkk = 1;delay(60);sclkk = 0;delay(60);}sdinn = 0;return dat;}void DS1302Write(uchar cmd, uchar dat) {cee = 0;sclkk = 0;cee = 1;DS1302_Writebyte(cmd);DS1302_Writebyte(dat);sclkk = 1;cee = 0;}uchar DS1302Read(uchar cmd) {uchar dat = 0;cee = 0;sclkk = 0;cee = 1;DS1302_Writebyte(cmd);dat = DS1302_Readbyte();sclkk = 1;cee = 0;return dat;}void Init_DS1302(void){Time_info time;uchar init_state;DS1302Write(0x8e,0x00);//写保护关init_state = DS1302Read(protect_r); DS1302Write(0x8e,0x80); //开写保护if(init_state != D_protect){time.year = 2014;time.month = 12;time.day = 5;time.hour = 18;time.minute = 57;time.second = 50; //日期换算为星期[c/4]-2c+y+[y/4]+[26(m+1)/10]+d-1time.week = (time.year/100)/4-2*(time.year/100)+(time.year %100)+(time.year%100)/4+26*(time.month+1)/10+time.da y-1;time.week %= 7;if(time.week == 0) time.week = 7;time_write2DS1302(time);DS1302Write(0x8e,0x00);//写保护关DS1302Write(protect_w,D_protect); //初始化一次标示DS1302Write(0x8e,0x80); //开写保护}DS1302Write(0x8e,0x00);//写保护关Alarm_buff[0] = DS1302Read(clock_base_r+0);Alarm_buff[1] = DS1302Read(clock_base_r+2);Alarm_buff[2] = DS1302Read(clock_base_r+4);Alarm_buff[3] = DS1302Read(clock_base_r+6);Alarm_buff[4] = DS1302Read(clock_base_r+8);Alarm_buff[5] = DS1302Read(clock_base_r+10);Alarm_state = DS1302Read(clock_state_r);DS1302Write(0x8e,0x80); //开写保护}void display_date(unsigned char X,unsigned char Y,Time_info Read_T) {xdata char dis_info[12];sprintf(dis_info, "%04d/%02d/%02d", (int)Read_T.year, (int) Read_T.month, (int)Read_T.day);LCD_write_english_string(X,Y,dis_info);sprintf(dis_info, "%02d/%02d/%02d", (int)Read_T.hour, (int) Read_T.minute, (int)Read_T.second);LCD_write_english_string(X+6,Y+1,dis_info);sprintf(dis_info, "Week:%d", (int)Read_T.week);LCD_write_english_string(X+12,Y+2,dis_info);}void Alarm_respond(){hour = (uint)Alarm_buff[0]*10+Alarm_buff[1];minute = (uint)Alarm_buff[2]*10+Alarm_buff[3];second = (uint)Alarm_buff[4]*10+Alarm_buff[5];if(Alarm_state==1){time_read_from_DS1302(&Read_Time);if((old_second < Read_Time.second)||(Read_Time.second==0)){if(arriveminute!=Read_Time.minute) Alarm_arrive = 0;if((Read_Time.hour == hour)&&(Read_Time.minute== minute)&&(Read_Time.second == second)){Alarm_arrive = 1;arriveminute = Read_Time.minute;}}old_second = Read_Time.second;}}void sendtime2computer(){time_read_from_DS1302(&Read_Time);printf("time%04d/%02d/%02d/%02d:%02d:%02d", Read_Time.year, Read_Time.month, Read_Time.day, Read_Time.hour, Read_Time.minute,Read_Time.second);printf("%d--\n",Read_Time.week);}。
DS1302 12864lcd单片机智能闹钟日期温度程序
/****************************************************************************/ //头文件#include <AT89X52.h>#include <string.h>/*****************************************************************************/ //灯与蜂鸣器定义sbit led_b = P2^7;sbit led_r = P1^4;sbit led_g1 = P1^3;sbit led_g2 = P1^2;sbit led_g3 = P1^0;sbit led_g4 = P1^1;sbit beep = P3^1;/*****************************************************************************/ //温度传感器定义sbit DQ = P3 ^ 0;//ds18B20/*****************************************************************************/ //键盘引脚定义sbit KEY_1 = P2^3; //左上sbit KEY_2 = P2^4; //左下sbit KEY_3 = P2^6; //右上sbit KEY_4 = P2^5; //右下sbit KEY_I = P3^2; //常0/****************************************************************************/ //引脚定义sbit SID = P2^1; //数据sbit SCLK = P2^2; //时钟/*****************************************************************************/ //定义DS1302时钟接口sbit clock_clk = P3 ^ 5;//ds1302_clk(时钟线)sbit clock_dat = P3 ^ 4;//ds1302_dat(数据线)sbit clock_Rst = P3 ^ 3;//ds1302_Rst(复位线)/*****************************************************************************/ //定义累加器A中的各位sbit a0 = ACC ^ 0;sbit a1 = ACC ^ 1;sbit a2 = ACC ^ 2;sbit a3 = ACC ^ 3;sbit a4 = ACC ^ 4;sbit a5 = ACC ^ 5;sbit a6 = ACC ^ 6;sbit a7 = ACC ^ 7;/****************************************************************************/ //定义全局变量unsigned char yy,mo,dd,xq,hh,mm,ss;//定义时间映射全局变量(专用寄存器)bit w = 0; //调时标志位static unsigned char menu = 0;//定义静态小时更新用数据变量static unsigned char keys = 0;//定义静态小时更新用数据变量static unsigned char timecount = 0;//定义静态软件计数器变量/****************************************************************************/ void DelayM(unsigned int a){//延时函数1MS/次unsigned char i;while( --a != 0){for(i = 0; i < 125; i++); //一个; 表示空语句,CPU空转。
DS1302时钟C语言编程完整实例
1 DS1302 简介:DS1302是美国D ALLAS 公司推出的一种高性能、低功耗的实时时钟芯片,附加31字节静态R AM,采用S PI 三线接口与C PU 进行同步通信,并可采用突发方式一次传送多个字节的时钟信号和R AM数据。
实时时钟可提供秒、分、时、日、星期、月和年,一个月小与31天时可以自动调整,且具有闰年补偿功能。
工作电压宽达2.5~5.5V。
采用双电源供电(主电源和备用电源),可设置备用电源充电方式,提供了对后背电源进行涓细电流充电的能力。
DS1302的外部引脚分配如图1所示及内部结构如图2所示。
DS1302用于数据记录,特别是对某些具有特殊意义的数据点的记录上,能实现数据与出现该数据的时间同时记录,因此广泛应用于测量系统中。
图1 DS1302的外部引脚分配图2 DS1302的内部结构各引脚的功能为:V cc1:主电源;Vcc2:备份电源。
当V cc2>Vcc1+0.2V 时,由Vcc2向D S1302供电,当V cc2< Vcc1时,由V cc1向D S1302供电。
SCLK:串行时钟,输入,控制数据的输入与输出;I/O:三线接口时的双向数据线;CE:输入信号,在读、写数据期间,必须为高。
该引脚有两个功能:第一,CE 开始控制字访问移位寄存器的控制逻辑;其次,CE 提供结束单字节或多字节数据传输的方法。
DS1302有下列几组寄存器:① D S1302有关日历、时间的寄存器共有12个,其中有7个寄存器 (读时81h ~8Dh ,写时80h ~8Ch ),存放的数据格式为 BCD 码形式, 如图3所示。
图 3 DS1302有关日历、时间的寄存器 小时寄存器(85h 、84h )的位7用于定义 D S1302是运行于12小时 模式还是24小时模式。
当为高时,选择12小时模式。
在12小时模式时,位5是 ,当为1时,表示 PM 。
在24小时模式时,位5是第二个10小时 位。
秒寄存器(81h 、80h )的位7定义为时钟暂停标志(CH )。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
51单片机+带字库液晶12864+DS1302数字时钟C源程序(无按键修改功能)2009-10-19 16:47经过两天的搜索与调试,在别人程序的基础上,不断修改,终于调试成功了这个程序。
目前还不能修改时间与日期,只是以预定时间以始。
适用于开发板:51单片机(AT89S52)+带字库液晶12864(ST7920)+DS1302(实时时钟)实现功能:简单,数字时钟+日期(以后会不断完美)。
C语言源程序如下:#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned int/*DS1302 端口设置 */sbit SCK=P3^6; //DS1302时钟sbit SDA=P3^4; //DS1302 IOsbit RST = P3^5; // DS1302复位bit ReadRTC_Flag; //读DS1302全局变量/* 12864端口定义*/#define LCD_data P0 //带字库液晶12864数据口sbit LCD_RS = P2^4; //寄存器选择输入sbit LCD_RW = P2^5; //液晶读/写控制sbit LCD_EN = P2^6; //液晶使能控制sbit PSB=P2^1; //并口控制sbit RES=P2^3;uchar code dis1[] = {" 电子设计天地"}; //液晶显示的汉字uchar code dis2[] = {"有志者,事竟成!"};uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'};unsigned char temp;#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};void lcd_pos(uchar X,uchar Y); //确定显示位置unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};//秒分时日月周年09-10-19 16:07:00code 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};void Write_Ds1302_byte(unsigned char temp);void Write_Ds1302( unsigned char address,unsigned char dat );unsigned char Read_Ds1302 ( unsigned char address );void Read_RTC(void);//read RTCvoid Set_RTC(void); //set RTCvoid InitTIMER0(void);//inital timer0/*******************************************************************//**//* 延时函数*//**//*******************************************************************/void delay(unsigned int m) //延时程序{unsigned int i,j;for(i=0;i<m;i++)for(j=0;j<10;j++);}/*******************************************************************//**//*检查LCD忙状态*//*lcd_busy为1时,忙,等待。
lcd-busy为0时,闲,可写指令与数据。
*/ /**//*******************************************************************/bit lcd_busy(){bit result;LCD_RS = 0;LCD_RW = 1;LCD_EN = 1;delayNOP();result = (bit)(P0&0x80);LCD_EN = 0;return(result);}/*******************************************************************/ /**//*写指令数据到LCD*//*RS=L,RW=L,E=高脉冲,D0-D7=指令码。
*//**//*******************************************************************/ void lcd_wcmd(uchar cmd){while(lcd_busy());LCD_RS = 0;LCD_RW = 0;LCD_EN = 0;_nop_();_nop_();P0 = cmd;delay(1);LCD_EN = 1;delay(1);LCD_EN = 0;}/*******************************************************************/ /**//*写显示数据到LCD*//*RS=H,RW=L,E=高脉冲,D0-D7=数据。
*/ /**//*******************************************************************/ void lcd_wdat(uchar dat){while(lcd_busy());LCD_RS = 1;LCD_RW = 0;LCD_EN = 0;P0 = dat;delayNOP();LCD_EN = 1;delay(1);LCD_EN = 0;}/*******************************************************************/ /**//* LCD初始化设定*//**//*******************************************************************/ void lcd_init(){P0=0xFF;P2=0xFF;delay(40);PSB=1; //并口方式。
delay(1);RES=0;delay(1);RES=1;delay(10);lcd_wcmd(0x30);delay(100);lcd_wcmd(0x30);delay(37);lcd_wcmd(0x08);delay(100);lcd_wcmd(0x10);delay(100);lcd_wcmd(0x0C); //显示开,关光标delay(100);lcd_wcmd(0x01); //清除LCD的显示内容delay(100);lcd_wcmd(0x06);delay(100);}void main(){uchar i;InitTIMER0();Set_RTC();lcd_init();//初始化LCDwhile(1){if(ReadRTC_Flag){ReadRTC_Flag=0;Read_RTC();lcd_pos(0,0); //设置显示位置为第一行的第1个字符i = 0;while(dis1[i] != '\0'){ //显示字符lcd_wdat(dis1[i]);i++;}lcd_pos(1,0); //设置显示位置为第二行的第1个字符i = 0;while(dis2[i] != '\0'){lcd_wdat(dis2[i]); //显示字符i++;}lcd_pos(2,0);lcd_wdat(dis4[l_tmpdate[6]/16]); //DS1302是BCD码,进行处理得到十位与个位数。
lcd_pos(2,1);lcd_wdat(dis4[l_tmpdate[6]%16]);lcd_pos(2,2);lcd_wdat('-');lcd_pos(2,3);lcd_wdat(dis4[l_tmpdate[4]/16]);lcd_pos(2,4);lcd_wdat(dis4[l_tmpdate[4]%16]);lcd_pos(2,5);lcd_wdat('-');lcd_pos(2,6);lcd_wdat(dis4[l_tmpdate[3]/16]);lcd_pos(2,7);lcd_wdat(dis4[l_tmpdate[3]%16]); //设置显示位置为第三行的第1个字符lcd_pos(3,0); //设置显示位置为第四行的第1个字符 lcd_wdat(dis4[l_tmpdate[2]/16]);lcd_pos(3,1);lcd_wdat(dis4[l_tmpdate[2]%16]);lcd_pos(3,2);lcd_wdat(':');lcd_pos(3,3);lcd_wdat(dis4[l_tmpdate[1]/16]);lcd_pos(3,4);lcd_wdat(dis4[l_tmpdate[1]%16]);lcd_pos(3,5);lcd_wdat(':');lcd_pos(3,6);lcd_wdat(dis4[l_tmpdate[0]/16]);lcd_pos(3,7);lcd_wdat(dis4[l_tmpdate[0]%16]);}}}/*********************************************************//**//* 设定显示位置*//**//*********************************************************/void lcd_pos(uchar X,uchar Y){uchar pos;if (X==0){X=0x80;}else if (X==1){X=0x90;}else if (X==2){X=0x88;}else if (X==3){X=0x98;}pos = X+Y ;lcd_wcmd(pos); //显示地址}void InitTIMER0(void){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[i]=Read_Ds1302(*p);p++;}}/***********************************************************************/ void Set_RTC(void) //设定日历{unsigned char i,*p,tmp;for(i=0;i<7;i++){ //BCD处理tmp=l_tmpdate[i]/10;l_tmpdate[i]=l_tmpdate[i]%10;l_tmpdate[i]=l_tmpdate[i]+tmp*16;}Write_Ds1302(0x8E,0X00);p=write_rtc_address; //传地址for(i=0;i<7;i++) //7次写入秒分时日月周年{Write_Ds1302(*p,l_tmpdate[i]);p++;}Write_Ds1302(0x8E,0x80);}void tim(void) interrupt 1 using 1//中断,用于数码管扫描{static unsigned char i,num;TH0=0xf5;TL0=0xe0;//P0=table[l_tmpdisplay[i]]; //查表法得到要显示数字的数码段//P2=table1[i];i++;if(i==8){i=0;num++;if(20==num) //隔段时间读取1302的数据。