Inkwaterman
Joined: 28 Apr 2004 Posts: 14
|
Problem with 1-Wire library |
Posted: Fri Mar 28, 2008 2:36 am |
|
|
Hi !
I use the "1-Wire" library done by j.d.sandoz for communicated with DS2760 and I have the following problem:
If I setup the internal clock to 8 Mhz the library works fine.
For my project I have to put the clock to 1 Mhz, but with this setup the library doesn't work....
If you look the code you'll find that there are only absolute delays (dely_ms() )..
Why the library doesn't work at 1 mhz ?
thanks !
Code: |
/*
defines: ONE_WIRE_PIN
Pin Per la comunicazione one-wire con il PIC
*/
#define ONE_WIRE_PIN PIN_J5
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
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
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.
output_float(ONE_WIRE_PIN);
//printf(debug_putc,"<>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 data) {
int count;
//static int debugS;
//printf(debug_putc,"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(&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, 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(&data,1,input(ONE_WIRE_PIN)); // and load result.
delay_us( 120 ); // wait until end of read slot.
}
//printf(debug_putc,"0x%x >onewire_readbyte(%u)\n\r",data,debugR++);
onewire_disable_interrupts(FALSE);
return( data );
}
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);
}
|
|
|