I2c bit banging, AT89C55WD, EEPROM BL24C04 interfacing

Discussion in 'Embedded Systems and Microcontrollers' started by Vindhyachal Takniki, Apr 16, 2016.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    1. I am using AT89C55WD(40 pin DIP), with 12Mhz crystal.
    Have to interface BL24C04 with it.

    2. System is +5V powered & I have made bit banging code in C for it.
    I have kept 50kbps speed. Took refereence from here while making code ( http://saeedsolutions.blogspot.in/2012/10/pic16f84a-i2c-bit-banging-code-proteus.html )

    3. I have simualted code in proteus & seems to be working as values shown in its I2c debugger.

    4. One issue I think may be its very small delay. At 50kbps , 1 bit delay is 20us, half bit delay is 10us & quarter bit delay is 5us. In codeI have used nop statements for 10us & 5us.

    5. In link above, blogger has used quarter(5us)/half(10us) but delay at many places. I dont know why quarter(5us) bit delay is used. Below is code. Is it ok?

    Code (C):
    1. sbit sda = P1^7;
    2. sbit scl = P1^6;
    3.  
    4. #define i2c_speed_kbps     50
    5. #define half_bit_delay_us  10     /* ( (1*1000000) / (i2c_speed_kbps*1000) )/ 2  */
    6. #define delay_10us()       _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    7. #define delay_5us()       _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    8.  
    9.  
    10. void all_tasks_manager(void)
    11. {    
    12.     uint8_t temp;
    13.  
    14.     i2c_init();
    15.  
    16. /* write to eeprom */
    17.     i2c_start();
    18.     while( 1U == i2c_write_byte(0xa0U) )
    19.     {
    20.         i2c_start();
    21.     }
    22. /* address */  
    23.     i2c_write_byte(0x00U);
    24. /* value */  
    25.     i2c_write_byte(0x05U);
    26. /* stop */  
    27.     i2c_stop();
    28.  
    29.     wait_delay_us(50000);
    30. /* write to eeprom */
    31.     i2c_start();
    32.     while( 1U == i2c_write_byte(0xa0U) )
    33.     {
    34.         i2c_start();
    35.     }
    36. /* address */  
    37.     i2c_write_byte(0x00U);  
    38.     i2c_restart();
    39.     i2c_write_byte(0xa1U);
    40.     temp = i2c_read_byte();
    41.     i2c_send_nack();
    42.  
    43.     i2c_stop();
    44.  
    45.     while(1);
    46.  
    47. } /* function ends here */  
    48.  
    49.  
    50. void i2c_init(void)
    51. {
    52.     sda = 1U;
    53.     scl = 1U;
    54. }
    55.  
    56. void i2c_start(void)
    57. {
    58.     scl = 1U;
    59.     sda = 1U;
    60.  
    61. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    62.     delay_10us();
    63.  
    64.     sda = 0U;
    65.  
    66. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    67.     delay_10us();
    68. }
    69.  
    70.  
    71. void i2c_restart(void)
    72. {
    73.     scl = 0U;              
    74.  
    75. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    76.     delay_5us();
    77.  
    78.     sda = 1U;
    79.  
    80. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    81.     delay_5us();
    82.  
    83.     scl = 1U;
    84.  
    85. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    86.     delay_5us();
    87.  
    88.     sda = 0U;
    89.  
    90. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    91.     delay_5us();
    92.  
    93. }
    94.  
    95.  
    96. void i2c_stop(void)
    97. {
    98.     scl = 0U;
    99.  
    100. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    101.     delay_5us();
    102.  
    103.     sda = 0U;
    104.  
    105. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    106.     delay_5us();
    107.  
    108.     scl = 1U;
    109.  
    110. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    111.     delay_5us();
    112.  
    113.     sda = 1U;
    114.  
    115. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    116.     delay_5us();
    117.  
    118. }
    119.  
    120.  
    121. void i2c_send_ack(void)
    122. {
    123.     scl = 0U;
    124.  
    125. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    126.     delay_5us();
    127.  
    128.     sda = 0U;
    129.  
    130. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    131.     delay_5us();
    132.  
    133.     scl = 1U;
    134.  
    135. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    136.     delay_10us();
    137.  
    138. }
    139.  
    140.  
    141. void i2c_send_nack(void)
    142. {
    143.     scl = 0U;
    144.  
    145. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    146.     delay_5us();  
    147.  
    148.     sda = 1U;
    149.  
    150. /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    151.     delay_5us();
    152.  
    153.     scl = 1U;
    154.  
    155. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    156.     delay_10us();
    157.  
    158. }
    159.  
    160.  
    161. uint8_t i2c_write_byte(uint8_t byte)
    162. {
    163.     uint8_t cnt;
    164.      
    165.     for(cnt = 0U ; cnt < 8U ; cnt++)
    166.     {
    167.         scl = 0U;
    168.     /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    169.         delay_5us();
    170.      
    171.      
    172.         if( (byte << cnt) & 0x80U )
    173.         {
    174.             sda = 1U;
    175.         }
    176.         else
    177.         {
    178.             sda = 0U;
    179.         }  
    180.  
    181.     /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    182.         delay_5us();
    183.      
    184.         scl = 1U;
    185.      
    186.     /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    187.         delay_10us();
    188.     }
    189.      
    190. /* get ack from slave */
    191.     scl = 0U;
    192.     sda = 1U;
    193.  
    194. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    195.     delay_10us();
    196.  
    197.     scl = 1U;
    198.  
    199. /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    200.     delay_10us();
    201.  
    202.     return sda;
    203. }
    204.  
    205.  
    206. uint8_t i2c_read_byte(void)
    207. {
    208.     uint8_t cnt;
    209.     uint8_t rxdata = 0U;
    210.     uint8_t temp;
    211.  
    212.     for(cnt = 0U ; cnt < 8U ; cnt++)
    213.     {
    214.         scl = 0U;
    215.         sda = 1U;
    216.      
    217.     /* half bit delay for 50kbps which is 10us for 12Mhz it is 10 nops */  
    218.         delay_10us();    
    219.      
    220.         scl = 1U;
    221.      
    222.     /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    223.         delay_5us();
    224.      
    225.         temp = sda;
    226.         rxdata = rxdata | (temp << (7U - cnt));  
    227.      
    228.     /* half bit delay/2 for 50kbps which is 5us for 12Mhz it is 5 nops */  
    229.         delay_5us();
    230.      
    231.     }
    232.     return rxdata;                      
    233. }
     
    Last edited by a moderator: Apr 16, 2016
  2. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Without seeing the assembly language output from you compiler I can't say for sure that you code will or won't work. If the part is a variation of the stock 8051 with 1 μsec instruction time @ 12 MHz., then I seriously doubt that you can make this work -- reliably.
     
  3. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    yes it is a part variation of 8051, why would you think it will not work??
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Because at 12 MHz. each instruction takes 1 μsec to execute. Your entire loop needs to be done in 10 instructions at that rate. There are some variations of the 8051 that take less than 12 clocks per instruction cycle, but I could not immediately tell if this was one of them.
     
  5. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    so will it work? i saw some code examples also put while loop for clock streching , do I need it here in interfacing bl24c04 eeprom?
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    I tried to answer that question, but I can not do that without more information. I need two things:
    1. The assembly language output of the compiler
    2. The timing of the instructions on the processor with a 12 MHz. clock
    Without those two things your guess about working or not working is as good as mine.
     
  7. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    Attached is complete keil code named "test.rar".
    Also listing file is in "main.rar" which has assembly output

    1. Osc is 12Mhz. It has 12 cycles, so 1us single instruction timing. i.e single nop takes 1us
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    I don't know if I can handle a .rar file. I'll get back to you.
     
  9. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,347
  10. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Why don't you try it on real hardware?

    At this point, you have done all you could with simulation and all left to do is to put it on real hardware and see how / if it works.
     
  11. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
  12. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If the processor timing really is 1usec per instruction I don't see how you can lose. For all the states and transitions listed, the timing criteria for the memory chip are slower than that. Looking in the other direction (meaning, how slow can the interface be) there seems to be no minimum speed for any operation.

    But I2C involves open-collector lines with pullups. I'm a PIC guy and I know a PIC processor can work that way, by manipulating the bits in the TRIS register, but I don't know about this 8051 variant. You need to be able to make your processor pins be driven low, or go hi-Z. Can they do that, and are you controlling the chip correctly?
     
  13. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Bit banging is hard enough to do in straight assembly language, but using a compiler is like tying both hands behind your back.

    Looking at the timing I can tell you it won't work the way you expect. The NOP's you inserted are completely useless. If you take them out completely you might actually be closer to your performance goal. I estimate that your bit times with the NOPs will be in the neighborhood of 42 μsec, which is twice what you expected them to be. In addition, the time it takes to send a byte will depend on the number of 1's and 0's in the byte. This is a very bad business since you will never be able to use a scope to look at the timing because it will be all over the map as the data changes. You should at least be able to observe SCL as a stable trace from it's initial edge.

    The listing was helpful, but the compiler is playing some games with conditional jumps. I think it is smart enough to use relative jumps over short ranges. The basic bit time is computed by counting instructions from ?C0014 to the xJNE instruction just above ?C0015. I'm also assuming that symbols like AR4 and AR7 refer to direct memory locations the compiler uses to hold function arguments.

    You must have assumed that the actual instructions were executed in zero time, which is what I originally suspected.

    The bit banging example you tried to emulate is for a situation where the bit time is orders of magnitude larger than the instruction time. It does not apply in this case.
     
  14. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    @John P
    The 8051 port output structure has an active pulldown output with a weak pullup (~40K-80K) which can be externally forced low. If you look at the I2C specification this pullup is too large to quickly pull SDA/SCL up from the low state, so an external pullup is a must. @cmartinez and I had an extensive discussion this about 3 or 4 months back. He specifically was interested in the magnitude of the 8051 "weak" pullup.
     
    cmartinez likes this.
  15. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Code (Text):
    1.  
    2.  
    3. [*]#define delay_10us()       _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    4. [*]#define delay_5us()       _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    This type of macro writing is generally not preferred. Think about someone using this:

    Code (Text):
    1.  
    2.   if (true) delay_10us();
    3.  
    What do you think will happen?

    Using a "do...while" loop around it to avoid that kind of mistakes.
     
  16. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    I agree, but this misses the point that no delay is required in order to achieve his performance objective. In fact it is killing any chance he has of making it.
     
  17. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    3,574
    2,543
    Some chips are sync-critical when performing SPI communications, in the sense that the clock has to have an almost perfect 50% duty cycle and a steady frequency. An example of this are some ADC's out there that perform their sampling cycle based on the SPI clock.
    However, since you're talking about an EEPROM, most likely it's not sync-critical, and the only restriction might be the maximum clock frequency, and not it's minimum, as John_P has already mentioned. Also, external pull up resistors might be needed, as Papabravo has already stated too.
    I would need to take a look at both chip's datasheets to tell you more.
     
  18. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    I have checked on hardware, MCU: AT89C55WD, 12Mhz crsytal & BL24C04.
    It is working.

    For me exact performance is not required, because I have to read it only once on startup & write only once.
     
  19. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    @Papabravo,
    If perofrmance not required then is code correct?
     
  20. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Didn't you just say that the code worked on real hardware?
     
Loading...