SPI驱动与DS1302(6页).doc
-SPI驱动与DS1302-第 6 页SPI驱动与DS1302#include"ds1302.h"uchar code READ_RTC_ADDR7 = 0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d; uchar code WRITE_RTC_ADDR7 = 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c;/-DS1302时钟初始化2016年1月1日星期二00点00分00秒。-/-存储顺序是秒分时日月周年,存储格式是用BCD码-/uchar TIME7 = 0, 0, 0x00, 0x01, 0x01, 0x02, 0x16;void Ds1302Write(uchar addr, uchar dat)uchar n;RST = 0;_nop_();SCLK = 0;/SCLK置低电平。_nop_();RST = 1; /RST(CE)置高电平。_nop_();for (n=0; n<8; n+)/传送八位地址命令DSIO = addr & 0x01;/数据从低位开始传送addr >>= 1;SCLK = 1;/数据在上升沿时,DS1302读取数据_nop_();SCLK = 0;_nop_();for (n=0; n<8; n+)/写入8位数据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;/将SCLK置低电平。_nop_();RST = 1;/将RST(CE)置高电平。_nop_();for(n=0; n<8; n+)/传送八位地址命令DSIO = addr & 0x01;/数据从低位开始传送addr >>= 1;SCLK = 1;/数据在上升沿时,读取数据_nop_();SCLK = 0;/下降沿时,放置数据_nop_();_nop_();for(n=0; n<8; n+)/读取8位数据dat1 = DSIO;/从最低位开始接收dat = (dat>>1) | (dat1<<7);/把dat1的最高位与dat 相或 即保留dat1 的最高位 SCLK = 1;/把dat1的数据付给dat _nop_();SCLK = 0;/DS1302下降沿时,放置数据_nop_();RST = 0;_nop_();/以下为DS1302复位的稳定时间,必须的。SCLK = 1;_nop_();DSIO = 0;_nop_();DSIO = 1;_nop_();return dat;void Ds1302Init()uchar n;Ds1302Write(0x8E,0X00); /地址+数据 /禁止写保护,就是关闭写保护功能for (n=0; n<7; n+) /写入7个字节的时钟信号:分秒时日月周年Ds1302Write(WRITE_RTC_ADDRn,TIMEn);Ds1302Write(0x8E,0x80); void Ds1302ReadTime()uchar n;for (n=0; n<7; n+ )/读取7个字节的时钟信号:分秒时日月周年TIMEn = Ds1302Read(READ_RTC_ADDRn);#ifndef _DS1302_H_#define _DS1302_H_#include<reg51.h>#include<intrins.h>#ifndef uchar#define uchar unsigned char#endif#ifndef uint #define uint unsigned int#endif/-定义ds1302使用的IO口-/sbit DSIO=P34;sbit RST=P35;sbit SCLK=P36;/-定义全局函数-/void Ds1302Write(uchar addr, uchar dat);uchar Ds1302Read(uchar addr);void Ds1302Init();void Ds1302ReadTime();/-加入全局变量-/extern uchar TIME7;#endif#include"lcd.h"void Lcd1602_Delay1ms(uint c) /延时 uchar a,b;for (; c>0; c-) for (b=199;b>0;b-) for(a=1;a>0;a-);#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#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;#elsevoid 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#ifndefLCD1602_4PINSvoid LcdInit() /LCD初始化子程序 LcdWriteCom(0x38); /开显示LcdWriteCom(0x0c); /开显示不显示光标LcdWriteCom(0x06); /写一个指针加1LcdWriteCom(0x01); /清屏LcdWriteCom(0x80); /设置数据指针起点#elsevoid LcdInit() /LCD初始化子程序LcdWriteCom(0x32); /将8位总线转为4位总线LcdWriteCom(0x28); /在四位线下的初始化LcdWriteCom(0x0c); /开显示不显示光标LcdWriteCom(0x06); /写一个指针加1LcdWriteCom(0x01); /清屏LcdWriteCom(0x80); /设置数据指针起点#endif#ifndef _LCD_H_#define _LCD_H_#define LCD1602_4PINS包含头文件#include<reg51.h>/-重定义关键词-/#ifndef uchar#define uchar unsigned char#endif#ifndef uint #define uint unsigned int#endifPIN口定义#define LCD1602_DATAPINS P0sbit LCD1602_E=P27;sbit LCD1602_RW=P25;sbit LCD1602_RS=P26;函数声明/*在51单片机12MHZ时钟下的延时函数*/void Lcd1602_Delay1ms(uint c); /误差 0us/*LCD1602写入8位命令子函数*/void LcdWriteCom(uchar com);/*LCD1602写入8位数据子函数*/void LcdWriteData(uchar dat);/*LCD1602初始化子程序*/void LcdInit(); #endif#include<reg51.h>#include"lcd.h"#include"ds1302.h"void LcdDisplay(); /液晶初始化函数void main()Ds1302Init(); /时钟计数初始化LcdInit(); /液晶初始化while(1)Ds1302ReadTime();LcdDisplay();void LcdDisplay()LcdWriteCom(0x80+0X40);LcdWriteData('0'+TIME2/16);/时LcdWriteData('0'+(TIME2&0x0f); LcdWriteData('-');LcdWriteData('0'+TIME1/16);/分LcdWriteData('0'+(TIME1&0x0f);LcdWriteData('-');LcdWriteData('0'+TIME0/16);/秒LcdWriteData('0'+(TIME0&0x0f);LcdWriteCom(0x80);LcdWriteData('2');LcdWriteData('0');LcdWriteData('0'+TIME6/16);/年LcdWriteData('0'+(TIME6&0x0f);LcdWriteData('-');LcdWriteData('0'+TIME4/16);/月LcdWriteData('0'+(TIME4&0x0f);LcdWriteData('-');LcdWriteData('0'+TIME3/16);/日LcdWriteData('0'+(TIME3&0x0f);LcdWriteCom(0x8D);LcdWriteData('0'+(TIME5&0x07);/星期