View previous topic :: View next topic |
Author |
Message |
ag6ju
Joined: 22 Jun 2021 Posts: 7 Location: Orange County Calif. USA
|
Software I2C control DAC_MCP4725A0T 32kHz clock |
Posted: Wed Jun 23, 2021 1:28 pm |
|
|
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
|
|
Posted: Thu Jun 24, 2021 10:07 am |
|
|
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
|
|
Posted: Thu Jun 24, 2021 10:46 am |
|
|
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: 19504
|
|
Posted: Thu Jul 01, 2021 6:10 am |
|
|
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. |
|
|
|