• AVR Freaks

Hot!I2C is making me crazy

Author
CelinSrl
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2020/07/07 06:48:02
  • Location: 0
  • Status: offline
2020/07/08 03:28:52 (permalink)
0

I2C is making me crazy

Hi there.
I tried for 5 days to make i2c working on pic18f47k40 and actually i'm not figuring out why it doesn't work.
i have to simply write the slave address, the LCD register and then the character to display.
I cannot see anything, and if i put a "flag code" after the byte sending (e.g. led turn on) the code is not executed.
So i gather that the instruction "I2C1_WriteNBytes(ADDR_lcd, bytetosend, 1);" is not working because it goes in a sort of while(!bus_available) state, and i cannot fix it.
I connected a pullup resistor in both lines. I'm using the hardware i2c mode with the mssp peripheral.
i configured the internal oscillator at 64MHz and the divider at 32, so i have a freq of 2MHz.
mssp set in i2c mode obviously, master, at freq of 100kHz.
mssp pins sda1 and scl1 set to portc4 portc3. (first time i use mcc, so maybe an error could be here).
 
Posting the code is useless because i'm searching for a code that will work sure. A simple and working I2C1_WriteNBytes(address, data, lenght);
I tried a couple of days ago to read data from an ADC. Same result, it stops executing when it arrives at the read instruction.
 
Can someone more experienced than me with i2c, help me find a code and a mcc setting set that works fine with i2c slaves?
 
Thank you.
 
#1

4 Replies Related Threads

    Jim Nickerson
    User 452
    • Total Posts : 6722
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: I2C is making me crazy 2020/07/08 08:38:29 (permalink)
    0
    CelinSrl
    Hi there.
    I tried for 5 days to make i2c working on pic18f47k40 and actually i'm not figuring out why it doesn't work.
    i have to simply write the slave address, the LCD register and then the character to display.
    I cannot see anything, and if i put a "flag code" after the byte sending (e.g. led turn on) the code is not executed.
    So i gather that the instruction "I2C1_WriteNBytes(ADDR_lcd, bytetosend, 1);" is not working because it goes in a sort of while(!bus_available) state, and i cannot fix it.
    I connected a pullup resistor in both lines. I'm using the hardware i2c mode with the mssp peripheral.
    i configured the internal oscillator at 64MHz and the divider at 32, so i have a freq of 2MHz.
    mssp set in i2c mode obviously, master, at freq of 100kHz.
    mssp pins sda1 and scl1 set to portc4 portc3. (first time i use mcc, so maybe an error could be here).
     
    Posting the code is useless because i'm searching for a code that will work sure. A simple and working I2C1_WriteNBytes(address, data, lenght);
    I tried a couple of days ago to read data from an ADC. Same result, it stops executing when it arrives at the read instruction.
     
    Can someone more experienced than me with i2c, help me find a code and a mcc setting set that works fine with i2c slaves?
     
    Thank you.
     


    Posting "your" code is the only way we might see what is wrong with your code.
    If you just want a working example to copy then use MCC.
    #2
    Jim Nickerson
    User 452
    • Total Posts : 6722
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: I2C is making me crazy 2020/07/08 08:46:11 (permalink)
    0
    There is "CHARACTER LCD" under Device Resources.
    Maybe that works.
    #3
    CelinSrl
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2020/07/07 06:48:02
    • Location: 0
    • Status: offline
    Re: I2C is making me crazy 2020/07/08 23:05:59 (permalink)
    0
    Hi Jim and thank you.
     
    #include "mcc_generated_files/mcc.h"
    #define ADDR 0x3F

    void rosso(bool state){
        RD4=!state;
    }

    uint8_t buf[]={0b10000000, 0b10000000, 0b01000000, 0b01000011};

    void main(void)
    {
        
        SYSTEM_Initialize();
        PIN_MANAGER_Initialize();
        I2C1_Initialize();
        rosso(false);
        
        __delay_ms(2000);
        I2C1_SetBuffer(buf, 4);
        
        while(I2C1_Open(ADDR)!=I2C1_NOERR){__delay_ms(100);}
        
        I2C1_MasterWrite();
        
        while(I2C1_Close()!=I2C1_NOERR){__delay_ms(100);}
        
        // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
        // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
        // Use the following macros to:

        // Enable the Global Interrupts
        //INTERRUPT_GlobalInterruptEnable();

        // Disable the Global Interrupts
        //INTERRUPT_GlobalInterruptDisable();

        // Enable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptEnable();

        // Disable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptDisable();
        
        
        rosso(true);
        
        while (1)
        {
            
        }
    }

      this is my code, as you can see simply i set a 4 byte buffer with some instruction in it for the LCD and everything is executed, but the problem is, the MasterWrite() function blocks the pic and make it reset. in fact, rosso(true) lits a led on, but the led remains turned off.
    Probably i'm missing something, but what i wanted to say is, my code is not essential. Is important for me to understand how this library works and then write some code to simply use a display, and then write something more complicated (i have to read data from ADC and some else..).
     
    Thanks
    #4
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C is making me crazy 2020/07/09 00:02:54 (permalink)
    +1 (1)
    Not your immediate problem, but you should get into the habit of writing to LATD4, not RD4.
    Only use the RD4 form for reading the pin.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5