LoginSignup
0
0

More than 5 years have passed since last update.

micro:bit / mbed / SPIデバイス(ADコンバーターMCP3008)を外づけする / それに意味のないとき

Last updated at Posted at 2018-07-02

概要

microbitにSPIデバイス(MCP3008)を外づけしてみる。前回はMicroPythonでプログラムを組んだが今度はmbedを利用する。mbed SPIクラスを利用する。

ソースコード

#include "MicroBit.h"
SPI            spi(MOSI, MISO, SCK);
MicroBitSerial serial(USBTX, USBRX);

// Use P16 as !CS pin.
MicroBitPin CS(MICROBIT_ID_IO_P16,
               MICROBIT_PIN_P16,
               PIN_CAPABILITY_DIGITAL);

class MCP3008{
    public:
        MCP3008(void);

        void     init(void);
        uint16_t read(char CH);
    //private:
};

MCP3008::MCP3008(void){
}
void MCP3008::init(void){
    CS.setDigitalValue(1);
    //spi.format(8, 0); // How many bits in one byte, and SPI mode0(0,0).
    //spi.frequency(1000000);
}
uint16_t MCP3008::read(char CH){
    char mosi[] = {1, 0b10000000 | CH<<4, 0};
    char miso[3];

    CS.setDigitalValue(0);
    for(int i=0; i<3; i++){
        miso[i] = spi.write(mosi[i]);
    }    
    CS.setDigitalValue(1);
    return  (miso[1]<<8 | miso[2]) & 1023;
}

int main(void){
    MCP3008 mcp; // Instantiate the MCP3008 class.
    mcp.init();  // Initialize the instance.

    while(1){
        uint16_t ADdata = mcp.read(0); // From which channel to read AD data.
        float    volt   = (float)ADdata * 3.23/1024.0; // 3.23 is the voltage measured at VREF pin.

        serial.printf("AD %d, %.2f(v)\n", ADdata, volt);
        wait(1.0);
    }

    release_fiber();
    return 0;
}

実行結果1

image.png

screenshot.png

実行結果2: serial.print()もwait()も入れずに実行したとき

サンプルレートは約18kspsであった。

screenshot.png

参考

Harry Fiarhead, micro:bit IoT in C, I/O Press, pp.131-146

関連

はてなブログ / ADコンバーターMCP3008 / SPI

0
0
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
0
0