Does this look like a reasonable (non-interrupt) example of using two SPI devices with different speeds, mode etc using the transactional SPI?
Edit I made a copy of Arduino 1.0.5-r2 with TeensyDuino 1.20-rc1 installed, then deleted the SPI folder in the linraries directory and added SPI from Paul's github. The above example compiles. Since it is using pretend devices A and B I can't test that it works though. This is the example in the HTML documentation (see next post).
Code:
#include <[color=#CC6600]SPI[/color].h> [color=#7E7E7E]// include the new SPI library:[/color]
[color=#7E7E7E]// using two incompatible SPI devices, A and B[/color]
[color=#CC6600]const[/color] [color=#CC6600]int[/color] slaveAPin = 20;
[color=#CC6600]const[/color] [color=#CC6600]int[/color] slaveBPin = 21;
[color=#7E7E7E]// set up the speed, mode and endianness of each device[/color]
[color=#CC6600]SPISettings[/color] settingsA(2000000, [color=#006699]MSBFIRST[/color], [color=#006699]SPI_MODE1[/color]);
[color=#CC6600]SPISettings[/color] settingsB(16000000, [color=#006699]LSBFIRST[/color], [color=#006699]SPI_MODE3[/color]);
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
[color=#7E7E7E]// set the Slave Select Pins as outputs:[/color]
[color=#CC6600]pinMode[/color] (slaveAPin, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color] (slaveBPin, [color=#006699]OUTPUT[/color]);
[color=#7E7E7E]// initialize SPI:[/color]
[color=#CC6600]SPI[/color].[color=#CC6600]begin[/color]();
}
[color=#CC6600]uint8_t[/color] stat, val1, val2, result;
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
[color=#7E7E7E]// read three bytes from device A[/color]
[color=#CC6600]SPI[/color].[color=#CC6600]beginTransaction[/color](settingsA);
[color=#CC6600]digitalWrite[/color] (slaveAPin, [color=#006699]LOW[/color]);
[color=#7E7E7E]// reading only, so data sent does not matter[/color]
stat = [color=#CC6600]SPI[/color].[color=#CC6600]transfer[/color](0);
val1 = [color=#CC6600]SPI[/color].[color=#CC6600]transfer[/color](0);
val2 = [color=#CC6600]SPI[/color].[color=#CC6600]transfer[/color](0);
[color=#CC6600]digitalWrite[/color] (slaveAPin, [color=#006699]HIGH[/color]);
[color=#CC6600]SPI[/color].[color=#CC6600]endTransaction[/color]();
[color=#7E7E7E]// if stat is 1 or 2, send val1 or val2 else zero[/color]
[color=#CC6600]if[/color] (stat == 1) {
result = val1;
} [color=#CC6600]else[/color] [color=#CC6600]if[/color] (stat == 2) {
result = val2;
} [color=#CC6600]else[/color] {
result = 0;
}
[color=#7E7E7E]// send result to device B[/color]
[color=#CC6600]SPI[/color].[color=#CC6600]beginTransaction[/color](settingsB);
[color=#CC6600]digitalWrite[/color] (slaveBPin, [color=#006699]LOW[/color]);
[color=#CC6600]SPI[/color].[color=#CC6600]transfer[/color](result);
[color=#CC6600]digitalWrite[/color] (slaveBPin, [color=#006699]HIGH[/color]);
[color=#CC6600]SPI[/color].[color=#CC6600]endTransaction[/color]();
}
Edit I made a copy of Arduino 1.0.5-r2 with TeensyDuino 1.20-rc1 installed, then deleted the SPI folder in the linraries directory and added SPI from Paul's github. The above example compiles. Since it is using pretend devices A and B I can't test that it works though. This is the example in the HTML documentation (see next post).
Last edited: