📄 my_ds18b20.c
字号:
#include "MY_DS18B20.h"
#include <1wire.h>
#include <ds18b20.h> // DS18b20 Temperature Sensor functions
#include <stdlib.h>
#include <stdio.h>
//============================ DS18B20接PC7脚 ==============================================
// 1 Wire Bus functions
#asm
.equ __w1_port=0x1B ;PORTA
.equ __w1_bit=0
#endasm
//==========================================================================================
unsigned char rom_code[MAX_DEVICES][9]; // DS18B20 devices ROM code storage area
unsigned char devices; //how many DS18B20 devices are connected to the 1 Wire bus
char lcd_buffer[33];
/***********************************************
* DS18B20初始化函数
************************************************/
void DS18B20_Init(void)
{
w1_init(); // 1 Wire Bus initialization
}
/***********************************************
* 获取温度函数
************************************************/
float GetTemperature(void)
{
float temp;
unsigned char temp_char[10];
temp=ds18b20_temperature(0);
ftoa(temp,5,temp_char); //转换浮点数temp为字符串temp_char,其中第二位参数指定四舍五入保留小数位(最多五位)。
sprintf(lcd_buffer,"%s\xdfC",temp_char);
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
return temp;
}
/**************************************************************
* 获取总线上18b20个数的函数,并取得18b20的ID号存入rom_code中
**************************************************************/
unsigned char Get_ALL_DS18B20_Num(void)
{
// detect how many DS18B20 devices are connected to the 1 Wire bus
devices=w1_search(0xf0,rom_code);
sprintf(lcd_buffer,"%u DS18B20 Device",devices);
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
delay_ms(2000);
return devices;
}
/***********************************************
* 显示所有18b20的ID号函数
************************************************/
void Show_ALL_DS18B20_ROM_Codes(void)
{
unsigned char i,j;
// display the ROM codes for each device
if (devices)
{
for(i=0;i<devices;i++)
{
sprintf(lcd_buffer,"#%u Code:",i+1);
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
delay_ms(2000);
for (j=0;j<8;j++)
{
sprintf(lcd_buffer,"%02X ",rom_code[i][j]);
//LCD_write_string(lcd_buffer);
if (j==2);
//LCD_set_xy(0,1);
}
delay_ms(5000);
}
}
else // stop here if no devices were found
{
//while (1);
sprintf(lcd_buffer,"no devices were found");
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
}
}
/***********************************************
* 设置所有18b20的函数
************************************************/
void Set_ALL_DS18B20(void)
{
unsigned char i;
// configure each DS18B20 device for 12 bit temperature measurement resolution
for (i=0;i<devices;)
if (!ds18b20_init(&rom_code[i++][0],20,30,DS18B20_12BIT_RES)) // stop here if init error
{
sprintf(lcd_buffer,"Init error for\ndevice #%u",i);
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
//while (1);
};
}
/***********************************************
* 获得所有18b20温度的函数
************************************************/
void Get_ALL_DS18B20_Temperature(void)
{
unsigned char i;
float temp;
unsigned char temp_char[10];
// measure and display the temperature(s) */
for (i=0;i<devices;i++)
{
temp=ds18b20_temperature(&rom_code[i][0]);
ftoa(temp,5,temp_char);
sprintf(lcd_buffer,"t%u=%s\xdfC",i+1,temp_char);
//LCD_Clear();
//LCD_write_stringXY(0,0,lcd_buffer);
delay_ms(500);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -