soulraven
Joined: 08 Feb 2009 Posts: 72 Location: campulung muscel
|
help me with this code |
Posted: Mon Feb 16, 2009 4:27 pm |
|
|
Can you help me with this code. Now I show the temperature in one part and I may want to associate each sensor with a certain variable separately. You have no idea how to do that?
Code: | // Returns 0 for one wire device presence, 1 for none
int8 ow_reset(void)
{
int8 presence;
output_low(DQ);
delay_us(488); // Min. 480uS
output_float(DQ);
delay_us(72); // Takes 15 to 60uS for devices to respond
presence = input(DQ);
delay_us(424); // Wait for end of timeslot
//printf(lcd_putc, "\fPresence= %i", presence);
//delay_ms(500);
return(presence);
}
/******************************************************************************/
// Read bit on one wire bus
int8 read_bit(void)
{
output_low(DQ);
delay_us(1); // 1uS min. Original code relied on 8051 being slow
output_float(DQ);
delay_us(20); // Wait at least 15mS from start of time slot
return(input(DQ)); // Delay to finish time slot (total 60 to 120uS)
} // must be done next.
/******************************************************************************/
void write_bit(int8 bitval)
{
output_low(DQ);
if(bitval == 1) {
delay_us(1); // 1uS min. Original code relied on 8051 being slow
output_float(DQ);
}
delay_us(105); // Wait for end of timeslot
output_float(DQ);
}
/******************************************************************************/
int8 read_byte(void)
{
int8 i;
int8 val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
delay_us(120); // To finish time slot
}
return val;
}
/******************************************************************************/
void write_byte(int8 val)
{
int8 i;
int8 temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
delay_us(105);
}
/******************************************************************************/
// One wire crc
int8 ow_crc(int8 x)
{
dowcrc = dscrc_table[dowcrc^x];
return dowcrc;
}
/******************************************************************************/
// Searches for the next device on the one wire bus. If there are no more
// devices on the bus then false is returned.
int8 Next(void)
{
int8 m = 1; // ROM Bit index
int8 n = 0; // ROM Byte index
int8 k = 1; // Bit mask
int8 x = 0;
int8 discrepMarker = 0;
int8 g; // Output bit
int8 nxt; // Return value
short flag;
nxt = FALSE; // Reset next flag to false
dowcrc = 0; // Reset the dowcrc
flag = ow_reset();
if (flag||doneFlag) // If no parts return false
{
lastDiscrep = 0; // Reset the search
return FALSE;
}
write_byte(0xF0); // Send SearchROM command
do
{
x = 0;
if (read_bit() == 1)
x = 2;
delay_us(120);
if (read_bit() == 1)
x |= 1; // And it's complement
if (x == 3) // There are no devices on the one wire bus
break;
else
{
if (x > 0) // All devices coupled have 0 or 1
g = x >> 1; // Bit write value for search
// If this discrepancy is before the last discrepancy on a previous
// Next then pick the same as last time.
else
{
if (m < lastDiscrep)
g = ((Rom_Bit[n] & k) > 0);
// If equal to last pick 1
else
g = (m == lastDiscrep); // If not then pick 0
// If 0 was picked then record position with mask k
if (g == 0) discrepMarker = m;
}
// Isolate bit in Rom_Bit[n] with mask k
if (g == 1)
Rom_Bit[n] |= k;
else
Rom_Bit[n] &= ~k;
write_bit(g); // ROM search write
m++; // Increment bit counter m
k = k << 1; // and shift the bit mask k
// If the mask is 0 then go to new ROM
if (k == 0)
{ // Byte n and reset mask
ow_crc(Rom_Bit[n]); // Accumulate the crc
n++;
k++;
}
}
} while (n < 8); // Loop through until through all ROM bytes 0-7
if (m < (65||dowcrc)) // If search was unsuccessful then
lastDiscrep = 0; // reset the last Discrepancy to zero
else // Search was successful, so set lastDiscrep, lastOne, nxt
{
lastDiscrep = discrepMarker;
doneFlag = (lastDiscrep == 0);
nxt = TRUE; // Indicates search not yet complete, more parts remain
}
return nxt;
}
/******************************************************************************/
// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void)
{
lastDiscrep = 0;
doneFlag = FALSE;
return Next(); // Call Next and return it's return value;
}
/******************************************************************************/
void FindDevices(void)
{
int8 m;
if(!ow_reset())
{
if(First()) // Begins when at least one part found
{
numROMs = 0;
do
{
numROMs++;
for (m=0;m<8;m++)
{
FoundROM[numROMs][m] = Rom_Bit[m]; // Identifies ROM no. on device
}
//printf("Device No.%u address ",numROMs);
printf(lcd_putc,"%d ",Rom_Bit[m]);
// printf(lcd_putc, "\f%X%X%X%X%X%X%X%X",
// FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5], \
// FoundROM[numROMs][4],FoundROM[numROMs][3],FoundROM[numROMs][2], \
// FoundROM[numROMs][1],FoundROM[numROMs][0]);
// delay_ms(100);
printf("%X%X%X%X%X%X%X%X\n\r",
FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5],
FoundROM[numROMs][4],FoundROM[numROMs][3],FoundROM[numROMs][2],
FoundROM[numROMs][1],FoundROM[numROMs][0]);
} while (Next() && (numROMs<10)); // Continues until no additional
// devices found.
}
else
{
printf(lcd_putc, "\fno ROM found");
//delay_ms(500);
}
}
else
{
printf(lcd_putc, "\fno ROM found2");
//delay_ms(500);
}
//putc('\n'); putc('\r');
}
/******************************************************************************/
// Sends Match ROM command to bus then device address
int8 Send_MatchRom(void)
{
int8 i;
if (ow_reset()) return FALSE; // 0 if device present
write_byte(0x55); // Match ROM
for (i=0;i<8;i++)
{
write_byte(FoundRom[numROMs][i]); // Send ROM code
}
return TRUE;
}
/******************************************************************************/
//read temperature from one wire DS18B20 sensors
//// de aici incepe citirea senzorilor si afisarea
//////////////////////////////////////////////////
void ReadTemp(void)
{
int8 i;
signed int16 stemp16; // Raw temperature data in 2's complement
int8 scratch[10]; // Lowest two bytes are for temperature
float result=0;
signed int16 temp;
// If not using parasitic power can send the temperature convert
// command to all DS18B20 and/or DS1822 devices on the bus.
if (!ow_reset()) // If a device is present
{
write_byte(0xCC); // Skip Rom command
write_byte(0x44); // Temperature convert command
// output_float(DQ);
delay_ms(750); // Max. conv. time is 750mS for 12 bit
ow_reset();
// Now get the device raw temperature data using Match ROM with the
// addresses obtained with FindDevices(). Scale to deg. C, scaling is
// 0.0625 (1/16) deg. C/bit with default 12 bit res. Could reduce res.
// and therefore shorten conversion time if necessary.
// Output the temperatures in whole degrees, apply rounding by adding
// half denom.
for (numRoms=1;numRoms<=4;numRoms++)
{
if (Send_MatchRom())
{
write_byte(0xBE); // Read scratch pad command
dowcrc = 0; // Reset the crc to start a new calculation
for (i=0;i<=7;i++)
{
scratch[i] = read_byte();
ow_crc(scratch[i]); // Accumulate the crc
}
scratch[8] = read_byte(); // Received crc byte
ow_reset();
// If calculated crc from incoming bytes equal to crc byte
// then data is valid. Calculate and output temperature.
if (scratch[8] == dowcrc)
{
//stemp16 = (signed int16) make16(scratch[1],scratch[0]);
// stemp16 = (stemp16 + 8)/16;
// printf(lcd_putc, "\fTemp%i=%4ld ",numRoms, stemp16);
//0.1 precision
temp = make16(scratch[1],scratch[0]);
result = (float) temp / 16.0; //0.1 deg C resolution
lcd_gotoxy(1,1);
printf(lcd_putc,"\nT%i= %3.1f \uC", numRoms, result);
//lcd_gotoxy(1,2);
//printf(lcd_putc,"\nT%i= %3.1f \uC", numRoms, result);
}
else printf(lcd_putc,"\f Data error."); // There was an error in the data
}
}
}
else
{
printf(lcd_putc,"\f Error ");
printf(lcd_putc,"\nSensors not found!");
delay_ms(200);
}
}
|
|
|