📄 ds18b20.c
字号:
/**********************DS18B20 基本控制函数******************************
[文件名] DS18B20.c+DS18B20.H
[版本] TV0.1
[修正] hxc
hxc last change time 2006.03.03 x:x
hxc last check time 2006.4.14 16:46
[功能说明]:
***************************************************************************/
#include "ASMselect.h"
#if ASMds18b20
#include <reg51x.h>
#include <tab.h>
#include <1-wire.h>
#include <stdio.h>
#include "ds18b20.h"
extern uchar idata digital_LED[6];
unsigned char code displaydata_table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff, //9.=9+10=19(0x10)
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//0 1 2 3---9 ,'-' "off", 0. 1. 2. 3. ---9. ,have radix point +12
uchar idata ds18b20id[8];//save ds18b20 rom_id
uchar idata ds2415id[8];//save ds2415 ID timer chip
//==================================ID_read_store=======================================
//=read on wire all device rom_id, have ds18b20 save to ds18b20id[8],not is save 1320id=
ID_read_store(){
uchar ip;
uchar jp;
uchar *idflag;
extern unsigned char idata ROM_ID[8];//rom_id[0] is 8bit family,rom_id[1~6]48bit ID,rom_id[8] 8 bit CRC
for(ip=0;ip<2;ip++){//read rom_id ->
if(WIRE_id_read(searchid_command)){
if(ROM_ID[0]==0X28){
idflag=ds18b20id;
}
else {
idflag=ds2415id;
}
for (jp=0;jp<8;jp++){
*idflag=ROM_ID[jp];
idflag++;
}
}
else {break;}
}
}
//==================================Id_matching======================================
//======-matching rom id(tx matching comand +*device_id)=============================
//--------------------------------
Id_matching(uchar *device_id){
uchar im;
WIRE_reset();
WIRE_tx_8(matchingid_command);
for (im=0;im<8;im++){
// printf("%bx,",*device_id);
WIRE_tx_8(*device_id);
device_id++;
}
//printf("\n");
}
//===========================DS18B20_config_store_eeporm=============================
//==config ds18b20(parameterTH,TL,Contgreg), store to DS18B20 EEPROM=================
//---------------------------------
#if ASMDS18B20_config_store
DS18B20_config_store_eeporm(uchar TH,char TL,uchar ContgReg){
//char TH; //temperature > TH warn
//char TL; //temperature< TL warn
//uchar ContgReg; //temperature precision ( 0 R1 R0 1 1111); R1 R0=(00) 9 bit time 93.75ms 0.5'C
//=(0 1) 10bit 0.25'C 187.5ms =(1 0) 375ms 0.125'C =(1 1) 12bit 750ms 0.0625'C
uchar ics;
uchar idata configure[3];
//---------------config write ds18b20
Id_matching(ds18b20id);
//TH=27; //high 'C
//TL=-1; //low 'C
//ContgReg=0X7F; //12bit precision
configure[0]=TH;
configure[1]=TL;
configure[2]=ContgReg;
WIRE_tx_8(write_ds18b20);
for(ics=0;ics<3;ics++){
WIRE_tx_8(configure[ics]);
}
//--------------storage config data to ds18b20 eeprom
Id_matching(ds18b20id);
WIRE_tx_8(copy_to_EEPROM); //data copy to eeprom
wire_power_cmos=1; //pu high ,gave wire device current
DELAY_X(750); //delay 10ms+2ms
wire_power_cmos=0;
WIRE_rx_1(); //COPY OK
}
//------------------------------look ds18b20 power mode
/* bit vcc_power_mode;
WIRE_tx_8(read_power_mode);
vcc_power_mode=WIRE_rx_1();
printf("ds18b20powermode:%d",(int)(vcc_power_mode));*/
#endif
//==================================tem_switch====================================
//================================temperature switch ================================
tem_switch(){
Id_matching(ds18b20id);
//------------------------------------temperature switch
WIRE_tx_8(temperature_switch);
wire_power_cmos=1;
//DELAY_X(32766); //delay 500ms +24ms
//DELAY_X(8000);
}
//==================================tem_red_ouput=================================
//========================== read temperature ,process and print=========================
tem_read_ouput(){
//------------------------------------read temperature
uchar it;
bit temperature_negative; //temperature negative =1 is' -',=0 si ' +'
uchar idata contgarray[9]; //read temperature value
uchar temperature_integer; //temperature integer
uint temperature_decimal; //temperature decimal
float temperature_f; //temperature float
uchar idata disp_tem[6]; //temperature digital_LED data
uchar hundred_uc,denary_uc;
uchar decimal_uc1000;
wire_power_cmos=0;
Id_matching(ds18b20id);
WIRE_tx_8(read_ds18b20);
for (it=0;it<9;it++){
contgarray[it]=WIRE_rx_8();
// printf("\ncontgarray=%bx",contgarray[it]);//OUPUT READ DS18B20 DATA
}
if (WIER_crc(contgarray,9)==0){//if temperature crc check error not process display update
//--------------- temperature process and output------------------------
temperature_negative=0;
if (contgarray[1]>0x80){ //if negative ,~x+1 process
contgarray[1]=~contgarray[1];
contgarray[0]=~contgarray[0];
contgarray[0]++;
if (contgarray[0]==0){contgarray[1]++;}
temperature_negative=0;
}
contgarray[1]=(contgarray[1]<<4)&0xf0; //process integer
temperature_integer=(contgarray[0]>>4)&0x0f;
temperature_integer|=contgarray[1];
temperature_f=0;
temperature_decimal=0;
if((bit)(contgarray[0]&0x01)){temperature_decimal=625;temperature_f+=0.0625;} //process decimal bit
if((bit)(contgarray[0]&0x02)){temperature_decimal+=1250;temperature_f+=0.125;}
if((bit)(contgarray[0]&0x04)){temperature_decimal+=2500;temperature_f+=0.25;}
if((bit)(contgarray[0]&0x08)){temperature_decimal+=5000;temperature_f+=0.5;}
printf("\nt="); //output temperature
if (temperature_negative)
{printf("-"); //+-negative out
disp_tem[0]=10;} //have '-' dispaly '-'~~~7~~~
else{disp_tem[0]=11;} //not '-' dispaly 'off'~~~7~~~~
printf("%d.",(int)(temperature_integer));
//~~~~~7~~~~~~~<
denary_uc=temperature_integer; //SEVEN display process
hundred_uc=temperature_integer/100;
if (hundred_uc){
disp_tem[0]=1;
denary_uc=temperature_integer-100; //if on hundred bit have amout '1' ,not display "-"or 'off',display "1"
disp_tem[1]=denary_uc/10; //denary display
}
else{
disp_tem[1]=denary_uc/10;
if(denary_uc/10==0)disp_tem[1]=11 ; //not hundred bit, on denary bit is '0' ,dispaly off
}
disp_tem[2]=temperature_integer%10+12;//unit bit display+radix point
decimal_uc1000=temperature_decimal/1000;
disp_tem[3]=decimal_uc1000;
if (disp_tem[3]==0){disp_tem[3]=0;}
disp_tem[4]=(temperature_decimal/100)%10;
disp_tem[5]=(temperature_decimal/10)%10;
//~~~~~~7~~~~~>
if (temperature_decimal==625)
{printf("0"); //decimal + 0
}
printf("%d\'c\n",temperature_decimal);
temperature_f+=temperature_integer; //conversion float count
if (temperature_negative)temperature_f=(-1)*temperature_f;
if (temperature_f!=85){
printf("t=%f\'C\n",temperature_f); //float output
for (it=0;it<6;it++){
digital_LED[it]=displaydata_table[ disp_tem[it]];
}
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -