0-5Vのスケールで計測したいが、mbedのADCでは3.3Vまでしか測れない
5V->3.3Vの分圧抵抗をはさむ
http://make.bcde.jp/circuit/5vから3-3vに変換する/
Slave
#include "mbed.h" SPISlave device(dp2,dp1,dp6,dp25); // mosi, miso, sclk, ssel Serial pc(USBTX, USBRX); AnalogIn ain(dp4); int main() { device.format(8,0); device.frequency(1000000); // uint8_t hbyte = 7; // uint8_t lbyte = 208; float fval; int ival; int counter = 0; while(1) { if(device.receive()) { fval = ain.read(); // Converts and read the analog input value (value from 0.0 to 1.0) ival = (int) (fval * 5000); // Change the value to be in the 0 to 3300 range uint8_t hbyte = ival / 256; uint8_t lbyte = ival % 256; int val = device.read(); pc.printf("received1: %d, ",val); device.reply(0xFF); int val2 = device.read(); pc.printf("received2: %d, ",val2); device.reply(hbyte); // Make this the next reply int val3 = device.read(); pc.printf("receive3: %d, ",val3); pc.printf("count: %d\n",counter); device.reply(lbyte); // Make this the next reply counter = counter+1; } } }
Master
#include "mbed.h" SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); Serial pc(USBTX, USBRX); DigitalOut led(LED1); #define DTIME 0.01 int main() { cs = 1; spi.format(8,0); spi.frequency(1000000); led = 0; while(1) { led = 1; cs=0; wait(DTIME); int byte_1 = spi.write(0x03); pc.printf("byte_1: %d ",byte_1); cs=1; cs=0; wait(DTIME); int byte_2 = spi.write(0xFF); pc.printf("byte_2: %d ",byte_2); cs=1; cs=0; wait(DTIME); int byte_waste = spi.write(0xFF); pc.printf("byte_waste: %d\n",byte_waste); cs=1; int rbyte = (byte_1<<8)+byte_2; pc.printf("rbyte: %d\n",rbyte); led = 0; wait(3); } }