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

aj.. 1wire me baby

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







aj.. 1wire me baby
PostPosted: Sat Apr 30, 2005 3:37 pm     Reply with quote

Hey, I change my code,
but it still doesnt work !

can you take a look on my code :


Code:
#include <16f870.h>
#use delay(clock=4000000)
#fuses XT,NOWDT,NOLVP

#define SKIP_ROM 0xCC
#define CONVERT_T 0x44
#define READ_SCRATCHPAD 0xBE

#define DQ PIN_C4
//#define Tx_DS1820 PIN_C5

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)


//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//
unsigned char ow_reset(void)
   {
      unsigned char presence;
      output_low(DQ); //pull DQ line low
      delay_us(480); // leave it low for 480us
      input(DQ); // allow line to return high
      delay_us(70); // wait for presence
      presence = DQ; // get presence signal
      delay_us(424); // wait for end of timeslot
      return(presence); // presence signal returned
}                       // 0=presence, 1 = no part




//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
unsigned char read_bit(void)
{
         unsigned char i;
         output_low(DQ); // pull DQ low to start timeslot
         input(DQ);// then return high
         for (i=0; i<3; i++); // delay 15us from start of timeslot
         return(DQ); // return value of DQ line
}




//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
      output_low(DQ); // pull DQ low to start timeslot
      if(bitval==1) input(DQ); // return DQ high if write 1
      delay_us(104); // hold value for remainder of timeslot
      input(DQ);
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us


//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
      unsigned char i;
      unsigned char value = 0;
      for (i=0;i<8;i++)
      {
      if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
      // shifts it left
      delay_us(120); // wait for rest of timeslot
}
      return(value);
}



//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//
void write_byte(char val)
{
      unsigned char i;
      unsigned char temp;
      for (i=0; i<8; i++) // writes byte, one bit at a time
      {
      temp = val>>i; // shifts val right 'i' spaces
      temp &= 0x01; // copy that bit to temp
      write_bit(temp); // write bit in temp into
}
      delay_us(104);
}

void main()
{
         char get[10];
         int k;
         char temp_f,temp_c;

   while(1)
      {

            ow_reset();
            write_byte(0xCC); //Skip ROM
            write_byte(0x44); // Start Conversion
            delay_us(104);
            ow_reset();
            write_byte(0xCC); // Skip ROM
            write_byte(0xBE); // Read Scratch Pad
            for (k=0;k<9;k++)
               {  get[k]=read_byte();
               }

            printf("\n ScratchPAD DATA = %X%X%X%X%X\n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);


      }
}



whats wrong??
Thx
burned
Guest







PostPosted: Sun May 01, 2005 9:36 am     Reply with quote

I spent 2 1/2 weeks beating my head on trying to get the 1820 to work.
I tried every code segment on this board -- and there are a lot --'just search the forum and you'll find it" -- yeah, you'll find em - lots of em.

BUT -- I got it working last nite. I finally narrowed it down to : CCS compiler does NOT do accurate timing with delay_us

the problem was not the code but the TIMING.

I added a local segment for us delay from some web site (could have been this one -- I spent so much time searching the web for 'working' code for this -- i lost track of what is from where)

Code:

#define   XTAL_FREQ   20MHZ      /* Crystal frequency in MHz */

#define   MHZ   *1000L         /* number of kHz in a MHz */

#define   delayus(x)   { unsigned char _dcnt; \
           _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
           while(--_dcnt != 0) \
              continue; }



then I replace all the ccs delay_us with the local function delayus and WHAM the code works -- in fact they all started working when I substituted the delay funciton.


The one I have listed has code to deal with the slower clock speeds (like your 4 mhz ) and I chopped that out -- so this prob won't work for you, but the deal is TIMING ! us timing.

find a us delay function for your clock speed to use instead of the library funciton.



good luck
geust
Guest







PostPosted: Sun May 01, 2005 11:58 am     Reply with quote

finally some good news!! Smile
Thx,
but how can I create a function that gives me uSeconds???

I'm just a rookie,sorry..
and I dont know how to creat a uSecond Segment

Please help
Thanks
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun May 01, 2005 4:26 pm     Reply with quote

Quote:
BUT -- I got it working last nite. I finally narrowed it down to : CCS compiler does NOT do accurate timing with delay_us

It always helps a lot when you mention the compiler version you are using.... See the thread http://www.ccsinfo.com/forum/viewtopic.php?t=19516 for the description of a delay_us() bug in v3.191 - 3.202, causing delays larger than 153 to fail.
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