找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1076|回复: 2
打印 上一主题 下一主题
收起左侧

单片机DS18B20多点测温程序构思,欢迎大家来指正交流

[复制链接]
跳转到指定楼层
楼主
ID:999824 发表于 2022-12-2 11:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
首先
写一个获取DS18B20的64位序列号的keil工程,获取需要连接的DS18B20器件的序列号。
uint8_t rom[8];
void read_rom(void)
{
        uint8_t index;
        
        ds18b20_init();
        ds18b20_write_byte(0x33);
        
        for (index = 0;index < 8;index++)
        {
                rom[index] = ds18b20_read_byte();
        }
}
之后在将获取到的DS18B20器件的序列号通过数组保存起来,再新建一个获取多点温度的keil工程,发送匹配64位序列号,获取温度即可。

详细内容见单片机源程序:
  1. uint8_t rom1[8]={0x28,0x0B,0xAC,0x79,0x97,0x16,0x03,0x39};
  2. uint8_t rom2[8]={0x28,0xF2,0x80,0x79,0x97,0x15,0x03,0x51};
  3. uint8_t rom3[8]={0x28,0xFF,0x64,0x79,0x97,0x16,0x03,0x8B};
  4. uint8_t rom4[8]={0x28,0x6F,0x31,0x79,0x97,0x15,0x03,0x28};
  5. uint8_t rom5[8]={0x28,0x1C,0x5C,0x79,0x97,0x15,0x03,0x14};
  6. uint8_t rom6[8]={0x28,0xED,0xD8,0x79,0x97,0x16,0x03,0xD0};
  7. uint8_t rom7[8]={0x28,0x94,0xB0,0x79,0x97,0x15,0x03,0xA4};
  8. uint8_t rom8[8]={0x28,0xAE,0xD5,0x79,0x97,0x16,0x03,0x10};
  9. uint8_t rom9[8]={0x28,0xC0,0xC2,0x79,0x97,0x16,0x03,0xED};

  10. void DS18B20_Matrom(unsigned char a)
  11. {
  12.         unsigned char j;  //用于循环
  13.         ds18b20_init();
  14.         ds18b20_write_byte(0x55);         //发送匹配ROM命令
  15.         if(a==1)
  16.         {
  17.                 for(j=0;j<8;j++)
  18.                 {
  19.                          ds18b20_write_byte(rom1[j]);//发送18B20的序列号,先发送低字节
  20.                 }
  21.         }

  22.         if(a==2)
  23.         {
  24.                 for(j=0;j<8;j++)
  25.                 {
  26.                          ds18b20_write_byte(rom2[j]);//发送18B20的序列号,先发送低字节
  27.                 }
  28.         }

  29.         if(a==3)
  30.         {
  31.                 for(j=0;j<8;j++)
  32.                 {
  33.                          ds18b20_write_byte(rom3[j]);//发送18B20的序列号,先发送低字节
  34.                 }
  35.         }

  36.         if(a==4)
  37.         {
  38.                 for(j=0;j<8;j++)
  39.                 {
  40.                          ds18b20_write_byte(rom4[j]);//发送18B20的序列号,先发送低字节
  41.                 }
  42.         }

  43.         if(a==5)
  44.         {
  45.                 for(j=0;j<8;j++)
  46.                 {
  47.                          ds18b20_write_byte(rom5[j]);//发送18B20的序列号,先发送低字节
  48.                 }
  49.         }

  50.         if(a==6)
  51.         {
  52.                 for(j=0;j<8;j++)
  53.                 {
  54.                          ds18b20_write_byte(rom6[j]);//发送18B20的序列号,先发送低字节
  55.                 }
  56.         }

  57.         if(a==7)
  58.         {
  59.                 for(j=0;j<8;j++)
  60.                 {
  61.                          ds18b20_write_byte(rom7[j]);//发送18B20的序列号,先发送低字节
  62.                 }
  63.         }

  64.         if(a==8)
  65.         {
  66.                 for(j=0;j<8;j++)
  67.                 {
  68.                          ds18b20_write_byte(rom8[j]);//发送18B20的序列号,先发送低字节
  69.                 }
  70.         }
  71.         if(a==9)
  72.         {
  73.                 for(j=0;j<8;j++)
  74.                 {
  75.                          ds18b20_write_byte(rom9[j]);//发送18B20的序列号,先发送低字节
  76.                 }
  77.         }
  78. }

  79. int16_t read_temp(uint8_t rom)          //启动温度转换,读取温度
  80. {
  81.         u8 tl=0,th=0;
  82.         if(ds18b20_init())
  83.                 return 0x7fff;
  84.         ds18b20_write_byte(0xCC);
  85.         ds18b20_write_byte(0x44);//启动DS18B20进行温度转换

  86.         DS18B20_Matrom(rom);//匹配RAM,适用多个点的情况
  87.        
  88.         ds18b20_write_byte(0xBE);//读DS18B20内部RAM中9字节的温度数据
  89.         tl=ds18b20_read_byte();//读低8位
  90.         th=ds18b20_read_byte();//读高8位
  91.         return (th<<8)+tl;
  92. }

  93. float get_ntemp(uint8_t rom)
  94. {
  95.         float         temp=0;
  96.         temp = read_temp(rom);
  97.         temp = temp*0.0625;
  98.        
  99.         return temp;
  100. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:688692 发表于 2022-12-3 14:27 | 只看该作者
在深入一点,写个二叉树搜索,就不用先存ROM表了。
回复

使用道具 举报

板凳
ID:170206 发表于 2022-12-4 01:35 | 只看该作者
使用链表,先扫描所有18B20,把序列号通过链表存储,然后根据链表信息进行读取温度
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表