电子万年历 - 图文(7)

2019-04-16 18:19

3.DS1302子程序

/*************************************************************************** * 文件名:DS1302程序 * * 说明:实时时钟管理 *

***************************************************************************/ #include #include #define uchar unsigned char #define uint unsigned int uchar flag; //时钟复位标志

sbit SCLK = P3^4; //DS1302时钟口P1.0 sbit IO = P3^5; //DS1302数据口P1.1 sbit RST = P3^6; //DS1302片选口P1.2

//秒 分 时 日 月 星期 年

uchar data init[] = {0x30, 0x24, 0x15, 0x08, 0x11, 0x01, 0x11}; //初始化时间 uchar data now[7]={0x00, 0x00, 0x20, 0x01, 0x01, 0x05, 0x10};

/************************************** 延时X微秒(STC12C5A16S2@12M)

此延时函数是使用1T的指令周期进行计算 **************************************/ void Delay() {

_nop_(); _nop_(); }

/************************************** 从DS1302读1字节数据

**************************************/ uchar DS1302_ReadByte() {

uchar i;

uchar dat = 0;

for (i=0; i<8; i++) //8位计数器 { SCLK = 0; //时钟线拉低 Delay(); //延时等待

dat >>= 1; //数据右移一位 if (IO) dat |= 0x80; //读取数据 SCLK = 1; //时钟线拉高 Delay(); //延时等待 }

return dat; }

/************************************** 向DS1302写1字节数据

**************************************/ void DS1302_WriteByte(uchar dat) {

char i;

for (i=0; i<8; i++) //8位计数器 {

SCLK = 0; //时钟线拉低 Delay(); //延时等待 dat >>= 1; //移出数据

IO = CY; //送出到端口 SCLK = 1; //时钟线拉高 Delay(); //延时等待 } }

/************************************** 读DS1302某地址的的数据

**************************************/ uchar DS1302_ReadData(uchar addr) {

uchar dat; RST = 0; Delay(); SCLK = 0; Delay(); RST = 1; Delay();

DS1302_WriteByte(addr); //写地址 dat = DS1302_ReadByte(); //读数据 SCLK = 1; RST = 0; return dat; }

/************************************** 往DS1302的某个地址写入数据

**************************************/ void DS1302_WriteData(uchar addr, uchar dat) {

RST = 0; Delay(); SCLK = 0; Delay(); RST = 1; Delay();

DS1302_WriteByte(addr); //写地址 DS1302_WriteByte(dat); //写数据 SCLK = 1; RST = 0; }

/************************************** 写入初始时间

**************************************/ void DS1302_SetTime(uchar *p) { uchar addr = 0x80; uchar n = 7; flag = DS1302_ReadData(0x81); if(flag & 0x80) { DS1302_WriteData(0x8e, 0x00); //允许写操作 while (n--) { DS1302_WriteData(addr, *p++); addr += 2; }

DS1302_WriteData(0x90, 0xa5); ////写充电控制寄存器 DS1302_WriteData(0x8e, 0x80); //写保护 DS1302_WriteData(0x80, 0x00); //时钟启动 } }

/************************************** 读取当前时间

**************************************/ void DS1302_GetTime(uchar *p) {

uchar addr = 0x81; uchar n = 7; while (n--) {

*p++ = DS1302_ReadData(addr); addr += 2; } }

/************************************** 初始化DS1302

**************************************/ void DS1302_Initial() { RST = 0; SCLK = 0; DS1302_WriteData(0x8e, 0x00); //允许写操作 DS1302_WriteData(0x80, 0x00); //时钟启动 DS1302_WriteData(0x90, 0xa6); //一个二极管+4K电阻充电 DS1302_WriteData(0x8e, 0x80); //写保护 }

4. 温度传感器DS18B20程序

/************************************** * 温度传感器DS18B20程序 * * 主芯片 : STC12C5A16S2 (1T) * 工作频率: 11.0592MHz * **************************************/

#include #include #define uchar unsigned char #define uint unsigned int

sbit DQ = P3^7; //DS18B20的数据口位P3.3 float temp;

void DelayXus(uchar n); void DS18B20_Reset();

void DS18B20_WriteByte(uchar dat); uchar DS18B20_ReadByte();

/************************************** 延时X微秒(STC12C5A16S2)

此延时函数是使用1T的指令周期进行计算 **************************************/ void DelayXus(uchar n) {

while (n--) {

_nop_(); _nop_(); } }

/************************************** 复位DS18B20,并检测设备是否存在

**************************************/ void DS18B20_Reset() {

CY = 1; while (CY) {

DQ = 0; //送出低电平复位信号 DelayXus(240); //延时至少480us DelayXus(240);

DQ = 1; //释放数据线 DelayXus(60); //等待60us

CY = DQ; //检测存在脉冲

DelayXus(240); //等待设备释放数据线 DelayXus(180); } }

/************************************** 从DS18B20读1字节数据

**************************************/ uchar DS18B20_ReadByte() {

uchar i;

uchar dat = 0;

for (i=0; i<8; i++) //8位计数器 {

dat >>= 1;

DQ = 0; //开始时间片 DelayXus(1); //延时等待 DQ = 1; //准备接收 DelayXus(1); //接收延时 if (DQ) dat |= 0x80; //读取数据

DelayXus(60); //等待时间片结束 }

return dat; }

/************************************** 向DS18B20写1字节数据

**************************************/ void DS18B20_WriteByte(uchar dat) {

char i;

for (i=0; i<8; i++) //8位计数器 {

DQ = 0; //开始时间片 DelayXus(1); //延时等待 dat >>= 1; //送出数据

DQ = CY;

DelayXus(60); //等待时间片结束 DQ = 1; //恢复数据线 DelayXus(1); //恢复延时 } }

void DS18B20() { uchar TPH; //存放温度值的高字节 uchar TPL; //存放温度值的低字节 uint temper; DS18B20_Reset(); DS18B20_WriteByte(0xCC); DS18B20_WriteByte(0x44); while (!DQ); DS18B20_Reset(); DS18B20_WriteByte(0xCC); DS18B20_WriteByte(0xBE); TPL = DS18B20_ReadByte(); TPH = DS18B20_ReadByte(); temper = TPH*0x100 + TPL; temper *= 0.625; temp = temper/16; }

//设备复位 //跳过ROM命令 //开始转换命令 //等待转换完成 //设备复位 //跳过ROM命令 //读暂存存储器命令 //读温度低字节 //读温度高字节


电子万年历 - 图文(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:教育心理学每章复习题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: