CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

1-wire implementation.. where to begin

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dwstudd



Joined: 22 Jan 2004
Posts: 3

View user's profile Send private message MSN Messenger

1-wire implementation.. where to begin
PostPosted: Thu Jan 22, 2004 11:38 pm     Reply with quote

Hello all,

I'm a student (computer engineering major) working on a research project where I need to implement a 1-wire bus. I am busy with also developing a power subsystem and have little time to work on the code. I have glanced through the pdfs at Dallas and also the SDK at ibutton's site, so I am somewhat familiar with the protocol and routines needed. The code needs to support multiple quad A/D converters, temp sensors, current sensors, and digital pots. The PIC (18 family, unsure at the moment of the exact model, an 80 pinner) has both USARTS in use, so the code needs to be software driven through an I/O pin, which I don't think the SDK from ibutton's site is designed for. Also, I have a huge deadline to have it working by approximately the end of April. What is the best course of action?

1) write the routines myself
2) somehow use the sdk software
3) get code from someone here

Please tell me which you suggest and how to do it, whatever is the fastest and reliable way to get there.

Thanks,
D-wayne

BTW, this is my first post!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 23, 2004 12:08 am     Reply with quote

Quote:
I'm a student (computer engineering major) working on a research project where I need to implement a 1-wire bus.
The code needs to support multiple quad A/D converters, temp sensors, current sensors, and digital pots.


Do you have to use 1-wire ? Those devices are probably all
available with an i2c bus. There are a lot more i2c drivers available,
than 1-wire drivers. Just look in the CCS drivers folder, to see this.

CCS has an example file, TOUCH.C, for a 1-wire iButton device.
This file is in your c:\Program Files\Picc\Drivers folder.
Also, you can search this forum for the word "touch" and
read a few comments on that driver.
Freddie



Joined: 06 Sep 2003
Posts: 49

View user's profile Send private message

Re: 1-wire implementation.. where to begin
PostPosted: Fri Jan 23, 2004 6:43 pm     Reply with quote

Below is some code for the DS18B20 1-wire temperature sensor. This code calls the CCS touch.c library. This code also uses the DS1302 Real Time Clock, which is not a 1-wire device.

Included int this code are the following 1-wire routines:
1) int8 calc_crc(byte *buff, byte num_vals)
2) void DS18B20GetSerialNumber(byte *dsROM)
3) float DS18B20MatchROM_TempF(byte *dsROM)
4) float DS18B20SkipROM_TempF(void)

This code used the Match ROM command to read the temperature of a specific DS18B20 that is on the bus. Using the above functions you can modify the code to read serial numbers and peform Skip ROM temperature reads.

Hope this helps.

// DS18B20 1-WireTemperature.
// DS1302 Time (not 1-wire)
//
// Reads DS1302 Time.
// Reads DS18B20 Serial number, performs a skip ROM temperature conversion,
// sends serial number, digital, C and F temperature data to PC serial port.
//

#include <16F876a.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <touch.c>
#include <ds1302.c>
#include <string.h>
#include <stdlib.h>

//// Function Declarations
int8 calc_crc(byte *buff, byte num_vals);
int8 day,mth,year,dow,hour,min,sec;
void DS18B20GetSerialNumber(byte *dsROM);
float DS18B20MatchROM_TempF(byte *dsROM);
float DS18B20SkipROM_TempF(void);


void main() {

int8 dsROM[10];
float temp_f;

//***** for programming DS1302 only
//day = 0x19;
//mth = 0x01;
//year = 0x04;
//dow = 0x02;
//hour = 0x18;
//min = 0x55;
//sec = 0x00;
//rtc_init();
//rtc_set_datetime(day,mth,year,dow,hour,min);
//***** for programming DS1302 only

printf("\r\nRunning...\r\n");

while (1)
{


//get the time
rtc_init();
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
printf("%2X%2X%2X, ",mth,day,year);
printf("%2X:%2X:%2X, ",hour,min,sec);



//DS18B20GetSerialNumber(dsROM);
//DS18B20SkipROM_TempF();
//printf( "SkipROM TempF= %7.3f degrees F \r\n\r\n", temp_f ); // print temp F

dsROM[0] = 0x28;
dsROM[1] = 0xAB;
dsROM[2] = 0x1A;
dsROM[3] = 0x3E;
dsROM[4] = 0x00;
dsROM[5] = 0x00;
dsROM[6] = 0x00;
dsROM[7] = 0x08;

temp_f = DS18B20MatchROM_TempF(dsROM);
printf( "%7.3f, ", temp_f ); // print temp F

dsROM[0] = 0x28;
dsROM[1] = 0x01;
dsROM[2] = 0xDD;
dsROM[3] = 0x3D;
dsROM[4] = 0x00;
dsROM[5] = 0x00;
dsROM[6] = 0x00;
dsROM[7] = 0x67;

temp_f = DS18B20MatchROM_TempF(dsROM);
printf( "%7.3f \r\n", temp_f ); // print temp F

delay_ms(20000);


} //end while(1)

} //end main


