玛尼玛尼哄 发表于 2023-4-13 21:05

PIC驱动18B20

#pragma config FOSC = HS    // 设置时钟源为高速晶振
#pragma config PWRT = ON    // 开启上电复位定时器
#pragma config BOREN = OFF// 关闭低电压复位
#pragma config WDT = OFF    // 关闭看门狗定时器
#pragma config LVP = OFF    // 禁止低压编程模式

#include <xc.h>
#include <stdio.h>
#include "ds18b20.h"

#define LED PORTAbits.RA0// 定义LED控制口

void main()
{
    TRISAbits.TRISA0 = 0;// 设置RA0为输出

    ds18b20_init();      // 初始化DS18B20

    // 初始化串口
    TRISCbits.TRISC6 = 1;// 设置TX为输入
    TRISCbits.TRISC7 = 1;// 设置RX为输入
    TXSTAbits.TXEN = 1;    // 打开串口发送
    RCSTAbits.CREN = 1;    // 打开串口接收
    SPBRG = 25;            // 设置波特率为9600

    while(1)
    {
      float temp = ds18b20_get_temp();// 读取温度

      // 打印温度
      char buf;
      sprintf(buf, "Temp = %.2f C\r\n", temp);
      for(int i = 0; i < strlen(buf); i++)
      {
            while(!TXIF);
            TXREG = buf;
      }

      LED = 1;         // 点亮LED
      __delay_ms(500);   // 延时500ms
      LED = 0;         // 熄灭LED
      __delay_ms(500);   // 延时500ms
    }
}


#ifndef DS18B20_H_
#define DS18B20_H_

#include <xc.h>

#define DS18B20_PORT PORTB
#define DS18B20_TRIS TRISB
#define DS18B20_PIN 1

void ds18b20_init();
void ds18b20_write_bit(unsigned char bit);
unsigned char ds18b20_read_bit();
void ds18b20_write_byte(unsigned char byte);
unsigned char ds18b20_read_byte();
float ds18b20_get_temp();

#endif /* DS18B20_H_ */


#include "ds18b20.h"
#include "delay.h"

void ds18b20_init() {
    DS18B20_TRIS &= ~(1 << DS18B20_PIN);
    DS18B20_PORT |= (1 << DS18B20_PIN);
}

void ds18b20_write_bit(unsigned char bit) {
    DS18B20_TRIS &= ~(1 << DS18B20_PIN);
    DS18B20_PORT &= ~(1 << DS18B20_PIN);
    delay_us(2);
    if (bit) {
      DS18B20_PORT |= (1 << DS18B20_PIN);
    }
    delay_us(60);
    DS18B20_PORT |= (1 << DS18B20_PIN);
}

unsigned char ds18b20_read_bit() {
    unsigned char bit = 0;
    DS18B20_TRIS &= ~(1 << DS18B20_PIN);
    DS18B20_PORT &= ~(1 << DS18B20_PIN);
    delay_us(2);
    DS18B20_PORT |= (1 << DS18B20_PIN);
    delay_us(15);
    bit = (DS18B20_PORT & (1 << DS18B20_PIN)) >> DS18B20_PIN;
    delay_us(45);
    return bit;
}

void ds18b20_write_byte(unsigned char byte) {
    unsigned char i = 0;
    for (i = 0; i < 8; i++) {
      ds18b20_write_bit((byte >> i) & 0x01);
    }
}

unsigned char ds18b20_read_byte() {
    unsigned char i = 0;
    unsigned char byte = 0;
    for (i = 0; i < 8; i++) {
      byte |= ds18b20_read_bit() << i;
    }
    return byte;
}

float ds18b20_get_temp() {
    unsigned char temp_l = 0, temp_h = 0;
    int temp = 0;
    ds18b20_init();
    ds18b20_write_byte(0xcc);
    ds18b20_write_byte(0x44);
    while (!ds18b20_read_bit()) {
      delay_ms(10);
    }
    ds18b20_init();
    ds18b20_write_byte(0xcc);
    ds18b20_write_byte(0xbe);
    temp_l = ds18b20_read_byte();
    temp_h = ds18b20_read_byte();
    temp = (temp_h << 8) | temp_l;
    return (float)temp / 16.0;
}








玛尼玛尼哄 发表于 2023-4-13 21:05

该文件中实现了ds18b20.h头文件中声明的各个函数。其中,ds18b20_init函数用于初始化DS18B20,ds18b20_write_bit和ds18b20_read_bit函数用于写入和读取一个位,ds18b20_write_byte和ds18b20_read_byte函数用于写入和读取一个字节,ds18b20_get_temp函数用于读取温度值。需要注意的是,该实现使用了delay.h头文件中的delay_us和delay_ms函数,这些函数需要在delay.c文件中实现。

weifeng90 发表于 2023-4-14 08:12

DS18B20温度传感器现在还有用的吗?

fengm 发表于 2023-5-11 15:41

如何用pic单片机写DS18B20数字温度传感器的程序

pentruman 发表于 2023-5-11 16:09

DS18B20使用一根数据线进行通信,需要正确连接到微控制器的引脚上

everyrobin 发表于 2023-5-11 16:48

从DS18B20读取的温度值是一个原始的数字值,需要进行计算才能得出实际的温度值。DS18B20支持12位、11位、10位或9位分辨率,不同的分辨率会影响计算公式。同时还需要注意精度和符号等问题。

tabmone 发表于 2023-5-11 17:44

我的ds18b20程序为什么读不到温度

macpherson 发表于 2023-5-11 18:29

需要18B20进行外部滤波和校准等处理。

earlmax 发表于 2023-5-11 18:48

DS18B20使用单总线协议进行通信,需要发送一系列特定的命令和数据才能获取温度值。

ulystronglll 发表于 2023-5-11 19:21

DS18B20温度传感器相对来说使用起来较为简单

qiufengsd 发表于 2023-5-11 19:46

DS18B20时序图应该怎么看?

phoenixwhite 发表于 2023-5-11 20:19

需要正确地连接硬件、了解通信协议、进行温度计算,并进行稳定性调试和优化

mickit 发表于 2023-5-11 21:32

在使用DS18B20读取温度时如何与定时中断一起使用

jackcat 发表于 2023-5-11 21:45

需要正确配置总线电阻和引脚映射等设置。

cemaj 发表于 2023-5-13 10:15

PIC16F877单片机可以和多个DS18B20传感器共用吗

biechedan 发表于 2023-5-13 10:31

ds18b20温度传感器读不出数据

guijial511 发表于 2023-5-13 16:07

现在还有应用DS18B20嘛?

nevermore008 发表于 2023-7-19 18:41

阔以.....

理想阳 发表于 2023-8-9 10:49

我的ds18b20步伐为何读不到温度
页: [1]
查看完整版本: PIC驱动18B20