位置情報を知るための電圧値を表示させる

Dependencies:   mbed

adc.cpp

Committer:
sayan2
Date:
2020-02-28
Revision:
0:aaea4a22c294
Child:
1:c3989f45366c

File content as of revision 0:aaea4a22c294:

#include "mbed.h"

SPI mySPI(D11, D12, A4); // mosi, miso, sclk
DigitalOut cs(D10);
Serial pc(USBTX, USBRX); // tx, rx
float Vref = 3.3 ;

int main(){ 
            cs=1;
            mySPI.format(8,0); //Setup the spi for 10 bit data, spi mode 0
            mySPI.frequency(1000000); // second edge capture, with a 1MHz clock rate

        while (1){
            cs=0;
            int highByte = mySPI.write(0b01101000); //Send the command to read the slave data
            pc.printf("highByte: %d ",highByte);
            cs=1;
            cs=0;
            int lowByte = mySPI.write(0xFF); // send dummy byte to receive the slave data
            pc.printf("lowByte: %d ",lowByte);
            cs=1;
            cs=0;
            int wasteByte = mySPI.write(0x00);
            cs=1;

            int dataCh0 =  ((highByte << 8) + lowByte)&0x03FF;
            float volts = dataCh0*Vref /1024;
            pc.printf("CH0 %f V\n",volts);
            //cs=1;
            wait(3);
            }
}