////////////////////////////////////////////////////////////////////////////////////////////////
////// Routines //////
////////////////////////////////////////////////////////////////////////////////////////////////

int8 calc_crc(byte *buff, byte num_vals)
{
int8 shift_reg=0, data_bit, sr_lsb, fb_bit, i, j;

for (i=0; i<num_vals; i++) // for each byte
{
for(j=0; j<8; j++) // for each bit
{
data_bit = (buff[i]>>j)&0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
{
shift_reg = shift_reg ^ 0x8c;
}
}
}
return(shift_reg);
} //end calc_crc


void DS18B20GetSerialNumber(byte *dsROM)
{
// requires the the calling funtion to have <touch.c> #included.

int8 crc;

//read SN command.
if(touch_present())
{
touch_write_byte(0x33); //read ROM command
dsROM[0] = touch_read_byte(); //family code
dsROM[1] = touch_read_byte(); //SN1 LSByte
dsROM[2] = touch_read_byte(); //SN2
dsROM[3] = touch_read_byte(); //SN3
dsROM[4] = touch_read_byte(); //SN4
dsROM[5] = touch_read_byte(); //SN5
dsROM[6] = touch_read_byte(); //SN6 MSByte
dsROM[7] = touch_read_byte(); //CRC

crc = calc_crc(dsROM, 7);

//printf ("S/N READ: %X %X %X %X %X %X %X %X %X\r\n",dsROM[0],dsROM[1],dsROM[2],dsROM[3],dsROM[4],dsROM[5],dsROM[6],dsROM[7], crc);

delay_ms (1500);

} //end if

}


float DS18B20MatchROM_TempF(byte *dsROM)
{
// returns a temperature in degrees F as a float.
// returns -FFFFFF if there was an error
// requires the the calling funtion to have <touch.c> #included.

int8 crc;
int16 temp_digital;
float temp_c, temp_f;


//read Temperature MATCH ROM
if(touch_present())
{
touch_write_byte(0x55); //match ROM
touch_write_byte(dsROM[0]); //send ROM data
touch_write_byte(dsROM[1]); //send ROM data
touch_write_byte(dsROM[2]); //send ROM data
touch_write_byte(dsROM[3]); //send ROM data
touch_write_byte(dsROM[4]); //send ROM data
touch_write_byte(dsROM[5]); //send ROM data
touch_write_byte(dsROM[6]); //send ROM data
touch_write_byte(dsROM[7]); //send ROM data
touch_write_byte(0x44); //temperature convert command
output_float(TOUCH_PIN); //strong pullup
delay_ms(1000);

touch_present();

touch_write_byte(0x55); //match ROM
touch_write_byte(dsROM[0]); //send ROM data
touch_write_byte(dsROM[1]); //send ROM data
touch_write_byte(dsROM[2]); //send ROM data
touch_write_byte(dsROM[3]); //send ROM data
touch_write_byte(dsROM[4]); //send ROM data
touch_write_byte(dsROM[5]); //send ROM data
touch_write_byte(dsROM[6]); //send ROM data
touch_write_byte(dsROM[7]); //send ROM data
touch_write_byte(0xBE); //read scratch pad command

//get scratchpad data
dsROM[0] = touch_read_byte();
dsROM[1] = touch_read_byte();
dsROM[2] = touch_read_byte();
dsROM[3] = touch_read_byte();
dsROM[4] = touch_read_byte();
dsROM[5] = touch_read_byte();
dsROM[6] = touch_read_byte();
dsROM[7] = touch_read_byte();
dsROM[8] = touch_read_byte();

crc = calc_crc(dsROM, 8);

//printf ("Temp read MATCH ROM: %X %X %X %X %X %X %X %X %X %X\r\n",dsROM[0],dsROM[1],dsROM[2],dsROM[3],dsROM[4],dsROM[5],dsROM[6],dsROM[7],dsROM[8],crc);
temp_digital = make16(dsROM[1],dsROM[0]);
//printf( "temp_digital= %LX \r\n", temp_digital);

if (temp_digital >= 0x800) //temperture is negative
{
temp_c = 0;
//calculate the fractional part
if(temp_digital & 0x0001) temp_c += 0.06250;
if(temp_digital & 0x0002) temp_c += 0.12500;
if(temp_digital & 0x0004) temp_c += 0.25000;
if(temp_digital & 0x0008) temp_c += 0.50000;

//calculate the whole number part
temp_digital = (temp_digital >> 4) & 0x00FF;
temp_digital = temp_digital - 0x0001; //subtract 1
temp_digital = ~temp_digital; //ones compliment
temp_c = temp_c - (float)(temp_digital & 0xFF);
}
else //temperture is positive
{
temp_c = 0;
//calculate the whole number part
temp_c = (temp_digital >> 4) & 0x00FF;
//calculate the fractional part
if(temp_digital & 0x0001) temp_c = temp_c + 0.06250;
if(temp_digital & 0x0002) temp_c = temp_c + 0.12500;
if(temp_digital & 0x0004) temp_c = temp_c + 0.25000;
if(temp_digital & 0x0008) temp_c = temp_c + 0.50000;
} //end if else

//printf( "TempC= %7.3f degrees C \r\n", temp_c ); // print temp C
temp_f = (temp_c* 9.00000)/5.00000 + 32.00000;
//printf( "TempF= %7.3f degrees F \r\n\r\n", temp_f ); // print temp F

return(temp_f);

} //end if touch present


} //end DS18B20MatchROM_TempF


