?? 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 + -