// PIC18 in C - Versione 0.1 - aprile 2016 // Copyright (c) 2016, Vincenzo Villa // Creative Commons | Attribuzione - Condividi allo stesso modo 4.0 Internazionale (CC BY-SA 4.0) // Creative Commons | Attribution-Share Alike 4.0 Unported // https://www.vincenzov.net/tutorial/PIC18/i2c-mcc.htm // PIC18F25K50 / MPLAB X 3.26 / XC8 1.37 / MCC 3.0.5 // I2C master: TC74, simple read /** Generated Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This is the main file generated using MPLAB(c) Code Configurator Description: This header file provides implementations for driver APIs for all modules selected in the GUI. Generation Information : Product Revision : MPLAB(c) Code Configurator - v3.00 Device : PIC18F25K50 Driver Version : 2.00 The generated drivers are tested against the following: Compiler : XC8 1.35 MPLAB : MPLAB X 3.20 */ /* Copyright (c) 2013 - 2015 released Microchip Technology Inc. All rights reserved. Microchip licenses to you the right to use, modify, copy and distribute Software only when embedded on a Microchip microcontroller or digital signal controller that is integrated into your product or third party product (pursuant to the sublicense terms in the accompanying license agreement). You should refer to the license agreement accompanying this Software for additional information regarding your rights and obligations. SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. */ #include "mcc_generated_files/mcc.h" #define TC74_ADDR 0x4D void main(void) { uint8_t readBuffer[10]; // Data from device will be read here I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_COMPLETE; // I2C status SYSTEM_Initialize(); // Initialize the device // Inside: I2C1_Initialize() - Initializes the MSSP // Manually set RB0 and RB1 as digital input INTERRUPT_GlobalInterruptHighEnable(); // Enable high priority global interrupts INTERRUPT_GlobalInterruptLowEnable(); // Enable low priority global interrupts. while (1) { for (int i = 0; i < 100; i++) MyLED_Toggle(); // Do something // if (status == I2C1_MESSAGE_COMPLETE) // Check if MSSP is idle I2C1_MasterRead(readBuffer, 1, TC74_ADDR, &status); // read 1 byte from slave for (int i = 0; i < 100; i++) MyLED_Toggle(); // Do something // Check I2C status (optional) // if (status == I2C1_MESSAGE_PENDING) MyLED1_SetHigh(); // if (status == I2C1_MESSAGE_FAIL) MyLED2_SetHigh(); __delay_us(300); } }