http://mag.switch-science.com/2014/05/26/update_firmware_on_lpc1114fn28/
2台のmbedでSPI通信する
master
mbed LPC1768
#include "mbed.h" SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); Serial pc(USBTX, USBRX); DigitalOut led(LED1); #define DTIME 0.005 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); cs=1; int rbyte = (byte_1<<8)+byte_2; pc.printf("rbyte: %d\n",rbyte); led = 0; wait(3); } }
slave
mbed LPC1114FN28
#include "mbed.h" SPISlave device(dp2,dp1,dp6,dp25); // mosi, miso, sclk, ssel Serial pc(USBTX, USBRX); int main() { device.format(8,0); device.frequency(1000000); uint8_t hbyte = 7; uint8_t lbyte = 208; int counter = 0; while(1) { if(device.receive()) { 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; } } }
LPC1114FN28でSPI Slave動作
以下によれば、SSピンはdp25に設定する
SPI SlaveではCSが内部SPIブロックで決められていることが分かりました。
具体的には、CSをdp25に割り当てるということ。
http://small-diy-life.blogspot.jp/2016/12/mbed-enable-lpc1114-fdh28fn28spi.html
SPISlaveクラスのインスタンシーエトは以下のようにする
SPISlave device(dp2,dp1,dp6,dp25); // mosi, miso, sclk, ssel
mbed LPC1768とNucleoF401REでSPI通信する
master
mbed LPC1768
#include "mbed.h" SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); Serial pc(USBTX, USBRX); DigitalOut led(LED1); #define DTIME 0.005 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); cs=1; int rbyte = (byte_1<<8)+byte_2; pc.printf("rbyte: %d\n",rbyte); led = 0; wait(3); } }
slave
NucleoF401RE
#include "mbed.h" SPISlave device(D4,D5,D3,A2); // mosi, miso, sclk, ssel Serial pc(SERIAL_TX, SERIAL_RX); int main() { device.format(8,0); device.frequency(1000000); uint8_t hbyte = 7; uint8_t lbyte = 208; int counter = 0; while(1) { if(device.receive()) { 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; } } }
LPC1114FN28のAD変換値をSPIでmbed LPC1768に送信する
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); } }