LoginSignup
1
1

More than 3 years have passed since last update.

PIC16F18346でI2C通信【Master】

Posted at

概要

PIC16F18346をI2C Masterとして使用するためのテスト。
動作確認用SlaveとしてPCF8574T(アドレス:0x27)を接続。
MCC ver3.85.1を使用してコーディング。(MCCの導入については省略)

MCCの設定

  • Resource Manegement -> Device Resource: MSSP1を選択
  • MSSP1: 下記の内容で設定
Serial Protocol: I2C
Mode: Master
I2C Clock Frequency(Hz): 100000
Enable I2C Interrupt: チェックを入れる

※後述のMCCで自動生成されるサンプルコードでイベント割り込みを使用しているため、割り込みをENABLEにしている。

  • Pin Manager: SCL1とSDA1をどのピンに割り当てるか設定
  • Pin Module: SCL1とSDA1のStart High、Analog、Output、WPU、ODのチェックを外す
  • Resource Manegement: Generateをクリック

コーディング

500ms毎にI2C_TxBufferの中身を、アドレス0x27宛に送信する。

main.c
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#define PCF8574T_ADD 0x27
uint8_t I2C_TxBuffer;

/*
                         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
        I2C1_WriteNBytes(PCF8574T_ADD, &I2C_TxBuffer, 1);
        I2C_TxBuffer++;
       __delay_ms(500);
    }
}

ポイント

  • サンプルコードを使用するために i2c1_master_example.h をインクルードする。
  • INTERRUPT_GlobalInterruptEnable()、INTERRUPT_PeripheralInterruptEnable()のコメントアウトを解除する。

※I2Cの初期化は SYSTEM_Initialize() の中に入っているため不要。
※I2C1_WriteNBytes()の処理内容については i2c1_master_example.c を参照。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1