找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5279|回复: 1
收起左侧

一个简单的onewire(单总线)温度测试小程序

[复制链接]
ID:406093 发表于 2018-10-30 17:13 | 显示全部楼层 |阅读模式
        onewire(单总线)是DALLAS公司推出的外围串行扩展总线,传输时钟信号又传输数据,而且能够进行双向通信,具有节省I/O口线、资源结构简单、成本低廉、便于总线扩展和维护等诸多优点。常用到单总线的器件,一般是稳定传感器,如DS18B20、DS2431。

以下是一个简单的onewire(单总线)温度测试程序,希望大家能够喜欢。
  1. // LCD module connections
  2. sbit LCD_RS at P2_0_bit;
  3. sbit LCD_EN at P2_1_bit;

  4. sbit LCD_D4 at P2_2_bit;
  5. sbit LCD_D5 at P2_3_bit;
  6. sbit LCD_D6 at P2_4_bit;
  7. sbit LCD_D7 at P2_5_bit;
  8. // End LCD module connections

  9. // OneWire pinout
  10. sbit OW_Bit at P1_2_bit;
  11. // end OneWire definition


  12. //  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
  13. //  18S20: 9  (default setting; can be 9,10,11,or 12)
  14. //  18B20: 12
  15. const unsigned short TEMP_RESOLUTION = 9;

  16. char *text = "000.0000";
  17. unsigned temp;

  18. void Display_Temperature(unsigned int temp2write) {
  19.   const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  20.   char temp_whole;
  21.   unsigned int temp_fraction;

  22.   // check if temperature is negative
  23.   if (temp2write & 0x8000) {
  24.     text[0] = '-';
  25.     temp2write = ~temp2write + 1;
  26.   }

  27.   // extract temp_whole
  28.   temp_whole = temp2write >> RES_SHIFT;

  29.   // convert temp_whole to characters
  30.   if (temp_whole/100)
  31.      text[0] = temp_whole/100  + 48;
  32.   else
  33.      text[0] = '0';

  34.   text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  35.   text[2] =  temp_whole%10     + 48;             // Extract ones digit

  36.   // extract temp_fraction and convert it to unsigned int
  37.   temp_fraction  = temp2write << (4-RES_SHIFT);
  38.   temp_fraction &= 0x000F;
  39.   temp_fraction *= 625;

  40.   // convert temp_fraction to characters
  41.   text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  42.   text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  43.   text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  44.   text[7] =  temp_fraction%10      + 48;         // Extract ones digit

  45.   // print temperature on LCD
  46.   Lcd_Out(2, 5, text);
  47. }

  48. void main() {

  49.   Lcd_Init();                                    // Initialize LCD
  50.   Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  51.   Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
  52.   Lcd_Out(1, 1, " Temperature:   ");
  53.   // Print degree character, 'C' for Centigrades
  54.   Lcd_Chr(2,13,223);  // different LCD displays have different char code for degree
  55.                       // if you see greek alpha letter try typing 178 instead of 223

  56.   Lcd_Chr(2,14,'C');

  57.   //--- main loop
  58.   do {
  59.     //--- perform temperature reading
  60.     Ow_Reset();                                  // Onewire reset signal
  61.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  62.     Ow_Write(0x44);                              // Issue command CONVERT_T
  63.     Delay_us(120);

  64.     Ow_Reset();
  65.     Ow_Write(0xCC);                              // Issue command SKIP_ROM
  66.     Ow_Write(0xBE);                              // Issue command READ_SCRATCHPAD

  67.     temp =  Ow_Read();
  68.     temp = (Ow_Read() << 8) + temp;

  69.     //--- Format and display result on Lcd
  70.     Display_Temperature(temp);

  71.     Delay_ms(500);
  72.   } while (1);
  73. }
复制代码
相关信息:http://www.51hei.com/bbs/dpj-136722-1.html


OneWire.jpg





评分

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

查看全部评分

回复

使用道具 举报

ID:258566 发表于 2018-10-30 17:33 | 显示全部楼层
带库的编译器mikroe
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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