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

Software I2C control DAC_MCP4725A0T 32kHz clock

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



Joined: 22 Jun 2021
Posts: 7
Location: Orange County Calif. USA

View user's profile Send private message Send e-mail Visit poster's website

Software I2C control DAC_MCP4725A0T 32kHz clock
PostPosted: Wed Jun 23, 2021 1:28 pm     Reply with quote

I use software I2C control, ( not hardware I2C on PIC ) and usually run on clock of 32khz to save current consumption.

Code:


UI1  SCL_input ( void )   
{
  UI1  ui1_input;
  //  input 
  ui1_input = input ( CLK );   
  return( ui1_input );
}  //



void SCL_output ( UI8 ui8_state  )
{
 
  switch ( ui8_state )
  {
    case I2C_IO_low:
      output_low ( CLK );   
      break;
         
       
    case I2C_IO_high:
      output_high ( CLK );   
      break;
   
       
    case I2C_IO_float:
      output_float ( CLK );   
      break;
 
       
    default:
      break;
     
  } //  switch ( ui8_state )
 
 
}  // void SCL_output ( UI8 ui8_state , UI8  ui8_i2c_select )




UI1  SDA_input ( void )   
{
  UI1  ui1_input;
 
  ui1_input = input ( DAT );   
 
  return( ui1_input );
 
}




void SDA_output ( UI8 ui8_state  )
{

  switch ( ui8_state )
  {
 
    case I2C_IO_low:
      output_low ( DAT );   
      break;
       
       
    case I2C_IO_high:
      output_high ( DAT );   
      break;
   
       
    case I2C_IO_float:
      output_float ( DAT );   
      break;
       
    default:
      break;
     
  } //  switch ( ui8_state )
 
 
}  // void SDA_output ( UI8 ui8_state , UI8  ui8_i2c_select )




void i2c_start_1 ( void )
{
  //  start condition

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );
  //delay_ms ( 2 );

  SDA_output ( I2C_IO_low );  // SDA -- LOW
 
  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 2 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );
 
  SCL_output ( I2C_IO_low );  // SCL -- low

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 2 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );

}  //  void i2c_start_1 (




void i2c_stop_1 ( void )
{

  SDA_output ( I2C_IO_low );   // SDA low
  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 2 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );

  SCL_output ( I2C_IO_high );  //  <<  SCL_output ( I2C_IO_float );  // SCL float

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 2 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );
 
  SDA_output ( I2C_IO_high );  //  <<  SDA_output ( I2C_IO_float );   // SDA float
 
  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 2 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 10 );

}  // void i2c_stop_1 (




void i2c_send_bit ( UI1 ui1_input_01 )
{

  if ( !ui1_input_01 )
  {
    // LOW
    SDA_output ( I2C_IO_low ); 
  }
  else
  {
    // OPEN
    SDA_output ( I2C_IO_high );  // <<  SDA_output ( I2C_IO_float );
  }


  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 5 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 3 );

  SCL_output ( I2C_IO_high );  //  <<  SCL_output ( I2C_IO_float );  // clock float

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 5 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 3 );

  SCL_output ( I2C_IO_low );  // clock low

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 5 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 3 );
 
} //  void i2c_send_bit ( UI1 ui1_input_01 )



UI1 i2c_receive_bit( void )
{
  UI1 ui1_output_01;
  SCL_output ( I2C_IO_high );  //  <<  SCL_output ( I2C_IO_float );  // clock float

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 5 );
  //delay_us ( I2C_DELAY_US );

  ui1_output_01 = SDA_input (  );  //  read SDA pin
 
  delay_us ( 3 );

  SCL_output ( I2C_IO_low );  // clock low

  //delay_cycles ( 20 );    //  5uS at 16 MHz
  //delay_cycles ( 200 );    // 
  //delay_ms ( 5 );
  //delay_us ( I2C_DELAY_US );
  delay_us ( 3 );

  return( ui1_output_01 );

} //  UI1 i2c_receive_bit( )


_________________
Orange County Calif. USA
http://ag6ju-ipv6.dynv6.net:5950/


Last edited by ag6ju on Tue Aug 17, 2021 12:39 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 24, 2021 10:07 am     Reply with quote

Why did you write your own software i2c routines ?
CCS has software i2c available as part of the #use i2c library code.
ag6ju



Joined: 22 Jun 2021
Posts: 7
Location: Orange County Calif. USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Jun 24, 2021 10:46 am     Reply with quote

two reason, it was from the time PIC did not have I2C interface yet 20 years ago or so. and so I can run it slower clock speed like 32khz clock to save current, many of my project runs on small battery where its current capacity is no more than several hundred microamp.
_________________
Orange County Calif. USA
http://ag6ju-ipv6.dynv6.net:5950/
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Jul 01, 2021 6:10 am     Reply with quote

The second 'reason' does not make any sense. The CCS software routines will
be faster for your clock rate.
Given you are showing this 'now', use the CCS routines.
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