Hot!PIC16F15313 I2c Communication

Author
antochildjesus
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2017/10/02 22:49:49
  • Location: 0
  • Status: offline
2017/10/06 22:56:19 (permalink)
0

PIC16F15313 I2c Communication

Hi,
I'm using the PIC16F15313 to do some I2C Communication. I Control the Pin using MCC plugin .MCC plugin is generate program automatically. But the issue is, Program running time  the pin RA1 is Not getting any clock Signal. Please guide me how to Solvethe problem.
 
Below I add my code
 
/**
Generated Main Source File

Company:
Microchip Technology Inc.

File Name:
main.c

Summary:
This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

Description:
This header file provides implementations for driver APIs for all modules selected in the GUI.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.45
Device : PIC16F15313
Driver Version : 2.00
The generated drivers are tested against the following:
Compiler : XC8 1.35
MPLAB : MPLAB X 3.40
*/

/*
(c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
software and any derivatives exclusively with Microchip products.

THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
TERMS.
*/

#include "mcc_generated_files/mcc.h"

/*
Main application
*/
void main(void)
{
unsigned char data[10] = {0x01,0xAA};
unsigned char flag = 0;
// initialize the device
SYSTEM_Initialize();
// RA2PPS=0X15;
//INLVLA5=0;
//PMD4=0X10;
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:

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

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

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

// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
TRISA5=0;
while (1)
{
// Add your application code
RA5=1;
I2C1_MasterWrite(
data,
0X01,
0XAB,
&flag);


}
}
/*
End of File
*/
#1

