• AVR Freaks

Hot!I2C and Timer0 module in PIC16F1519 (and PIC16F18325)

Author
Avinash_1588
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/10/10 23:23:59
  • Location: 0
  • Status: offline
2020/06/05 21:13:42 (permalink)
0

I2C and Timer0 module in PIC16F1519 (and PIC16F18325)

Hello All,
I am referring code of Thermometer Demonstration of Microchip.
I have uploaded here main.c and i2c.c file here for reference. I have understand how they have implemented I2C in i2c.c file and using it in function  
unsigned int IR_temp_read(unsigned char command)

But in the same function they are using below loop and there I dont know why they have used Timer0 flag to check.  
Timer0 flag with SSP1 flag for checking data received or not, then they reading buffer.
while((PIR1bits.SSP1IF == 0) || (INTCONbits.TMR0IF == 1));
if(INTCONbits.TMR0IF == 0)
{
TMR0 = 0;
INTCONbits.TMR0IF = 0;
}

Please help to understand this.
 
Timer0 is initialized as below. I don't know how & why they used Timer0 as below.
void Timer0_Init (void)
{
INTCON = 0x00;
TMR0 = 0;
OPTION_REG = 0x83;
}

I am thinking that they might have used Timer0 to figure out Time has not gone beyond limit for receiving data. If gone, they are reading buffer. So please help me to find purpose of checking Timer0 flag.
 
What if I not use Timer0, what will happen ? Because I just want to use I2C for data reception.
 
Also I want to migrate this code of PIC16F1519 to PIC16F18325. I have changed all the I2C code with proper syntax since PIC16F18325 have two MSSP. But I stuck migrating Timer0 because of above function.
 
Thanks in advance for help & Stay safe.
Avinash
 
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 00:16:19 (permalink)
    +1 (1)
    Where exactly did you get this code?
    The i2c.c module has some terrible code in it. Did it really come from Microchip?
     
     

    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!
    #2
    Avinash_1588
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/10/10 23:23:59
    • Location: 0
    • Status: offline
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 00:25:52 (permalink)
    0
    Hey Ric, I was waiting for your reply. Thanks
     
    Yes I got the code from microchip site. This is link for code.
    The webpage is here Connected Thermometer Demonstration Board.
     
    Here they are interfacing MLX90615 Temp Sensor with PIC16F1519 using I2C interface.
    Then they showing its data on OLED display with 8 bit data mode. 

     
    #3
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 00:37:44 (permalink)
    +1 (1)
    I have to assume they assigned some first year intern to write that code, and he skipped programming 101 and didn't really understand I2C either.
    You'd be better off studying the datasheet and writing your own code from scratch. That is simply awful.
    I'm not surprised you're confused by their use of the timer. It's totally pointless.
    They have plainly tried to implement a timeout, and failed dismally.
     
     

    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!
    #4
    Avinash_1588
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/10/10 23:23:59
    • Location: 0
    • Status: offline
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 00:58:38 (permalink)
    0
    Ok ric thanks.
    Yeah, they made it more complicated and written huge code with mtouch, I2C and OLED.
     
    I guess they are just overflowing Timer 0 from 00h to FFh since it is 8 bit timer with TMR0 = 0 and checking TMR0IF with Instruction clock (fosc/4), so as you said they are just implementing Timeout function. (But this shows they are using 256 instruction cycle, and it is more than needed.)
     
    If in above case what if I just removed Timer0 flag Timeout function, will it be useful ?
    I guess that only SSP1IF can be helpful to check whether Start or Stop bit transmitted or received.
     
    Does I2C need Timeout function when its communicating with single slave peripheral.
    If you have any code of I2C with Timeout function, please share.
     
    #5
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 01:17:04 (permalink)
    +1 (1)
    The code contains crude time delays that should not be required, extra status flag tests that are not required, incorrect handling of the ACKSTAT flag, and it includes htc.h instead of xc.h.
    In short, it's a disaster, and requires a total rewrite.
     

    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!
    #6
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 01:22:41 (permalink)
    +1 (1)
    It also includes stdlib.h despite not requiring anything in that file
    and stdint.h, correctly commenting that it "Provides "uint#_t" typedefs for each compiler", but then doesn't use them.

    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!
    #7
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3992
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 02:35:48 (permalink)
    +1 (1)
    Some one on here the other day said to use a timer and even wrote a detailed list.
    Very poor nube advice.
     
    A delay comes after a "stop" not during tx/rx of data to slave device.
    Poll the device after a stop until ready in the case of eeprom.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #8
    Avinash_1588
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/10/10 23:23:59
    • Location: 0
    • Status: offline
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/06 03:11:26 (permalink)
    -1 (1)
    As you suggested ric, writing code of I2C from scratch will help.
    No need consider Timeout function using Timer0.
     
    So I will write it using MCC.
    #9
    Avinash_1588
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/10/10 23:23:59
    • Location: 0
    • Status: offline
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/07 03:30:25 (permalink)
    0
    Hey, I have wrote I2C code by myself.
    I have used MCC and Foundation library service for I2C.
    Since i don't find how to use functions generated by MCC in actual code for reading 3 bytes data.
    So I wrote my functions as shown in below attached main.c file.
    I also implemented below function
    unsigned int IR_temp_read(unsigned char command)

    as
    unsigned int IR_Thermometer_Temp_Read(unsigned char command)

    I have implemented I2C Write & Read in above function as per below sequence.
    START - Address & W bit as 0 - Command - Address & R bit as 1 - Read1stbyte - Read2ndbyte - Read3rdbyte - STOP
     
    Please let me know if above is ok ? or need modification, suggest if any.
     
    Also when I compile code I get below warning since I am not calling / using those functions.
    warning: (2053) function "_i2c_poller" is never called

    So please let me know how to get rid of these warning things, or what will happen if I let it as it is ? Will it cause any trouble ?
    #10
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C and Timer0 module in PIC16F1519 (and PIC16F18325) 2020/06/07 04:08:56 (permalink)
    +2 (2)
    avi_n_chaure
    I have implemented I2C Write & Read in above function as per below sequence.

    You're missing one very important step.
    START - Address & W bit as 0 - Command - RESTART - Address & R bit as 1 - Read1stbyte - Read2ndbyte - Read3rdbyte - STOP
    Edit: I see you are doing that, just forgot to mention it.
    I also see this is largely still the awful Microchip code.
     
    post edited by ric - 2020/06/07 04:16:13

    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!
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5