2016年10月28日金曜日

I/OエキスパンダーのMCP23S17をNucleo F401RE(mbed)で使ってみる。

I/OエキスパンダーのMCP23S17をNucleo F401RE(mbed)で使ってみる。

ライブラリはRomilly CockingさんのMCP23S17 https://developer.mbed.org/users/romilly/code/MCP23S17/を使った。

配線図


配線はArduinoの場合とほぼ同じ。Nucleo F401REにはSPIが3つあるのでSPI3を使ってみた。

mbed:
https://developer.mbed.org/users/ryood/code/Nucleo_MCP23S17_Test/ Revision:2

/*
 * MCP23S17 Test
 *
 * https://developer.mbed.org/users/romilly/code/MCP23S17/
 *
 * mbed:  rev 121
 * mbed-rtos: rev 117
 *
 * Created: 2016.10.28
 *
 */

#include "mbed.h"
#include "rtos.h"
#include "MCP23S17.h"

#define OPCODE (0x40)

SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk
//SPI Spi(PA_7, PA_6, PA_5); // SPI1:  mosi, miso, sclk

// MCP23S17(SPI& spi, PinName ncs, char writeOpcode);
MCP23S17 Mcp23s17(Spi, PD_2, OPCODE);

int main()
{
    printf("\r\n\n*** MCP23S17 Test ***\r\n");

    // PORTA output
    Mcp23s17.direction(PORT_A, 0x00);

    // PORTB input
    Mcp23s17.direction(PORT_B, 0xFF);
    // PORTB pull-up
    Mcp23s17.configurePullUps(PORT_B, 0xFF);
    
    // LED Check
    for (int i = 0; i < 8; i++) {
        Mcp23s17.write(PORT_A, (1 << i));
        wait(0.2);
    }
    Mcp23s17.write(PORT_A, 0x00);
    
    while (true) {
        char data = ~Mcp23s17.read(PORT_B);
        Mcp23s17.write(PORT_A, data);
        
        //printf("%02x\r\n", data);
    }
}

メモ:

ポーリングだとSPI3を使っても動作できたが、割り込みは試してみたがうまく動かせなかった。MCP23S17から割込み信号が出力されない。

ポーリングで動作させてもオシロのプローブをSCK/MOSIあたりにあてるとLEDがふんわりと明るくなって点滅する。外せば正常。


0 件のコメント:

コメントを投稿