【Blue Bridge Cup MCU】DS18B20

Publisher:平和的心情Latest update time:2020-07-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

DS18B20 and the microcontroller use a single bus connection. The file used in the competition is the provided "onewire.c"


Programming steps:


(1) Initialization


(2) Send ROM operation commands, including: Read ROM [33H], Match ROM [55H], Skip ROM [CCH], Search ROM [F0H], Alarm search [ECH]


(3) Send memory operation commands, including: Write Scratchpad (write temporary storage memory) [4EH], Read Scratchpad (read temporary storage memory) [BEH], Copy Scratchpad (copy temporary storage memory) [48H], Convert Temperature (temperature conversion) [44H], Recall EPROM (recall) [B8H], Read Power supply (read power supply) [B4H]


(4) Memory or appropriate delay




PS: When using IAP15, make the following changes to void Delay_OneWire(unsigned int t) in onewire.c:


// Single bus delay function

void Delay_OneWire(unsigned int t)

{

unsigned char i;

while(t--){

for(i=0;i<12;i++);

}

}


Multiply the previous delay time by 12, because the machine cycle of the IAP15 used is the oscillation cycle, and it cannot be read if it is not modified.



Attachment: Code for measuring temperature


uchar low,high;

int gettemper()

{

int t;

 

Init_DS18B20(); //1. Initialization timing

Write_DS18B20(OW_SKIP_ROM); //2. ROM operation command

    Write_DS18B20(DS18B20_CONVERT); //3. Memory operation command

Delay_OneWire(200);

//end

 

    Init_DS18B20(); //1. Initialization timing

Write_DS18B20(OW_SKIP_ROM); //2. ROM operation command

    Write_DS18B20(DS18B20_READ); //3. Memory operation command

 

EA = 0;

low = Read_DS18B20();

high = Read_DS18B20();

    EA = 1;

 

t = high<<8;

t = t|low;

 

return t;

// return 0xf13c; //test

}

Does this function return the exact data in the DS18B20 temperature register? If you want to choose to return an integer value or a floating point value, data processing is required.


The delay 2 value (200) cannot be changed.




Appendix: Data processing functions


float get_f_temp()

{

int t;

float temp;

t = gettemper();

if((t&0xf000)>>12 == 0x000f) //negative number

temp = ((~t)+1)*0.0625*(-1.0);

else

temp = t*0.0625;

 

return temp;

}

Note: When the temperature read is negative, the highest bit returned by gettemper() is 1. In this case, you need to invert the returned value, add 1, and finally multiply it by the 12-bit precision of 0.0625.

Reference address:【Blue Bridge Cup MCU】DS18B20

Previous article:【Blue Bridge Cup MCU Group】(3)Keyboard
Next article:【Blue Bridge Cup MCU Group】Real-time clock RTC (DS1302)

Recommended ReadingLatest update time:2024-11-16 11:45

DS18B20 usage program PIC microcontroller
#include pic.h __CONFIG(0x1832);         //Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off, encryption, 4M crystal HS oscillation #define uch unsigned char //Give unsigned char an alias name of uch #define DQ RA2 //define 18B20 data port  #define DQ_DI
[Microcontroller]
51 single chip microcomputer based on DS18B20 temperature detection and display on LCD1602
The microcontroller source program is as follows: #include reg52.h #include stdio.h sbit DQ = P1^3; sbit RS = P1^5; sbit RW = P1^6; sbit EN = P1^7; void delay(unsigned int i); void init_DS18B20(void); unsigned char read_DS18B20(void); void write_DS18B20(unsigned char dat); double read_temperature(void); void init_l
[Microcontroller]
51 single chip microcomputer based on DS18B20 temperature detection and display on LCD1602
DS18B20 header file for AVR M16 8M
Just call gettemp(); and it's done! The export parameter wmh is the high bit of the display, and wml is the low bit of the display, then call your display program!  /****************************************************************************  ds18b20 header file     M16 internal 8M          *********************
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号