📄 18b20.c
字号:
#include <delay.h>
#include "18B20.h"
#include "app.h"
#define FREQUENCY 7.3728
#define FALSE 0
#define TRUE 1
float g_ftemp_c[1];
int g_itemp_c[1];
unsigned char ts_ROM[8]; // ROM Bit
unsigned char ts_lastDiscrep = 0; // last discrepancy
unsigned char ts_doneFlag = 0; // Done flag
unsigned char ts_FoundROM[1][8]; // table of found ROM codes
unsigned char ts_numROMs;
//unsigned char ts_ErrorCounter;
unsigned char ts_dowcrc;
unsigned char ts_dscrc_table[] = {
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
#define NOP #asm("nop");#asm("nop");#asm("nop");#asm("nop");#asm("nop");#asm("nop");#asm("nop");#asm("nop");
unsigned int ts_times = 0;
#define TS_DOUT PORTE.7
#define TS_DIN PINE.7
void ts_init()
{
TS_DOUT = 0;
DDRE.7 = 1;
ts_times = (unsigned int)(11.3728/FREQUENCY * 10.0);
}
// 10 times = 15us 1.5us/s
void ts_delay(unsigned int counter)
{
int s;
for (s=0; s<counter;s++);
}
void ts_delay_0(unsigned int wcounter, unsigned int incounter )
{
unsigned int s,t;
for (s=0; s<wcounter;s++)
for (t=0; t<incounter; t++);
}
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-10)/1.5 = 313.3 - we use 314. Presence check
// another 70us later, so delay is (70-10)/1.5 = 40 - we use 3.
//
unsigned char ts_ow_reset()
{
unsigned char presence;
TS_DOUT = 0; //pull DQ line low
ts_delay(400); // leave it low for 600us
TS_DOUT = 1; // allow line to return high
ts_delay(40); // leave it low for 60us
presence = TS_DIN; // get presence signal
// ts_delay(276); // wait for end of timeslot
ts_delay(160); // wait for 240us
return(presence); // presence signal returned
}
unsigned char ts_read_bit()
{
unsigned char i;
unsigned times = 0;
NOP;
times = 3 * ts_times;
TS_DOUT = 0; // pull DQ low to start timeslot
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
TS_DOUT = 1; // then return high
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
for (i=0; i<25; i++);
return(TS_DIN); // return value
}
void ts_write_bit(char bitval)
{
TS_DOUT = 0; // pull DQ low to start timeslot
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
if(bitval==1)
TS_DOUT =1; // return DQ high if write 1
ts_delay(63); // hold value for remainder of timeslot
TS_DOUT = 1;
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
#asm("NOP");#asm("NOP");#asm("NOP");#asm("NOP");
}
unsigned char ts_read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(ts_read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
ts_delay(74); // wait for rest of timeslot
}
return(value);
}
void ts_write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
ts_write_bit(temp); // write bit in temp into
}
ts_delay(63);
}
void Read_Temperature(void)
{
char get[10];
char temp_lsb,temp_msb;
// float ftemp_c;
int k;
// char temp_f,temp_c;
ts_ow_reset();
ts_write_byte(0xCC); //Skip ROM
ts_write_byte(0x44); // Start Conversion
ts_delay(63);
ts_ow_reset();
ts_write_byte(0xCC); // Skip ROM
ts_write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++){get[k]=ts_read_byte();}
//printf("\n ScratchPAD DATA = %X%X%X%X%X\n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);
temp_msb = get[1]; // Sign byte + lsbit
temp_lsb = get[0]; // Temp data plus lsb
}
//////////////////////////////////////////////////////////////////////
// FIND DEVICES
void ts_FindDevices(void)
{
unsigned char m;
// unsigned int i,j; //for counter
// declare a pointer to the structure
// struct eeprom_18b20ID eeprom *ep_stru_id;
//same access to structure members using a pointer
// ep_stru_id = &stru_id; // initialize the pointer with the structure address
if(!ts_ow_reset()) //Begins when a presence is detected
{
if(ts_First()) //Begins when at least one part is found
{
ts_numROMs=0;
do
{
for(m=0;m<8;m++)
{
ts_FoundROM[ts_numROMs][m]=ts_ROM[m]; //Identifies ROM
}
ts_numROMs++; //number on found device
}while (ts_Next()&&(ts_numROMs<9)); //Continues until no additional devices are found
}
}
m=m;
/* //add serial numbers to struct in eeprom
for (i=0; i<15; i++)
{
ep_stru_id->ID = i+1;
for(j=0; j<6; j++)
{
ep_stru_id->ts_SerialNum[j] = ts_FoundROM[i][j+1];
}
ep_stru_id++;
}
i=i; ////necessary??
j=j;*/
}
unsigned char ts_First(void)
{
ts_lastDiscrep = 0; // reset the rom search last discrepancy global
ts_doneFlag = FALSE;
return ts_Next(); // call Next and return its return value
}
unsigned char ts_Next(void)
{
unsigned char m = 1; // ROM Bit index
unsigned char n = 0; // ROM Byte index
unsigned char k = 1; // bit mask
unsigned char x = 0;
unsigned char discrepMarker = 0; // discrepancy marker
unsigned char g; // Output bit
unsigned char nxt; // return value
int flag;
nxt = FALSE; // set the next flag to false
ts_dowcrc = 0; // reset the dowcrc
flag = ts_ow_reset(); // reset the 1-wire
if(flag||ts_doneFlag) // no parts -> return false
{
ts_lastDiscrep = 0; // reset the search
return FALSE;
}
ts_write_byte(0xF0); // send SearchROM command
do
// for all eight bytes
{
x = 0;
if(ts_read_bit()==1) x = 2;
ts_delay(73);
if(ts_read_bit()==1) x |= 1; // and its complement
if(x ==3) // there are no devices on the 1-wire
break;
else
{
if(x>0) // all devices coupled have 0 or 1
g = x>>1; // bit write value for search
else
{
// if this discrepancy is before the last
// discrepancy on a previous Next then pick
// the same as last time
if(m<ts_lastDiscrep)
g = ((ts_ROM[n]&k)>0);
else // if equal to last pick 1
g = (m==ts_lastDiscrep); // if not then pick 0
// if 0 was picked then record
// position with mask k
if (g==0) discrepMarker = m;
}
if(g==1) // isolate bit in ROM[n] with mask k
ts_ROM[n] |= k;
else
ts_ROM[n] &= ~k;
ts_write_bit(g); // ROM search write
m++; // increment bit counter m
k = k<<1; // and shift the bit mask k
if(k==0) // if the mask is 0 then go to new ROM
{ // byte n and reset mask
ts_ow_crc(ts_ROM[n]); // accumulate the CRC
n++; k++;
}
}
}while(n<8); //loop until through all ROM bytes 0-7
if(m<65||ts_dowcrc) // if search was unsuccessful then
ts_lastDiscrep=0; // reset the last discrepancy to 0
else
{
// search was successful, so set lastDiscrep,
// lastOne, nxt
ts_lastDiscrep = discrepMarker;
ts_doneFlag = (ts_lastDiscrep==0);
nxt = TRUE; // indicates search is not complete yet, more
// parts remain
}
return nxt;
}
unsigned char ts_ow_crc( unsigned char x)
{
ts_dowcrc = ts_dscrc_table[ts_dowcrc^x];
return ts_dowcrc;
}
void Read_Temp(unsigned int nID)
{
char get[10];
char temp_lsb,temp_msb;
float ftemp_c;
int k;
//char temp_f,temp_c;
int temp_c,itemp_c;
DINT;
ts_ow_reset();
ts_write_byte(0x55); //Skip ROM
ts_write_byte(ts_FoundROM[nID][0]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][1]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][2]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][3]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][4]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][5]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][6]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][7]); // Start Conversion
ts_write_byte(0x44);
EINT;
wdr();
delay_ms(800);
wdr();
DINT;
ts_ow_reset();
ts_write_byte(0x55); //Skip ROM
ts_write_byte(ts_FoundROM[nID][0]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][1]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][2]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][3]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][4]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][5]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][6]); // Start Conversion
ts_write_byte(ts_FoundROM[nID][7]); // Start Conversion
ts_write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++){get[k]=ts_read_byte();} //Read 9 bytes HighSpeed Scratchpad
temp_msb = get[1]; // Sign byte + lsbit
temp_lsb = get[0]; // Temp data plus lsb
temp_c = ((get[1] & 0x0F)<<4)|((get[0]>>4)&(0x0F));
ftemp_c = (float)temp_c;
temp_c = get[0]&0x0F;
ftemp_c = ftemp_c + temp_c*0.0625;
itemp_c = (int)ftemp_c;
if((ftemp_c - itemp_c)>0.5)
itemp_c ++;
if ((ftemp_c<125.0)&&(ftemp_c>-125.0))
g_ftemp_c[nID] = ftemp_c;
if ((itemp_c<125)&&(itemp_c>-125))
g_itemp_c[nID] = itemp_c;
EINT;
}
void temp_deal()
{
// unsigned char nloop = 0;
Read_Temp(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -