#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[32]; sprintf(buf, "Temp = %.2f C\r\n", temp); for(int i = 0; i < strlen(buf); i++) { while(!TXIF); TXREG = buf[i]; } 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; }
#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; }
#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; }
收藏0 举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
发帖类勋章
人才类勋章
等级类勋章
时间类勋章
196
3261
2
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号