spi api
https://nodemcu.readthedocs.io/en/master/en/modules/spi/
引腳圖
IMG_6250.JPG
timg.jpg
0a4.png
0a3.png
接法
- SCLR(10腳): 低點(diǎn)平時(shí)將移位寄存器的數(shù)據(jù)清零肤粱。通常我將它接Vcc典奉。
- SCK(11腳):上升沿時(shí)數(shù)據(jù)寄存器的數(shù)據(jù)移位问潭。QA-->QB-->QC-->...-->QH;
下降沿移位寄存器數(shù)據(jù)不變干跛。(脈沖寬度:5V時(shí),大于幾十納秒就行了祟绊。我通常都選微秒級) - RCK(12腳):上升沿時(shí)移位寄存器的數(shù)據(jù)進(jìn)入數(shù)據(jù)存儲寄存器楼入,下降沿時(shí)存儲寄存器數(shù)據(jù)不變。通常我將RCK置為低電平牧抽,當(dāng)移位結(jié)束后嘉熊,在RCK端產(chǎn)生一個正脈沖(5V時(shí),大于幾十納秒就行了扬舒。我通常都選微秒級)阐肤,更新顯示數(shù)據(jù)。
- G(13腳): 高電平時(shí)禁止輸出(高阻態(tài))。如果單片機(jī)的引腳不緊張孕惜,用一個引腳控制它愧薛,可以方便地產(chǎn)生閃爍和熄滅效果。比通過數(shù)據(jù)端移位控制要省時(shí)省力衫画。
youtube教學(xué)
單595連通led
image.png
https://gist.github.com/lucsmall/66d9b6539df7a0daa569
youtube此視頻提供的代碼:
latch_pin = 2
gpio.mode(3,gpio.INPUT)
gpio.mode(latch_pin, gpio.OUTPUT)
gpio.write(latch_pin,gpio.LOW)
result = spi.setup(1,spi.MASTER,spi.CPOL_HIGH,spi.CPHA_LOW,8,0)
print(result)
index = 0x00ff
function next()
print(index, string.format("0x%02X", index))
spi.send(1,index)
gpio.write(latch_pin,gpio.HIGH)
gpio.write(latch_pin,gpio.LOW)
index = index+1
end
next()
gpio.trig(3,"up",next)
按照上面接線, 成功點(diǎn)亮, 但是貌似沒有邏輯, 邏輯分析儀顯示也和led亮滅相符
num | bitcode | led real | led should | correct ? |
---|---|---|---|---|
0 | 0000 | - | - | right |
1 | 0001 | - | 1 | wrong |
2 | 0010 | - | 2 | wrong |
3 | 0011 | 1,2 | 1,2 | right |
4 | 0100 | - | 3 | wrong |
5 | 0101 | - | 1,3 | wrong |
6 | 0110 | 3 | 2,3 | wrong |
7 | 0111 | 1,2,3 | 1,2,3 | right |
8 | 1000 | - | 4 | wrong |
9 | 1001 | - | 1,4 | wrong |
10 | 1010 | - | 2,4 | wrong |
11 | 1011 | 2 | 1,2,4 | wrong |
12 | 1100 | 3,4 | 3,4 | wrong |
13 | 1101 | 1,3,4 | 1,3,4 | right |
14 | 1110 | 3,4 | 2,3,4 | wrong |
15 | 1111 | 1,2,3,4 | 1,2,3,4 | right |
后來發(fā)現(xiàn)把spi的clock divide設(shè)置為1就正常了, 其他分頻都不正常, 未知原因, 沒有邏輯分析儀, 無法深入研究, 會的可以留言說明一下
雙595級聯(lián)
現(xiàn)在是正確的級聯(lián)方案,
latch_pin = 2
gpio.mode(3,gpio.INPUT)
gpio.mode(latch_pin, gpio.OUTPUT)
gpio.write(latch_pin,gpio.LOW)
result = spi.setup(1,spi.MASTER,spi.CPOL_LOW,spi.CPHA_HIGH,16,8)
print(result)
index = 0x00ff
function next()
print(index, string.format("0x%02X", index))
spi.send(1,index)
gpio.write(latch_pin,gpio.HIGH)
gpio.write(latch_pin,gpio.LOW)
index = index+1
-- if(index>255) then
-- index = 0
-- end
end
next()
gpio.trig(3,"up",next)
595級聯(lián)壓降問題
第二個595 led有高電平時(shí), 第一個595對應(yīng)的led壓降為0.23~0-26
第二個595 led無高電平時(shí), 第一個595對應(yīng)的led壓降為0.012
現(xiàn)象: 不管第一級led亮沒亮, 第二級led都不會很亮
16燈全亮狀態(tài)