micropython SPI syntax problem for nodemcu

Thread Starter

yef smith

Joined Aug 2, 2020
1,446
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.


Code:
import machine
from machine import SPI, Pin
from machine import Pin
from time import sleep

cs=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-bus
https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf
1723869576091.png1723869554374.png
 

be80be

Joined Jul 5, 2008
2,394
Spi don't setup like that
Code:
import machine
from machine import Pin, SPI
from time import sleep

hspi = SPI(1)  # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
dc = Pin(4)    # data/command
rst = Pin(5)   # reset
cs = Pin(15)   # chip select, some modules do not have a pin for this
 

Thread Starter

yef smith

Joined Aug 2, 2020
1,446
Hello, I need to send the following command for my DAC8004.
0000 0011 0000 1111 1111 1111 1111 0000
Its 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 data
uint8_t B15_B8=0b11111111;//data bits
uint8_t B23_B16=0b00000000;//channel A DATA total 0000 1111 1111 1111
uint8_t B31_B24=0b00000011;//write to buffer and update DAC ,Write /rest dont care
How do i send these variables in micropython?
Thanks.

https://docs.micropython.org/en/latest/library/machine.SPI.html
1723886606977.png
 

Thread Starter

yef smith

Joined Aug 2, 2020
1,446
UPDATE:


Hello , I have built the following code and defined hspi.
I defined buf2[0] but as you can see below i cant send it.
Where di i go wrong sending this variable?


*************************************
import machine
from machine import Pin, SPI
from time import sleep
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
#hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
dc = Pin(4) # data/command
rst = Pin(5) # reset
cs = Pin(15) # chip select, some modules do not have a pin for this
cs.value(0)

cs.value(1)



***********************************************
>>> hspi
HSPI(id=1, baudrate=1000000, polarity=0, phase=0)
>>> buf2[0]
3
>>> hspi.write(buf[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object with buffer protocol required
>>>
 

Thread Starter

yef smith

Joined Aug 2, 2020
1,446
UPDATE:


Hello,my command is, SPI is defined as MSB first. did i send the data properly?
0000 0011 0000 1111 1111 1111 1111 0000
MSB *********************************** LSB


import machine
from machine import Pin, SPI
from time import sleep
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
#hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
buf = bytearray(4)
buf[0] = 0b11110000 # B7_B0
buf[1] = 0b11111111 # B15_B8
buf[2] = 0b00000000 # B23_B16
buf[3] = 0b00000011 # B31_B24

dc = Pin(4) # data/command
rst = Pin(5) # reset
cs = Pin(15) # chip select, some modules do not have a pin for this
cs.value(0)
# Send the binary byte over SPI
hspi.write(buf[3:4]) # Send B31_B24
hspi.write(buf[2:3]) # Send B23_B16
hspi.write(buf[1:2]) # Send B15_B8
hspi.write(buf[0:1]) # Send B7_B0
cs.value(1)
 
Last edited:
Top