• AVR Freaks

Hot!16F18346 with MikroC, trying to send i2c

Author
newbadboy
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2019/03/01 03:15:18
  • Location: 0
  • Status: offline
2019/08/15 02:37:28 (permalink)
0

16F18346 with MikroC, trying to send i2c

I'm trying to send i2c using the I2C2_Init(400000), but it compiles with error "undeclared identifier" so i guess it's some syntax error.
 
I have used this command with other chips and it has always worked. 
 
I'm not sure what to do
#1

17 Replies Related Threads

    hexreader
    Super Member
    • Total Posts : 1033
    • Reward points : 0
    • Joined: 2008/05/04 03:06:55
    • Location: England
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 03:53:04 (permalink)
    0
    Compiles OK for me....
     
    Are you using latest compiler (7.5.0) ?
     
    Have you ticked I2C_Remappable and PPS library options?
     
    Your post shows no semicolon at end of line.
     
    // PIC16F18346 with 4MHz clock

    void main() {
        I2C2_Init(400000);

        while(1);
    }

     
    post edited by hexreader - 2019/08/15 03:55:45

    Experienced Hobbyist
    #2
    katela
    Super Member
    • Total Posts : 1382
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 03:57:42 (permalink)
    +1 (1)
    Did you configure the PPS? Maybe you need to remap your pins first before initializing the I2C if fhe library supports your chip.
    Maybe then after you can initialize the I2C like this:
    I2C2_Remappable_Init(400000); // initialize I2C communication
    I2C2_Remappable_Start(); // issue I2C start signal

    It always good to post the minimum code the causes your error, not just one line.
    Did you select the I2C library in the library manager?
    It would have been better if you posted your question in the mikroC forum.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #3
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 04:07:29 (permalink)
    0
    yup latest 750.
    Semicolon present in code
     
    actually i missed ticking the i2c_remap lib. Is PPS Peripheral pin select? with these 2 ticked i get a new error, even more confusing for me at least.
     
    it says now.
     
    There is not enough ROM space octpowers                                      Lib_sprintf.c
    There is not enough ROM space dpowers
    There is not enough ROM space hexpowers
    There is not enough ROM space hexb
    There is not enough ROM space space
     
    #4
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 04:14:02 (permalink)
    0
    Ihave used MikroC forum severall times. The knowledge level is way to low and the activity in teh forum is also slow.
     
    The code is not written by me, its the code form adafruit to be used as driver for the ssd1306 oled display. Ive tried with the suggested 18F pic which is used in the example and it worked well, but failing when trying with this pic
     
     
    [
     
    #include "SSD1306driver_lite.h" // include SSD1306 OLED display driver source code
     
    char txt[11];
     
    void testdrawcircle(void) {
    unsigned short i;
    for (i = 0; i < SSD1306_LCDHEIGHT; i += 2) {
    SSD1306_DrawCircle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2, i);
    SSD1306_Display();
    delay_ms(1);
    }
    }
     
    void testfillrect(void) {
    unsigned short i;
    SSD1306_Color = true;
    for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 3) {
    // alternate SSD1306_Colors
    SSD1306_FillRect(i, i, SSD1306_LCDWIDTH - i*2, SSD1306_LCDHEIGHT - i*2);
    SSD1306_Display();
    delay_ms(1);
    if(SSD1306_Color) SSD1306_Color = false;
    else SSD1306_Color = true;
    }
    SSD1306_Color = true;
    }
     
    void testdrawtriangle(void) {
    unsigned short i;
    for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 5) {
    SSD1306_DrawTriangle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2 - i,
    SSD1306_LCDWIDTH/2 - i, SSD1306_LCDHEIGHT/2 + i,
    SSD1306_LCDWIDTH/2 + i, SSD1306_LCDHEIGHT/2 + i);
    SSD1306_Display();
    delay_ms(1);
    }
    }
     
    void testfilltriangle(void) {
    int i;
    SSD1306_Color = true;
    for (i = SSD1306_LCDHEIGHT/2; i > 0; i -= 5) {
    SSD1306_FillTriangle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2 - i,
    SSD1306_LCDWIDTH/2 - i, SSD1306_LCDHEIGHT/2 + i,
    SSD1306_LCDWIDTH/2 + i, SSD1306_LCDHEIGHT/2 + i);
    if(SSD1306_Color) SSD1306_Color = false;
    else SSD1306_Color = true;
    SSD1306_Display();
    delay_ms(1);
    }
    SSD1306_Color = true;
    }
     
    void testdrawroundrect(void) {
    unsigned short i;
    for (i = 0; i < SSD1306_LCDHEIGHT/2 - 2; i += 2) {
    SSD1306_DrawRoundRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i, SSD1306_LCDHEIGHT/4 - i/2);
    SSD1306_Display();
    delay_ms(1);
    }
    }
     
    void testfillroundrect(void) {
    unsigned short i;
    SSD1306_Color = true;
    for (i = 0; i < SSD1306_LCDHEIGHT/2 - 2; i += 2) {
    SSD1306_FillRoundRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i, SSD1306_LCDHEIGHT/4 - i/2);
    if(SSD1306_Color) SSD1306_Color = false;
    else SSD1306_Color = true;
    SSD1306_Display();
    delay_ms(1);
    }
    SSD1306_Color = true;
    }
     
    void testdrawrect(void) {
    unsigned short i;
    for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 2) {
    SSD1306_DrawRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i);
    SSD1306_Display();
    delay_ms(1);
    }
    }
     
    void testdrawline() {
    int i;
    for (i = 0; i < SSD1306_LCDWIDTH; i += 4) {
    SSD1306_DrawLine(0, 0, i, SSD1306_LCDHEIGHT - 1);
    SSD1306_Display();
    delay_ms(1);
    }
    for (i = 0; i < SSD1306_LCDHEIGHT; i += 4) {
    SSD1306_DrawLine(0, 0, SSD1306_LCDWIDTH - 1, i);
    SSD1306_Display();
    delay_ms(1);
    }
    delay_ms(250);
     
    SSD1306_ClearDisplay();
    for (i = 0; i < SSD1306_LCDWIDTH; i += 4) {
    SSD1306_DrawLine(0, SSD1306_LCDHEIGHT - 1, i, 0);
    SSD1306_Display();
    delay_ms(1);
    }
    for (i = SSD1306_LCDHEIGHT - 1; i >= 0; i -= 4) {
    SSD1306_DrawLine(0, SSD1306_LCDHEIGHT - 1, SSD1306_LCDWIDTH - 1, i);
    SSD1306_Display();
    delay_ms(1);
    }
    delay_ms(250);
     
    SSD1306_ClearDisplay();
    for (i = SSD1306_LCDWIDTH - 1; i >= 0; i -= 4) {
    SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, SSD1306_LCDHEIGHT - 1, i, 0);
    SSD1306_Display();
    delay_ms(1);
    }
    for (i = SSD1306_LCDHEIGHT - 1; i >= 0; i -= 4) {
    SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, SSD1306_LCDHEIGHT - 1, 0, i);
    SSD1306_Display();
    delay_ms(1);
    }
    delay_ms(250);
     
    SSD1306_ClearDisplay();
    for (i = 0; i < SSD1306_LCDHEIGHT; i += 4) {
    SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, 0, 0, i);
    SSD1306_Display();
    delay_ms(1);
    }
    for (i = 0; i < SSD1306_LCDWIDTH; i += 4) {
    SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, 0, i, SSD1306_LCDHEIGHT - 1);
    SSD1306_Display();
    delay_ms(1);
    }
    delay_ms(250);
    }
     
    void testscrolltext(void) {
    SSD1306_ClearDisplay();
    SSD1306_GotoXY(50, 8);
    SSD1306_TextSize(2);
    SSD1306_Print("scroll");
    SSD1306_Display();
    delay_ms(1);
     
    SSD1306_StartScrollRight(0x00, 0x0F);
    delay_ms(2000);
    SSD1306_StopScroll();
    delay_ms(1000);
    SSD1306_StartScrollLeft(0x00, 0x0F);
    delay_ms(2000);
    SSD1306_StopScroll();
    delay_ms(1000);
    SSD1306_StartScrollDiagRight(0x00, 0x07);
    delay_ms(2000);
    SSD1306_StartScrollDiagLeft(0x00, 0x07);
    delay_ms(2000);
    SSD1306_StopScroll();
    }
     
    // main function
    void main()
    {
    //OSCCON = 0x70; // set internal oscillator to 16MHz
    ANSELC = 0; // configure all PORTC pins as digital
    ANSELA = 0;
    ANSELB = 0;
     
    delay_ms(1000); // wait a second
    I2C2_Init(400000); // initialize I2C communication with clock frequency of 400kHz
     
    // initialize the SSD1306 OLED with an I2C addr = 0x7A (default address)
    SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
    // print image buffer
    SSD1306_Display();
    delay_ms(5000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    SSD1306_Color = true; // set color to true (ON)
    // draw a single pixel
    SSD1306_DrawPixel(10, 10);
    // Show the display buffer on the hardware.
    // NOTE: You _must_ call SSD1306_Display() after making any drawing commands
    // to make them visible on the display hardware!
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw many lines
    testdrawline();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw rectangles
    testdrawrect();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw multiple rectangles
    testfillrect();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw mulitple circles
    testdrawcircle();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw a white circle, 10 pixel radius
    SSD1306_FillCircle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2, 10);
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    testdrawroundrect();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    testfillroundrect();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    testdrawtriangle();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    testfilltriangle();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // draw scrolling text
    testscrolltext();
    SSD1306_Display();
    delay_ms(2000);
    SSD1306_ClearDisplay(); // clear the buffer
     
    // text display tests
    SSD1306_Color = 1;
    SSD1306_TextSize(1);
    SSD1306_GotoXY(2, 7);
    SSD1306_Print("heejsan, world!");
     
    sprintf(txt, "%.6f", 3.141592);
    SSD1306_GotoXY(2, 16);
    SSD1306_Print(txt);
     
    SSD1306_TextSize(2);
    SSD1306_GotoXY(2, 26);
    sprintf(txt, "0x%LX", 0xDEADBEEF);
    SSD1306_Print(txt);
    SSD1306_Display();
    delay_ms(2000);
     
    while(1) {
    // invert the display
    SSD1306_InvertDisplay(true);
    delay_ms(1000);
    SSD1306_InvertDisplay(false);
    delay_ms(1000);
    }
     
    }
    // end of code.
     

     
    post edited by newbadboy - 2019/08/15 04:22:41
    #5
    pcbbc
    Super Member
    • Total Posts : 1252
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 04:44:45 (permalink)
    0
    Not familiar with MikroC but...
    Looks like you are out of program memory to me due to inclusion of sprintf library.
    PIC18 devices can have more ROM than PIC16.
    Which specific PIC18 device were you successful with? How much program memory does it have? How much did your project actually use?
    #6
    katela
    Super Member
    • Total Posts : 1382
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 05:25:36 (permalink)
    0
    As explained above it's likely that you are running out of memory. The sprintf is memory intensive function, try to comment it out and compile again.
    View your memory usage in statistics. View -> Statistics

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #7
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/15 05:32:11 (permalink)
    0
    yes that was my conclusion. I will try to scale down the whole file to what i'm actually will be using and i hope that will make it small enough.
     
    I will do some other more basic test first since this is the first time i'm using this chip.
     
     
    post edited by newbadboy - 2019/08/15 05:40:38
    #8
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/16 05:24:54 (permalink)
    0
    So now i have sorted out the compilation erros and will go on with actually trying to write some text and so.
     
    At the moment i'm trying to enable the i2c module but there is not traffic at all. Any ideas?
     

    #include "SSD1306driver_lite.h" // include SSD1306 OLED display driver source code
    #define VFB5 PORTA.F5
    #define TEMP PORTA.F4
    #define BTN PORTC.F5
    #define OUT PORTC.F4
    #define ADC4 PORTC.F6
    #define EN4 PORTC.F7
    #define ADC3 PORTB.F6
    #define EN3 PORTB.F4
    #define ADC2 PORTC.F2
    #define EN2 PORTC.F1
    #define ADC1 PORTC.F0
    #define EN1 PORTA.F2

    char txt[11];
    Init_main(){
    OSCCON1=0B00000001;
    OSCFRQ=0b00000110; // set internal oscillator to 16MHz
    OSCTUNE=0B00000000;

    ANSELA=0b00110000; // configure all PORTS
    ANSELB=0b01000000;
    ANSELC=0b01000101;
    TRISA=0b01000000;
    TRISB=0b11100000;
    TRISC=0b01100101;
    delay_ms(1000); // wait a second
    I2C2_Init(400000); // initialize I2C communication with clock frequency of 400kHz

    EN1=1;
    EN2=1;
    EN3=1;
    EN4=1;
    }
    // main function
    void main(){
    Init_main();

    while(1){
    I2C2_Wr(0xA2);
    delay_ms(500);


    }
    }

    post edited by newbadboy - 2019/08/16 05:27:19
    #9
    katela
    Super Member
    • Total Posts : 1382
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/16 06:37:32 (permalink)
    +1 (1)
    You need to follow the I2C communication protocols procedures, in the code you have just posted unless you did not post the full code, you don't seem to follow that, I don't see the I2C start command.
    Depending on the slave device you are communicating with, for example to write you could send these commands (please read your datasheet for more info):
    • I2C_Init();
    • I2C_Start();
    • I2C_Wr(0xD0);// D0 is the address of the slave device
    • I2C_Wr(0x03);// write Register address
    • I2C_Wr(0xA2);// write your data
    • I2C_Stop();
    Make sure you also have pull-up resistors with correct values on the I2C lines.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #10
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/16 12:32:45 (permalink)
    0
    Yes i know. At the moment im not trying to send real data to The slave. Im just trying the enable the i2c module. There is no traffic at all. So there is somthing wrong with the setup for the module.

    Im trying to send a simple write command over and over again and check with oscilloscope, but there is nothing
    #11
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/16 15:57:32 (permalink)
    0
    That's not how it works. There won't be any traffic just after ENABLING the module.
    It only happens when you follow the prescribed sequence. If you don't send a START, then it won't do anything else.
    You really have to understand how I2C works to use it. It is nothing at all like RS232.
    Have a look at https://en.wikipedia.org/wiki/I%C2%B2C
    and the actual I2C specification at http://www.nxp.com/docume..user_manual/UM10204.pdf

    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!
    #12
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/16 23:57:30 (permalink)
    0
    Alright. Ill try to send data i correct the correct order
    #13
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/17 03:46:04 (permalink)
    0
    So ive tried this and still no life on the i2c bus
     

    #include "SSD1306driver_lite.h" // include SSD1306 OLED display driver source code
    #define VFB5 PORTA.F5
    #define TEMP PORTA.F4
    #define BTN PORTC.F5
    #define OUT PORTC.F4
    #define ADC4 PORTC.F6
    #define EN4 PORTC.F7
    #define ADC3 PORTB.F6
    #define EN3 PORTB.F4
    #define ADC2 PORTC.F2
    #define EN2 PORTC.F1
    #define ADC1 PORTC.F0
    #define EN1 PORTA.F2

    char txt[11];
    Init_main(){
    OSCCON1=0B00000001;
    OSCFRQ=0b00000110; // set internal oscillator to 16MHz
    OSCTUNE=0B00000000;

    ANSELA=0b00110000; // configure all PORTS
    ANSELB=0b01000000;
    ANSELC=0b01000101;
    TRISA=0b01000000;
    TRISB=0b11100000;
    TRISC=0b01100101;
    delay_ms(1000); // wait a second
    I2C2_Init(400000); // initialize I2C communication with clock frequency of 400kHz

    EN1=1;
    EN2=1;
    EN3=1;
    EN4=1;
    }
    // main function
    void main(){
    Init_main();

    while(1){
    I2C2_Start();
    I2C2_Wr(0xD0);// D0 is the address of the slave device
    I2C2_Wr(0x03);// write Register address
    I2C2_Wr(0xA2);// write your data
    I2C2_Stop();
    delay_ms(500);


    }
    }

     
     
    #14
    pcbbc
    Super Member
    • Total Posts : 1252
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/17 04:34:19 (permalink)
    0
    And you are now trying to send real data to a slave that exist on the bus?
    You realise that after writing the slave address the master is expecting to receive an acknowledgment?
    Is your main while loop running?
    Try debugging it and see.
    If not, where is it hanging?
    #15
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/17 04:57:44 (permalink)
    0
    No the slave does not has other adress. But still i should able to see some data or clock. But nada. Main loop is running. If i toggle a io pin i can see it change state
    #16
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/17 05:02:35 (permalink)
    0
    What state are SDA and SCL in?
    High, or low?
     

    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!
    #17
    newbadboy
    Starting Member
    • Total Posts : 46
    • Reward points : 0
    • Joined: 2019/03/01 03:15:18
    • Location: 0
    • Status: offline
    Re: 16F18346 with MikroC, trying to send i2c 2019/08/17 05:12:05 (permalink)
    0
    High
    #18
    Jump to:
    © 2019 APG vNext Commercial Version 4.5