; 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, write + read with Transaction Request Blocks /** 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 #define TC74_RTR 0 void main(void) { uint8_t readBuffer[10]; // Data from slave will be put here uint8_t writeBuffer[10]; // Data to write to slave I2C1_TRANSACTION_REQUEST_BLOCK TRB[2]; // List of I2C transactions I2C1_MESSAGE_STATUS status; // 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) { writeBuffer[0] = TC74_RTR; // Data sent to slave (TC74 register 0) // Prepare Transaction Request Blocks (TRB) I2C1_MasterWriteTRBBuild(&TRB[0], writeBuffer, 1, TC74_ADDR); I2C1_MasterReadTRBBuild(&TRB[1], readBuffer, 1, TC74_ADDR); I2C1_MasterTRBInsert(2, TRB, &status); // Insert TRB into I2C queue // wait for the message to be sent or status has changed. // while (status == I2C1_MESSAGE_PENDING); // if (status == I2C1_MESSAGE_COMPLETE); - Alle queue TRB sent... __delay_ms(1); } }