float DS18B20SkipROM_TempF(void)
{
// returns a temperature in degrees F as a float.
// returns -FFFFFF if there was an error
// requires the the calling funtion to have <touch.c> #included.

int8 dsROM[10], crc;
int16 temp_digital;
float temp_c, temp_f;

//read Temperature SKIP ROM
if(touch_present())
{
touch_write_byte(0xCC); //skip ROM
touch_write_byte(0x44); //temperature convert command
output_float(TOUCH_PIN); //strong pullup
delay_ms(1000);

touch_present();
touch_write_byte(0xCC); //skip ROM
touch_write_byte(0xBE); //copy scratchpad command

//get scratchpad data
dsROM[0] = touch_read_byte();
dsROM[1] = touch_read_byte();
dsROM[2] = touch_read_byte();
dsROM[3] = touch_read_byte();
dsROM[4] = touch_read_byte();
dsROM[5] = touch_read_byte();
dsROM[6] = touch_read_byte();
dsROM[7] = touch_read_byte();
dsROM[8] = touch_read_byte();

crc = calc_crc(dsROM, 8);

//printf ("Temp read SKIP ROM: %X %X %X %X %X %X %X %X %X %X\r\n",dsROM[0],dsROM[1],dsROM[2],dsROM[3],dsROM[4],dsROM[5],dsROM[6],dsROM[7],dsROM[8],crc);
temp_digital = make16(dsROM[1],dsROM[0]);
//printf( "temp_digital= %LX \r\n", temp_digital);

if (temp_digital >= 0x800) //temperture is negative
{
temp_c = 0;
//calculate the fractional part
if(temp_digital & 0x0001) temp_c += 0.06250;
if(temp_digital & 0x0002) temp_c += 0.12500;
if(temp_digital & 0x0004) temp_c += 0.25000;
if(temp_digital & 0x0008) temp_c += 0.50000;

//calculate the whole number part
temp_digital = (temp_digital >> 4) & 0x00FF;
temp_digital = temp_digital - 0x0001; //subtract 1
temp_digital = ~temp_digital; //ones compliment
temp_c = temp_c - (float)(temp_digital & 0xFF);
}
else //temperture is positive
{
temp_c = 0;
//calculate the whole number part
temp_c = (temp_digital >> 4) & 0x00FF;
//calculate the fractional part
if(temp_digital & 0x0001) temp_c = temp_c + 0.06250;
if(temp_digital & 0x0002) temp_c = temp_c + 0.12500;
if(temp_digital & 0x0004) temp_c = temp_c + 0.25000;
if(temp_digital & 0x0008) temp_c = temp_c + 0.50000;
} //end if else

//printf( "TempC= %7.3f degrees C \r\n", temp_c ); // print temp C
temp_f = (temp_c* 9.00000)/5.00000 + 32.00000;
//printf( "TempF= %7.3f degrees F \r\n\r\n", temp_f ); // print temp F

return(temp_f);

} //end if(touch_present())


} //end DDS18B20SkipROM_TempF
Bad_Mujo
Guest







PostPosted: Mon Jan 26, 2004 9:06 am     Reply with quote

Here are some functions for a DS2450 Quad A/D.
It only uses channels C and D but its easy to change
The driver have I done but the Onewire routines are copied/borrowed from one at this forum (Sorry cant remember Your name)

