• AVR Freaks

I2C and SPI shared same pins

Page: 12 > Showing page 1 of 2
Author
moahrs
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2011/12/07 04:28:51
  • Location: 0
  • Status: offline
2012/09/13 07:40:51 (permalink)
0

I2C and SPI shared same pins

Hi all.
 
I am braziliam, then sry for my english.
 
I need to use the same pins for 2 memorys, 23K256 (Serial RAM with SPI) and 24C32 (Serial EEPROM with I2C). I use 16F877A with hi-tech compiler. I mounted the circuit in below, but not function. I isolated the SCL/SDA from/to 24C32 with 3 ports from 74LS126, activated with signal HIGH. Then I used pin RA5 to activate CS in 23K256 (when low) and activated 74LS126 in 24C32 (when high). 
 
I am using 877_i2c.h to use comunications. I am not tryed to use SPI, this is my second preocupation. I am not sure if the problem is in my priogram, but I am thinking that can be in circuit. I dont know if using 74LS126 to isolate 24C32 can be a good idea.
 
 
 
Ty a lot.
Moacir Jr.
 
post edited by moahrs - 2012/09/13 07:43:55
#1

22 Replies Related Threads

    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 08:50:57 (permalink)
    0
    You can't do it that as you have back-to-back TTL buffers with no direction control which will jam the bus.
     
    Try a 74HC4066 quad analog switch + a inverter.  The inverter is used to make the switch function as a DPST (dual changeover) switch.
    #2
    JorgeF
    Super Member
    • Total Posts : 3341
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 08:57:52 (permalink)
    0
    Hi
     
    I never tried with a mix of buses I2C and SPI.
    But with 2 or more devices in a SPI bus, its common to use the CS (chip select) line to select the active device at a given moment.
    Usually buses like I2C and SPI have their lines tri-stated (high impedance) when the chip is not selected, so the bus can be shared by multiple devices.
     
    Take a good look at the datasheets for your serial memories, maybe you can do without any external hardware.
     
    EDIT:
    For the 25LC256 (SPI EEPROM) and the MCP23S17 (SPI bus expander) on board a PIC 18 Explorer board this CS approach works like a breeze.
     
     
    Best regards
    Jorge
     
    post edited by JorgeF - 2012/09/13 09:06:15
    #3
    WaltR
    Super Member
    • Total Posts : 3770
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 09:03:55 (permalink)
    0
    Yep as Ian states.
    I2C requires open-collect (open-drain) outputs for I2C. So either an analog switch or open-drain as a buffer is needed.
     
    As to your code. Get the SPI device working without the I2C circuits connected then get the I2C working without the SPI device working.
    Last, use the correct buffers and get both working (only hardware at this point since the code for each of SPI and I2C works).
    #4
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 09:35:54 (permalink)
    0
    Ian.M

    You can't do it that as you have back-to-back TTL buffers with no direction control which will jam the bus.

    Try a 74HC4066 quad analog switch + a inverter.  The inverter is used to make the switch function as a DPST (dual changeover) switch.

    I understood, I have used 4066 in others projects, and I know this IC. I dont understand is the inverter... how the mecanics works ? The inverter will not distorce the signal ?
     
     
    Ty a lot.
    Moacir Jr.
     
    #5
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 09:43:17 (permalink)
    0
    WaltR

    Yep as Ian states.
    I2C requires open-collect (open-drain) outputs for I2C. So either an analog switch or open-drain as a buffer is needed.

    As to your code. Get the SPI device working without the I2C circuits connected then get the I2C working without the SPI device working.
    Last, use the correct buffers and get both working (only hardware at this point since the code for each of SPI and I2C works).

     
    I will do this test. Fisrt only with i2c, after only with spi, and after, puting analog switch (4066), and see if all works...
     
     
     
    ty a lot.
    Moacir Jr.
     
    #6
    Ian.M
    Super Member
    • Total Posts : 13268
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 09:58:03 (permalink)
    0
    The inverter is to invert the control signal to two of the 4066 enable pins so when two switches are open, the other two are closed.
    #7
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/13 10:12:13 (permalink)
    +2 (1)
    Ian.M
    The inverter is to invert the control signal to two of the 4066 enable pins so when two switches are open, the other two are closed.

     
    Oh yeah... it's true. If I am undertstood correctly, I will use only 2 analog switchs. 1 for SCL signal and other for SDA signal. In the case of SPI, I dont need analog swiths, because I have CS pin in device. Am I right ?
     
     
    ty a lot
    Moacir Jr.
     
    #8
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/14 04:00:12 (permalink)
    0
    I switched the 74LS126 by 4066.
     
    I Tested only with I2C in circuit, and nothing. Dont function. I think wich can be the program. I revised him many times, but I cant find the problem.
     
    I will put here, 2 codes. The initialization from my PIC16F877A, and the routine I2C. If any have Any idea, I thanks.
     
    I am using SERIAL EEPROM ATMEL 24C32.

    Sry, BUT I cant know I ident the code in this editor.. When I paste the code here, all be confused.
     
    main.c

     
    ...
     
    __CONFIG (CPD_OFF & CP_OFF & PWRTE_OFF & WDTE_OFF & BOREN_OFF & FOSC_HS & LVP_OFF);

    void main() {
    ADCON1 = 0x06;
    CMCON = 0x07;

    OPTION_REG = 0xD0;
    PIE1 = 0x00;
    INTCON = 0x00;
     
    PORTA = 0b00100000;
    PORTB = 0b00000000;
    PORTC = 0b00000000;
    PORTD = 0b00000000;
    PORTE = 0b00000000;
     
    TRISA = 0b11010000; // RA7-N/A, RA6-N/A, RA5-Select 23K256/24LC32, RA4-NC, RA3-RESET(LCD), RA2-???, RA1-???, RA0-???
    TRISB = 0b11111111; // RB7 A RB0-Comunicacao com o LCD
    TRISC = 0b00011000; // RC7-CS2(LCD)/SELY(TOUCH), RC6-CS1(LCD)/SELX(TOUCH), RC5-SDO(SPI), RC4-SDA(I2C)/SDI(SPI) RC3-SCL(I2C)/SCK(SPI), RC2-E(LCD), RC1-R/W(LCD), RC0-D/I(LCD)
    TRISD = 0b11111111; // RD7 A RD0-Comunicacao com a CPU
    TRISE = 0b00010111; // RE7-IBF, RE6-OBF, RE5-IBOV, RE4-SSP MODE, RE3-N/A, RE2-CS(LOW), RE1-RD(LOW), REO-WR(LOW)
     
    _delay(10);
     
    SEL_I2C_SPI = 0x01;  // SELECT I2C
    ...
     
    i2c_start();
    data = read_ext_eeprom(0x00, 0x00);
     
    ...
     

     
     
    877_i2c.h

    void i2c_init(){
    SEL_I2C_SPI = 0x01;
    SSPCON = 0x38;      // set I2C master mode
    SSPCON2 = 0x00;
    SSPADD = 0x0C;     // 0x06 p/ 400kHz bus with 10MHz xtal - use 0x0C with 20MHz xtal
    CKE=0;     // use I2C levels      worked also with '0'
    SMP=0;     // disable slew rate control  worked also with '0'
    BCLIF=0;      // clear bus collision flag}
     
    /******************************************************************************************/
    void i2c_waitForIdle(){
    while (( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 )); // wait for idle and not writing
    }
     
    /******************************************************************************************/
    void i2c_start(){
    i2c_waitForIdle();
    SEN=1;
    }
     
    /******************************************************************************************/
    void i2c_repStart(){
    i2c_waitForIdle();
    RSEN=1;
    }
     
    /******************************************************************************************/
    void i2c_stop(){
    i2c_waitForIdle();
    PEN=1;
    }
     
    /******************************************************************************************/
    int i2c_read( unsigned char ack ){
    unsigned char i2cReadData;
     
    i2c_waitForIdle();
    RCEN=1;
    i2c_waitForIdle();
    i2cReadData = SSPBUF;
    i2c_waitForIdle();
     
    if ( ack ) ACKDT=0;
    else ACKDT=1;
     
    ACKEN=1;               // send acknowledge sequence return( i2cReadData );
    }
     
    /******************************************************************************************/
    unsigned char i2c_write( unsigned char i2cWriteData ) {
    i2c_waitForIdle();
     
    SSPBUF = i2cWriteData;
     
    return ( ! ACKSTAT  ); // function returns '1' if transmission is acknowledged
    }
     
    /******************************************************************************************/
    void write_ext_eeprom(unsigned char addressM, unsigned char addressL, unsigned char data){
    i2c_start();
    i2c_write(0xA0);
    i2c_write(addressM); i2c_write(addressL);
    i2c_write(data);
    i2c_stop();
    DelayMs(11);
    }
     
    /******************************************************************************************/
    unsigned char read_ext_eeprom(unsigned char addressM, unsigned char addressL){
    unsigned char data;
     
    i2c_start();
    i2c_write(0xA0);
    i2c_write(addressM); i2c_write(addressL);
    i2c_repStart();
    i2c_write(0xA1);
    data=i2c_read(0);
    i2c_stop();
     
    return(data);
    }

     
    post edited by moahrs - 2012/09/14 04:06:09
    #9
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/14 09:53:13 (permalink)
    0
    Does anyone know if at least my code is correct?
     
    Oh Yes, i am using my 16F877A AT 20MHz Clock.
     
     
    ty a lot
    moacir jr.
     
    #10
    WaltR
    Super Member
    • Total Posts : 3770
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/14 10:59:38 (permalink)
    0
    Re-check the EEPROM doc for the Read/Write bit in the I2C address. You are using the same value in both the write and read functions:
    i2c_write(0xA0);
     
    And as I suggested above try without any extra hardware on the I2C/SPI lines to get the code working first. Read the Microchip App Notes on I2C from EEPROMs.
    And use an O'scope to see if the signals are correct (compare to the scope captures in the app notes).
    post edited by WaltR - 2012/09/14 11:02:44
    #11
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/14 11:13:13 (permalink)
    0
    WaltR

    Re-check the EEPROM doc for the Read/Write bit in the I2C address. You are using the same value in both the write and read functions:
    i2c_write(0xA0);

    And as I suggested above try without any extra hardware on the I2C/SPI lines to get the code working first. Read the Microchip App Notes on I2C from EEPROMs.
    And use an O'scope to see if the signals are correct (compare to the scope captures in the app notes).

    Hi,
     
    Yeah, I am testing only with PIC and I2C, without another hardware.
     
    In the datasheet, use "A0" to send address (MSB and LSB) and send "A1" to READ the data.
     
    I thing that my code is stoping in i2c_waitidle, but I will verify more...
     
     
    ty
    moacir jr.
     
    #12
    NKurzman
    A Guy on the Net
    • Total Posts : 18245
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/14 16:41:24 (permalink)
    0
    There are dedicated I2C bus multiplexer from NXP and others that are also available.
    #13
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/15 03:29:48 (permalink)
    0
    I loved the help that you guys gave me, however I'm giving up i2c and spi. I missed two weeks of my life, read, read again, tested, modified, but nothing. I checked my source code thousands of times, compared with other codes and correct this. My scheme, compared with other, putting the 24C32 directly with the "PIC" and still does not work.
     
    Yesterday I discovered that when I set the "SEN" to start, and the "PIC" can not clear the "SEN", so do not have started the process. I'll work with parallel EEPROM, I know it works. I'll spend more chips, 74LS373 and 74LS245 and others, but I will not lose another 2 weeks of my life, maybe a few hours welding.

    Again, thank you to everyone who tried to help me, but patience has a limit. Getting punched in giving knifepoint, Is not with me.
     
    post edited by moahrs - 2012/09/15 03:34:54
    #14
    JorgeF
    Super Member
    • Total Posts : 3341
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/18 06:06:08 (permalink)
    0
    Hi
     
    Microchip application note AN989 features an simple and straighforward explanation of the operation of a PIC MSSP in I2C master mode to comunicate with an EEPROM.
    The sample code culdn't be simpler.
    http://www.microchip.com/...4&appnote=en023325
     
    As for the SPI, I can prvide you a piece of code of my own that is working fine with a SPI EEPROM, shoulb be usable for the SRAM as is.
     
    Best regards
    Jorge
     
     
    post edited by JorgeF - 2012/09/18 06:09:15
    #15
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/18 09:21:40 (permalink)
    0
    Hi Jorge.
     
    Ty for help. Now I got excited about going back to work with I2C. AN989 This seems to be very interesting. I'll take a good look and see what can help me.
    Friend, once again, thank you.
     
     
     
    Ty a lot
    Moacir Jr.
     
    #16
    JorgeF
    Super Member
    • Total Posts : 3341
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/18 14:06:37 (permalink)
    0
    Hi
     
    As promised, I'm attaching to this post a few scraps of code extracted from one of my projects.
    These are all the "C" definitions and code to access the SPI EEPROM onboard the PIC 18 Explorer board from Microchip.
    This is my first version, so it can be improved, but it as been tested and its working.
     
    This code as been writen for the Boost C compiler from www.sourceboost.com
    BTW:
    I'm also a newbie in I2C and SPI, and in using the (M)SSP devices of the PIC micros.
    I already have a tested I2C Slave, a BoostC + Novo RTOS implementation of the ASM code from Microchip for the 28 pin demo board (PIC16F886) from the Pickit Serial Analyzer kit.
    Now, to learn/practice the implementation of a I2C Master, I'm going to put the PIC 18 Explorer board to talk to my I2C Slave described above.
     

    Best regards
    Jorge
    post edited by JorgeF - 2012/09/18 14:29:18
    #17
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/19 04:11:11 (permalink)
    0
    JorgeF, Thank's for source code. I will analise it. 
     
    JorgeF And all other that can helpme. I Dont believe that my 16F877A was byed broken. Because I have tested, with i/o pins and same think, I cant at least "start" the ATMEL 24C32.
     
    I Have ic-prog and jdm programmer compatible, and the 24C32 works fine. I can write and read very fine. But in circuit, I cant.
     
    Schematic: 16F877A, 20MHz, 2 resistor 10K ohms connect at scl and sda and to vcc (+5v). I am using 100KHz to test (because the resistors of 10K ohms, if work's, I will put in 400KHz and change the resistor to 2k2 ohms). I Desisted from share pins, only I2C I am using.
     
    I atached, all my code, with includes, mcp, mcw and other's. I am using hi-tech compiler with mplab. In this version, I am using "SPPIF" to wait for idle. I just used SSPCON2 and SSPSTAT.R_W and same think, nothing works. Too in this code, have the file 877_i2c_io.h, that have the code for use i2c with i/o pins, I dont know if is correctm because dont works too with i/o pins.
     
    If any1 can see if is all ok in my code, I will revise the circuit, because I dont have any other idea, or I will buy another 16F877A. Is so much left luck to buy one pic damaged.... dam.....
     
     
    Ty a lot for patience and help
    Moacir jr.
     
     
     
    post edited by moahrs - 2012/09/19 04:17:22
    #18
    JorgeF
    Super Member
    • Total Posts : 3341
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/19 10:49:19 (permalink)
    0
    Hi
     
    A defective PIC?!
     
    After several years and lots of PIC mcus (still have some with the window for UV erasing) I never found a defective one. Even when I did "try" to kill them with wrong wiring. wink
    From time to time I suspect of such an ocurrence, but after a few more digging I end up discovering that the only defective part in the project is me. Smile
     
    So don't jump to such a conclusion too fast, just look again and look better.
    Its easier to find a ill documented or buggy library than a defective PIC.
     
    BTW:
    I already mentioned that I'm a novice in SPI, I2C and MSSP, for that reason I'm not using any libs, I'm doing it all from scratch.
    I like to know how things work before I start using them "of the shelf".
    Its perfectly legitime that you all find me a bit on the "crazy" side. But along 30+ years the crazyness brought results, so I don't have plans to change. SmileSmile
     
     
    Best regrads
    Jorge
    post edited by JorgeF - 2012/09/19 10:53:02
    #19
    moahrs
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2011/12/07 04:28:51
    • Location: 0
    • Status: offline
    Re:I2C and SPI shared same pins 2012/09/19 11:02:38 (permalink)
    0
    JorgeF,
     
    Yeah, I also do not believe that is defective in the pic, but I looked and looked many times, already put leds testing, and other things, but nothing. The whole process is stuck in the "SEN = 1". Neither "SSPIF" goes to 1, and even the "SEN" goes to 0, it is as if the PIC was expecting an "ACK" or trying to send startup insistently. Tonight (Here in Brazil are 03:00 pm now), I'll try a few more times.
     
    I really wanted to learn more about "i2c and SPI" work as well as you, of 0, but my time is very short, and I usually leave it to later learn.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5