Problem interfacing pic16f1936 with i2c serial eeprom 24lc01b

Discussion in 'Embedded Systems and Microcontrollers' started by Interfacer, Sep 6, 2016.

  1. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    I'm trying to read and write to an i2c serial eeprom (24lc01b) from a pic16f1936 to test functionality on an existing PCB design that has been implemented already. I'm using a library I got from the microchip website that was intended for the 16f877 using the hitech c compiler (Lite Mode). I just renamed constants (e.g. PSPIF to SSPIF; STAT_CKE to CKE) in the header file so it could compile. It compiles, I upload it to the board and no joy... Here's the header file:

    Code (Text):
    1.  
    2. /******************************************************************************************/
    3. // Hardware I2C single master routines for PIC16F877
    4. // for HI-TEC PIC C COMPILER.
    5. //
    6. // i2c_init  - initialize I2C functions
    7. // i2c_start - issue Start condition
    8. // i2c_repStart- issue Repeated Start condition
    9. // i2c_stop  - issue Stop condition
    10. // i2c_read(x) - receive unsigned char - x=0, don't acknowledge - x=1, acknowledge
    11. // i2c_write - write unsigned char - returns ACK
    12. //
    13. // modified from CCS i2c_877 demo
    14. //
    15. // by
    16. //
    17. // Michael Alon - MICROCHIP FAE
    18. // My E-mail michael@elina-micro.co.il
    19. // www.elina-micro.co.il
    20. // Tel: 972-3-6498543/4
    21. // Fax: 972-3-6498745
    22. // Elina Micro LTD .ISRAEL
    23. //
    24. /******************************************************************************************/
    25.  
    26. void i2c_init()
    27. {
    28.   TRISC3=1;           // set SCL and SDA pins as inputs
    29.  TRISC4=1;
    30.  
    31.   SSPCON = 0x38;      // set I2C master mode
    32.  SSPCON2 = 0x00;
    33.  
    34.  //file://SSPADD = 0x6;     // 400kHz bus with 10MHz xtal - use 0x0C with 20MHz xtal
    35.  SSPADD = 10;            // 100k at 4Mhz clock
    36.  
    37.  CKE=1;     // use I2C levels      worked also with '0'
    38.  SMP=1;     // disable slew rate control  worked also with '0'
    39.  
    40.  SSPIF=0;      // clear SSPIF interrupt flag
    41.  BCLIF=0;      // clear bus collision flag
    42. }
    43.  
    44. /******************************************************************************************/
    45.  
    46. void i2c_waitForIdle()
    47. {
    48.  while (( SSPCON2 & 0x1F ) | R_nW ) {}; // wait for idle and not writing
    49. }
    50.  
    51. /******************************************************************************************/
    52.  
    53. void i2c_start()
    54. {
    55.  i2c_waitForIdle();
    56.  SEN=1;
    57. }
    58.  
    59. /******************************************************************************************/
    60.  
    61. void i2c_repStart()
    62. {
    63.  i2c_waitForIdle();
    64.  RSEN=1;
    65. }
    66.  
    67. /******************************************************************************************/
    68.  
    69. void i2c_stop()
    70. {
    71.  i2c_waitForIdle();
    72.  PEN=1;
    73. }
    74.  
    75. /******************************************************************************************/
    76.  
    77. int i2c_read( unsigned char ack )
    78. {
    79.  unsigned char i2cReadData;
    80.  
    81.  i2c_waitForIdle();
    82.  
    83.  RCEN=1;
    84.  
    85.  i2c_waitForIdle();
    86.  
    87.  i2cReadData = SSPBUF;
    88.  
    89.  i2c_waitForIdle();
    90.  
    91.  if ( ack )
    92.   {
    93.   ACKDT=0;
    94.   }
    95.  else
    96.   {
    97.   ACKDT=1;
    98.   }
    99.   ACKEN=1;               // send acknowledge sequence
    100.  
    101.  return( i2cReadData );
    102. }
    103.  
    104. /******************************************************************************************/
    105.  
    106. unsigned char i2c_write( unsigned char i2cWriteData )
    107. {
    108.  i2c_waitForIdle();
    109.  SSPBUF = i2cWriteData;
    110.  
    111.  return ( ! ACKSTAT  ); // function returns '1' if transmission is acknowledged
    112. }
    113.  
    114. /******************************************************************************************/
    115.  
    116.  
    117.  
    118.  
    and here's a snippet of my code:

    Code (Text):
    1.  
    2.  
    3. #include <htc.h>
    4. #include "877_i2c.h"
    5.  
    6.    
    7. **************************** CONFIG FUSES *****************************/
    8. //Set-up in MPLAB IDE...
    9.  
    10. void Delay10xUs(unsigned char );
    11. void    Delay10Us(void);  
    12. void setup_main(void);
    13. void led(unsigned char);
    14. void ledflash_slow(unsigned char);
    15. void ledflash_fast(unsigned char);
    16.  
    17.  
    18.  
    19. unsigned char cByteToRead;
    20.  
    21. //use these defintions for the ee library
    22. //#define SCL            RC3
    23. //#define SDA            RC4
    24.  
    25.  
    26. /////////////Delay Routines - based on 8MHZ internal clock///////////////
    27.  
    28.     char _dcnt, _cnt;
    29.  
    30. void    Delay10Us()
    31.     {_dcnt=1;
    32.     while(--_dcnt)continue;
    33.     }
    34.    
    35. void    Delay10xUs(unsigned char x)
    36. {
    37. _cnt=x;
    38. while(--_cnt)
    39. Delay10Us();
    40. }
    41.  
    42. void DelayMs(unsigned char p)
    43. {
    44.     do
    45.     {
    46.         Delay10xUs(16);
    47.         Delay10xUs(16);
    48.         Delay10xUs(16);
    49.         Delay10xUs(16);
    50.     }while(--p != 0);
    51. }
    52.  
    53. void Delay250Ms(unsigned char q)
    54. {
    55.     do
    56.     {
    57.         DelayMs(250);
    58.     }while(--q != 0);
    59. }
    60.  
    61. void DelayS(unsigned char r)
    62. {
    63.     do
    64.     {
    65.         Delay250Ms(4);
    66.     }while(--r != 0);
    67. }
    68.  
    69. /////////////End Delay Routines///////////////
    70.  
    71.  
    72. /******************************************************************************************/
    73.  
    74. void write_ext_eeprom(unsigned char address, unsigned char data)
    75.  {
    76.    i2c_start();
    77.    i2c_write(0xa0);
    78.    i2c_write(address);
    79.    i2c_write(data);
    80.    i2c_stop();
    81.    DelayMs(11);
    82. }
    83.  
    84. /******************************************************************************************/
    85.  
    86. unsigned char read_ext_eeprom(unsigned char address)
    87. {
    88.    unsigned char data;
    89.  
    90.    i2c_start();
    91.    i2c_write(0xa0);
    92.    i2c_write(address);
    93.    i2c_repStart();
    94.    i2c_write(0xa1);
    95.    data=i2c_read(0);
    96.    i2c_stop();
    97.    return(data);
    98. }
    99.  
    100. /******************************************************************************************/
    101.  
    102.  
    103. void main(void)
    104. {
    105. setup_main();
    106. write_ext_eeprom(0x00, 0x01);
    107.  
    108. DelayS(1);
    109.  
    110.    
    111. cByteToRead= read_ext_eeprom(0);
    112.  
    113.  ledflash_fast(cByteToRead);
    114. DelayS(1);
    115. }
    116.  
    117. void setup_main(void)
    118. {
    119. OSCCON=0b11110010;    //To select internal oscillator at 8MHz
    120. ANSELA=0X02;     //Set only Pin 28 in Port A (RA1 - Bump input) to analog input
    121. ANSELB=0;         //Set all analog pins in Port B to digital I/O
    122. TRISA=0X3F;        //Set RA6 and RA7 to outpur and the rest (RA5-0) to inputs
    123. TRISB=0XFF;        //Set all I/O pins on PortB to inputs
    124. TRISC=0;        //Set all I/O pins on PortC to outputs
    125. PORTC=0;
    126. }
    127.  
    128.  
    129.  
    Expecting an led to flash once... nothing... But the ledflash routine is working because I use it elsewhere in the program. It's as if it is stuck. Any code after trying to read eeprom does not run. I'm certain all the hardware is ok because when the micro is loaded with firmware. The EEPROM does it's job.
    Notes:
    - No pull up on SCL Line (must work without because other software makes it work);
    - Internal oscillator used @8MHz (set in software using OSCCON register) - seems to work fine

    Suspecting some interrupt related problem affecting the eeprom read routine. Like its getting stuck waiting for something from the eeprom. Maybe pic16f1936 interrupt settings are different (or does MSSP settings override?)
    Please help.
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    Wow. You may want to read up on i2c first.

    Fat chance you can get it to work without pull up.
     
  3. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    If your software is using I2C according to the specification then both lines need a pull-up.
     
  4. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    First off. Thanks for the reply. I know for a fact that it works without this. As I said before, it is a fixed hardware design (product is on the market) therefore I cannot make any hardware changes (trying to design hardware test firmware), and it works with original firmware on the pic (I only have hex file - no source code). If you can't figure out why it is not working then we both might learn something new :) My first guess was maybe internal pull up?

    A more pertinent question - has anyone tried something like this without the pull-up on on SCL? If I had spare parts I'd test this on a bread board. I'm more concerned with the validity of this code and correct setting up of the registers. I'm new to pic (used to arduino), and learning how to PROPERLY use new registers can be a daunting task, hence why I looked online for a library that would need minimal changes to make work.

    By the way, the serial eeprom is a 5-Lead SOT-23 if that has any significance.
     
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Some older ApNotes show SCL actively driven by the port instead of using a pullup. That can work if none of the I2C slaves ever have to stretch the clock to interrupt sending of data by the master. Most EEPROMs have page write buffers and do not stretch the clock so you see more active SCL in that application - I've used it myself in bit-banged I2C. But with anything else on the bus, including I2C analyzers, PIC MSSP slaves etc. you must conform to I2C spec. As the others have indicated, your HiTech library AND the Master Mode MSSP probably expects the pullup. 4.7K is usually a good starting point.

    In your code I don't see you calling i2c_init(). That is necessary to config SCL/SDA as inputs. Be sure that the corresponding bits in the PORT registers are '0' - flipping TRIS bits is how most I2C libraries (with 2 pullups) work.
    The read/write sequences look OK.
    Note the the library code you show is blocking in nature. That means that problems will tend to lock up your code flow while the blocking code waits forever on something that won't happen. Combined with probably looking at the wrong register, the code below has the potential to hang things if something is preventing the IDLE conditon (SCL==SDA==1 after a stoP).

    Code (C):
    1. /******************************************************************************************/
    2. void i2c_waitForIdle()
    3. {
    4. while (( SSPCON2 & 0x1F ) | R_nW ) {}; // wait for idle and not writing
    5. }
    PSPIF is associated with the Parallel Slave Port on the '877. Not sure why it appears in I2C code.

    If you don't have a scope it will be hard to debug this. You might be able to do some simple stuff without it such as check for SCL, SDA =1 after init, SCL=0 after sending start etc.

    Good luck.
     
  6. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    You could drive SCL high (i.e. output logic '1') instead of letting it float if you know that the devices on the I2C don't use clock stretching.
    You will need to modify the I2C library to do that.
    The part of library code you have shown includes these two lines:
    TRISC3=1; // set SCL and SDA pins as inputs
    TRISC4=1;
    If there is no pull up on SCL then whichever of C3 and C4 is SCL is going to be left floating at an undefined level by this code. The I2C lines should be fed by open drain and the way this is done with the pic is to switch them between input (pulled high by the resistor) and output (with the output set to '0'.
    You will have to go through the library code and change it so that instead of setting the SCL pin as an input, set it to output '1'.

    The danger with that is if a device uses clock stretching then your master will be driving SCL high while the peripheral is pulling it low - not a good situation.

    PS I see a reply (JohnInTX) while I was writing this. We agree.
     
    Interfacer and JohnInTX like this.
  7. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    Thanks JohnInTX. I did not notice that i2c_init() was missing there. I added it before calling eeprom routines. Now I'm seeing led flashing, but it is not flashing/reading the number i stored in address 0x00 (1). I think I stopped counting after 50. As for the blocking, maybe I can add a timeout routines to the library code (I don't know how long this would have to be though). You mentioned PSPIF register. Recall that I changed this to SSPIF thinking that that was the corresponding register for the pic16f1936. I was really thinking that I made a mistake by/when translating this library for 16f1936. Is that still a possibility looking at the header file I uploaded?

    AlbertHall's idea sounds risky because I'm not sure about clocking stretching capabilities of the eeprom chip, and seems less like that is the problem now.

    Any ideas as to what I can try to debug? The hardware is definitely working.
     
  8. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    @AlbertHall gives a good description of what you have to do if you are bit banging. Your code makes mention of using the MASTER mode (where the MSSP generates the I2C clock by itself) but your init does not show anything that configures SSPCON1 to set up master mode. If you are using the I2C master mode (as opposed to bit-banging as a master) you must use the pullups. That's how it works. The I2C master peripheral toggles SDA and SCL by switching the lines from an active low to HiZ with the pullup. That allows other I2C devices to stretch the clock, perform bus arbitration and the like. Its time to scab a resistor onto SCL.

    What do you have to debug with? A 2 channel DSO is about the minimum. Without a scope you can do things like executing one line at a time and look at the voltages on the pins i.e. before sending Start both SCL and SDA =1. After sending Start (but before anything else) check that SCL is low. Stuff like that.

    As far as using libraries, I personally write my own for the reasons you are seeing, especially when moving to similar but not exact peripherals. You don't know how good the author was, whether his constraints are compatible with yours (no blocking code, for example) etc. Finally, grabbing a library from somewhere in the hopes that you can get something working without really understanding what is going on is nearly always doomed to failure because when it doesn't work, you don't know where to start looking.

    So, do you have a DSO?
     
    Interfacer likes this.
  9. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    Have you checked the eeprom is address A0 , the A0-A2 pins at 0v
    edit My error no A0-2 on the 24xx01
     
    Last edited: Sep 6, 2016
  10. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    The posted code does include
    SSPCON = 0x38; // set I2C master mode
    SSPCON2 = 0x00;

    The pic16f1936 does not have a register called SSPCON. There are three SSPCON registers numbered 1,2 and 3 but maybe the header defines SSPCON as SSPCON1, in which case the above code does look like it sets the pic to use the built in I2C hardware which definitely won't work without a pull-up resistor.

    If you want to use I2C without that resistor you cannot use the pics hardware, it will need to be bit banged. There is code on www that will do this.
     
    JohnInTX likes this.
  11. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    @AlbertHall
    After some consideration, I will retry this code with pullup when I get the parts (It will probably be with a 18f252 though). For now I want to shift the focus to the 'bit banging' option. I assume this means a software i2c routine independant of i2c hardware peripheral built into the pic. I got another library from the microchip site that looked something like this:
    Code (Text):
    1.  
    2.  
    3. #include    <pic.h>
    4. #include    "ee.h"
    5.  
    6. #define    SDA        RC4
    7. #define    SCL        RC3
    8. #define    TRUE    1
    9. #define    FALSE    0
    10.  
    11.  
    12. /******************************************************************************/
    13. /***                           24C01A routines                              ***/
    14. /******************************************************************************/
    15. /******************************************************************************/
    16. /*Name          : ee_delay                                                    */
    17. /*Description   : Dummy procedure that delays for eerom                       */
    18. /*                                                                            */
    19. /*Author        : Cameron Pearce                                              */
    20. /*Date          : 24/08/98                                                    */
    21. /*Version       : 1.0                                                         */
    22. /*                                                                            */
    23. /*Inputs        : none                                                        */
    24. /*Outputs       : none                                                        */
    25. /*Destroys      : nothing                                                     */
    26. /*Globals       : none                                                        */
    27. /*                                                                            */
    28. /*Comments      :                                                             */
    29. /*Uses          : a byte                                                      */
    30. /******************************************************************************/
    31. void ee_delay(void)
    32. {byte delay;
    33.  
    34.  for(delay=0;delay<10;delay++);
    35.  
    36. }
    37. /******************************************************************************/
    38. /*Name          : ee_byte_to_ee                                               */
    39. /*Description   : Places the dat byte at address in the EEROM                 */
    40. /*                                                                            */
    41. /*Author        : Cameron Pearce                                              */
    42. /*Date          : 24/08/98                                                    */
    43. /*Version       : 1.0                                                         */
    44. /*                                                                            */
    45. /*Inputs        : dat, the byte written                                       */
    46. /*                address, the byte address of the data                       */
    47. /*Outputs       : Returns True if write was successful                        */
    48. /*Destroys      : nothing                                                     */
    49. /*Globals       : none                                                        */
    50. /*                                                                            */
    51. /*Comments      :                                                             */
    52. /*Uses          : nothing                                                     */
    53. /******************************************************************************/
    54. bit ee_byte_to_ee(byte address, byte dat)
    55. {
    56. /* Returns True if write was successfully acknowledged */
    57.  ee_start();                /* Send start sequence */
    58.  
    59.  ee_byte(0xa0);             /* Control byte (WRITE) */
    60.  
    61.  
    62.  if (ack()) return(FALSE);     /* Test Acknowledge    */
    63.  
    64.  ee_byte(address);           /* Address Byte */
    65.  
    66.  if (ack()) return(FALSE);
    67.  
    68.  ee_byte(dat);               /* Data byte */
    69.  
    70.  if (ack()) return(FALSE);
    71.  
    72.  SCL=0;
    73.  ee_delay();
    74.  SDA=0;                     /* Stop condition */
    75.  TRISC4=0;                    /* SDA OUTPUT     */
    76.  ee_delay();
    77.  SCL = 1;
    78.  ee_delay();
    79.  SDA = 1;
    80.  ee_delay();
    81.  return(TRUE);
    82. }
    83.  
    84. /******************************************************************************/
    85. /*Name          : ee_byte                                                     */
    86. /*Description   : transmits the x byte to the eerom                           */
    87. /*                                                                            */
    88. /*Author        : Cameron Pearce                                              */
    89. /*Date          : 25/08/98                                                    */
    90. /*Version       : 1.0                                                         */
    91. /*                                                                            */
    92. /*Inputs        : x, the byte sent to the EEROM                               */
    93. /*Outputs       : none                                                        */
    94. /*Destroys      : nothing                                                     */
    95. /*Globals       : none                                                        */
    96. /*                                                                            */
    97. /*Comments      :                                                             */
    98. /*Uses          : 1 int                                                      */
    99. /******************************************************************************/
    100. void ee_byte(byte x)
    101. {int i;
    102.  
    103.  i=0x80;
    104.  do
    105.   {
    106.    SCL=0;
    107.    TRISC4 =0;                /* SDA OUTPUT     */
    108.    ee_delay();
    109.    if (x&i) SDA=1;
    110.     else SDA=0;
    111.    ee_delay();
    112.    SCL=1;
    113.    ee_delay();
    114.    i>>=1;
    115.   }while(i!=0);
    116. }
    117. /******************************************************************************/
    118. bit ack(void)
    119. {
    120.  SCL=0;                 /* Acknowledge         */
    121.  ee_delay();
    122.  TRISC4=1;                /* SDA Input        */
    123.  ee_delay();
    124.  SCL=1;
    125.  ee_delay();            /* Test Acknowledge    */
    126.  return(SDA);
    127. }
    128. /******************************************************************************/
    129. /*Name          : ee_byte_from_ee                                             */
    130. /*Description   : Gets a byte at address from the EEROM                       */
    131. /*                                                                            */
    132. /*Author        : Cameron Pearce                                              */
    133. /*Date          : 25/08/98                                                    */
    134. /*Version       : 1.0                                                         */
    135. /*                                                                            */
    136. /*Inputs        : dat, a pointer to the byte read                             */
    137. /*                address, the byte address of the data                       */
    138. /*Outputs       : Returns True if read was successful                         */
    139. /*Destroys      : nothing                                                     */
    140. /*Globals       : none                                                        */
    141. /*                                                                            */
    142. /*Comments      :                                                             */
    143. /*Uses          : nothing                                                     */
    144. /******************************************************************************/
    145. bit ee_byte_from_ee(byte address,byte *dat)
    146. {
    147. /* Returns True if read was successfully acknowledged */
    148.  
    149.  ee_start();                /* Send start sequence */
    150.  
    151.  ee_byte(0xa0);             /* Write Control byte */
    152.  
    153.  if (ack()) return(FALSE);
    154.  
    155.  ee_byte(address);           /* Address Byte */
    156.  
    157.  if (ack()) return(FALSE);
    158.  
    159.  SCL=0;
    160.  ee_delay();
    161.  SCL=1;
    162.  ee_delay();
    163.  SDA=0;                     /* Start Condition */
    164.  TRISC4=0;                    /* SDA OUTPUT       */
    165.  ee_delay();            
    166.  
    167.  ee_byte(0xa1);          /* Read Control byte */
    168.  
    169.  if (ack()) return(FALSE);
    170.  
    171.  ee_data(dat);           /* Get the data byte */
    172.  
    173.  ack();
    174.  
    175.  SCL=0;                  /* Stop condition */
    176.  ee_delay();
    177.  SDA=0;
    178.  TRISC4=0;                /* SDA OUTPUT       */
    179.  ee_delay();
    180.  SCL=1;
    181.  ee_delay();
    182.  SDA=1;
    183.  return(TRUE);
    184. }
    185. /******************************************************************************/
    186. /*Name          : ee_data                                                     */
    187. /*Description   : receives a byte from the eerom                              */
    188. /*                                                                            */
    189. /*Author        : Cameron Pearce                                              */
    190. /*Date          : 27/08/98                                                    */
    191. /*Version       : 1.0                                                         */
    192. /*                                                                            */
    193. /*Inputs        : x, a pointer to the byte received                           */
    194. /*Outputs       : none                                                        */
    195. /*Destroys      : nothing                                                     */
    196. /*Globals       : none                                                        */
    197. /*                                                                            */
    198. /*Comments      :                                                             */
    199. /*Uses          : 1 int                                                      */
    200. /******************************************************************************/
    201. void ee_data(byte *x)
    202. {int i;
    203.  
    204.  TRISC4=1;        /* SDA INPUT */
    205.  *x=0;
    206.  i=0x80;
    207.  do
    208.   {
    209.    SCL=0;
    210.    ee_delay();
    211.    SCL=1;
    212.    ee_delay();
    213.    if (SDA) *x=(*x)|i;
    214.    i>>=1;
    215.   }while(i!=0);
    216. }
    217. /******************************************************************************/
    218. void ee_start(void)
    219. {
    220.     TRISC3 = 0;
    221.     TRISC4 = 0;            /* make SCL and SDA outputs     */
    222.  SDA = 0;
    223.  ee_delay();
    224.  SCL = 1;
    225.  ee_delay();
    226.  SDA = 1;
    227.  ee_delay();             /* Stop condition */
    228.  
    229.  SDA=0;
    230.  ee_delay();             /* Start Condition */
    231. }
    232.  
    233. void MasterAck(void)
    234. {
    235.  SCL=0;                 /* Acknowledge         */
    236.  ee_delay();
    237.  SDA=0;
    238.  TRISC4=0;                /* SDA Output        */
    239.  ee_delay();
    240.  SCL=1;
    241.  ee_delay();          
    242.  SCL=0;
    243. }
    244.  
    245. void ee_RepeatStart(void)
    246. {
    247.  SCL = 0;
    248.  ee_delay();
    249.  TRISC3 = 0;
    250.  TRISC4 = 0;            /* make SCL and SDA outputs     */
    251.  ee_delay();
    252.  SDA = 1;
    253.  ee_delay();
    254.  SCL = 1;
    255.  ee_delay();
    256.  SDA = 0;                        /* Restart now! */
    257. }
    258.  
    259.  
    260.  
    Now. I looked at the datasheet for bus timing on master write and reception and compared it to timing from this code (I stepped through the whole library and generated some timing diagrams that were identical to those in the datasheet). This code didn't work either after running it. I suspected timing issue so I commented out the last two 'return FALSE' statements in the ee_byte_to_ee(byte address, byte dat). It seems I'm not even getting an acknowledge after sending the slave address (0xa0). I thought that maybe it is running to fast so I even decreased the internal oscillator frequency to 4MHz (Eeprom max speed is 400KHz so is the uC slow enough by looking at ee_delay routine?).

    There is one thing that could also add some insight. Is it safe to say reading ack between rising and falling edge of the 9th clock pulse is correct? Furthermore will the slave (24lc01b) keep the SDA line low until it gets rising/falling edge from SCL that is driven by the master.

    I'm sure we can agree that the pullup on SCL line should not be a problem now. If I can get the first acknowledge, that would be a good start I think.
     
  12. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    The code as written uses the (correct) open drain outputs for SCL and SDA so this code will need a pull-up resistor. To use it without a resistor you will need to modify it to output a logic '1' on SCL instead of switching the pin to an input (allowing it to float).

    I don't know how long the delay routine will take. It will differ depending on what code the compiler produces for this routine. Do you have a simulator (e.g. MPSIM) which can measure the actual delay time?
     
  13. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    Fat chance.

    You should really read up on the i2c protocol before attempting to code to it.
     
  14. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    finally got it working after polling for ack instead of using delays and sampling the SDA line. I was using MPLABX 3.35 at this time (at home). I copied the source files and compiled at work (using MPLAB 8.36) successfully. Back to the problem of no ack!!!? I use the same version compiler hitech 9.65 Lite mode for both IDEs though. Is it possible for this to be a complication?
     
  15. Interfacer

    Thread Starter New Member

    Dec 15, 2012
    9
    0
    My mistake. the compiler at home was hi-tech v9.60. I tried it at work with v9.65 using mplabx and no joy. The problem was the compiler. Now I think I understand why microchip did away with this compiler. Using xc8 now for this application and it works perfectly as well. Perhaps I didn't even have to poll ack? I will try it and let you know. @dannyf the Phillips i2c standard does not need to be implemented to the nail on all devices. A generic software i2c would be a challenge I agree, but the datasheets for the device in question is a better guide. #StopDemotivatingNovices
     
  16. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    The IDE shouldn't matter if the compiler's project properties are set the same. If I had to guess, I would say that the different compiler versions generate different enough delay code to change the delay. Also the professional and 'lite' versions of the compiler may have different optimization levels.

    Delays like Delay10Us() that take the clock into account are a better option. If your compiler library doesn't have one you could fake the short delays needed with inline assembler within the delay function.
    #asm
    nop
    nop
    ..
    #endasm
    Check the compiler manual for the exact syntax. Note that if you want to get fancy, the delay between SDA and SCL for Start/stoP and data setup is shorter than the SCL width so you might use different delay routines for different things.

    You need to be able poll ACK/NAK on the 9th SCL. Set SCL =1 and read SDA then set SCL=0; Besides knowing that the slave is actually there, its a way to know if previous writes have completed. If the EEPROM is still writing (it takes a few ms) you will get a NAK after writing the first address. When that happens, you write a stoP and try again later. You also could send rS I suppose but stoP is better.

    The code shown is old and bare-bones. It might be a nice starting point but you'll likely have to punch it up a bit for a practical, robust interface. For example, the 24LC01 like a lot of I2C devices can get out of sync if you don't send it the proper number of SCL (like resetting the master in the middle of a transaction). Sometimes this results in SDA seemingly being 'stuck low'. To recover, be prepared to send a recovery sequence of up to 9 SCL, polling for SDA=1 each time. Then send stoP.

    Good luck!
    #StopDemotivatingNovices +1
     
Loading...