[PIC®/AVR®/dsPIC®产品] PIC驱动18B20

[复制链接]
2386|18
 楼主| 玛尼玛尼哄 发表于 2023-4-13 21:05 | 显示全部楼层 |阅读模式
  1. #pragma config FOSC = HS    // 设置时钟源为高速晶振
  2. #pragma config PWRT = ON    // 开启上电复位定时器
  3. #pragma config BOREN = OFF  // 关闭低电压复位
  4. #pragma config WDT = OFF    // 关闭看门狗定时器
  5. #pragma config LVP = OFF    // 禁止低压编程模式

  6. #include <xc.h>
  7. #include <stdio.h>
  8. #include "ds18b20.h"

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

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

  13.     ds18b20_init();        // 初始化DS18B20

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

  20.     while(1)
  21.     {
  22.         float temp = ds18b20_get_temp();  // 读取温度

  23.         // 打印温度
  24.         char buf[32];
  25.         sprintf(buf, "Temp = %.2f C\r\n", temp);
  26.         for(int i = 0; i < strlen(buf); i++)
  27.         {
  28.             while(!TXIF);
  29.             TXREG = buf[i];
  30.         }

  31.         LED = 1;           // 点亮LED
  32.         __delay_ms(500);   // 延时500ms
  33.         LED = 0;           // 熄灭LED
  34.         __delay_ms(500);   // 延时500ms
  35.     }
  36. }

  1. #ifndef DS18B20_H_
  2. #define DS18B20_H_

  3. #include <xc.h>

  4. #define DS18B20_PORT PORTB
  5. #define DS18B20_TRIS TRISB
  6. #define DS18B20_PIN 1

  7. void ds18b20_init();
  8. void ds18b20_write_bit(unsigned char bit);
  9. unsigned char ds18b20_read_bit();
  10. void ds18b20_write_byte(unsigned char byte);
  11. unsigned char ds18b20_read_byte();
  12. float ds18b20_get_temp();

  13. #endif /* DS18B20_H_ */

  1. #include "ds18b20.h"
  2. #include "delay.h"

  3. void ds18b20_init() {
  4.     DS18B20_TRIS &= ~(1 << DS18B20_PIN);
  5.     DS18B20_PORT |= (1 << DS18B20_PIN);
  6. }

  7. void ds18b20_write_bit(unsigned char bit) {
  8.     DS18B20_TRIS &= ~(1 << DS18B20_PIN);
  9.     DS18B20_PORT &= ~(1 << DS18B20_PIN);
  10.     delay_us(2);
  11.     if (bit) {
  12.         DS18B20_PORT |= (1 << DS18B20_PIN);
  13.     }
  14.     delay_us(60);
  15.     DS18B20_PORT |= (1 << DS18B20_PIN);
  16. }

  17. unsigned char ds18b20_read_bit() {
  18.     unsigned char bit = 0;
  19.     DS18B20_TRIS &= ~(1 << DS18B20_PIN);
  20.     DS18B20_PORT &= ~(1 << DS18B20_PIN);
  21.     delay_us(2);
  22.     DS18B20_PORT |= (1 << DS18B20_PIN);
  23.     delay_us(15);
  24.     bit = (DS18B20_PORT & (1 << DS18B20_PIN)) >> DS18B20_PIN;
  25.     delay_us(45);
  26.     return bit;
  27. }

  28. void ds18b20_write_byte(unsigned char byte) {
  29.     unsigned char i = 0;
  30.     for (i = 0; i < 8; i++) {
  31.         ds18b20_write_bit((byte >> i) & 0x01);
  32.     }
  33. }

  34. unsigned char ds18b20_read_byte() {
  35.     unsigned char i = 0;
  36.     unsigned char byte = 0;
  37.     for (i = 0; i < 8; i++) {
  38.         byte |= ds18b20_read_bit() << i;
  39.     }
  40.     return byte;
  41. }

  42. float ds18b20_get_temp() {
  43.     unsigned char temp_l = 0, temp_h = 0;
  44.     int temp = 0;
  45.     ds18b20_init();
  46.     ds18b20_write_byte(0xcc);
  47.     ds18b20_write_byte(0x44);
  48.     while (!ds18b20_read_bit()) {
  49.         delay_ms(10);
  50.     }
  51.     ds18b20_init();
  52.     ds18b20_write_byte(0xcc);
  53.     ds18b20_write_byte(0xbe);
  54.     temp_l = ds18b20_read_byte();
  55.     temp_h = ds18b20_read_byte();
  56.     temp = (temp_h << 8) | temp_l;
  57.     return (float)temp / 16.0;
  58. }








 楼主| 玛尼玛尼哄 发表于 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步伐为何读不到温度
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部