//
// Onewire routines
//
// onewire rom registers
#define ROM_DEVTYPE 0
#define ROM_SERIAL1 1
#define ROM_SERIAL2 2
#define ROM_SERIAL3 3
#define ROM_SERIAL4 4
#define ROM_SERIAL5 5
#define ROM_SERIAL6 6
#define ROM_CRC 7

// 1-wire rom command set
#define ONEWIRE_CMD_READROM 0x33
#define ONEWIRE_CMD_SKIPROM 0xCC
#define ONEWIRE_CMD_SEARCHROM 0xF0
#define ONEWIRE_CMD_MATCHROM 0x55
#define ONEWIRE_CMD_ALARMSEARCH 0xEC
#define ONEWIRE_CMD_SKIPROM_OVERDRIVE 0x3C //Might be ds2423 specific
#define ONEWIRE_CMD_MATCHROM_OVERDRIVE 0x69 //Might be ds2423 specific

// ds2450 function command set
#define DS2450_CMD_WRITE_MEMORY 0x55
#define DS2450_CMD_READ_MEMORY 0xAA
#define DS2450_CMD_CONVERT 0x3C

// ds2450 constants
#define AD_CHANNEL_D 0x08
/*******************************************************/

// method declarations
int OWFirst();
int OWNext();
int OWVerify();
void OWTargetSetup(unsigned char family_code);
void OWFamilySkipSetup();
int OWSearch();

// note that not all applications need to disable interrupts when
// performing onewire transactions. but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.

void onewire_disable_interrupts(int disable)
{
if (disable)
disable_interrupts(GLOBAL);
else
enable_interrupts(GLOBAL);
}


short int onewire_init_with_error_check()
{
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
// delay_us( 5 ); // allow pin to stabilize
delay_us(10);
if (!input(ONE_WIRE_PIN))
{
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (1-wire leads shorted)
}
delay_us( 80 ); // wait for presence pulse, allowing for device variation
// delay_us(65);
if (input(ONE_WIRE_PIN))
{
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (no 1-wire devices present)
}
delay_us( 420 ); // wait-out remaining initialisation window.
// delay_us(240);
output_float(ONE_WIRE_PIN);
// printf("<>ok >onewire_init\n\r");
onewire_disable_interrupts(FALSE);
return ( TRUE ); // device(s) present and initialised.
}


void onewire_init()
{ // OK if just using a single permanently connected device
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
delay_us( 80 ); // wait for presence pulse, allowing for device variation
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
onewire_disable_interrupts(FALSE);
}


void onewire_sendbyte(int temp_data)
{
int count;
// static int debugS;
// printf("0x%x >onewire_sendbyte(%u)\n\r",data,debugS++);
onewire_disable_interrupts(TRUE);
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&temp_data,1,0)); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
}
onewire_disable_interrupts(FALSE);
}


int onewire_readbyte()
{
int count, temp_data;
// static int debugR;
onewire_disable_interrupts(TRUE);
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
shift_right(&temp_data,1,input(ONE_WIRE_PIN)); // and load result.
delay_us( 120 ); // wait until end of read slot.
}
// printf("0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( temp_data );
}

void onewire_sendbit(int1 bit)
{
int count;
// static int debugS;
// printf("0b%x >onewire_sendbit(%u)\n\r",bit,debugS++);
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, bit); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
onewire_disable_interrupts(FALSE);
}


int onewire_readbit()
{
int count;
int1 bit;
// static int debugR;
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
bit = input(ONE_WIRE_PIN); // and load result.
delay_us( 120 ); // wait until end of read slot.
// printf("0b%x >onewire_readbit(%u)\n\r",bit,debugR++);
onewire_disable_interrupts(FALSE);
return( bit );
}

int onewire_crc(int oldcrc, int newbyte)
{
// see http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf
int shift_reg, data_bit, sr_lsb, fb_bit, j;

shift_reg=oldcrc;
for(j=0; j<8; j++)
{ // for each bit
data_bit = (newbyte >> j) & 0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
shift_reg = shift_reg ^ 0x8c;
}
return(shift_reg);
}

