• AVR Freaks

Hot!configuring PIC16F1825 as i2c slave via mcc

Author
kart
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
2020/06/09 05:51:10 (permalink)
0

configuring PIC16F1825 as i2c slave via mcc

Dear all,
 
I am trying to implement a solution where the PIC16F1825 has to act as a i2c slave for another type of hardware. I have used mcc to create the code. I choosed slave mode, 100kHz, Address 0x01.
 
mcc generated a huge set of single files in my source file directory but it seams that the generated main() doesn't even start the i2c bus at all.
 
CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
/*
    (c) 2018 Microchip Technology Inc. and its subsidiaries.
    
    Subject to your compliance with these terms, you may use Microchip software and any
    derivatives exclusively with Microchip products. It is your responsibility to comply with third party
    license terms applicable to your use of third party software (including open source software) that
    may accompany Microchip software.
    
    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.
    
    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.
*/

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // 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();

    
    
    while (1)
    {
        
        // Add your application code
    }
}
/**
 End of File
*/

 
Which of the many generated functions do I have to insert to get the i2c protocoll runinning on RC0, and RC1?
 
thanks
Michael
post edited by kart - 2020/06/09 06:10:28
#1

1 Reply Related Threads

    meanilkp
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/08/19 22:12:10
    • Location: India
    • Status: offline
    Re: configuring PIC16F1825 as i2c slave via mcc 2020/06/25 22:33:49 (permalink)
    0
    here is the EEPROM example implementation code for using MCC I2C Slave
    #include "mcc_generated_files/mcc.h"

    /*
                             Main application
     */
    #define SLAVE_EEPROM_SIZE             10
    /**
     Section: Global Variables
     */

    volatile uint8_t i2c1RxSlaveAddr = 0x00;
    volatile uint8_t i2c1EEPMemAddr = 0x00;
    volatile uint8_t EEPROM[SLAVE_EEPROM_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05,
                                                  0x06, 0x07, 0x08, 0x09, 0x0A};
    volatile bool isMemoryAddr = false;

    void EEPROM_SlaveRdDevAddrFromBus(void);
    void EEPROM_SlaveRdDataFromEepromWrToBus(void); // Master Read from EEPROM
    void EEPROM_SlaveRdDataFromBusWrToEeprom(void); // Master Write to EEPROM

    void main(void)
    {
        // initialize the device
        SYSTEM_Initialize();

        // 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();
        printf("PIC16F18875_I2C_SLAVE_TEST\r\n");
        I2C1_Open();
        I2C1_SlaveSetAddrIntHandler(EEPROM_SlaveRdDevAddrFromBus);
        I2C1_SlaveSetWriteIntHandler(EEPROM_SlaveRdDataFromEepromWrToBus);
        I2C1_SlaveSetReadIntHandler(EEPROM_SlaveRdDataFromBusWrToEeprom);

        while (1)
        {
            // Add your application code
        }
    }

    void EEPROM_SlaveRdDevAddrFromBus(void)
    {
        i2c1RxSlaveAddr = I2C1_Read();
        if(!I2C1_IsRead()) //Check for Write
        {
            isMemoryAddr = true;
        }
    }

    void EEPROM_SlaveRdDataFromEepromWrToBus(void)
    {
        if(i2c1EEPMemAddr >= SLAVE_EEPROM_SIZE)
        {
            i2c1EEPMemAddr = 0x00;
        }
        I2C1_Write(EEPROM[i2c1EEPMemAddr++]);
    }

    void EEPROM_SlaveRdDataFromBusWrToEeprom(void)
    {
        if(isMemoryAddr)
        {
            i2c1EEPMemAddr = I2C1_Read();
            if(i2c1EEPMemAddr >= SLAVE_EEPROM_SIZE)
            {
                i2c1EEPMemAddr = 0x00;
            }
            isMemoryAddr = false;
            return;
        }
        EEPROM[i2c1EEPMemAddr++] = I2C1_Read();
    }

    /**
     End of File
    */
    post edited by meanilkp - 2020/06/25 22:36:03
    #2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5