18B20 temperature sensor program based on ARM9

Publisher:leader4Latest update time:2015-12-25 Source: eefocusKeywords:ARM9  18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
18B20 temperature sensor is a typical timing operation device. It is very convenient to write it with 51 single-chip microcomputer. However, it still needs a certain translation skill to transplant this ready-made program to FPGA, ARM and 430.

 

//-----------------------------------------------------------------------------------------------------
//Function name: dmsec //Entry parameter: count //Function function: delay subroutine
//-------------------------------------------------------------------------------------
void dmsec(uint count) { uchar i;
 while(count--) {
  for(i=0;
  i<125;
  i++);
 }
}
//-----------------------------------------------------------------------------------------------------
//Function name: tmreset //Entry parameter: none//Function function:
 //-------------------------------------------------------------------------------------
void tmreset(void) {
  ushort i;
  dq=0;
  i=343;
  while(i>0) i--;
  //about 900us dq=1;
  i=13;
  while(i>0) i--;
} //-----------------------------------------------------------------------------------------------------
 //Function name: tmpre //Entry parameter: none//Function function: wait for DS18B20 response
 //-----------------------------------------------------------------------------------------------------
void tmpre(void) {
  uchar i;
  while(dq);
  while(~dq);
  i=13;
  while(i>0) i--;
 } //-------------------------------------------------------------------------------------
 //Function name: tmrbit //Entry parameter: None//Return value: dat //Function function: read one bit on the bus
 //-------------------------------------------------------------------------------------
bit tmrbit(void) {
  uchar i;
  bit dat;
  dq=0;
  i++;
  dq=1;
  i++;
  i++;
  dat=dq;
  i=30;
  while(i>0) i--;
  return(dat);
} //-----------------------------------------------------------------------------------------------------
 //Function name: tmrbyte //Entry parameter: None//Return value: dat //Function function: read a byte
 //-------------------------------------------------------------------------------------
uchar tmrbyte(void) {
  uchar i,j,dat=0;
  for(i=1;i<=8;i++) {
   j=tmrbit();
   dat=(j<<7)|(dat>>1);
  }
  return(dat);
} //-------------------------------------------------------------------------------------
 //Function name: tmwbyte //Entry parameter: dat //Function: write command
 //-------------------------------------------------------------------------------------
void tmwbyte(uchar dat) {
  uint i;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++) {
   testb=dat&0x01;
   dat=dat>>1;
   if(testb) {
    dq=0;
    //write 1 i++;
    i++;
    dq=1;
    i=27;
    while(i>0) i--;
   }
   else {
    dq=0;
    //write 0 i=27;
    while(i>0) i--;
    dq=1;
    i++;
    i++;
   }
  }
} //--------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------
 ​Today //Photo:Photo
 ///-------------------------------------- ---------------------------------------------
void read_tr( ) {
 show a,b;
 short y;
 reset();
 time();
 dmsec(1);
 tmwbyte(0xcc);
 tmwbyte(0x44);
 dmsec(100);
 reset();
 time();
 dmsec(1);
 tmwbyte(0xcc);
 tmwbyte(0xbe);
 a=begin();
 //lb=size();
 //hy=b&0x0f;
 y=y<<8;
 y=a|y;
 if(y>0x800) {
  y=0x1000-y;
  bjTr=cal12bit(y);
  bjTr=bjTr+0x8000;
 }
 else
 {
  bjTr=cal12bit(y);
 }
 } //12void
temp_init(void) {
 tmreset();
 time();
 tmwbyte(0xcc);
 tmwbyte(0x4e);
 tmwbyte(0x88);
 tmwbyte(0x44);
 tmwbyte(0x7f);
 reset();
}

Keywords:ARM9  18B20 Reference address:18B20 temperature sensor program based on ARM9

Previous article:About debugging STM32 into HardFault_Handler
Next article:STM32f10x startup code analysis

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号