《SPI驱动与DS1302(6页).doc》由会员分享,可在线阅读,更多相关《SPI驱动与DS1302(6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-SPI驱动与DS1302-第 6 页SPI驱动与DS1302#includeds1302.huchar 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;v
2、oid Ds1302Write(uchar addr, uchar dat)uchar n;RST = 0;_nop_();SCLK = 0;/SCLK置低电平。_nop_();RST = 1; /RST(CE)置高电平。_nop_();for (n=0; n= 1;SCLK = 1;/数据在上升沿时,DS1302读取数据_nop_();SCLK = 0;_nop_();for (n=0; n= 1;SCLK = 1;/数据在上升沿时,读取数据_nop_();SCLK = 0;_nop_();RST = 0;/传送数据结束_nop_();uchar Ds1302Read(uchar addr)
3、uchar n,dat,dat1;RST = 0;_nop_();SCLK = 0;/将SCLK置低电平。_nop_();RST = 1;/将RST(CE)置高电平。_nop_();for(n=0; n= 1;SCLK = 1;/数据在上升沿时,读取数据_nop_();SCLK = 0;/下降沿时,放置数据_nop_();_nop_();for(n=0; n1) | (dat17);/把dat1的最高位与dat 相或 即保留dat1 的最高位 SCLK = 1;/把dat1的数据付给dat _nop_();SCLK = 0;/DS1302下降沿时,放置数据_nop_();RST = 0;_no
4、p_();/以下为DS1302复位的稳定时间,必须的。SCLK = 1;_nop_();DSIO = 0;_nop_();DSIO = 1;_nop_();return dat;void Ds1302Init()uchar n;Ds1302Write(0x8E,0X00); /地址+数据 /禁止写保护,就是关闭写保护功能for (n=0; n7; n+) /写入7个字节的时钟信号:分秒时日月周年Ds1302Write(WRITE_RTC_ADDRn,TIMEn);Ds1302Write(0x8E,0x80); void Ds1302ReadTime()uchar n;for (n=0; n7;
5、 n+ )/读取7个字节的时钟信号:分秒时日月周年TIMEn = Ds1302Read(READ_RTC_ADDRn);#ifndef _DS1302_H_#define _DS1302_H_#include#include#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,
6、uchar dat);uchar Ds1302Read(uchar addr);void Ds1302Init();void Ds1302ReadTime();/-加入全局变量-/extern uchar TIME7;#endif#includelcd.hvoid Lcd1602_Delay1ms(uint c) /延时 uchar a,b;for (; c0; c-) for (b=199;b0;b-) for(a=1;a0;a-);#ifndef LCD1602_4PINS /当没有定义这个LCD1602_4PINS时void LcdWriteCom(uchar com) /写入命令LCD
7、1602_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 = co
8、m;/由于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
9、_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口的高四位,所以传送高四位不用
10、改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
11、); /写一个指针加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
12、LCD1602_4PINS包含头文件#include/-重定义关键词-/#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位命令子函
13、数*/void LcdWriteCom(uchar com);/*LCD1602写入8位数据子函数*/void LcdWriteData(uchar dat);/*LCD1602初始化子程序*/void LcdInit(); #endif#include#includelcd.h#includeds1302.hvoid LcdDisplay(); /液晶初始化函数void main()Ds1302Init(); /时钟计数初始化LcdInit(); /液晶初始化while(1)Ds1302ReadTime();LcdDisplay();void LcdDisplay()LcdWriteCom(
14、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);/星期