crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
Pin state problem in function |
Posted: Sun Jun 15, 2008 10:31 am |
|
|
Hi guys, i've got a very annoying problem. I'm trying to determine the state of a pin after i have set it, left it for a few uS, then let it float. but his does not seem to work.
The part i'm referring to is in the OWWriteByte function.
The code was sourced from AN126 "1-wire communication through software" and the code works otherwise, even the OWAccess works, except that it does not return an error when the device is not attached, i have two identical devices on a network with a 4k7 pull up, both devices are the DS18B20 temp sensors.
As i've said the code works, so there is nothing wrong with hardware but with how the pin state is read. I've tried various methods and changed the OWWriteByte code alot (currently posted is original AN126 code) but no luck.
The flow diagram for the MATCH_ROM cycle is in the DS18B20 datasheet, effectively what it does is: send 1 bit(of serial number - LSB first), then check if line status is same as bit sent, if so, continue, else reset and start over.
Any suggestions/help would be greatly appreciated!!
Code: |
#define MATCH_ROM 0x55
#define owpin pin_c6 //rs232 has been moved to another port to use this pin
#Define A 6
#Define B 64
#Define C 60
#Define D 10
#Define E 9
#Define F 55
#Define G 0
#Define H 480
#Define I 70
#Define J 410
/***************************************************************
Generate a 1-Wire reset, return 0 if no presence was found,
return 1 otherwise.
***************************************************************/
int1 OWReset(void)
{
int1 result;
delay_us(G); //tickDelay(G);
output_low(owpin); //outp(PORTADDRESS,0x00); // Drives DQ low
delay_us(H); //tickDelay(H);
output_float(owpin); //outp(PORTADDRESS,0x01); // Releases the bus
delay_us(I); //tickDelay(I);
result = !input(owpin); //result = inp(PORTADDRESS) & 0x01; // Sample for presence pulse from slave
delay_us(J); //tickDelay(J); // Complete the reset sequence recovery
return(Result); // Return sample presence pulse result
}
/***************************************************************
Send a 1-Wire write bit. Function also provides a 10uS
recovery time.
***************************************************************/
int1 OWWriteBit(int1 bit)
{
int1 res;
if (bit)
{
// Write '1' bit
//printf("1");
output_low(owpin); //outp(PORTADDRESS,0x00); // Drives DQ low
delay_us(A); //tickDelay(A);
output_float(owpin); //outp(PORTADDRESS,0x01); // Releases the bus
delay_us(B); //tickDelay(B); // Complete the time slot and 10us recovery
}
else
{
// Write '0' bit
//printf("0");
output_low(owpin); //outp(PORTADDRESS,0x00); // Drives DQ low
delay_us(C); //tickDelay(C);
output_float(owpin); //outp(PORTADDRESS,0x01); // Releases the bus
delay_us(D); //tickDelay(D);
}
return res;
}
/***************************************************************
Read a bit from the 1-Wire bus and return it. Funtion also
provides 10uS recovery time.
***************************************************************/
int1 OWReadBit(void)
{
int1 result;
output_low(owpin); //outp(PORTADDRESS,0x00); // Drives DQ low
delay_us(A); //tickDelay(A);
output_float(owpin); //outp(PORTADDRESS,0x01); // Releases the bus
delay_us(E); //tickDelay(E);
result = input_state(owpin); //result = inp(PORTADDRESS) & 0x01; // Sample the bit value from the slave
delay_us(F); //tickDelay(F); // Complete the time slot and 10us recovery
return result;
}
/***************************************************************
Write a 1-Wire data byte and return the sampled result
***************************************************************/
int8 OWWriteByte(int8 data)
{
int8 loop, result=0;
for (loop = 0; loop < 8; loop++)
{
// shift the result to get it ready for the next bit
result >>= 1;
// If sending a '1' then read a bit else write a '0'
if (data & 0x01)
{
if (OWReadBit())
result |= 0x80;
}
else
OWWriteBit(0);
// shift the data byte for the next bit
data >>= 1;
}
return result;
}
/***************************************************************
Resets the 1-Wire bus and sends a match rom command and if
the command echo was valid the 1-Wire device is ready to accept
device-specific commands.
Returns: True: Device is present and ready for commands
False: Device not present
***************************************************************/
Boolean OWAccess()
{
int8 sendpacket[8],i;
int1 bad_echo;
if(!OWReset()) //No devices on network
return(FALSE);
OWWriteByte(MATCH_ROM); //Send the match rom command 0x55
// send/recieve the transfer buffer
for(i=0;i<8;i++)
{
sendpacket[i] = OWWriteByte(ROM_NO[i]);
}
// verify that the echo of the writes was correct
for (i = 0; i < 8; i++)
{
if (sendpacket[i] != ROM_NO[i])
bad_echo = TRUE;
}
// if echo ok then success
if (!bad_echo)
return TRUE;
else
return FALSE;
}
//Example
int8 ROM_NO[8] = {0x28,0x52,0x8E,0x9B,0x01,0x00,0x00,0xE9};
while(true)
{
if(OWAccess())
{
printf("Device found and ready\n\r")
}
else
{
printf("No such device found\n\r");
}
delay_ms(1000);
} |
|
|