yefj
Advanced Member level 5
- Joined
- Sep 12, 2019
- Messages
- 1,539
- Helped
- 1
- Reputation
- 2
- Reaction score
- 5
- Trophy points
- 38
- Activity points
- 9,267
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))
import machine
from machine import Pin, SPI
from time import sleep
# Initialize SPI (HSPI)
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
# Buffer data to send
buf = bytearray(4)
buf[0] = 0b11110000 # B7_B0
buf[1] = 0b11111111 # B15_B8
buf[2] = 0b00000000 # B23_B16
buf[3] = 0b00000011 # B31_B24
# Initialize control pins
dc = Pin(4, Pin.OUT) # Data/command
rst = Pin(5, Pin.OUT) # Reset
cs = Pin(15, Pin.OUT) # Chip select
# Start transmission
cs.value(0) # Set CS low to enable communication
# Send data over SPI
hspi.write(buf) # Send all buffer data at once
cs.value(1) # Set CS high to end communication
Every oscilloscope that I've ever come across has the time going left to right. (If you want proof for yours, turn the timebase down to (say) 1 second per division and you will see the trace slowly going left to right.)Hello,I am not sure if my data on the osciloscope is going left to right or right to left.
NO. You need to understand the order that Micropython will access the elements of the array and set the array values accordingly. As it will send the index '0' first, and you need to send the data with MSB first, then the value for index '0' needs to be the most significant byte, then for index '1' the next 8 bits etc..we need to send the command MSB first.
So first i send buf[3] then buf[2] then buf[1] then buf[0].
However Python sends each byte in reverse order, So maybe i should try to do LSB first?
I don't mean to be rude but you really do need to learn to read and understand the documentation. (Also I thin k you need to stop simply copying code from elsewhere without understanding what is does.) The first figure in your post #2 shows all of the parameters that can be passed to the function - including one called 'firstbit'. Actually you use exactly that parameter in the code in that same post.How do i cahnge the SPI object hspi = SPI(1, baudrate=1000000, polarity=0, phase=0) to be LSB first?
(See above for how to correct this code.)0000 0011 0000 0000 1111 1111 1111 0000
MSB*********************************LSB
buf[0] = 0b11110000 # B7_B0
buf[1] = 0b11111111 # B15_B8
buf[2] = 0b00000000 # B23_B16
buf[3] = 0b00000011 # B31_B24
import machine
from machine import Pin, SPI
from time import sleep
# Initialize SPI (HSPI)
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
# Buffer data to send
buf = bytearray(4)
buf[3] = 0b11110000 # B7_B0
buf[2] = 0b11111111 # B15_B8
buf[1] = 0b00000000 # B23_B16
buf[0] = 0b00000011 # B31_B24
# Initialize control pins
dc = Pin(4, Pin.OUT) # Data/command
rst = Pin(5, Pin.OUT) # Reset
cs = Pin(15, Pin.OUT) # Chip select
# Start transmission
cs.value(0) # Set CS low to enable communication
# Send data over SPI
hspi.write(buf) # Send all buffer data at once
cs.value(1) # Set CS high to end communication
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?