void onewire_read_rom()
{ /* rtns ROM info, one byte at a time */
// int data[8];

// if (!onewire_init_with_error_check())
// return (0);
onewire_init();
onewire_sendbyte(ONEWIRE_CMD_READROM);

onewire_data[ROM_DEVTYPE]=onewire_readbyte(); // 0 family code
onewire_data[ROM_SERIAL1]=onewire_readbyte(); // 1 serial number LSB
onewire_data[ROM_SERIAL2]=onewire_readbyte(); // 2 serial number
onewire_data[ROM_SERIAL3]=onewire_readbyte(); // 3 serial number
onewire_data[ROM_SERIAL4]=onewire_readbyte(); // 4 serial number
onewire_data[ROM_SERIAL5]=onewire_readbyte(); // 5 serial number
onewire_data[ROM_SERIAL6]=onewire_readbyte(); // 6 serial number MSB
onewire_data[ROM_CRC]=onewire_readbyte(); // 7 CRC
/*
if (field > 7)
{
//printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else
{
//printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}*/
}

//******************************************************************//
//--------------------------------------------------------------------------
// Find the 'first' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : no device present
//
int OWFirst()
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
//DEBUG printf("In OWFirst()\n\r");
return OWSearch();
}

//--------------------------------------------------------------------------
// Find the 'next' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
int OWNext()
{
// leave the search state alone
//DEBUG printf("In OWNext()\n\r");
return OWSearch();
}
//--------------------------------------------------------------------------
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
// search state.
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
int OWSearch()
{
int id_bit_number;
int last_zero, rom_byte_number, search_result;
int id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;
//DEBUG printf("Entered OWSearch()\n\r");
// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
crc8 = 0;
//DEBUG printf("OWSearch() init ready\n\r");
// if the last call was not the last one
if (!LastDeviceFlag)
{
// 1-Wire reset
if (!onewire_init_with_error_check())
{
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
// fprintf(DEBUG,"OWSearch() reset failed\n\r");
return FALSE;
}

// issue the search command
onewire_sendbyte(0xF0);
// loop to do the search
do
{
// read a bit and its complement
id_bit = onewire_readbit();
cmp_id_bit = onewire_readbit();
// check for no devices on 1-wire
//DEBUG printf("OWSearch() id_bit = %U, cmp_id_bit = %U\n\r",id_bit,cmp_id_bit);
if ((id_bit == 1) && (cmp_id_bit == 1))
break;
else
{
//DEBUG printf("OWSearch() found a device\n\r");
// 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;

// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy = last_zero;
}
}

// 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
onewire_sendbit(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)
{
// docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
//DEBUG printf("OWSearch() search complete\n\r");
// if the search was successful then
if (!((id_bit_number < 65) || (crc8 != 0)))
{
// 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;
LastFamilyDiscrepancy = 0;
search_result = FALSE;
}
//DEBUG printf("OWSearch() exiting the search routine result = %U\n\r",search_result);
return search_result;
}

//--------------------------------------------------------------------------
// Verify the device with the ROM number in ROM_NO buffer is present.
// Return TRUE : device verified present
// FALSE : device not present
//
int OWVerify()
{
unsigned char rom_backup[8];
int i,rslt,ld_backup,ldf_backup,lfd_backup;

// keep a backup copy of the current state
for (i = 0; i < 8; i++)
rom_backup[i] = ROM_NO[i];
ld_backup = LastDiscrepancy;
ldf_backup = LastDeviceFlag;
lfd_backup = LastFamilyDiscrepancy;

// set search to find the same device
LastDiscrepancy = 64;
LastDeviceFlag = FALSE;

if (OWSearch())
{
// check if same device found
rslt = TRUE;
for (i = 0; i < 8; i++)
{
if (rom_backup[i] != ROM_NO[i])
{
rslt = FALSE;
break;
}
}
}
else
rslt = FALSE;

// restore the search state
for (i = 0; i < 8; i++)
ROM_NO[i] = rom_backup[i];
LastDiscrepancy = ld_backup;
LastDeviceFlag = ldf_backup;
LastFamilyDiscrepancy = lfd_backup;

// return the result of the verify
return rslt;
}

//--------------------------------------------------------------------------
// Setup the search to find the device type 'family_code' on the next call
// to OWNext() if it is present.
//
void OWTargetSetup(unsigned char family_code)
{
int i;

// set the search state to find SearchFamily type devices
ROM_NO[0] = family_code;
for (i = 1; i < 8; i++)
ROM_NO[i] = 0;
LastDiscrepancy = 64;
LastFamilyDiscrepancy = 0;
LastDeviceFlag = FALSE;
}

//--------------------------------------------------------------------------
// Setup the search to skip the current device type on the next call
// to OWNext().
//
void OWFamilySkipSetup()
{
// set the Last discrepancy to last family discrepancy
LastDiscrepancy = LastFamilyDiscrepancy;
LastFamilyDiscrepancy = 0;

// check for end of list
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
}


/********************************************************/
// ds2450 counter address
#define TA1_SRAM_DS2450 0x08
#define TA2_SRAM_DS2450 0x00

int onewire_ds2450_setup(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_WRITE_MEMORY);
onewire_sendbyte(TA1_SRAM_DS2450); //The LSByte of the address
onewire_sendbyte(TA2_SRAM_DS2450); //The MSByte of the address
//Channel A
onewire_sendbyte(0xC0); //data byte? (0x0008)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x00); //Next data byte (0x0009)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel B
onewire_sendbyte(0xC0); //data byte? (0x000A)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x00); //Next data byte (0x000B)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel C
onewire_sendbyte(0x00); //data byte? (0x000C) Sets the channel to A/D
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x01); //Next data byte (0x000D)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel D
onewire_sendbyte(0x00); //data byte? (0x000E) Sets the channel to A/D
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
onewire_sendbyte(0x01); //Next data byte (0x000F)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x01

onewire_init(); //Ends the transition
}

