PIC16F877 MCU DS18B20 digital tube thermometer simulation program can display negative temperature

Publisher:fuehrd努力的Latest update time:2019-10-29 Source: 51heiKeywords:PIC16F877  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The circuit diagram is as follows:

//************************************************************

// Function implemented: Digital tube displays real-time temperature, supports negative temperature

// Chip PIC16F877

// XT:4MHZ

//************************************************************

#include //Includes the predefined internal resources of the MCU

#define LVP 0x3f39


// Crystal: XT; Code: No code protection; Power-on delay timer off;

//Low voltage reset disabled; watchdog disabled; low voltage programming disabled

__CONFIG (XT & UNPROTECT & PWRTDIS & BORDIS & WDTDIS & LVP);


#define uch unsigned char //Give unsigned char an alias name of uch

#define DQ RA2 //Define 18B20 data port 

#define DQ_DIR TRISA2 //Define 18B20D port direction register 

#define DQ_HIGH() DQ_DIR =1 //Set the data port to input

#define DQ_LOW() DQ_DIR = 0; DQ = 0 //Set the data port to output


const unsigned char ledcode[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};

//Common cathode digital tube 0123456789 segment code without decimal point, positive and negative sign bit

const unsigned char ledcode1[12]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x00,0x40};   

//Common cathode digital tube with decimal point 0123456789 segment code, positive and negative sign bit 


void init_port(void);

void delay(char x,char y); 

void delay_1ms(void);

void delay_ms(unsigned int time);

void interrupt dealtime();

void tmint(void);

void timetoseg(uch fh_temp,uch bai_temp,uch shi_temp,uch ge_temp,uch sf_temp,uch bf_temp,uch qf_temp,uch wf_temp);

void binary_temp(uch TL , signed char TH);

void reset(void);

void write_byte(uch val);

uch read_byte(void);

void get_temp(void);


unsigned char display_data[8];

unsigned char intcount=0; 

uch TLV=0 ; //The collected temperature is 8 bits high

uch THV=0; //The collected temperature is 8 bits lower


union temp //define a union

{

int T;        

uch TV[2];        

}temp;


signed char TZ=0; //The integer part of the converted temperature value, with a sign bit

uch TX=0; //The decimal part of the converted temperature value


unsigned int wd; //BCD code format of the converted temperature value


unsigned char fh; //sign bit

unsigned char bai; // integer hundreds place

unsigned char shi; // tens digit

unsigned char ge; // integer digit

unsigned char shifen; //tenths place

unsigned char baifen; //percentile

unsigned char qianfen; //thousandths

unsigned char wanfen; //ten thousandth place



//************************************************************

// Main program

//************************************************************

void main(void)

{

init_port();

tmint();

while(1)

  {         

   get_temp();

   timetoseg(fh,bai,shi,ge,shifen,baifen,qianfen,wanfen);  


  }

}

//************************************************************

//Port initialization

// PORTD is used as digital tube segment driver (high effective)

//PORTE is used as digital tube bit selection driver (low effective)

//************************************************************

void init_port(void)

{

  RBPU=0;

// PORTB = 0xFF;

  TRISB=0xFF; 

  PORTD=0x00; //

  TRISC=0x00; //Port C controls the LED indicator, set to output

  TRISD=0; //Port D is used as a digital tube segment and is set to output

  ADCON1=0x07; // Make port A and port E all digital I/O ports

  TRISE=0x00; //Port E is used as the digital tube position selection control pin, set to output   

  PORTE=0x00; 

}

//************************************************************

// Delay procedure

//************************************************************

void delay(char x,char y) 

{

  char z;

  do{

      z=y;

      do{;}while(--z);

     }while(--x);

}

//The instruction time is: 7+(3*(Y-1)+7)*(X-1). If we add the 7 instructions for calling the function, setting the page, and passing parameters, the instruction time is: 7+(3*(Y-1)+7)*(X-1).

//Then it is: 14+(3*(Y-1)+7)*(X-1).

//************************************************************

// Delay procedure

//************************************************************

void delay_1ms(void)

{

  unsigned int n;

  for(n=0;n<50;n++)

   {

    NOP();

   }

}

//************************************************************

void delay_ms(unsigned int time)

{

  for(;time>0;time--)

   {

    delay_1ms();

   }

}


//-----------------------------------------------

//Reset DS18B20 function

void reset(void)

{

  uch presence=1;

  while(presence)

  { 

    DQ_LOW(); //Host pulls to low level

    delay(2,90); //delay>480503us

    DQ_HIGH(); //Release the bus and wait for the resistor to pull the bus high and keep it for 15~60us

    delay(2,8); //delay>60us

    if(DQ==1) presence=1; //No response signal received, continue to reset

    else presence=0; //Receive response signal

    delay(2,70); //delay>240us

   }

  }


//-----------------------------------------------

//Write 18b20 write byte function

void write_byte(uch val)

