使用ct017d平台测试ds18b20时,我自己建立一个工程,然后把实验例程的程序复制进来,为什么不行


最近刚好在写这个开发板的固件库,我用的是IAP15F2K61S2,希望对你有帮助。
回帖长度似乎有限制就只贴上关键源文件的部分,程序中的delay函数可以自己用STC-ISP的自带延时函数生成模块实现并引入到头文件,在此其相关头文件和C文件就不贴出了。
首先是涉及相关宏定义的头文件:
- /*
- * @Author:NUIST_XKFYT
- * @Board:CT107D
- * @Coding:UTF-8
- * @E-mail:weilun_fong@nuist.edu.cn
- * @IDE:Keil v4
- * @Note:declare some macro definitions
- *
- * @Log:(1)2016-12-18:creat
- * (2)2017-02-21:add action definitions
- */
- #ifndef ___CT107D_MACRO_H___
- #define ___CT107D_MACRO_H___
- #define false 0
- #define true 1
- /* type define */
- typedef bit u1;
- typedef unsigned char u8;
- typedef unsigned int u16;
- /* define user actions */
- typedef enum
- {
- ACTION_DISABLE = 0,
- ACTION_ENABLE = 1
- } ACTION;
- #endif
- /* -------------------- END OF FILE -------------------- */
接着是硬件部分的头文件:
- /*
- * @Author:NUIST_XKFYT
- * @Board:CT107D
- * @Coding:UTF-8
- * @E-mail:weilun_fong@nuist.edu.cn
- * @IDE:Keil v4
- * @Note:for operating on-board DS1302
- *
- * @Log:(1)2017-02-03:creat
- */
- #ifndef ___CT107D_DS1302_H___
- #define ___CT107D_DS1302_H___
- /* Note:please refer to datasheet for detailed information */
- #include <STC15F2K60S2.h>
- #include "CT107D_delay.h"
- #include "CT107D_macro.h"
- /* actually,DS1302's serial interface is following SPI protocol */
- #define DS1302_CS DS1302_CE
- #define DS1302_MOSI DS1302_IO
- #define DS1302_SCK DS1302_SCK
- #define DS1302_reset DS1302_defaultInit
- sbit DS1302_CE = P1^3; /* equal to RST',vaild at the low level */
- sbit DS1302_IO = P2^3;
- sbit DS1302_SCK = P1^7;
- /* a speficied type to operate DS1302 */
- typedef enum
- {
- ADDR_DS1302_SEC = 0x80,
- ADDR_DS1302_MIN = 0x82,
- ADDR_DS1302_HOUR = 0x84,
- ADDR_DS1302_MDAY = 0x86,
- ADDR_DS1302_MON = 0x88,
- ADDR_DS1302_WDAY = 0x8A,
- ADDR_DS1302_YEAR = 0x8C,
- ADDR_DS1302_WP = 0x8E,
- ADDR_DS1302_BRST = 0x90
- }ADDR_DS1302;
- /* @override tm struct in <time.h> */
- struct tm
- {
- int tm_sec; /* [0,59] */
- int tm_min; /* [0,59] */
- int tm_hour; /* [0,23]/[1,12] */
- int tm_mday; /* [1,31] */
- int tm_mon; /* [1,12] */
- int tm_year; /* [0,99] */
- int tm_wday; /* [1,7]1 equal to Sunday */
- };
- /* ----- function map ----- */
- extern void DS1302_defaultInit(void);
- extern void DS1302_config(struct tm *ts);
- extern u8 DS1302_getHour(void);
- extern u8 DS1302_getMinute(void);
- extern u8 DS1302_getSecond(void);
- extern void DS1302_isEnableWrittenProtect(ACTION a);
- extern u8 DS1302_readData(ADDR_DS1302 addr);
- extern u8 DS1302_readSingleByte(void);
- extern void DS1302_setHour(u8 hour);
- extern void DS1302_setMinute(u8 min);
- extern void DS1302_setSecond(u8 sec);
- extern void DS1302_writeData(ADDR_DS1302 addr,u8 dat);
- extern void DS1302_writeSingleByte(u8 dat);
- #endif
- /* -------------------- END OF FILE -------------------- */
最后是硬件部分的C文件:
- /*
- * @Author:NUIST_XKFYT
- * @Board:CT107D
- * @Coding:UTF-8
- * @E-mail:weilun_fong@nuist.edu.cn
- * @IDE:Keil v4
- * @Note:for operations on-board DS18B20
- *
- * @Log:(1)2017-02-19:creat
- */
- #include "CT107D_ds18b20.h"
- static float fac = 0.0625f; /* default temperature factor */
- /*
- * @Prototype:extern u16 DS18B20_getTemperature(void)
- * @Argument:None
- * @Ret-val:original data(a 16-bit data)
- * @Note:get temperature data from DS18B20
- */
- extern u16 DS18B20_getTemperature(void)
- {
- u8 t[2] = { 0x00,0x00 };
- DS18B20_init(); /* STEP-1:initialization timing firstly(@Something important-point 1) */
- DS18B20_writeByte(DS18B20_CMD_SKROM); /* STEP-2:ROM operation command */
- DS18B20_writeByte(DS18B20_CMD_CONVT); /* STEP-3:function command */
- DS18B20_init();
- DS18B20_writeByte(DS18B20_CMD_SKROM);
- DS18B20_writeByte(DS18B20_CMD_RSCRA);
- t[0] = DS18B20_readByte();
- t[1] = DS18B20_readByte();
- DS18B20_reset();
- return ((t[1] << 0x08) | t[0]);
- }
- /*
- * @Prototype:extern float DS18B20_handleTemperature(u16 n)
- * @Argument:None
- * @Ret-val:result of handling temperature
- * @Note:translate original temperature data into float format
- */
- extern float DS18B20_handleTemperature(u16 t)
- {
- u8 pkg[2];
- float val = 0.0f;
- pkg[0] = (u8)t;
- pkg[1] = (u8)(t >> 0x08);
- if((pkg[1] & 0xF0) == 0xF0) /* check wether the current temperatire is below zero */
- {
- if(pkg[0] == 0x00)
- {
- pkg[0] = ~pkg[0] + 1;
- pkg[1] = ~pkg[1] + 1;
- }
- else
- {
- pkg[0] = ~pkg[0] + 1;
- pkg[1] = ~pkg[1];
- }
- }
- return (float)(pkg[1]*0xFF + pkg[0])*fac;
- }
- /*
- * @Prototype:extern u1 DS18B20_init(void)
- * @Argument:None
- * @Ret-val:<1>true:ready;<2>false:busy or not exist
- * @Note:initialization/reset timing of DS18B20
- */
- extern u1 DS18B20_init(void)
- {
- u1 flag = false;
- /* STEP-1:make one-wire bus is in low level staus */
- DS18B20_DQ = 0;
- /* STEP-2:wait for 480us */
- DELAY_480us();
- /* STEP-3:release bus,wait for low level */
- DS18B20_DQ = 1;
- /* STEP-3:wait for 80us */
- DELAY_80us();
- /* STEP-4:sample in order to make sure the DS18B20 is existing */
- flag = DS18B20_DQ;
- /* STEP-5:wait for 400us(until bus recover) */
- DELAY_400us();
- return (!flag); /* NOT operation for handling return value from DS18B20 */
- }
- /*
- * @Prototype:extern u8 DS18B20_readByte(void)
- * @Argument:None
- * @Ret-val:reading result
- * @Note:read one byte from DS18B20
- */
- extern u8 DS18B20_readByte(void)
- {
- u8 i = 0x00; /* count variable */
- u8 r = 0x00; /* result variable */
- for(i = 0x00;i< 0x08;i++)
- {
- /* STEP-1:read from LSB */
- r = r >> 0x01;
- /* STEP-2:make bus is in low level status and produce a read signal */
- DS18B20_DQ = 0;
- /* STEP-3:wait for 5us */
- DELAY_5us();
- /* STEP-4:release bus and ready for reading data */
- DS18B20_DQ = 1;
- /* STEP-5:wait about 5us again */
- DELAY_5us();
- /* STEP-6:read bus status */
- if(DS18B20_DQ)
- {
- r = r | 0x80;
- }
- /* STEP-7:following the timing,it requires wait about 30us */
- DELAY_30us();
- /* STEP-8:pull up bus for next use */
- DS18B20_DQ = 1;
- }
- return r;
- }
- /*
- * @Prototype:extern void DS18B20_writeByte(u8 dat)
- * @Argument:<1>dat:the data you want to write
- * @Ret-val:None
- * @Note:write one byte into DS18B20
- */
- extern void DS18B20_writeByte(u8 dat)
- {
- u8 i = 0x00;
- u8 j = 0x00;
- for(i = 0x00;i < 0x08;i++)
- {
- /* STEP-1:make bus is in low level status and produce a write signal */
- DS18B20_DQ = 0;
- /* STEP-2:wait about 15us */
- DELAY_15us();
- /* STEP-3:judge and write into bus(from LSB) */
- if(dat & 0x01)
- {
- DS18B20_DQ = 1;
- }
- else
- {
- DS18B20_DQ = 0;
- }
- /* STEP-4:wait about 60us */
- DELAY_60us();
- /* STEP-5:release bus and wait for DS18B20 recovering */
- DS18B20_DQ = 1;
- /* STEP-6:move for writing bit */
- dat = dat >> 0x01;
- }
- //DS18B20_DQ = 1; //this line can be ignored
- }
- /* -------------------- END OF FILE -------------------- */
这个库函数的用法很简单,只需要在你自己的代码中定义一个浮点型的变量,例如v。然后令v=DS18B20_handleTemperature(DS18B20_getTemperature());即可获取当前的小数形式的温度值。
补充内容 (2017-3-10 23:16):
回头看了下头文件贴错了。特此补充!
/*
* @Author:NUIST_XKFYT
* @Board:CT107D
* @Coding:UTF-8
* @E-mail:weilun_fong@nuist.edu.cn
* @IDE:Keil v4
* @Note:for operations on-board DS18B20
*
* @Log:(1)2017-02-19:creat
*/
#ifndef ___CT107D_DS18B20_H___
#define ___CT107D_DS18B20_H___
/*
* @Something important:
* (1)All communication with the DS18B20 begins with an initialization sequence that consists of a reset pulse from the master followed by a presence pulse from the DS18B20.
*/
#include <STC15F2K60S2.h>
#include "CT107D_delay.h"
#define DS18B20_reset DS18B20_init /* redefine function */
typedef enum
{
DS18B20_CMD_CONVT = 0x44, /* convert temperature */
DS18B20_CMD_SKROM = 0xCC, /* skip ROM */
DS18B20_CMD_RSCRA = 0xBE
} CMD_DS18B20;
sbit DS18B20_DQ = P1^4;
/* ----- function map ----- */
extern u16 DS18B20_getTemperature(void);
extern float DS18B20_handleTemperature(u16 t);
extern u1 DS18B20_init(void);
extern u8 DS18B20_readByte(void);
extern void DS18B20_writeByte(u8 dat);
#endif
/* -------------------- END OF FILE -------------------- */
51的芯片?先贴程序出来啊,不贴程序你光问有啥用?
#include "reg52.h"
sbit DQ = P1^4;
//单总线延时函数
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80); // 延时大于480us
DQ = 1;
Delay_OneWire(10); // 14
initflag = DQ; // initflag等于1初始化失败
Delay_OneWire(5);
return initflag;
}
//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)
{
unsigned char low,high;
unsigned char temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
temp = high<<4;
temp |= (low>>4);
return temp;
}
#include "reg52.h"
sbit DQ = P1^4;
//单总线延时函数
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80); // 延时大于480us
DQ = 1;
Delay_OneWire(10); // 14
initflag = DQ; // initflag等于1初始化失败
Delay_OneWire(5);
return initflag;
}
//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)
{
unsigned char low,high;
unsigned char temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
temp = high<<4;
temp |= (low>>4);
return temp;
}
#include "reg52.h"
sbit DQ = P1^4;
//单总线延时函数
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80); // 延时大于480us
DQ = 1;
Delay_OneWire(10); // 14
initflag = DQ; // initflag等于1初始化失败
Delay_OneWire(5);
return initflag;
}
//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)
{
unsigned char low,high;
unsigned char temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
temp = high<<4;
temp |= (low>>4);
return temp;
}
#include "reg52.h"
sbit DQ = P1^4;
//单总线延时函数
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80); // 延时大于480us
DQ = 1;
Delay_OneWire(10); // 14
initflag = DQ; // initflag等于1初始化失败
Delay_OneWire(5);
return initflag;
}
//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)
{
unsigned char low,high;
unsigned char temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
temp = high<<4;
temp |= (low>>4);
return temp;
}
#include "reg52.h"
sbit DQ = P1^4;
//单总线延时函数
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80); // 延时大于480us
DQ = 1;
Delay_OneWire(10); // 14
initflag = DQ; // initflag等于1初始化失败
Delay_OneWire(5);
return initflag;
}
//DS18B20温度采集程序:整数
unsigned char rd_temperature(void)
{
unsigned char low,high;
unsigned char temp;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //读取寄存器
low = Read_DS18B20(); //低字节
high = Read_DS18B20(); //高字节
temp = high<<4;
temp |= (low>>4);
return temp;
}
这个程序有问题吗
嗯嗯,已经解决,谢谢大神
