micropython SPI syntax problem for nodemcu - Page 1
Forum section: electronics / general

author: john23
author_rank: Frequent Contributor
timestamp: August 17, 2024, 04:59:34 am
content: Hello, I am trying to communicate with DAC8004 using SPI and thonny micropython.from the manufacturer manual the periphery is pins 12-15.However when i try to run the following code of the manual, I get an error shown below that says thatI have a problem in the SPI command.Where did i go wrong implementing this command?Thanks.import machinefrom machine import SPI, Pinfrom machine import Pinfrom time import sleepcs=machine.Pin(15,machine.Pin.OUT)spi=machine.SPI(0,baudrate=1000000,polarity=0,phase=0,bits=8,firstbit=machine.SPI.MSB,sck=machine.Pin(14),mosi=machine.Pin(13),miso=machine.Pin(12))ERROR:Traceback (most recent call last):File "<stdin>", line 10, in <module>ValueError:>>>https://docs.micropython.org/en/latest/esp8266/quickref.html#hardware-spi-bushttps://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

author: RoGeorge
author_rank: Super Contributor
timestamp: August 17, 2024, 08:08:27 am
content: You are importingPinfrom themachinemodule twice, maybe that's why the error.I would remove the 3rd line completely (removefrom machine import Pin), then try again.

author: john23
author_rank: Frequent Contributor
timestamp: August 17, 2024, 09:52:30 am
content: Hello, I need to send the following command for my DAC8004.0000 0011 0000 1111 1111 1111 1111 0000Its 32bit command.So i need to lower CS send 4 times 8 bits and raise CS.from the manual below I see a sample of command spi.write(b"12345678").The problem with this command is that its not binary.In C i have written the following variables.uint8_t B7_B0=0b11110000; //first 4 are dont care,first 4 bits datauint8_t B15_B8=0b11111111;//data bitsuint8_t B23_B16=0b00000000;//channel A DATA total 0000 1111 1111 1111uint8_t B31_B24=0b00000011;//write to buffer and update DAC ,Write /rest dont careHow do i send these variables in micropython?Thanks.https://docs.micropython.org/en/latest/library/machine.SPI.html

author: ledtester
author_rank: Super Contributor
timestamp: August 17, 2024, 11:04:51 am
content: Try:spi.write(b"\xf0")or:spi.write((0xf0).to_bytes(1,"big")) // or maybe "little"or:Code:buf=bytearray(1)spi.readinto(buf, 0xf0)

author: john23
author_rank: Frequent Contributor
timestamp: August 17, 2024, 11:34:31 am
content: Hello , I have built the following code and defined hspi.I defined buf2 but as you can see below i cant send it.Where di i go wrong sending this variable?*************************************import machinefrom machine import Pin, SPIfrom time import sleephspi = SPI(1, baudrate=1000000, polarity=0, phase=0)#hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused)dc = Pin(4) # data/commandrst = Pin(5) # resetcs = Pin(15) # chip select, some modules do not have a pin for thiscs.value(0)cs.value(1)***********************************************>>> hspiHSPI(id=1, baudrate=1000000, polarity=0, phase=0)>>> buf23>>> hspi.write(buf)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: object with buffer protocol required>>>

author: ledtester
author_rank: Super Contributor
timestamp: August 17, 2024, 11:51:00 am
content: buf is a bytearraybuf is a bytethe .write method needs a bytearray not a byte

author: john23
author_rank: Frequent Contributor
timestamp: August 17, 2024, 06:14:31 pm
content: Hello,my command is, SPI is defined as MSB first. did i send the data properly?0000 0011 0000 1111 1111 1111 1111 0000MSB LSBimport machinefrom machine import Pin, SPIfrom time import sleephspi = SPI(1, baudrate=1000000, polarity=0, phase=0)#hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused)buf = bytearray(4)buf = 0b11110000 # B7_B0buf = 0b11111111 # B15_B8buf = 0b00000000 # B23_B16buf = 0b00000011 # B31_B24dc = Pin(4) # data/commandrst = Pin(5) # resetcs = Pin(15) # chip select, some modules do not have a pin for thiscs.value(0)# Send the binary byte over SPIhspi.write(buf[3:4]) # Send B31_B24hspi.write(buf[2:3]) # Send B23_B16hspi.write(buf[1:2]) # Send B15_B8hspi.write(buf[0:1]) # Send B7_B0cs.value(1)

author: ledtester
author_rank: Super Contributor
timestamp: August 17, 2024, 08:33:29 pm
content: Since you are writing the four bytes all at once you can just do:spi.write(b"\x03\x00\xff\xf0")or put them in the correct order to begin with and issue a single write call:buf = bytearray(4)buf = 3buf = 0buf = 255buf = 240spi.write(buf)I would expect the SPI write call to send the MSB first (as a default) but you'll have to check that yourself with a scope or logic analyzer.