; 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: TSL2561, 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 TSL2561_ADDR 0x29 // (0x29 or 0x39 or 0x49) Table 1 Slave Address Selection #define TSL2561_CONTROL 0x80 // Table 3. Command Register #define TSL2561_ON 0x03 // Table 4. Control Register #define TSL2561_CH0 0xAC //Table 12. ADC Channel Data Registers #define TSL2561_CH1 0xAE //Table 12. ADC Channel Data Registers #define TSL2561_CH0_CH1 0x9C //Table 12. Dual ADC Channel Data Registers #define TSL2561_GAIN 0x81 // Table 5. Timing Register #define TSL2561_H_GAIN 0x12 // Gain X 16, 400 ms 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 readTRB[2]; // List of I2C transactions I2C1_MESSAGE_STATUS status; // I2C status unsigned int infrared, visiblePlusInfrared; 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. // Power On TSL2561 writeBuffer[0] = TSL2561_CONTROL; writeBuffer[1] = TSL2561_ON; I2C1_MasterWrite(writeBuffer, 2, TSL2561_ADDR, &status); while (status == I2C1_MESSAGE_PENDING); // Wait end of transaction // Set high gain (only indoor) writeBuffer[0] = TSL2561_GAIN; writeBuffer[1] = TSL2561_H_GAIN; I2C1_MasterWrite(writeBuffer, 2, TSL2561_ADDR, &status); while (status == I2C1_MESSAGE_PENDING); // Wait end of transaction while (1) { // Read CH1 and CH2 as 4 byte block writeBuffer[0] = TSL2561_CH0_CH1; // Data sent to slave (TSL2561 register for CH1) // Prepare Transaction Request Blocks (TRB) I2C1_MasterWriteTRBBuild(&readTRB[0], writeBuffer, 1, TSL2561_ADDR); I2C1_MasterReadTRBBuild(&readTRB[1], readBuffer, 4, TSL2561_ADDR); I2C1_MasterTRBInsert(2, readTRB, &status); // Insert TRB into I2C queue while (status == I2C1_MESSAGE_PENDING); // Wait end of transaction visiblePlusInfrared = (((unsigned int) readBuffer[1] << 8) + readBuffer[0]); infrared = (((unsigned int) readBuffer[3] << 8) + readBuffer[2]); __delay_ms(10); } }