int onewire_ds2450_start_convert(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_CONVERT);
onewire_sendbyte(AD_CHANNEL_D); //Mask channel D
onewire_sendbyte(0x00); //The MSByte of the address

crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();

m = 0;
do
{
onewire_data[m] = onewire_readbyte();
m++;
// if(m > 20)
// m = 0;
}while(onewire_data[m - 1] != 0xFF);
onewire_init(); //Ends the transition
}

int onewire_ds2450_read_converted(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_READ_MEMORY);
onewire_sendbyte(0x00); //Address to the converted value (MSB)
onewire_sendbyte(0x00); //MSByte of the address
for(m=0;m<8;m++)
onewire_data[m] = onewire_readbyte(); //Receive channel info

crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();

onewire_init(); //Ends the transition
}
Bad_Mujo
Guest







PostPosted: Mon Jan 26, 2004 9:07 am     Reply with quote

Here are some functions for a DS2450 Quad A/D.
It only uses channels C and D but its easy to change
The driver have I done but the Onewire routines are copied/borrowed from one at this forum (Sorry cant remember Your name)

//
// Onewire routines
//
// onewire rom registers
#define ROM_DEVTYPE 0
#define ROM_SERIAL1 1
#define ROM_SERIAL2 2
#define ROM_SERIAL3 3
#define ROM_SERIAL4 4
#define ROM_SERIAL5 5
#define ROM_SERIAL6 6
#define ROM_CRC 7

// 1-wire rom command set
#define ONEWIRE_CMD_READROM 0x33
#define ONEWIRE_CMD_SKIPROM 0xCC
#define ONEWIRE_CMD_SEARCHROM 0xF0
#define ONEWIRE_CMD_MATCHROM 0x55
#define ONEWIRE_CMD_ALARMSEARCH 0xEC
#define ONEWIRE_CMD_SKIPROM_OVERDRIVE 0x3C //Might be ds2423 specific
#define ONEWIRE_CMD_MATCHROM_OVERDRIVE 0x69 //Might be ds2423 specific

// ds2450 function command set
#define DS2450_CMD_WRITE_MEMORY 0x55
#define DS2450_CMD_READ_MEMORY 0xAA
#define DS2450_CMD_CONVERT 0x3C

// ds2450 constants
#define AD_CHANNEL_D 0x08
/*******************************************************/

// method declarations
int OWFirst();
int OWNext();
int OWVerify();
void OWTargetSetup(unsigned char family_code);
void OWFamilySkipSetup();
int OWSearch();

// note that not all applications need to disable interrupts when
// performing onewire transactions. but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.

void onewire_disable_interrupts(int disable)
{
if (disable)
disable_interrupts(GLOBAL);
else
enable_interrupts(GLOBAL);
}


short int onewire_init_with_error_check()
{
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
// delay_us( 5 ); // allow pin to stabilize
delay_us(10);
if (!input(ONE_WIRE_PIN))
{
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (1-wire leads shorted)
}
delay_us( 80 ); // wait for presence pulse, allowing for device variation
// delay_us(65);
if (input(ONE_WIRE_PIN))
{
onewire_disable_interrupts(FALSE);
return ( FALSE ); // error (no 1-wire devices present)
}
delay_us( 420 ); // wait-out remaining initialisation window.
// delay_us(240);
output_float(ONE_WIRE_PIN);
// printf("<>ok >onewire_init\n\r");
onewire_disable_interrupts(FALSE);
return ( TRUE ); // device(s) present and initialised.
}


void onewire_init()
{ // OK if just using a single permanently connected device
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
delay_us( 80 ); // wait for presence pulse, allowing for device variation
delay_us( 420 ); // wait-out remaining initialisation window.
output_float(ONE_WIRE_PIN);
onewire_disable_interrupts(FALSE);
}