15 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 5760
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC16F15313 I2c Communication 2017/10/07 14:39:43 (permalink)
    0
    "Not getting any clock signal" is not a complete description. What is the pin doing? Is it high, or is it low?
    How about the SDA pin?
    Do you have external pullup resistors on those two pins?
     
    #2
    antochildjesus
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2017/10/02 22:49:49
    • Location: 0
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/08 23:05:12 (permalink)
    0
    I just want to generate a clock signal for PIC16F15313 pin no - RA1 ,please give some solution.  
    #3
    qhb
    Superb Member
    • Total Posts : 5760
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC16F15313 I2c Communication 2017/10/08 23:40:09 (permalink)
    +2 (2)
    The I2C peripheral will NOT generate any clock if it does not see an idle bus.
    That is why I asked all those questions.
    If you don't want to answer them, fine, find someone else to help you.
     
    #4
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/09 07:56:19 (permalink)
    +2 (2)
    The I2C peripheral on the PIC16F15313 does work, I used it only a few days ago. I did not, however, use MCC. Frankly my experience of MCC is it's not trustworthy, particularly on newer parts, and for simple peripherals like the MSSP, MCC just gets in the way.
     
    They might market MCC as a universal panacea, but in practice for real projects, on simple peripherals it's more trouble that it's worth. I strongly recommend reading the datasheet and errata instead and writing your own code.
    #5
    KTrenholm
    Super Member
    • Total Posts : 207
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/09 08:12:57 (permalink)
    0
    antochildjesus
    I just want to generate a clock signal for PIC16F15313 pin no - RA1 ,please give some solution.  




    None of use here can read minds. qhb is asking for more information and this post provides none.
    Do you have pull up resistors on your SDA and SCL lines?  What are you seeing on the pins when you run?   Are they high or low?
    #6
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/10 13:29:20 (permalink)
    0
    Here is my bare metal code that counts up on a standard I2C Alphanumeric 8x2 LCD panel.
     

    #pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
    #pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
    //#pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
    #pragma config WDTE = ON // Watchdog Timer Enable bits (WDT enabled, SWDTEN is ignored)
    #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled)
    #pragma config LVP = ON // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)
    #pragma config PPS1WAY = OFF // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence))
    #include <xc.h>
    #include <stdint.h>
    #include <stdio.h>
    #define _XTAL_FREQ 32000000
    #define I2C_TRIS_SCL TRISAbits.TRISA4
    #define I2C_TRIS_SDA TRISAbits.TRISA2
    #define I2C_BAUD_RATE 100000
    #define I2C_BRG ((_XTAL_FREQ/4)/I2C_BAUD_RATE-1)
    //#define LCD_I2C_ADDR 0x3E // Farnell part number 2063208, MIDAS Part No MCCOG21605B6W-SPTLYI
    #define LCD_I2C_ADDR 0x3E // AE-AQM0802
    #define LCD_TRIS_RESET TRISAbits.TRISA5
    #define LCD_LAT_RESET LATAbits.LATA5
    void putch(char c)
    {
    while (!TX1STAbits.TRMT)
    {
    NOP();
    }
    TX1REG=c;
    }
    void I2CInit(void)
    {
    I2C_TRIS_SCL=1;
    I2C_TRIS_SDA=1;
    SSP1STATbits.SMP=1; /* Slew rate disabled */
    SSP1CON1bits.SSPM=8; // = 0x28; /* SSPEN = 1, I2C Master mode, clock = _XTAL_FREQ/(4 * (SSPADD + 1)) */
    SSP1CON1bits.SSPEN=1;
    SSP1ADD=I2C_BRG;
    }
    void I2CStart()
    {
    SSP1CON2bits.SEN = 1; /* Start condition enabled */
    while(SSP1CON2bits.SEN) /* automatically cleared by hardware */
    { /* wait for start condition to finish */
    NOP();
    }
    }
    void I2CStop()
    {
    SSP1CON2bits.PEN = 1; /* Stop condition enabled */
    while(SSP1CON2bits.PEN) /* Wait for stop condition to finish */
    { /* PEN automatically cleared by hardware */
    NOP();
    }
    }
    void I2CRestart()
    {
    SSP1CON2bits.RSEN = 1; /* Repeated start enabled */
    while(SSP1CON2bits.RSEN) /* wait for condition to finish */
    {
    NOP();
    }
    }
    void I2CAck()
    {
    SSP1CON2bits.ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
    SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
    while(SSP1CON2bits.ACKEN) /* wait for ack data to send on bus */
    {
    NOP();
    }
    }
    void I2CNak()
    {
    SSP1CON2bits.ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
    SSP1CON2bits.ACKEN = 1; /* Ack data enabled */
    while(SSP1CON2bits.ACKEN) /* wait for ack data to send on bus */
    {
    NOP();
    }
    }
    void I2CWait()
    {
    while ((SSP1CON2 & 0x1F ) || ( SSP1STATbits.R_nW /* .R_NOT_W*/ ) ) /* wait for any pending transfer */
    {
    NOP();
    }
    }
    void I2CSend(uint8_t u8)
    {
    SSP1BUF = u8; /* Move data to SSPBUF */
    while(SSP1STATbits.BF) /* wait till complete data is sent from buffer */
    {
    NOP();
    }
    I2CWait(); /* wait for any pending transfer */
    }
    uint8_t I2CRead(void)
    {
    uint8_t u8;
    /* Reception works if transfer is initiated in read mode */
    SSP1CON2bits.RCEN = 1; /* Enable data reception */
    while(!SSP1STATbits.BF) /* wait for buffer full */
    {
    NOP();
    }
    u8 = SSP1BUF; /* Read serial buffer and store in temp register */
    I2CWait(); /* wait to check any pending transfer */
    return u8; /* Return the read data from bus */
    }
    uint8_t MyI2CReceiveByte(uint8_t u8I2CAddr,uint8_t u8RegAddr)
    {
    uint8_t u8Data;

    I2CStart();

    I2CSend((uint8_t)(u8I2CAddr << 1) | 0U);
    I2CSend(u8RegAddr);
    I2CRestart();
    I2CSend((uint8_t)(u8I2CAddr << 1) | 1U);
    u8Data=I2CRead();
    I2CNak();
    I2CStop();
    return u8Data;
    }
    void MyI2CSendByte(uint8_t u8I2CAddr,uint8_t u8RegAddr,uint8_t u8Data)
    {
    I2CStart();

    I2CSend((uint8_t)(u8I2CAddr << 1) | 0U);
    I2CSend(u8RegAddr);
    I2CSend(u8Data);
    I2CStop();
    }
    void MyLCDInit(void)
    {
    // 5V init
    LCD_LAT_RESET=0; // Assert LCD Reset
    LCD_TRIS_RESET=0; // LCD Reset
    __delay_ms(40);
    LCD_LAT_RESET=1;
    __delay_ms(40);
    LCD_LAT_RESET=0;
    __delay_ms(40);
    LCD_LAT_RESET=1;
    __delay_ms(40);

    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x38);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x39);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x14);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x70);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x56);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x6C);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x38);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x0C);
    __delay_us(30);
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x01); // Clear display
    __delay_ms(2);
    }
    void MyLCDPutC(char c)
    {
    MyI2CSendByte(LCD_I2C_ADDR,0x40,c);
    }
    void MyLCDPutU32(unsigned long u32,signed char n8NumDigits)
    {
    unsigned long u32Div=1000000000UL;
    n8NumDigits-=10;
    while (u32Div!=0)
    {
    unsigned char u8=u32/u32Div;
    unsigned long u32a=u8*u32Div;
    u32-=u32a;
    u32Div/=10;
    if (n8NumDigits++>=0)
    {
    MyLCDPutC(u8+'0');
    }
    }
    }
    void MyLCDPutU16(uint16_t u16,int8_t n8NumDigits)
    {
    uint16_t u16Div=10000UL;
    n8NumDigits-=5;
    while (u16Div!=0)
    {
    uint8_t u8=u16/u16Div;
    uint16_t u16a=u8*u16Div;
    u16-=u16a;
    u16Div/=10;
    if (n8NumDigits++>=0)
    {
    MyLCDPutC(u8+'0');
    }
    }
    }
    void MyLCDClear(void)
    {
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x01);
    __delay_ms(10);
    }
    void MyLCDCursor(unsigned char u8x,unsigned char u8y)
    {
    MyI2CSendByte(LCD_I2C_ADDR,0x00,0x80 | ((u8y & 1U)<<6) | (u8x & 0x3FU)); // Set DDRAM address
    }
    void MyLCDPutS(const char *psz)
    {
    char c;
    while ((c=*psz++)!='\0')
    {
    MyLCDPutC(c);
    }
    }
    void main(void)
    {
    WDTCON0bits.WDTPS=0b01010; // 0b01010 -> 1:32768, ~1s PIC16f15313
    // WDTCONbits.WDTPS=0b01010; // 0b01010 -> 1:32768, ~1s PIC16f18313
    // Set up pin multiplexer
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCK = 0x00; // unlock PPS
    RA4PPS=0x15; // 0x15 -> SCK1/SCL1 PIC16f15313
    RA2PPS=0x16; // 0x16 -> SDO1/SDA1 PIC16f15313
    // RA4PPS=0x18; // 0x18 -> SCK1/SCL1 PIC16f18313
    // RA2PPS=0x19; // 0x19 -> SDO1/SDA1 PIC16f18313
    SSP1CLKPPSbits.SSP1CLKPPS=4; // 4 -> RA4
    SSP1DATPPSbits.SSP1DATPPS=2; // 2 -> RA2
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCK = 0x01; // lock PPS

    ANSELA=0;
    I2CInit();
    CLRWDT();
    MyLCDInit();
    CLRWDT();
    MyLCDPutS("ABCD");
    CLRWDT();

    while (1)
    {
    static uint16_t u16=0;

    MyLCDCursor(0,1);
    MyLCDPutU16(u16,5);
    u16++;
    SLEEP();
    }
    return;
    }

    #7
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/10 13:37:57 (permalink)
    0
    Two possible problems.
     
    First, you have your programmer on the RA1 GPIO (it's also ICSPCLK).
     
    Second, you must set the PPS inputs AND outputs appropriately. Although by default the PPS inputs for SCL1/SDA1 are RA1 and RA2, the PPS outputs must also be set explicitly. See my code above, I also move SCL1 completely to RA4 so it's not shared with a programmer/debugger pin.
     
    I would assume that MCC _should_ do your pin remapping for you, but I wouldn't trust it to. Check with a debugger how the RA1PPS and RA2PPS are set.
     
    You also need to set both SCL & SDA GPIO TRIS bits to 1.
     
    See the data sheet for all this stuff, but I realise it's not always obvious. This is a prime reason why I don't like MCC, you have to know how the peripherals work anyway at a hardware and register level, so what value does MCC add, it just seems to be something extra to learn (and debug).
     
    #8
    khalis5203
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2017/10/10 09:18:53
    • Location: 0
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/12 00:54:29 (permalink)
    +1 (1)
    Hi,
     
    Please check your MCU errata datasheet. Currently PIC16F15313 with silicon revision A2 got problem with I2C driver.
    #9
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/12 09:53:21 (permalink)
    +2 (2)
    khalis5203
    Hi, Please check your MCU errata datasheet. Currently PIC16F15313 with silicon revision A2 got problem with I2C driver.


    The problem is to do with I2C levels, it defaults to CMOS Schmitt levels. In almost all practical cases, I would suggest it’s a non-problem except for a few edge cases where it’s probably in risky marginal territory anyway. Certainly I haven’t found a problem with it, and I was aware of it particularly as I’d had a few problems getting it to work myself, turned out to be operator error and lack of DS reading.
    #10
    khalis5203
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2017/10/10 09:18:53
    • Location: 0
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/13 11:38:36 (permalink)
    0
    Howard Long
    khalis5203
    Hi, Please check your MCU errata datasheet. Currently PIC16F15313 with silicon revision A2 got problem with I2C driver.


    The problem is to do with I2C levels, it defaults to CMOS Schmitt levels. In almost all practical cases, I would suggest it’s a non-problem except for a few edge cases where it’s probably in risky marginal territory anyway. Certainly I haven’t found a problem with it, and I was aware of it particularly as I’d had a few problems getting it to work myself, turned out to be operator error and lack of DS reading.



    So you are very certain that your chip silicon revision A2 which has I2C problem instead of A3?. Currently I do not have tool to probe the clock signal of SCL. I did tried changed the default to TTL level but still no luck.
    #11
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/14 08:51:53 (permalink)
    +1 (1)
    khalis5203
    Howard Long
    khalis5203
    Hi, Please check your MCU errata datasheet. Currently PIC16F15313 with silicon revision A2 got problem with I2C driver.


    The problem is to do with I2C levels, it defaults to CMOS Schmitt levels. In almost all practical cases, I would suggest it’s a non-problem except for a few edge cases where it’s probably in risky marginal territory anyway. Certainly I haven’t found a problem with it, and I was aware of it particularly as I’d had a few problems getting it to work myself, turned out to be operator error and lack of DS reading.



    So you are very certain that your chip silicon revision A2 which has I2C problem instead of A3?. Currently I do not have tool to probe the clock signal of SCL. I did tried changed the default to TTL level but still no luck.



    My I suggest you get hold of a scope to probe?
     
    Or let us have the _entire_ project to look at? This is one of many problems of MCC, you end up with a ton of files when you want to demonstrate what could have been 20 lines of hand-written code.
    #12
    khalis5203
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2017/10/10 09:18:53
    • Location: 0
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/14 12:44:10 (permalink)
    0
    Hi Howard Long
    Definitely I will grab a scope to check the clock whether does it generates clock when performing I2C read.
    Previously you did mentioned that the I2C did worked on your PIC16F15313. Here I would like to know whether your silicon revision is A2 or A3. If yours also A2 and I2C did worked so well, it is worth for me to revisit my code and re-do without relying to MCC. Smile: Smile 
     
    #13
    Howard Long
    Super Member
    • Total Posts : 324
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/14 13:00:44 (permalink)
    0
    khalis5203
    Hi Howard Long
    Definitely I will grab a scope to check the clock whether does it generates clock when performing I2C read.
    Previously you did mentioned that the I2C did worked on your PIC16F15313. Here I would like to know whether your silicon revision is A2 or A3. If yours also A2 and I2C did worked so well, it is worth for me to revisit my code and re-do without relying to MCC. Smile: Smile 
     



    Connecting to MPLAB REAL ICE...
    Currently loaded firmware on REAL ICE
    Firmware Suite Version.....01.50.04
    Firmware type..............Enhanced Midrange
    Target voltage detected
    Target device PIC16F15313 found.
    Device ID Revision = 0x2002
     
    Looks like mine is Rev A2.
     
    I'd be happy to try your code for you if you can send the complete thing. The PIC16F153xx series are reasonably new, I would not be at all surprised if there's an MCC bug, I've had my fair share of them, particularly on newer parts, and that, among other things, is a key reason why I gave up with MCC after a few false starts and went back to bare metal.
    #14
    khalis5203
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2017/10/10 09:18:53
    • Location: 0
    • Status: offline
    Re: PIC16F15313 I2c Communication 2017/10/15 12:26:08 (permalink)
    0
    Hi Howard Long
     
    FYI, im still struggling to debug the reason why the I2C is not working on this PIC16F15313.
    Moreover, unable to borrow scope from my friend since he also busy debugging other stuffs. I  am using Curiosity board from Microchip to evaluate sensor that using I2C. I have made a few minor board configuration so that it could be used by 8-pins micro-controller. 
    I have removed most of the I2C part which was generated from MCC before and had integrated your I2C code into mine.
    From my observation, it seemed like to have problem when trying to send 'START'.
    Currently I am trying to port over the example from this link https://electronza.com/mplab-xpress-proximity-xc8-vcnl4010/3/   to PIC16F15313.
     
    Thanks
    #15
    qhb
    Superb Member
    • Total Posts : 5760
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC16F15313 I2c Communication 2017/10/15 14:08:19 (permalink)
    +1 (1)
    So you're still floundering around six days later, and still have not found the time to answer some basic question about your circuit...
     
    #16
    Jump to:
    © 2017 APG vNext Commercial Version 4.5