2016年10月02日

るあこん:SPI を実装した

 るあこん に SPI を実装した。

  • spi.setup(channel, speed, mode, bpw): channel0/1/2 または 10/11/12, それぞれ SPI0, SPI1 の CE0/CE1/CE2 に対応。
  • spi.read(channel, table, length)
  • spi.write(channel, table, length)
  • spi.readwrite(channel, table, length)
  • spi.close(channel)

 こんな風に使う。ここでは、Microchip の MCP4922 (AD コンバータ) と MCP3204 (DA コンバータ) をそれぞれ SPI1 の CE0, CE2 に接続して、MCP4922 の出力を MCP3204 の入力と直結して、テストしている。

gpio.setup()
gpio.pinmode(22, 1) -- MCP4922 LDAC
gpio.dwrite(22, 1)
spi.setup(10, 1000000, 0, 8)
spi.setup(12, 1000000, 0, 8)
a = {}
for j = 0, 20 do
  for i = 0, 1 do
    v = (1 - i) * 4000
    a[1] = 0x30 + ((v >> 8) & 0x0f)
    a[2] = (v & 0xff)
    gpio.dwrite(22, 1)
    spi.readwrite(10, a, 2)
    gpio.dwrite(22, 0)
    wait(1)
    a[1] = 0x06 -- 0x06 for single channel, 0x04 for differential
    a[2] = 0x40 -- 0x00: CH0, 0x40: CH1
    a[3] = 0 -- Don't care
    spi.readwrite(12, a, 3)
    v2 = (a[2] & 0x0f) * 256 + (a[3] & 0xff)
    print(v, v2)
  end
end
Posted at 2016年10月02日 23:59:28
email.png