void onewire_sendbyte(int temp_data)
{
int count;
// static int debugS;
// printf("0x%x >onewire_sendbyte(%u)\n\r",data,debugS++);
onewire_disable_interrupts(TRUE);
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&temp_data,1,0)); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
}
onewire_disable_interrupts(FALSE);
}


int onewire_readbyte()
{
int count, temp_data;
// static int debugR;
onewire_disable_interrupts(TRUE);
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
shift_right(&temp_data,1,input(ONE_WIRE_PIN)); // and load result.
delay_us( 120 ); // wait until end of read slot.
}
// printf("0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( temp_data );
}

void onewire_sendbit(int1 bit)
{
int count;
// static int debugS;
// printf("0b%x >onewire_sendbit(%u)\n\r",bit,debugS++);
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, bit); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
onewire_disable_interrupts(FALSE);
}


int onewire_readbit()
{
int count;
int1 bit;
// static int debugR;
onewire_disable_interrupts(TRUE);
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
bit = input(ONE_WIRE_PIN); // and load result.
delay_us( 120 ); // wait until end of read slot.
// printf("0b%x >onewire_readbit(%u)\n\r",bit,debugR++);
onewire_disable_interrupts(FALSE);
return( bit );
}

int onewire_crc(int oldcrc, int newbyte)
{
// see http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf
int shift_reg, data_bit, sr_lsb, fb_bit, j;

shift_reg=oldcrc;
for(j=0; j<8; j++)
{ // for each bit
data_bit = (newbyte >> j) & 0x01;
sr_lsb = shift_reg & 0x01;
fb_bit = (data_bit ^ sr_lsb) & 0x01;
shift_reg = shift_reg >> 1;
if (fb_bit)
shift_reg = shift_reg ^ 0x8c;
}
return(shift_reg);
}

void onewire_read_rom()
{ /* rtns ROM info, one byte at a time */
// int data[8];

// if (!onewire_init_with_error_check())
// return (0);
onewire_init();
onewire_sendbyte(ONEWIRE_CMD_READROM);

onewire_data[ROM_DEVTYPE]=onewire_readbyte(); // 0 family code
onewire_data[ROM_SERIAL1]=onewire_readbyte(); // 1 serial number LSB
onewire_data[ROM_SERIAL2]=onewire_readbyte(); // 2 serial number
onewire_data[ROM_SERIAL3]=onewire_readbyte(); // 3 serial number
onewire_data[ROM_SERIAL4]=onewire_readbyte(); // 4 serial number
onewire_data[ROM_SERIAL5]=onewire_readbyte(); // 5 serial number
onewire_data[ROM_SERIAL6]=onewire_readbyte(); // 6 serial number MSB
onewire_data[ROM_CRC]=onewire_readbyte(); // 7 CRC
/*
if (field > 7)
{
//printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
return(0);
}
else
{
//printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
return(data[field]);
}*/
}

//******************************************************************//
//--------------------------------------------------------------------------
// Find the 'first' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : no device present
//
int OWFirst()
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
//DEBUG printf("In OWFirst()\n\r");
return OWSearch();
}

