Principle and implementation of PIC microcontroller controlling DS18B20 digital temperature sensor

Publisher:机械梦想家Latest update time:2016-11-03 Source: eefocusKeywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#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,
                        0x87,0xff,0xef};
uint temper;
uchar a1,a2,a3,a4;
void delay(uint x);
void delayus(uint,uchar);
void init();
void disp(uchar num1,uchar num2,uchar num3,uchar num4);
void write_byte(uchar date);
uchar read_byte();
void get_tem();
void main()
{
 init();
 while(1)
 {
/*  delayus(0,0);//20
  delayus(1,1);//30
  delayus(2,2);//45
  delayus(4,4);//70
  delayus(70,30);//750
  delayus(50,10);//500*/
 // uchar num;
  get_tem();
 // for(num=20;num>0;num--)
 //  disp(a1,a2,a3,a4);
 } 
}
void reset()
{
 uchar st=1;
 DQ_HIGH();
 NOP();NOP();
 while(st)
 {
  DQ_LOW();
  delayus(70,30);
  DQ_HIGH();
  delayus(4,4);
  if(DQ==1)
   st=1;
  else
   st=0;
  delayus(50,10);
 }
}
void write_byte(uchar date)
{
 uchar i,temp;
 DQ_HIGH();
 NOP();NOP();
 for(i=8;i>0;i--)
 {
  temp=date&0x01;//01010101
  DQ_LOW();
  delayus(0,0);
  if(temp==1)
   DQ_HIGH();
  delayus(2,2);
  DQ_HIGH();
  date=date>>1;//00101010
 }
}
uchar read_byte()
{
 uchar i,date;
 static bit j;
 for(i=8;i>0;i--)
 {
  date=date>>1;
  DQ_HIGH();
  NOP();NOP();
  DQ_LOW();
  NOP();NOP();NOP();NOP();NOP();NOP();
  DQ_HIGH();
  NOP();NOP();NOP();NOP();
  j=DQ;
  if(j==1)
   date=date|0x80;//1000 0000
  delayus(1,1);
 }
 return (date);
}

void get_tem()
{
 uchar tem1,tem2,num;
 float aaa;
 reset(); //Reset
 write_byte(0xCC); //Skip ROM
 write_byte(0x44); //Temperature conversion
 for(num=100;num>0 ;num--)
  disp(a1,a2,a3,a4);
 reset();
 write_byte(0xCC);
 write_byte(0xBE);
 tem1=read_byte();
 tem2=read_byte();
 aaa=(tem2*256+tem1 )*6.25;
 temper=(int)aaa;
 a1=temper/1000;
 a2=temper%1000/100;
 a3=temper%100/10;
 a4=temper%10;
}
void delayus(uint x,uchar y)
{
 uint i;
 uchar j;
 for(i=x;i>0;i--);
 for(j=y;j>0;j--);
}
void delay(uint x)
{
 uint a,b;
 for(a=x;a>0;a--)
  for(b=110;b> 0;b--);
}
void init()
{
 TRISD=0;
 TRISA=0;
 PORTD=0;
 PORTA=0;
}
void disp(uchar num1,uchar num2,uchar num3,uchar num4)
{
 PORTD=table[ num1]; //Display the first digital tube
 PORTA=0x20; //0010 0000
 delay(2);
 PORTD=table1[num2]; //Display the second digital tube
 PORTA=0x10; //0001 0000
 delay(2 );
 PORTD=table[num3]; //Display the third digital tube
 PORTA=0x08; //0000 1000
 delay(2);
 PORTD=table[num4];//Display the fourth digital tube
 PORTA=0x04;//0000 0100
 delay(2);
/* PORTD=table[num5];//Display the fifth digital tube
 PORTA=0x02;/ /0000 0010
 delay(2);
 PORTD=table[num6];//display the sixth digital tube
 PORTA=0x01;//0000 0001
 delay(2);*/
}

Keywords:PIC Reference address:Principle and implementation of PIC microcontroller controlling DS18B20 digital temperature sensor

Previous article:How to use PIC microcontroller timer 1
Next article:Principle and implementation of PIC microcontroller controlling relay

Recommended ReadingLatest update time:2024-11-16 15:37

Some basic knowledge of PIC microcontroller
Oscillator Type LP mode: Low power crystal oscillator mode XT method: Crystal/ceramic oscillator method HS method: High-Speed ​​Crystal/Resonator HS4 mode: High-speed crystal/ceramic oscillator mode with PLL enabled RC mode: resistance-capacitance oscillation mode RCIO mode: external RC oscillation mode wi
[Microcontroller]
Problems in PIC+DS18B20+LCD design and programming
1、mcu:pic18f25k22  2. Since the ds18b20 program has been written in the previous article, it will not be outlined for now;  3. LCD display uses SMS Sun LCD display;  4. The actual picture is as follows: 5. Use xc to compile and debug. When using static bit, you cannot pass parameters to functions. It is a headache.
[Microcontroller]
Problems in PIC+DS18B20+LCD design and programming
PIC16F72 Timer1 Usage
PIC16F72 timer 1 is used, and the program is executed as a result, and the LED flashes. Please refer to the datasheet for details of the registers used.   #include pic.h __CONFIG(11111110111001);//bit13-bit7=1;bit6 undervoltage enable (1 enable);bit5=1;bit4 code protection (0 protect); //bit3 power-on delay
[Microcontroller]
5. PIC32 series timer TMR-16-bit timer usage
1. PIC32 Reference Resources PIC32 Family Reference Manual Chinese version Link address: PIC32 Family Reference Manual Chapter 14 Timer 2. Use Harmony3 to configure the timer to generate code 1. Start the MPLAB Harmony3 Configurator component;  2. Select the Harmony path, don’t worry, just follow the given one, an
[Microcontroller]
5. PIC32 series timer TMR-16-bit timer usage
LCD1602 control essentials (pic microcontroller)
When using LCD on PIC board, J401 needs to be removed. LCD 4 pin (RS data/command selection terminal H/L)-----RB2 LCD 5 pin (R/W read/write selection terminal H/L)---------RB1 LCD 6 pin (E enables reading H, writing L- H)----------RB0 Control essentials: If there is no busy check, an appropriate delay can be made
[Microcontroller]
The following factors affect the power consumption of PIC microcontrollers:
I have been working on the power consumption of PIC microcontrollers. Since the project is powered by batteries, the power consumption issue is very important. According to the data sheet and information on the Internet, the power consumption of microcontrollers is mainly affected by the following factors:   1: All I/
[Microcontroller]
PIC microcontroller CCP pulse (PWM) output
#include pic.h //This program is used to make the CCP1 module generate a PWM waveform with a resolution of 10 bits and a duty cycle of 50%. The frequency is calculated according to PWM_WIDE. Proteus simulation initializes the subroutine through the PWM working mode of //CCP1 (same setting for CCP2) module (why can't
[Microcontroller]
Application of ICL7135 and PIC microcontroller in temperature transmitter
introduction A/D converters are often used in the design of intelligent instruments. Among the commonly used A/D converters, 7135 is the most widely used. It has 41/2-bit A/D conversion accuracy, strong anti-interference ability, and low price. It is mainly used for the measurement and display of detection para
[Microcontroller]
Application of ICL7135 and PIC microcontroller in temperature transmitter
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号