{

uch i;

uch temp;

for(i=8;i>0;i--)

{

   temp=val&0x01; //lowest bit shifted out

   DQ_LOW(); 

   NOP();

   NOP();

   NOP();

   NOP();

   NOP(); //Pull from high level to low level to generate write time gap

   if(temp==1) DQ_HIGH(); //If write 1, pull the level high

   delay(2,7); //delay 63us

   DQ_HIGH(); 

   NOP();

   NOP();

   val=val>>1; //Move right one bit

  }

}


//------------------------------------------------

//18b20 read byte function

uch read_byte(void)

{

uch i;

uch value=0; //Read the temperature

static bit j;

for(i=8;i>0;i--)

{

   value>>=1; 

   DQ_LOW(); //Each read time slot is initiated by the host, pulling the bus low for at least 1μs.

   NOP();

   NOP();

   NOP();

   NOP();

   NOP();

   NOP(); //6us

   DQ_HIGH(); //Release the bus within 15μs after the start of the read time slot, pull it to a high level, and prepare to sample the bus.

   NOP(); 

   NOP();

   NOP();  

   NOP(); 

   NOP(); //5us

   j=DQ; //sampling bus

   if(j) value|=0x80; //Put the sampled data into value

   delay(2,7); //All read time slots are at least 60μs, here about 63us

  }

  return(value);

}


//-------------------------------------------------

//Start the temperature conversion function

void get_temp()

int i;

DQ_HIGH();

reset(); //Reset and wait for slave response 

write_byte(0XCC); //Ignore ROM match 

write_byte(0X44); //Send temperature conversion command  

for(i=10;i>0;i--)

    {    

        delay(201,132);                       

    }    


reset(); //Reset again and wait for the slave to respond 

write_byte(0XCC); //Ignore ROM match 

write_byte(0XBE); //Send the temperature read command 


TLV=read_byte(); //Read the lower 8 bits of temperature 

THV=read_byte(); //Read the high 8 bits of temperature 


DQ_HIGH(); //Release the bus 



TZ=(TLV>>4)|(THV<<4); //Temperature integer part

TX=TLV<<4; //Temperature decimal part, note that the last four digits of TX are invalid

binary_temp(TX, TZ ); //Convert the corresponding temperature binary value into a decimal number

}


//Convert the binary values ​​of the corresponding temperature integer and decimal parts into decimal numbers


void binary_temp(char TL , signed char TH)

{

  if(TH>=0) //If it is positive temperature

  {

    fh=0x0A; //positive number sign bit

    bai=TH/100; //hundreds digit of integer

    shi=(TH%100)/10; //Tens digit //Tens digit

    ge=(TH%100)%10; //unit digit //unit digit of integer part


    wd=0;  

    if (TL & 0x80) wd=wd+5000;

    if (TL & 0x40) wd=wd+2500;

    if (TL & 0x20) wd=wd+1250;

    if (TL & 0x10) wd=wd+625; //The above 4 instructions convert the decimal part into BCD code             


    shifen=wd/1000; //tenth place                    

    baifen=(wd%1000)/100; //percentile

    qianfen=(wd%100)/10; //thousandths

    wanfen=wd%10; //ten thousandth place

    NOP();

   }

else //Otherwise, it is a negative temperature, requiring complement

{

  temp.TV[0]=TL;temp.TV[1]=TH;

  temp.T=(~temp.T)+0x0010; //complement code, add 1       

  TL=temp.TV[0];

  TH=temp.TV[1];


    fh=0x0B; //Negative sign bit

    bai=TH/100; //hundreds digit of integer

    shi=(TH%100)/10; //Tens digit //Tens digit

    ge=(TH%100)%10; //unit digit //unit digit of integer part


    wd=0;  

    if (TL & 0x80) wd=wd+5000;

    if (TL & 0x40) wd=wd+2500;

    if (TL & 0x20) wd=wd+1250;

    if (TL & 0x10) wd=wd+625; //The above 4 instructions convert the decimal part into BCD code             


    shifen=wd/1000; //tenth place                    

    baifen=(wd%1000)/100; //percentile

    qianfen=(wd%100)/10; //thousandths

    wanfen=wd%10; //ten thousandth place

    NOP();

}                

}


// Convert each temperature value into segment code

//************************************************************

void timetoseg(uch fh_temp,uch bai_temp,uch shi_temp,uch ge_temp,uch sf_temp,uch bf_temp,uch qf_temp,uch wf_temp)

{

   display_data[0] = ledcode[wf_temp];

   display_data[1] = ledcode[qf_temp];

   display_data[2] = ledcode[bf_temp];

   display_data[3] = ledcode[sf_temp];

   display_data[4] = ledcode1[ge_temp];

   display_data[5] = ledcode[shi_temp];

   display_data[6] = ledcode[bai_temp];

   display_data[7] = ledcode[fh_temp];

}



