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 support@ccsinfo.com

Dallas OneWire Using Input Capture & Output Compare

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
smee



Joined: 16 Jan 2014
Posts: 24

View user's profile Send private message

Dallas OneWire Using Input Capture & Output Compare
PostPosted: Fri Feb 08, 2019 10:26 am     Reply with quote

I have been using this on a pic16F1847 at 32 MHz.
i have been running the search rom mainly, and did this so that it will live with the system tick interupts and spi slave and spi master running too.

It worked quite well, but i have run out of code space on the 1847 so am moving it to the dspic33fj128gp802 which requires some modifications, so before i chop it up, here it is.


compiler is 4.120 in case.

Code:

void Initialise_One_Wire( void )
{
// this routine configures the pin for the one wire
// this is for the pin_a7 using ccp module on pic16f1847
bit_set(apfcon0,3);   // make sure on port pin A7
// assumes pin set as input

bit_set(tristatea,7);   // make line input
bit_set(latcha,7);      // set latch to same state

} // end of init one wire
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
signed int8 onewire_init_with_error_check(void)
{ // returns are 0(no) 1(yes) -1(error)
bit_set(latcha,7);      // set latch to high state
bit_clear(tristatea,7);   // set line to output
// this is start of the REset Pulse generator and detector
disable_interrupts(GLOBAL);            // inhibit ints for a little while
setup_ccp2(CCP_COMPARE_CLR_ON_MATCH);   // pull output low step1
set_timer1(0);                      // pull output low step2
CCP_2=0;                        // pull output low step3
clear_interrupt(INT_CCP2);            // make sure int clear
setup_ccp2(CCP_COMPARE_SET_ON_MATCH); // Configure CCP1 to go high when match
CCP_2 = 3840; // = 480uS
enable_interrupts(GLOBAL);            // allow ints now
// this bit sets the output high
while(get_timer1()<3840) ;
// so now line is high
disable_interrupts(GLOBAL);
bit_set(tristatea,7);   // make line an input line
bit_set(latcha,7);      // ensure output is a high
set_timer1(0);
if (input(PIN_A7)==0)
   {
   enable_interrupts(GLOBAL);
   return(-1); // bus locked low error return
   }
clear_interrupt(INT_CCP2);
setup_ccp2(CCP_CAPTURE_RE);    // Configure CCP2 to capture rise
bit_set(apfcon0,3);   // make sure on port pin A7
enable_interrupts(GLOBAL);
while(get_timer1()<3840) ;
return(CCP_2<3840);
} // end of init with error check
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

unsigned int8 onewire_myread_slot( void )
{
// this is start of the write slot
disable_interrupts(GLOBAL);            // inhibit ints for a little while
setup_ccp2(CCP_COMPARE_CLR_ON_MATCH);   // pull output low step1
set_timer1(0);                      // pull output low step2
CCP_2=0;                        // pull output low step3
clear_interrupt(INT_CCP2);      // make sure int clear
setup_ccp2(CCP_COMPARE_SET_ON_MATCH); // Configure CCP1 to go high when match
CCP_2 = 160; // = 20uS
while (interrupt_active(INT_CCP2)==0) ;
bit_set(tristatea,7);   // make an input
bit_set(latcha,7);   // output a high
bit_set(apfcon0,3);   // make sure on port pin A7
setup_ccp2(CCP_CAPTURE_RE);    // Configure CCP2 to capture rise
bit_set(apfcon0,3);   // make sure on port pin A7
clear_interrupt(INT_CCP2);
enable_interrupts(GLOBAL);
while(get_timer1()<480) ;
return(!(CCP_2>160));
// if no RE then value will be from the previous compare = 160
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
signed int8 onewire_mywrite_slot(int1 state)
{
bit_set(latcha,7);   // set the lat to output a 1
bit_clear(tristatea,7);   // make it output port
// this is start of the write slot
disable_interrupts(GLOBAL);            // inhibit ints for a little while
setup_ccp2(CCP_COMPARE_CLR_ON_MATCH);   // pull output low step1
set_timer1(0);                      // pull output low step2
CCP_2=0;                        // pull output low step3
clear_interrupt(INT_CCP2);            // make sure int clear
setup_ccp2(CCP_COMPARE_SET_ON_MATCH); // Configure CCP1 to go high when match
if (state)
   { // we doing a write 1
   CCP_2 = 60; // = 7.5uS
   }
else
   {
   CCP_2=450; // = 60uS
   }
enable_interrupts(GLOBAL);      // allow ints now

// this bit sets the output high
while (get_timer1()<480);     // test for timeout
input(PIN_A7);
bit_set(latcha,7);
return(1);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void onewire_sendbyte(unsigned int8 data) {
unsigned int8 x;
   for (x=0;x<8;x++)
      {
      onewire_mywrite_slot(bit_test(data,x));
       }
//return(true);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

unsigned int8 onewire_readbyte() {
   unsigned int8 x, data;
   unsigned int8 err;
   for (x=0;x<8;x++)
      {
      err=onewire_myread_slot();
      shift_right(&data,1,bit_test(err,0));   // shift the bit in place
      }
   return( data );
}



Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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