本帖最后由 taotie 于 2019-11-16 22:19 編輯
在這個程序的基礎上還可以玩出許多花樣~~~
ws2812.PNG (63.82 KB, 下載次數: 79)
下載附件
2019-11-16 20:32 上傳
rem 老外的代碼:mega16 ,10Mhz外部晶振驅動WS2812彩帶
rem 運行環境:編譯器bascom-avr 仿真Proteus 8 .8
rem 功能:點亮1個WS2812x8彩帶,綠色亮度遞增
rem 兩個SUB過程800好用。400不知何原因待研究。
rem 仿真時需將芯片晶振屬性改為10Mhz外部晶振。
$regfile = "m16def.dat" ' specify the used micro
$crystal = 10000000 ' used crystal frequency
$hwstack = 64 ' default use 32 for the hardware stack
$swstack = 32 ' default use 10 for the SW stack
$framesize = 32 ' default use 40 for the frame space
Declare Sub Send_frame_800(byval Number_of_ic As Byte)
Declare Sub Send_frame_400(byval Number_of_ic As Byte)
' 包含每個通道的 PWM 值
' Gr?e 數組的 e 是根據 WS2811 控制器的數量 = 3 計算的
' 在示例程序中 3 × 3 = 9
Dim Led_buffer(9) As Byte '緩沖區9字節
' 用于測試的輔助變量
Dim Rnd_value As Byte
Dim Loop_counter As Byte ' 循環計數器變量
' 數據線的配置
Sdi Alias Portd.6
Config Portd.6 = Output
' --------- 主要程序--------------
Do
'For Loop_counter = 1 To 9
'Led_buffer(loop_counter) = Rnd(255) '隨機值載入緩存
'Call Send_frame_400(3)
'Call Send_frame_800(3) '發送幀
' Next Loop_counter '退出循環
'使用控制器進行測試
Led_buffer(1) =70 ' 綠色
Led_buffer(2) = 0 ' 紅
Led_buffer(3) =0 ' 藍色
Led_buffer(4) = 100 ' 綠色
Led_buffer(5) = 0 ' 紅
Led_buffer(6) =0 ' 藍色
Led_buffer(7) = 130 ' 綠色
Led_buffer(8) =0 ' 紅
Led_buffer(9) =0 ' 藍色
Led_buffer(10) = 160 ' 綠色
Led_buffer(11) =0 ' 紅
Led_buffer(12) =0 ' 藍色
Led_buffer(13) = 190 ' 綠色
Led_buffer(14) =0 ' 紅
Led_buffer(15) =0 ' 藍色
Led_buffer(16) = 220 ' 綠色
Led_buffer(17) =0 ' 紅
Led_buffer(18) =0 ' 藍色
Led_buffer(19) = 255 ' 綠色
Led_buffer(20) =0 ' 紅
Led_buffer(21) =0 ' 藍色
Led_buffer(22) = 255 ' 綠色
Led_buffer(23) =0 ' 紅
Led_buffer(24) =0 ' 藍色
Call Send_frame_800(3)
Waitms 300
Loop
'--------- Ende Hauptprogramm --------------
Sub Send_frame_800(byval Number_of_ic As Byte)
Local I As Byte
Local J As Byte
Local Last_channel As Byte
Local Temp As Byte
Local Mask As Byte
Last_channel = Number_of_ic * 3
Mask = 128
Sdi = 0
Waitms 1
' Bit Zeiten
' für eine 1, T1H 0.6 μs T1L 0.65 μs
' für eine 0, T0H 0.25 μs T0L 1.0 μs
push r0 ' enth?lt das aktuelle Byte des Kanals
push r16 ' Bitz?hler
push r17 ' Kanalz?hler
Loadadr Last_channel , X
ld r17,x
Loadadr Led_buffer(1) , X
Channel_loop_800:
ld r0,x+
ldi r16,8
Bit_loop_800:
lsl r0
brcc BIT_0_800
Bit_1_800:
Sbi $12 , 6
nop
nop
nop
nop
cbi $12 , 6
rjmp Next_bit_800
Bit_0_800:
Sbi $12 , 6
nop
cbi $12 , 6
Next_bit_800:
dec r16
cpi r16,0
brne BIT_LOOP_800
dec r17
cpi r17,0
brne CHANNEL_LOOP_800
pop r17
pop r16
pop r0
Waitus 60
End Sub
'******************************************
'
'
'******************************************
Sub Send_frame_400(byval Number_of_ic As Byte)Local I As Byte
Local J As Byte
Local Last_channel As Byte
Local Temp As Byte
Local Mask As Byte
Last_channel = Number_of_ic * 3
Mask = 128
Sdi = 0
Waitms 1
' Bit Zeiten
' für eine 1, T1H 1.2 μs T1L 1.3 μs
' für eine 0, T0H 0.5 μs T0L 2.0 μs
push r0 ' enth?lt das aktuelle Byte des Kanals
push r16 ' Bitz?hler
push r17 ' Kanalz?hler
Loadadr Last_channel , X
ld r17,x
Loadadr Led_buffer(1) , X
Channel_loop_400:
ld r0,x+
ldi r16,8
Bit_loop_400:
lsl r0
brcc BIT_0_400
Bit_1_400:
Sbi $12 , 6
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
cbi $12 , 6
nop
nop
rjmp Next_bit_400
Bit_0_400:
Sbi $12 , 6
nop
nop
nop
cbi $12 , 6
nop
nop
nop
nop
nop
nop
nop
nop
nop
Next_bit_400:
dec r16
cpi r16,0
brne BIT_LOOP_400
dec r17
cpi r17,0
brne CHANNEL_LOOP_400
pop r17
pop r16
pop r0
Waitus 60
End Sub
|