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

Problem with 1-Wire library

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



Joined: 28 Apr 2004
Posts: 14

View user's profile Send private message

Problem with 1-Wire library
PostPosted: Fri Mar 28, 2008 2:36 am     Reply with quote

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);
}
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Fri Mar 28, 2008 7:52 am     Reply with quote

Providing the #FUSES statement and the #USE delay
are almost always necessary when asking others to look at your code.
Since you assert your code works at 8mhz then it will be a fools errand for others to look at it. The error will probably be in the fuses or some other undisclosed snippet of your code.
nemena12
Guest







PostPosted: Mon Sep 07, 2009 3:39 am     Reply with quote

A good choice is to choose a XTAL that provides 1us of instruction cycle or minus.
For PIC's the instruction cycle = 1/ (fXTAL/4).
So in your case 4 mhz is required....
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