main.c
#include
#include
#include "def.h"
#include "ds18b20.h"
__CONFIG(0xFF32);
void Delay_ms(u16 xms)
{
int i,j;
for(i=0;i { for(j=0;j<71;j++) ; } } void uart_init(void) { TXSTA=0x24; //Turn on the transmit enable bit and high baud rate. The initial value of TRMT can be 0 or 1. RCSTA=0x80; SPBRG=0x19; //4M crystal oscillator, baud rate 9600, then the initial value of SPBRG is 25 } //printf function redirection void putch(u8 c) { TXREG = c; while(TRMT==0); } short temperature; void main() { uart_init(); Delay_ms(5); printf("ds18b20 demo\n"); while(1) { if(Init_18B20()==0) { printf("init ok1!\n"); Skip(); Convert(); Delay_ms(750); if(Init_18B20()==0) { printf("init ok2!\n"); Skip(); Read_SP(); RomCode[0]=Read_18B20(); RomCode[1]=Read_18B20(); temperature=ReadTemp(RomCode); temperature=CalcTemp(temperature); printf("code=%02x%02x\t->\t%d.%d℃\n",RomCode[0],RomCode[1],temperature/10,temperature%10); } } Delay_ms(3000); } } ds18b20.h #include #include "def.h" #ifndef __ds1820_h #define __ds1820_h external u8 RomCode[2]; #define DQ1 RC1=1 #define DQ0 RC1=0 #define DQ_in TRISC1=1 #define DQ_out TRISC1=0 #define DQ_val RC1 #define _DINT() of() #define _EINT() ei() #define _NOP() asm("nop") u8 Init_18B20(void); short CalcTemp(short a); void Skip(void); void Convert(void); void Read_SP(void); u8 Read_18B20(void); short ReadTemp(u8 *r); #endif ds18b20.c /********************************* PIC16F877A@4MHz *********************************/ #include "ds18b20.h" #include "def.h" u8 RomCode[2]; #define DelayNus _delay u8 Init_18B20(void) { u8 Error; DQ_out; _DINT(); DQ0; DelayNus(500); //Delay at least 480us DQ1; DelayNus(55); //Release DQ and then read after 120us (60~240) DQ_in; _NOP(); if(DQ_val) Error = 1; // Initialization failed else Error = 0; // Initialization successful DQ_out; DQ1; _UNITE(); DelayNus(400); return Error; } void Write_18B20(u8 wdata) { u8 i; for(i = 0; i < 8;i++) { _DINT(); DQ0; //DelayNus(6); //Delay 2us _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); if(wdata & 0X01) DQ1; else DQ0; wdata >>= 1; DelayNus(70); //Delay 91us DQ1; _UNITE(); _NOP(); } } u8 Read_18B20(void) { u8 i; u8 temp = 0; for(i = 0;i < 8;i++) { temp >>= 1; _DINT(); DQ0; _NOP(); _NOP(); _NOP(); _NOP(); DQ1; //DelayNus(8); //Delay 9us DQ_in; //DelayNus(25); //Delay 45us if(DQ_val) temp |= 0x80; DQ_out; DQ1; DelayNus(70); //Delay 10us _UNITE(); _NOP(); } return temp; } void Skip(void) { Write_18B20(0xcc); } void Convert(void) { Write_18B20(0x44); } void Read_SP(void) { Write_18B20(0xbe); } short ReadTemp(u8 *r) { u8 temp_low; short temp; temp_low = r[0]; //read low bit temp = r[1]; //read high bit temp = (temp<<8) | temp_low; return temp; } const u8 dottab[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9}; //Optimize reading algorithm short CalcTemp(short a) { u8 b; if(a<0) { a=~a; a++; b=a; a>>=4; a*=-10; a-=dottab[b&0x0f]; } else { b=a; a>>=4; a*=10; a+=dottab[b&0x0f]; } return a; } def.h #ifndef __DEF__H_ #define __DEF__H_ #define u8 unsigned char #define u16 unsigned int #endif
Previous article:PIC microcontroller - PWM wave
Next article:PIC16F877A internal EEPROM read and write demonstration Demo
Recommended ReadingLatest update time:2024-11-16 11:51
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- After deleting a wire that was previously laid, AD09 got stuck.
- Saiyuan MCU SC93F8332 lights up the RGB light of WS2812B
- Implementation of a Programmable Fully Digital Phase-Locked Loop
- Canaan wants to record a video about K510. What kind of content do you hope to see? (Attached is the official information about K510)
- Share some difficult problems with the LM25116EN chip
- [RVB2601 Creative Application Development] Dynamically loading MBRE Postscript
- Distance of ESD tip discharge needle
- Bone Vibration Sensor LIS25BA-Anti-Wind Noise Example
- SensorTile.box Trial (3) Expert Mode Trial
- Introduction to CEDV Electricity Calculation Method