//************************************************************

// Timer interrupt initialization (OPTION_REG)

//************************************************************

void tmint(void)

{

  T0CS=0; //Clock source is internal instruction cycle                   

  PSA=0; //The divider is assigned to TMR0 

//  

  PS2=0; //The division ratio of TMR0 is 1:16          

  PS1=1;

  PS0=1;

//

  GIE=1; //Enable general interrupt 

  T0IE=1; //Enable timer 0 overflow interrupt

  T0IF=0; // Clear timer 0 interrupt flag

  TMR0=0X06; //Preset initial value T=(256-6)x16=4000uS

}

//************************************************************

void interrupt dealtime() //Interrupt entry, this interrupt completes the dynamic scanning of the digital tube

{ //Each interruption lasts for 4 milliseconds

    T0IF=0;

    TMR0=0X06;


    PORTD = 0x00; //Turn off the display first

   if(intcount==0)

     {

      PORTD = display_data[0];

      PORTE=0x00;

      intcount+=1;

     }

   else if(intcount==1)

     {

      PORTD = display_data[1];

      PORTE=0x01;

      intcount+=1;

     }

   else if(intcount==2)

     {

      PORTD = display_data[2];

      PORTE=0x02;

      intcount+=1;

[1] [2]
Keywords:PIC16F877  DS18B20 Reference address:PIC16F877 MCU DS18B20 digital tube thermometer simulation program can display negative temperature

Previous article:PIC microcontroller button controls PWM output LED light brightness C language program
Next article:PIC12F675 MCU LED control program

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

STM32 DS18B20 code detailed study summary
DS18B20 is the most commonly used to learn a new development tool. The programs are similar. The main thing is to pay attention to the delay in the timing and the correct instructions. Record them here! ------------------The first part is--------ds18b20.h---------------------- #ifndef __DS18B20_H #define __DS18B20_H
[Microcontroller]
STM32 DS18B20 code detailed study summary
ds18b20 pic54 microcontroller communication source program
 ds18b20 p IC 54  microcontroller  communication source program ORG     PIC54      GOTO    MAIN      ORG     0 ;---------------------- ;---------------------------- DELAY22                     MOV LW   D'200'               ; DELAY 2*250=500mS         MOV WF    COUNT1 DE32    MOV LW   D'250'                ; 8*250=2mS
[Microcontroller]
Detailed Timing and Code Analysis of DS18B20 Temperature Sensor
Brief description of temperature measurement: The core function of DS18B20 is its direct digital temperature sensor. The accuracy of the temperature sensor is user-programmable 9, 10, 11 or 12 bits, increasing in increments of 0.5°C, 0.25°C, 0.125°C and 0.0625°C respectively  . The default accuracy is 12 bits in the p
[Microcontroller]
Detailed Timing and Code Analysis of DS18B20 Temperature Sensor
Principle and implementation of PIC microcontroller controlling DS18B20 digital temperature sensor
#include #define uchar unsigned char #define uint  unsigned int #define DQ RC1 #define DQ_HIGH() TRISC1=1 #define DQ_LOW() TRISC1=0;DQ=0 __CONFIG(0x3B31); const uchar table ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,                         0x07,0x7f,0x6f}; const uchar table1 ={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
[Microcontroller]
DS18B20 digital computer thermometer with LCD display
  There are many types of LCDs. According to the display mode, they can be divided into segment type, line dot matrix type and full dot matrix type. The segment type is similar to the digital tube. The line dot matrix type is generally English characters, and the full dot matrix type can display any information, such
[Microcontroller]
DS18B20 digital computer thermometer with LCD display
Real-time temperature acquisition and analysis system based on AVR microcontroller and DS18B20
Introduction: The real-time temperature acquisition and analysis system described in this article is a real-time temperature acquisition and analysis system based on the combination of AVR series microcontroller ATmega16 and temperature sensor DS18B20. The real-time temperature acquisition and analysis system descri
[Microcontroller]
Real-time temperature acquisition and analysis system based on AVR microcontroller and DS18B20
Design of Ultrasonic Distance Measurement System Based on PIC16F877 Microcontroller
introduction In an autonomous walking robot system, the robot must collect environmental information in real time to avoid obstacles and navigate in an unknown and uncertain environment. This must be achieved by relying on a sensor system that can perceive environmental information. Visual, infrared, laser, ult
[Microcontroller]
Design of Ultrasonic Distance Measurement System Based on PIC16F877 Microcontroller
Design of agricultural environment temperature monitoring system based on AT89C51 microcontroller and DS18B20
Modern facility agriculture is a high-tech, high-efficiency modern agricultural production method that integrates biotechnology, engineering technology, and environmental technology. It is an effective way to adjust the agricultural industry, adapt to the development of market economy and improve the utilization rate
[Microcontroller]
Design of agricultural environment temperature monitoring system based on AT89C51 microcontroller and DS18B20
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号