微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 使用ct017d平台测试ds18b20时,我自己建立一个工程,然后把实验例程的程序复制进来,为什么不行

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

时间:10-02 整理:3721RD 点击:
运行实验例程数码管显示的是19,而我建完工程,把实验历程的源码全部复制进去,运行的结果是数码管上先显示一下19然后迅速变为5,这是为什么?求大神详解。



最近刚好在写这个开发板的固件库,我用的是IAP15F2K61S2,希望对你有帮助。
回帖长度似乎有限制就只贴上关键源文件的部分,程序中的delay函数可以自己用STC-ISP的自带延时函数生成模块实现并引入到头文件,在此其相关头文件和C文件就不贴出了。
首先是涉及相关宏定义的头文件:

  1. /*
  2. * @Author:NUIST_XKFYT
  3. * @Board:CT107D
  4. * @Coding:UTF-8
  5. * @E-mail:weilun_fong@nuist.edu.cn
  6. * @IDE:Keil v4
  7. * @Note:declare some macro definitions
  8. *
  9. * @Log:(1)2016-12-18:creat
  10. *      (2)2017-02-21:add action definitions
  11. */

  12. #ifndef ___CT107D_MACRO_H___
  13. #define ___CT107D_MACRO_H___

  14. #define false 0
  15. #define true  1

  16. /* type define */
  17. typedef bit           u1;
  18. typedef unsigned char u8;
  19. typedef unsigned int  u16;

  20. /* define user actions */
  21. typedef enum
  22. {
  23.     ACTION_DISABLE = 0,
  24.     ACTION_ENABLE  = 1
  25. } ACTION;

  26. #endif

  27. /* -------------------- END OF FILE -------------------- */

复制代码


接着是硬件部分的头文件:

  1. /*
  2. * @Author:NUIST_XKFYT
  3. * @Board:CT107D
  4. * @Coding:UTF-8
  5. * @E-mail:weilun_fong@nuist.edu.cn
  6. * @IDE:Keil v4
  7. * @Note:for operating on-board DS1302
  8. *
  9. * @Log:(1)2017-02-03:creat
  10. */
  11. #ifndef ___CT107D_DS1302_H___
  12. #define ___CT107D_DS1302_H___

  13. /* Note:please refer to datasheet for detailed information */

  14. #include <STC15F2K60S2.h>
  15. #include "CT107D_delay.h"
  16. #include "CT107D_macro.h"

  17. /* actually,DS1302's serial interface is following SPI protocol */
  18. #define DS1302_CS   DS1302_CE
  19. #define DS1302_MOSI DS1302_IO
  20. #define DS1302_SCK  DS1302_SCK

  21. #define DS1302_reset DS1302_defaultInit

  22. sbit DS1302_CE  = P1^3;      /* equal to RST',vaild at the low level */
  23. sbit DS1302_IO  = P2^3;
  24. sbit DS1302_SCK = P1^7;

  25. /* a speficied type to operate DS1302 */
  26. typedef enum
  27. {
  28.     ADDR_DS1302_SEC  = 0x80,
  29.     ADDR_DS1302_MIN  = 0x82,
  30.     ADDR_DS1302_HOUR = 0x84,
  31.     ADDR_DS1302_MDAY = 0x86,
  32.     ADDR_DS1302_MON  = 0x88,
  33.     ADDR_DS1302_WDAY = 0x8A,
  34.     ADDR_DS1302_YEAR = 0x8C,
  35.     ADDR_DS1302_WP   = 0x8E,
  36.     ADDR_DS1302_BRST = 0x90
  37. }ADDR_DS1302;

  38. /* @override tm struct in <time.h> */
  39. struct tm
  40. {
  41.     int tm_sec;              /* [0,59] */
  42.     int tm_min;              /* [0,59] */
  43.     int tm_hour;             /* [0,23]/[1,12] */
  44.     int tm_mday;             /* [1,31] */
  45.     int tm_mon;              /* [1,12] */
  46.     int tm_year;             /* [0,99] */
  47.     int tm_wday;             /* [1,7]1 equal to Sunday */
  48. };

  49. /* ----- function map ----- */
  50. extern void DS1302_defaultInit(void);
  51. extern void DS1302_config(struct tm *ts);
  52. extern u8   DS1302_getHour(void);
  53. extern u8   DS1302_getMinute(void);
  54. extern u8   DS1302_getSecond(void);
  55. extern void DS1302_isEnableWrittenProtect(ACTION a);
  56. extern u8   DS1302_readData(ADDR_DS1302 addr);
  57. extern u8   DS1302_readSingleByte(void);
  58. extern void DS1302_setHour(u8 hour);
  59. extern void DS1302_setMinute(u8 min);
  60. extern void DS1302_setSecond(u8 sec);
  61. extern void DS1302_writeData(ADDR_DS1302 addr,u8 dat);
  62. extern void DS1302_writeSingleByte(u8 dat);

  63. #endif

  64. /* -------------------- END OF FILE -------------------- */

复制代码


最后是硬件部分的C文件:

  1. /*
  2. * @Author:NUIST_XKFYT
  3. * @Board:CT107D
  4. * @Coding:UTF-8
  5. * @E-mail:weilun_fong@nuist.edu.cn
  6. * @IDE:Keil v4
  7. * @Note:for operations on-board DS18B20
  8. *
  9. * @Log:(1)2017-02-19:creat
  10. */

  11. #include "CT107D_ds18b20.h"

  12. static float fac = 0.0625f;                     /* default temperature factor */

  13. /*
  14. * @Prototype:extern u16 DS18B20_getTemperature(void)
  15. * @Argument:None
  16. * @Ret-val:original data(a 16-bit data)
  17. * @Note:get temperature data from DS18B20
  18. */
  19. extern u16 DS18B20_getTemperature(void)
  20. {
  21.     u8 t[2] = { 0x00,0x00 };

  22.     DS18B20_init();                              /* STEP-1:initialization timing firstly(@Something important-point 1) */
  23.     DS18B20_writeByte(DS18B20_CMD_SKROM);        /* STEP-2:ROM operation command */
  24.     DS18B20_writeByte(DS18B20_CMD_CONVT);        /* STEP-3:function command */

  25.     DS18B20_init();
  26.     DS18B20_writeByte(DS18B20_CMD_SKROM);
  27.     DS18B20_writeByte(DS18B20_CMD_RSCRA);
  28.     t[0] = DS18B20_readByte();
  29.     t[1] = DS18B20_readByte();
  30.     DS18B20_reset();

  31.     return ((t[1] << 0x08) | t[0]);
  32. }

  33. /*
  34. * @Prototype:extern float DS18B20_handleTemperature(u16 n)
  35. * @Argument:None
  36. * @Ret-val:result of handling temperature
  37. * @Note:translate original temperature data into float format
  38. */
  39. extern float DS18B20_handleTemperature(u16 t)
  40. {
  41.     u8 pkg[2];
  42.     float val = 0.0f;

  43.     pkg[0] = (u8)t;
  44.     pkg[1] = (u8)(t >> 0x08);
  45.     if((pkg[1] & 0xF0) == 0xF0)        /* check wether the current temperatire is below zero */
  46.     {
  47.         if(pkg[0] == 0x00)
  48.         {
  49.             pkg[0] = ~pkg[0] + 1;
  50.             pkg[1] = ~pkg[1] + 1;
  51.         }
  52.         else
  53.         {
  54.             pkg[0] = ~pkg[0] + 1;
  55.             pkg[1] = ~pkg[1];
  56.         }
  57.     }
  58.     return (float)(pkg[1]*0xFF + pkg[0])*fac;
  59. }

  60. /*
  61. * @Prototype:extern u1 DS18B20_init(void)
  62. * @Argument:None
  63. * @Ret-val:<1>true:ready;<2>false:busy or not exist
  64. * @Note:initialization/reset timing of DS18B20
  65. */
  66. extern u1 DS18B20_init(void)
  67. {
  68.     u1 flag = false;

  69.     /* STEP-1:make one-wire bus is in low level staus */
  70.     DS18B20_DQ = 0;
  71.     /* STEP-2:wait for 480us */
  72.     DELAY_480us();
  73.     /* STEP-3:release bus,wait for low level */
  74.     DS18B20_DQ = 1;
  75.     /* STEP-3:wait for 80us */
  76.     DELAY_80us();
  77.     /* STEP-4:sample in order to make sure the DS18B20 is existing */
  78.     flag = DS18B20_DQ;
  79.     /* STEP-5:wait for 400us(until bus recover) */
  80.     DELAY_400us();

  81.     return (!flag);     /* NOT operation for handling return value from DS18B20 */
  82. }

  83. /*
  84. * @Prototype:extern u8 DS18B20_readByte(void)
  85. * @Argument:None
  86. * @Ret-val:reading result
  87. * @Note:read one byte from DS18B20
  88. */
  89. extern u8 DS18B20_readByte(void)
  90. {
  91.     u8 i = 0x00;                       /* count variable */
  92.     u8 r = 0x00;                       /* result variable */

  93.     for(i = 0x00;i< 0x08;i++)
  94.     {
  95.         /* STEP-1:read from LSB */
  96.         r = r >> 0x01;
  97.         /* STEP-2:make bus is in low level status and produce a read signal */
  98.         DS18B20_DQ = 0;
  99.         /* STEP-3:wait for 5us */
  100.         DELAY_5us();
  101.         /* STEP-4:release bus and ready for reading data */
  102.         DS18B20_DQ = 1;
  103.         /* STEP-5:wait about 5us again */
  104.         DELAY_5us();
  105.         /* STEP-6:read bus status */
  106.         if(DS18B20_DQ)
  107.         {
  108.             r = r | 0x80;
  109.         }
  110.         /* STEP-7:following the timing,it requires wait about 30us */
  111.         DELAY_30us();
  112.         /* STEP-8:pull up bus for next use */
  113.         DS18B20_DQ = 1;
  114.     }

  115.     return r;
  116. }

  117. /*
  118. * @Prototype:extern void DS18B20_writeByte(u8 dat)
  119. * @Argument:<1>dat:the data you want to write
  120. * @Ret-val:None
  121. * @Note:write one byte into DS18B20
  122. */
  123. extern void DS18B20_writeByte(u8 dat)
  124. {
  125.     u8 i = 0x00;
  126.     u8 j = 0x00;

  127.     for(i = 0x00;i < 0x08;i++)
  128.     {
  129.         /* STEP-1:make bus is in low level status and produce a write signal */
  130.         DS18B20_DQ = 0;
  131.         /* STEP-2:wait about 15us */
  132.         DELAY_15us();
  133.         /* STEP-3:judge and write into bus(from LSB) */
  134.         if(dat & 0x01)
  135.         {
  136.             DS18B20_DQ = 1;
  137.         }
  138.         else
  139.         {
  140.             DS18B20_DQ = 0;
  141.         }
  142.         /* STEP-4:wait about 60us */
  143.         DELAY_60us();
  144.         /* STEP-5:release bus and wait for DS18B20 recovering */
  145.         DS18B20_DQ = 1;
  146.         /* STEP-6:move for writing bit */
  147.         dat = dat >> 0x01;
  148.     }
  149.     //DS18B20_DQ = 1;        //this line can be ignored
  150. }

  151. /* -------------------- 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;
}  
这个程序有问题吗

嗯嗯,已经解决,谢谢大神

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top