#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
// I2C defines
#define I2C_PORT ( i2c1 )
#define I2C_SDA ( 6 )
#define I2C_SCL ( 7 )
enum MCP23017_BANK0_ADR
{
ADR_IODIRA,
ADR_IODIRB,
ADR_IPOLA,
ADR_IPOLB,
ADR_GPINTENA,
ADR_GPINTENB,
ADR_DEFVALA,
ADR_DEFVALB,
ADR_INTCONA,
ADR_INTCONB,
ADR_IOCON,
ADR_IOCON_,
ADR_GPPUA,
ADR_GPPUB,
ADR_INTFA,
ADR_INTFB,
ADR_INTCAPA,
ADR_INTCAPB,
ADR_GPIOA,
ADR_GPIOB,
ADR_OLATA,
ADR_OLATB,
ADR_MAX
};
#define MCP23017_SADR ( 0x20 ) //0x2X
#define INIT_CMD_MAX ( 3 )
const uint8_t MCP23017_INIT_CMD[ INIT_CMD_MAX ][ 2 ] =
{
{ ADR_IOCON , 0x00 }, // default
{ ADR_IODIRA, 0x00 }, // all out
{ ADR_GPIOA , 0x00 }, // all low
};
int main()
{
stdio_init_all();
// I2C Initialisation. Using it at 400Khz.
i2c_init( I2C_PORT, 400*1000 );
gpio_set_function( I2C_SDA, GPIO_FUNC_I2C );
gpio_set_function( I2C_SCL, GPIO_FUNC_I2C );
gpio_pull_up( I2C_SDA );
gpio_pull_up( I2C_SCL );
// 初期化
{
for ( uint8_t i = 0; i < INIT_CMD_MAX; i++ )
{
i2c_write_blocking( I2C_PORT, MCP23017_SADR, &MCP23017_INIT_CMD[ i ][ 0 ], 2, false );
}
}
while ( 1 )
{
static uint8_t data = 0x01;
uint8_t buf[ 2 ];
buf[ 0 ] = ADR_GPIOA;
buf[ 1 ] = data;
i2c_write_blocking( I2C_PORT, MCP23017_SADR, &buf[ 0 ], 2, false );
sleep_ms( 300 );
data = (uint8_t)( ( data * 2 ) % 0xFF );
}
return 0;
}
コメント