/* C言語&MCCによるPICプログラミング大全 * p324 リスト5-2-5 */ #include "mcc_generated_files/mcc.h" #include "proc/pic16f1503.h" #define LEDA RA5 #define LEDC RC5 //変数定義 uint8_t wState, rState; uint16_t result; /************************ * I2C Read Callback ************************/ void I2C_ReadProcess(void) { switch (rState) { case 0: LEDA = (I2C_Read() == 0xa0) ? 1 : 0; //1バイト目受信 rState++; break; case 1: LEDC = (I2C_Read() == 0x0a) ? 1 : 0; //2バイト目受信 rState++; break; case 2: LEDA = LEDC = (I2C_Read() == 0x41) ? 0 : 1; //3バイト目受信 rState = 0; break; default: rState = 0; break; } } /************************ * I2C Write Callback ************************/ void I2C_WriteProcess(void) { //データ受信 switch (wState) { case 0: I2C_Write(0x41); //1バイト目送信 wState++; break; case 1: I2C_Write(0x42); //2バイト目送信 wState++; break; case 2: I2C_Write(0x43); //3バイト目送信 wState++; break; case 3: I2C_Write(0x44); //4バイト目送信 wState++; break; case 4:wState = 0; //送信終了 default: break; } } /** 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.81.8 Device : PIC16F1503 Driver Version : 2.00 */ /* (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. */ /* 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(); /************************ * ここから追記 I2Cスレーブ処理 ************************/ I2C_Open(); I2C_SlaveSetReadIntHandler(I2C_ReadProcess); I2C_SlaveSetWriteIntHandler(I2C_WriteProcess); //割込許可 INTERRUPT_GlobalInterruptEnable(); //GIP=1; INTERRUPT_PeripheralInterruptEnable(); //PIE=1 /************************ * ここまで ************************/ while (1) { // Add your application code } } /** End of File */