<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ATmega16L學(xué)習(xí)板18B20測試程序

          ATmega16L學(xué)習(xí)板18B20測試程序

          作者: 時間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
          ATmega16L學(xué)習(xí)板18B20測試程序

          晶振頻率: 8MHz
          編譯: ICCAVR 6.31

          本文引用地址:http://www.ex-cimer.com/article/201611/316774.htm

          #include
          #include
          #include "../include/board.h"

          /*===================================================================
          // 函數(shù)功能: DS18B20數(shù)據(jù)校驗(yàn)函數(shù)
          // 形參: void
          // 返回: unsigned char 校驗(yàn)結(jié)果
          // 編寫: 2004/8/25
          // 備注: CRC公式為:CRC = X^8 + X^5 + X^4 + 1
          ===================================================================*/
          unsigned char crccheck(unsigned char *p,unsigned char len)
          {
          unsigned char bit0,cbit,r,temp,i,j,byte;
          temp = 0;
          for(j = 0; j < len; j++)
          {
          byte = p[j];
          for(i = 0; i < 8; i++)
          {
          cbit = temp & 0x01;
          bit0 = byte&0x01;
          temp >>= 1;
          r = cbit ^ bit0;
          if(r == 1)
          temp ^= 0x8c;
          byte >>= 1;
          }
          }
          return temp;
          }

          /*===================================================================
          // 函數(shù)功能: us延時函數(shù)
          // 形參: void
          // 返回: void
          // 編寫: 2004/8/25
          ===================================================================*/
          void delay_us(unsigned int time)
          {
          do
          {
          time--;
          }
          while (time>1);
          }

          /*===================================================================
          // 函數(shù)功能: 判斷總線應(yīng)答
          // 形參: void
          // 返回: unsigned char true為應(yīng)答
          // 編寫: 2004/8/25
          ===================================================================*/
          unsigned char ds1820_ack(void)
          {
          unsigned char ack;
          DDRC |= DQ;
          PORTC &= ~DQ;
          delay_us(500); // reset
          PORTC |= DQ;
          DDRC &= ~DQ;
          delay_us(45);
          ack = DQ & PINC;
          delay_us(500); // host receive
          if(ack)
          return true;
          else
          return false;
          }

          /*===================================================================
          // 函數(shù)功能: 從 1-wire 總線上讀取一個字節(jié)
          // 形參: void
          // 返回: unsigned char 讀到的值
          // 編寫: 2004/8/25
          ===================================================================*/
          unsigned char read_byte(void)
          {
          unsigned char i;
          unsigned char value = 0;
          for(i = 8; i > 0; i--)
          {
          value >>= 1; // low bit first
          DDRC |= DQ;
          PORTC &= ~DQ; // pull DQ low to start timeslot
          delay_us(3);
          PORTC |= DQ;
          DDRC &= ~DQ; // release bus
          delay_us(10);
          if(DQ & PINC)
          value|=0x80;
          delay_us(100);
          DDRC |= DQ;
          delay_us(5); // time interval
          }
          return(value);
          }

          /*===================================================================
          // 函數(shù)功能: 向 1-WIRE 總線上寫一個字節(jié)
          // 形參: value 寫到總線上的值
          // 返回: void
          // 編寫: 2004/8/25
          ===================================================================*/
          void write_byte(unsigned char value)
          {
          unsigned char i;
          DDRC |= DQ;
          for(i = 8; i > 0; i--)
          {
          if(value & 0x01)
          {
          PORTC &= ~DQ; // pull DQ low to start timeslot
          delay_us(10);
          PORTC |= DQ;
          delay_us(100);
          }
          else
          {
          PORTC &= ~DQ; // pull DQ low to start timeslot
          delay_us(100);
          PORTC |= DQ;
          delay_us(10);
          }
          value >>= 1;
          }
          }

          /*===================================================================
          // 函數(shù)功能: 讀取溫度
          // 形參: *temperature 溫度存儲空間
          // 返回: unsigned char true為有效
          // 編寫: 2004/8/25
          ===================================================================*/
          unsigned char Read_Temperature(unsigned int *temperature)
          {
          unsigned char i;
          union{
          unsigned char c[2];
          unsigned int x;
          }temp;
          unsigned char temporary[9];

          ds1820_ack();
          write_byte(0xCC); // Skip ROM
          write_byte(0x44); // Start Conversion
          for(i = 0; i < 16; i++)
          delay_us(50000);
          ds1820_ack();
          write_byte(0xCC); // Skip ROM
          write_byte(0xBE); // Read Scratch Pad
          for(i = 0; i < 9; i++)
          temporary[i] = read_byte();
          temp.c[0] = temporary[0];
          temp.c[1] = temporary[1];

          if(crccheck(temporary,9))
          return false;
          else
          {
          *temperature = temp.x;
          return true;
          }
          }

          /*===================================================================
          // 函數(shù)功能: 讀取Rom Code
          // 形參: *temp DS18B20的Rom Code存儲空間
          // 返回: unsigned char true為有效
          // 編寫: 2004/8/25
          ===================================================================*/
          unsigned char Read_RomCode(unsigned char *temp)
          {
          ds1820_ack();
          write_byte(0x33);
          temp[0] = read_byte();
          temp[1] = read_byte();
          temp[2] = read_byte();
          temp[3] = read_byte();
          temp[4] = read_byte();
          temp[5] = read_byte();
          temp[6] = read_byte();
          temp[7] = read_byte();
          if(crccheck(temp,8))
          return false;
          else
          return true;
          }

          /*===================================================================
          // 函數(shù)功能: 匹配DS18B20
          // 形參: *p DS18B20的Rom Code
          // 返回: void
          // 編寫: 2004/8/25
          ===================================================================*/
          void ds1820_match(unsigned char *p)
          {
          unsigned char i;
          ds1820_ack();
          write_byte(0x55);
          for(i=0;i<8;i++)
          write_byte(p[i]);
          }



          關(guān)鍵詞: ATmega16L18B20測試程

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();