Guest User

i2c bitbanging for avr

a guest
Jun 2nd, 2010
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. // Port for the I2C
  2. #define I2C_DDR DDRD
  3. #define I2C_PIN PIND
  4. #define I2C_PORT PORTD
  5.  
  6. // Pins to be used in the bit banging
  7. #define I2C_CLK 0    
  8. #define I2C_DAT 1
  9.  
  10. #define I2C_DATA_HI() I2C_DDR &= ~( 1 << I2C_DAT );
  11. #define I2C_DATA_LO() I2C_DDR |=  ( 1 << I2C_DAT );
  12.  
  13. #define I2C_CLOCK_HI() I2C_DDR &= ~( 1 << I2C_CLK );
  14. #define I2C_CLOCK_LO() I2C_DDR |=  ( 1 << I2C_CLK );
  15.  
  16. void I2C_WriteBit( unsigned char c )
  17. {
  18.     if ( c > 0 )
  19.     {
  20.         I2C_DATA_HI();
  21.     }
  22.     else
  23.     {
  24.         I2C_DATA_LO();
  25.     }
  26.  
  27.     I2C_CLOCK_HI();
  28.     _delay_ms(1);
  29.  
  30.     I2C_CLOCK_LO();
  31.     _delay_ms(1);
  32.  
  33.     if ( c > 0 )
  34.     {
  35.         I2C_DATA_LO();
  36.     }
  37.  
  38.     _delay_ms(1);
  39.    
  40. }
  41.  
  42. unsigned char I2C_ReadBit()
  43. {
  44.     I2C_DATA_HI();
  45.  
  46.     I2C_CLOCK_HI();
  47.     _delay_ms(1);
  48.  
  49.     unsigned char c = I2C_PIN;
  50.  
  51.     I2C_CLOCK_LO();
  52.     _delay_ms(1);
  53.  
  54.     return ( c >> I2C_DAT ) & 1;
  55. }
  56.  
  57. // Inits bitbanging port, must be called before using the functions below
  58. //
  59. void I2C_Init()
  60. {
  61.     I2C_PORT &= ~( ( 1 << I2C_DAT ) | ( 1 << I2C_CLK ) );
  62.  
  63.     I2C_CLOCK_HI();
  64.     I2C_DATA_HI();
  65.  
  66.     _delay_ms(1);
  67. }
  68.  
  69. // Send a START Condition
  70. //
  71. void I2C_Start()
  72. {
  73.     // set both to high at the same time
  74.     I2C_DDR &=  ~( ( 1 << I2C_DAT ) | ( 1 << I2C_CLK ) );
  75.     _delay_ms(1);
  76.    
  77.     I2C_DATA_LO();
  78.     _delay_ms(1);
  79.  
  80.     I2C_CLOCK_LO();
  81.     _delay_ms(1);
  82. }
  83.  
  84. // Send a STOP Condition
  85. //
  86. void I2C_Stop()
  87. {
  88.     I2C_CLOCK_HI();
  89.     _delay_ms(1);
  90.  
  91.     I2C_DATA_HI();
  92.     _delay_ms(1);
  93. }
  94.  
  95. // write a byte to the I2C slave device
  96. //
  97. unsigned char I2C_Write( unsigned char c )
  98. {
  99.     for ( char i=0;i<8;i++)
  100.     {
  101.         I2C_WriteBit( c & 128 );
  102.    
  103.      c<<=1;
  104.     }
  105.  
  106.     return I2C_ReadBit();
  107. }
  108.  
  109.  
  110. // read a byte from the I2C slave device
  111. //
  112. unsigned char I2C_Read( unsigned char ack )
  113. {
  114.     unsigned char res = 0;
  115.  
  116.     for ( char i=0;i<8;i++)
  117.     {
  118.         res <<= 1;
  119.         res |= I2C_ReadBit();  
  120.     }
  121.  
  122.     if ( ack > 0)
  123.     {
  124.         I2C_WriteBit( 0 );
  125.     }
  126.     else
  127.     {
  128.         I2C_WriteBit( 1 );
  129.     }
  130.  
  131.     _delay_ms(1);
  132.  
  133.     return res;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment