Skip to main content

Temperature Data Logger part 4

C-Program CODE

#include <p18F4550.h>
#include<stdio.h>

#include "delayy.h"
#include "DS1820.h"
#include "serTr.h"
#include "VI.h"
#define TRUE 1;
#define FALSE 0;

typedef unsigned char bool;
typedef unsigned char uint8;

static uint8 ROM_NO[8];
static uint8 LastDiscrepancy;
static uint8 LastDeviceFlag;

void DS1820(void);
void FindDevices(void);
uint8 OWFirst(void);
uint8 OWSearch(void);


void main()
{
int r;

TXSTA = 0x20; // for asynchronous , tx enable settings
SPBRG = 18; // for 12mhz baud rate.
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
for(r=0;r<10;r++)
DS1820_DelayUs(10000); //1sec delay
DS1820(); // this function also contains V and I calling functions
}
/*****************************************/
void DS1820(void)
{
uint8 rslt, i,j;
uint8 All_ROM[5][8];
int r,tem[9];
int cnt;
cnt = 0;
rslt = OWFirst();
if (!DS1820_Reset())
{
while (rslt) // performing search of all the DS connected
{
for(i=0;i<8;i++){
All_ROM[cnt][i]=ROM_NO[i]; // store the current ROM address found in All_Rom[][]
}
cnt++;
rslt = OWSearch(); // search for next ROM address
} // while is closed when all ROMs are found
} // if closed, indicating Search Rom Algorithm is completed

// cnt has the no. of DS IC found
for(j=0;j<6;j++){
for(i=0;i<cnt;i++){ // performing conversion of Temperature for
ConvTemp(&All_ROM[i][0]); // removing error values for all DS ICs
DS1820_DelayUs(10);
ReadTemp(&tem[0],&All_ROM[i][0],i);
DS1820_DelayUs(10);
DS1820_DelayUs(2000);
}
}
while(1){

for(i=0;i<cnt;i++){
// performing Temperature conversion and
// serial send of the Temp readings for all DS ICs
ConvTemp(&All_ROM[i][0]);
DS1820_DelayUs(1);
ReadTemp(&tem[0],&All_ROM[i][0],i);
DS1820_DelayUs(100);
DispTemp(&tem[0],i,&All_ROM[i][0]);
DS1820_DelayUs(20000);
} // FOR DS1820 Temp

} // whil(1) closed

} //ds1820 closed



/*****************************************/
// FIND DEVICES

uint8 OWFirst(void)
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
return OWSearch();
}



/******************************************************************************/
uint8 OWSearch(void)
{
int id_bit_number,x;
unsigned char last_zero, rom_byte_number, search_result;
int id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;

// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
// if the last call was not the last one
if (!LastDeviceFlag)
{
// 1-Wire reset
if (DS1820_Reset())
{
SerTx(' ');
SerTx('R');
SerTx('S');
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
return FALSE;
}

// issue the search command
DS1820_WriteByte(0xF0);

// loop to do the search
do
{
id_bit=0;
cmp_id_bit=0;
if(DS1820_ReadBit()==1) id_bit = 1;
Delay(3);
if(DS1820_ReadBit()==1) cmp_id_bit= 1;

// check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1))
{
break;
}
else {
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit) {
search_direction = id_bit; // bit write value for search
}

else {

// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < LastDiscrepancy)
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
else
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == LastDiscrepancy);

// if 0 was picked then record its position in LastZero
if (search_direction == 0)
last_zero = id_bit_number;

} // 2ND ELSE CLOSED

// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
ROM_NO[rom_byte_number] |= rom_byte_mask;
else
ROM_NO[rom_byte_number] &= ~rom_byte_mask;

// serial number search direction write bit
DS1820_WriteBit(search_direction);

// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;

// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0)
{
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7

// if the search was successful then
if (!(id_bit_number < 65))
{
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy = last_zero;

// check for last device
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
search_result = TRUE;
}
}

// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !ROM_NO[0])
{
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
search_result = FALSE;
}

return search_result;

}

Comments

Popular posts from this blog

CAN INTERVIEW QUESTIONS

Qualifiers and Modifier in C

RTOS Real time operating system interview questions

CAN INTERVIEW QUESTIONS 2

What is UDS protocol

Memory mapping in c

TOP IOT PLATFORMS