📄 lcd+定时做的流水灯.bas
字号:
'*****************************************************************
'超级流水灯程序
'程序语言:BASCOM-AVR
'编译版本BASCOM-AVR 1.11.8.3
'本程序利用定时器做的流水灯,分五种花样,五种速度,两个按键控制
'程序编译、仿真通过
'参考WWW.BASCOMAVR.COM网站的定时器做的流水灯
'******************************************************************
$regfile = "m16def.dat" ' 定义目标单片机是ATmega88
$crystal = 8000000
'/////LCD1602 定义//////// ////////
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5
Config Lcd = 16 * 2
Dim S As String * 16
'//////////定时器中断定义//////////
Config Timer2 = Timer , Prescale = 1024 ' 定义单片机时钟周期是1000000Hz
Enable Interrupts '允许全局中断 'timer0中断溢出时间=200ms
Enable Timer2
On Timer2 Timer2_isr
Timer2 = &HD3
Config Timer1 = Timer , Prescale = 8
Enable Timer1
On Timer1 Key_isr
'////////输入输出定义/////////////////////
Config Pind.2 = Input
Config Pind.3 = Input
Portd.2 = 1
Portd.3 = 1
Config Porta = Output
'///////////变量定义/////////////////////////
Dim Key1 As Bit , Key2 As Bit
Dim A As Byte
Dim T As Integer
Dim G As Byte
Dim N As Byte
Dim M As Byte
Dim Tcounter As Byte
'/////////////程序初始化/////////////////////
Key1 = Pind.2
Key2 = Pind.3
Tcounter = 0
T = 0
N = 0
M = 0
A = 0
G = 1
Start Timer1
Cursor Off
Cls
S = "Ready? go!"
Waitms 10
Lcd S
Do
Porta = &HFF
Waitms 50
Porta = Not Porta
Waitms 50
Loop Until Pind.2 = 0
Cls
Cursor Off
S = "*speedis:fastest"
Waitms 10
Lcd S
Locate 2 , 1
S = "*style is:"
Waitms 10
Lcd S
Start Timer2
'////////////////////主程序开始工作////////////////////
Star:
Do
If A < 5 Then
On A Gosub S0 , S1 , S2 , S3 , S4
Delay
Locate 2 , 11
Delay
Lcd S
End If
If A <= 4 Then
On A Gosub Huayang0 , Huayang1 , Huayang2 , Huayang3 , Huayang4
Else
A = 0
End If
Loop
End
'//////////////定时器2中断服务程序控制花样运行方式///////////////
Timer2_isr:
Timer2 = &HD3
Tcounter = Tcounter + 1
If Tcounter > T Then
If N = 7 Then
M = 1
Else
If N = 0 Then M = 0
End If
If M = 0 Then
Incr N
Else
If M = 1 Then
Decr N
End If
Enable Interrupts
End If
Tcounter = 0
End If
Return
'//////////////定时器1中断服务程序扫描键盘/////////////////////
Key_isr:
Key1 = Pind.2
Key2 = Pind.3
If Key1 = 0 Then
Incr A
End If
If Key2 = 0 Then
G = G + 1
Select Case G
Case 2 : S = "faster "
Case 3 : S = "normal "
Case 4 : S = "slower "
Case 5 : S = "slowest"
Case Else : S = "fastest"
End Select
Locate 1 , 10
Waitms 5
Lcd S
End If
If G <= 5 Then
T = G * 5
Else
G = 1
End If
Return
'///////////////花样//////////////////
Huayang0:
Select Case N
Case 0
Porta = &HFE
Case 1
Porta = &HFD
Case 2
Porta = &HFB
Case 3
Porta = &HF7
Case 4
Porta = &HEF
Case 5
Porta = &HDF
Case 6
Porta = &HBF
Case 7
Porta = &H7F
End Select
Return
Huayang1:
Select Case N
Case 0
Porta = &B00000001
Case 1
Porta = &B00000010
Case 2
Porta = &B00000100
Case 3
Porta = &B00001000
Case 4
Porta = &B00010000
Case 5
Porta = &B00100000
Case 6
Porta = &B01000000
Case 7
Porta = &B10000000
End Select
Return
Huayang2:
Select Case N
Case 0
Porta = &B00111111
Case 1
Porta = &B11001111
Case 2
Porta = &B11110011
Case 3
Porta = &B11111100
Case Else
N = 0
End Select
Return
Huayang3:
Select Case N
Case 0
Porta = &B01111110
'
Case 1
Porta = &B10111101
Case 2
Porta = &B11011011
Case 3
Porta = &B11100111
Case 4
Porta = &B11011011
Case 5
Porta = &B10111101
Case Else
N = 0
End Select
Return
Huayang4:
Select Case N
Case 0
Porta = &B10101010
Case 1
Porta = Not Porta
Case Else
N = 0
End Select
Return
'//////////////////显示字符/////////////////////
S1:
S = "two "
Return
S2:
S = "three"
Return
S3:
S = "four "
Return
S4:
S = "five "
Return
S0:
S = "one "
Return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -