音乐编程

提供几个程序给大家,如果有兴趣可以下BASCOME-AVR试试看

LCD编程:

Config Lcdpin = Pin , E = Portd.0 , Rs = Portd.1 , Db4 = Portd.3
Config Lcdpin = Pin , Db5 = Portd.4 , Db6 = Portd.5 , Db7 = Portd.6
Config Lcd = 16 * 2
Deflcdchar 0 , 32 , 14 , 10 , 14 , 10 , 14 , 32 , 32
Deflcdchar 1 , 2 , 2 , 31 , 2 , 10 , 2 , 6 , 2
Deflcdchar 2 , 4 , 15 , 16 , 14 , 4 , 14 , 4 , 6
Deflcdchar 3 , 4 , 4 , 31 , 21 , 31 , 4 , 4 , 4             ' replace ? with number (0-7)' replace ? with number (0-7)' replace ? with number (0-7)' replace ? with number (0-7)         '在工具栏(TOOLS)内有造字工具。
Cls

Locate 2 , 3
Lcd "AM"
 Locate 1 , 6
Lcd Chr(0) ; Chr(1) ; "  " ; Chr(2) ; Chr(3)


 Config Lcdpin = Pin , Db4 = Portd.3 , Db5 = Portd.4 , Db6 = Portd.5 , Db7 = Portd.6
Config Lcdpin = Pin , E = Portd.0 , Rs = Portd.1
Config Lcd = 16 * 2
Dim S As Byte , M As Byte , H As Byte                       ' 定义秒、分、时的变量名


Do
Cursor Off                                                  '   光标关闭(不显示)
H = 9                                                       'H是小时
Locate 2 , 7                                                '开始
If H < 10 Then Lcd "0"
Lcd H ; ":"
For M = 30 To 59                                            'M是分钟
Locate 2 , 10
If M < 10 Then Lcd "0"
Lcd M ; ":"
For S = 00 To 59                                            '秒
Locate 2 , 13
 If S < 10 Then Lcd "0"
Lcd S
Waitms 50
 Next M = M + 1
Next S
Loop
End

编曲:伦敦桥

Config Portb = Output                                       'B口插数码管插件,静态显示要用短路插
Config Pind.6 = Output                                      ' D6口,音乐输出口
Config Pind.0 = Input                                       ' 使数码管位选失效
Config Pind.1 = Input                                       '使数码管位选失效
Config Pind.3 = Input                                       '接按钮
Config Pind.4 = Input                                       '接按钮
Config Pind.5 = Input                                       '接按钮
Portd.3 = 1                                                 '接上拉电阻
Portd.4 = 1                                                 '接上拉电阻
Portd.5 = 1                                                 '接上拉电阻
Dim S As Byte , F As Byte
Dim A As Word , B As Word , C As Word
Dim P As Byte , E As Byte , G As Byte
Do                                                          '演奏完毕回到等待选曲状态
   Do
      Waitms 200
      E = Pind And &B00111000                               '按钮检测
   Loop Until E < 56                                        '按钮未按下为56,<56时有按钮按下,退出循环
   If E = 48 Then Restore Music1                            '  根据数值确定按下的按钮,选出乐曲
   If E = 40 Then Restore Music2
   If E = 24 Then Restore Music3
Do
   Read S : Read G
   Select Case G
      Case 11 : F = 254                                     ' F=0,不显示
      Case 12 : F = 226                                     '根据F值,选择简谱各音的数码显示笔划译码
      Case 13 : F = 202
      Case 14 : F = 190
      Case 15 : F = 170
      Case 16 : F = 151                                     '低音3,左边最高位为1,小数点亮
      Case 17 : F = 135                                     '显示4,113为#4音数据,120为4音数据
      Case 1 : F = 127
      Case 2 : F = 113
      Case 3 : F = 101
      Case 4 : F = 95
      Case 5 : F = 85
      Case 6 : F = 75
      Case 7 : F = 67
      Case 21 : F = 63
      Case 22 : F = 56
      Case 23 : F = 51
      Case 24 : F = 48
      Case 25 : F = 42
      Case 26 : F = 37
      Case 27 : F = 33
   End Select
   Portb = P
   Waitms 20
   If S = 0 Then Exit Do
   A = 8000 / F
   B = A * S
   C = F * 5
   Sound Portd.6 , B , C
Loop
   Waitms 200
Loop
End
Music1:
Data 6 , 5 , 2 , 6 , 4 , 5 , 4 , 4 , 4 , 3 , 4 , 4 , 8 , 5
Data 4 , 2 , 4 , 3 , 8 , 4 , 4 , 3 , 4 , 4 , 8 , 5,
Data 6 , 5 , 2 , 6 , 4 , 5 , 4 , 4 , 4 , 3 , 4 , 4 , 8 , 5
Data 8 , 2 , 8 , 5 , 4 , 3 , 12 , 1
Data 0 , 0
Music2:
Data 4 , 1 , 4 , 2 , 4 , 3 , 4 , 4 , 4 , 5 , 4 , 6 , 4 , 7 , 4 , 27
Data 0 , 0
Music3:
Data 4 , 7 , 4 , 6 , 4 , 5 , 4 , 4 , 4 , 3 , 4 , 2 , 4 , 1
Data 0 , 0

贪吃蛇:

Config Portb = Output
Config Portd = Output
Dim A As Byte
Do
A = &B10000000
Do
Portb = A
Rotate A , Right , 1
Waitms 200
Loop Until A = &B10000000
Portb = &B00000000
Wait 1
A = &B00000001
Do
Portd = A
Rotate A , Left , 1
Waitms 200
Loop Until A = &B00000001
Portd = &B00000000
Loop
End

LED依次点亮,依次熄灭

Config Portb = Output
Dim A As Byte
Dim B As Byte
Dim C As Byte
Dim D As Byte
A = &B00000011
B = &B00000011

Do
Portb = A
Waitms 200
D = A
Rotate B , Left , 2
A = D Or B
Portb = A
Loop Until A = &B11111111
B = &B01111111
Do
Waitms 200
D = A
A = D And B
Rotate B , Right , 2
Portb = A
Loop Until A = &B00000000

End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值