//--------------------------------------------------------------------------
// Find the 'next' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
int OWNext()
{
// leave the search state alone
//DEBUG printf("In OWNext()\n\r");
return OWSearch();
}
//--------------------------------------------------------------------------
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
// search state.
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
int OWSearch()
{
int id_bit_number;
int last_zero, rom_byte_number, search_result;
int id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;
//DEBUG printf("Entered OWSearch()\n\r");
// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
crc8 = 0;
//DEBUG printf("OWSearch() init ready\n\r");
// if the last call was not the last one
if (!LastDeviceFlag)
{
// 1-Wire reset
if (!onewire_init_with_error_check())
{
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
// fprintf(DEBUG,"OWSearch() reset failed\n\r");
return FALSE;
}

// issue the search command
onewire_sendbyte(0xF0);
// loop to do the search
do
{
// read a bit and its complement
id_bit = onewire_readbit();
cmp_id_bit = onewire_readbit();
// check for no devices on 1-wire
//DEBUG printf("OWSearch() id_bit = %U, cmp_id_bit = %U\n\r",id_bit,cmp_id_bit);
if ((id_bit == 1) && (cmp_id_bit == 1))
break;
else
{
//DEBUG printf("OWSearch() found a device\n\r");
// 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;

// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy = last_zero;
}
}

// 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
onewire_sendbit(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)
{
// docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
//DEBUG printf("OWSearch() search complete\n\r");
// if the search was successful then
if (!((id_bit_number < 65) || (crc8 != 0)))
{
// 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;
LastFamilyDiscrepancy = 0;
search_result = FALSE;
}
//DEBUG printf("OWSearch() exiting the search routine result = %U\n\r",search_result);
return search_result;
}

//--------------------------------------------------------------------------
// Verify the device with the ROM number in ROM_NO buffer is present.
// Return TRUE : device verified present
// FALSE : device not present
//
int OWVerify()
{
unsigned char rom_backup[8];
int i,rslt,ld_backup,ldf_backup,lfd_backup;

// keep a backup copy of the current state
for (i = 0; i < 8; i++)
rom_backup[i] = ROM_NO[i];
ld_backup = LastDiscrepancy;
ldf_backup = LastDeviceFlag;
lfd_backup = LastFamilyDiscrepancy;

// set search to find the same device
LastDiscrepancy = 64;
LastDeviceFlag = FALSE;

if (OWSearch())
{
// check if same device found
rslt = TRUE;
for (i = 0; i < 8; i++)
{
if (rom_backup[i] != ROM_NO[i])
{
rslt = FALSE;
break;
}
}
}
else
rslt = FALSE;

// restore the search state
for (i = 0; i < 8; i++)
ROM_NO[i] = rom_backup[i];
LastDiscrepancy = ld_backup;
LastDeviceFlag = ldf_backup;
LastFamilyDiscrepancy = lfd_backup;

// return the result of the verify
return rslt;
}

//--------------------------------------------------------------------------
// Setup the search to find the device type 'family_code' on the next call
// to OWNext() if it is present.
//
void OWTargetSetup(unsigned char family_code)
{
int i;

// set the search state to find SearchFamily type devices
ROM_NO[0] = family_code;
for (i = 1; i < 8; i++)
ROM_NO[i] = 0;
LastDiscrepancy = 64;
LastFamilyDiscrepancy = 0;
LastDeviceFlag = FALSE;
}

//--------------------------------------------------------------------------
// Setup the search to skip the current device type on the next call
// to OWNext().
//
void OWFamilySkipSetup()
{
// set the Last discrepancy to last family discrepancy
LastDiscrepancy = LastFamilyDiscrepancy;
LastFamilyDiscrepancy = 0;

// check for end of list
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
}


/********************************************************/
// ds2450 counter address
#define TA1_SRAM_DS2450 0x08
#define TA2_SRAM_DS2450 0x00

int onewire_ds2450_setup(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_WRITE_MEMORY);
onewire_sendbyte(TA1_SRAM_DS2450); //The LSByte of the address
onewire_sendbyte(TA2_SRAM_DS2450); //The MSByte of the address
//Channel A
onewire_sendbyte(0xC0); //data byte? (0x0008)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x00); //Next data byte (0x0009)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel B
onewire_sendbyte(0xC0); //data byte? (0x000A)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x00); //Next data byte (0x000B)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel C
onewire_sendbyte(0x00); //data byte? (0x000C) Sets the channel to A/D
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0xC0
onewire_sendbyte(0x01); //Next data byte (0x000D)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
//Channel D
onewire_sendbyte(0x00); //data byte? (0x000E) Sets the channel to A/D
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x00
onewire_sendbyte(0x01); //Next data byte (0x000F)
crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();
onewire_data[0] = onewire_readbyte(); //Should be 0x01

onewire_init(); //Ends the transition
}

int onewire_ds2450_start_convert(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_CONVERT);
onewire_sendbyte(AD_CHANNEL_D); //Mask channel D
onewire_sendbyte(0x00); //The MSByte of the address

crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();

m = 0;
do
{
onewire_data[m] = onewire_readbyte();
m++;
// if(m > 20)
// m = 0;
}while(onewire_data[m - 1] != 0xFF);
onewire_init(); //Ends the transition
}

int onewire_ds2450_read_converted(unsigned int *rom_data)
{ /* */
unsigned int m;

if (!onewire_init_with_error_check())
return (0);
// onewire_sendbyte(ONEWIRE_CMD_SKIPROM);
onewire_sendbyte(ONEWIRE_CMD_MATCHROM);
for(m=0;m<8;m++)
onewire_sendbyte(rom_data[m]);
onewire_sendbyte(DS2450_CMD_READ_MEMORY);
onewire_sendbyte(0x00); //Address to the converted value (MSB)
onewire_sendbyte(0x00); //MSByte of the address
for(m=0;m<8;m++)
onewire_data[m] = onewire_readbyte(); //Receive channel info

crc[0] = onewire_readbyte();
crc[1] = onewire_readbyte();

onewire_init(); //Ends the transition
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group