#M8废鼠标四位LED做多功能电子表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
M8废鼠标四位LED做的多功能电子表<说明和源程序)<附上原理图)
做饭总是忘了时间,结果经常吃糊饭,就用M8、四位LED数码管和废鼠标做了个电子表。
1.BASCOM编程,有电子表、闹钟、倒计时闹响功能<煮饭时用这个功能,哈哈)
2.四位LED显示时间,蜂鸣器闹响
3.废鼠标做外壳,保留鼠标三个键做模式转换、时间、闹钟调整
4.因为接线少,没有做PCB板,用导线直接连线,用热溶胶固定、绝缘。
图片附件:
图片附件:
图片附件:
'(
现在用的这个机器没装protel,没时间画电路图了,作个简单硬件说明:1、M8<1片),电阻8只<可选1-3k),四位7段led数码管1只<共阳),废鼠标1个,28IC座1个,电池1块,蜂鸣器1只 , 导线若干
2、portd口0-7顺序接led的A-G和DP,中间串8只电阻,led四个阳脚分别接portc口的1-4
3、portc.0接speaker;portb的0-2分别接鼠标中、左、右的微动开关,开关共地
4、我没有做pcb板,用热溶胶绝缘固定,防止短路。
几点说明:
1、使用内振,稳定性难以保证,若要高精度,建议用外振。
2、TCNT1的值可根据实验误差重新计算,此程序的是理论值。
3、LED数码管耗电较大,可选用lcd数码管
4、本来只想做个倒计时表放在厨房里,补来补去补成这个程序
5、程序由bascom-avr1.11.7.7编译通过,编译大小 5.14K
6、刚学单片机很短时间,错误和遗漏请批评指点,谢谢!
功能说明:
1、电子表、闹钟、倒计时闹响<以分钟为单位)
2、中键选择模式,切换电子表和倒计时
3、倒计时模式时,左键增加时间,右键减少时间,闹响时按键停止,重新倒计时,按中间键切换到电子表模式
4、电子表模式时,按先左后右键进入调整闹钟时间,小时闪烁,左加右减,按中间键切换到调整分钟,分钟闪烁,左加右减,再按中间键退出返回电子表模式
5、电子表模式时,按先右后左键进入调整时间状态,方法同上。
'>
$regfile = "m8def.dat"
$crystal = 8000000
Config Portb = Input
Config Portc = Output
Config Portd = Output
Config Timer1 = Timer , Prescale = 64 '8-1us 64-8us 256-32us 1024-
128us
Declare Sub Countdown(>
Declare Sub Disptime(byval Hh As Byte , Byval Mm As Byte>
Declare Sub Settime(>
Declare Sub Playmusic(>
Dim Leddig(10> As Byte
Dim Bsecond As Byte , Timecounter As Bit
Dim Set_minutes As Byte
Dim S As Single
Dim I As Integer , P As Integer , N As Integer
Dim K As Bit , Dotflash As Bit , Countmark As Bit , Enalarm As Bit
Dim Timeflash As Byte
Dim Myhour As Byte , Myminute As Byte
Dim Alarmhour As Byte , Alarmminute As Byte
Dim H As Byte , M As Byte
Portb = &HFF
Portd = &HFF
Portc = 0
'Tcnt1h = &H0B '赋定时器初始值 '3036 'Tcnt1l = &HDC
Counter1 = &H0BDC
Osccal = &HB8 '各芯片有不同校验值Readeeprom Set_minutes , 0 '读已存的倒计时设定分钟
Restore Leddata '读LED 显示位码<0-9)For P = 1 To 10
Read Leddig(p>
Next
Myhour = 11
Myminute = 11
Timeflash = 2
On Timer1 Tim1_isr
Enable Timer1
Enable Interrupts
Do
'============显示时间======== 此部分不要用DIDPTIME子程序,容易出现闪烁感 P = Myminute Mod 10
P = P + 1
Portc = &B00010
Portd = Leddig(p> '显示分钟个位 Waitms 5
S = Myminute / 10
P = Fix(s>
P = P + 1
Portc = &B00100
Portd = Leddig(p> '显示分钟十位 Waitms 5
P = Myhour Mod 10
P = P + 1
Portc = &B01000
Portd = Leddig(p> '显示小时个位
If Dotflash = 1 Then '闪烁小时和分钟中间两个分位点 Set Portd.7 '1秒钟亮灭1次
Else
Reset Portd.7
End If
Waitms 5
S = Myhour / 10
P = Fix(s>
P = P + 1
Portc = &B10000
Portd = Leddig(p> '显示小时十位 Waitms 5
'========== 切换倒计时 ======== If Pinb.0 = 0 Then '转换键<鼠标中间键)按下切换倒计时 Do
Loop Until Pinb.0 = 1
Call Countdown
End If
'===========闹时设置============= If Pinb.1 = 0 Then '先左后右两键同时按下
Do
Call Disptime(myhour , Myminute >
Loop Until Pinb.1 = 1 Or Pinb.2 = 0
If Pinb.2 = 0 Then
Do
Loop Until Pinb.1 = 1 And Pinb.2 = 1 '等待都抬起
Readeeprom Alarmhour , 1 '读存储的设定闹时 Readeeprom Alarmminute , 2
H = Alarmhour : M = Alarmminute
Call Settime(> 'alarmhour , Alarmminute >
Alarmhour = H : Alarmminute = M
Writeeeprom Alarmhour , 1 '存储闹时 Writeeeprom Alarmminute , 2
End If
End If
'===========时间设置============= If Pinb.2 = 0 Then
Do
Call Disptime(myhour , Myminute >
Loop Until Pinb.1 = 0 Or Pinb.2 = 1
If Pinb.1 = 0 Then '先右后左两键同时按下
Do
Loop Until Pinb.1 = 1 And Pinb.2 = 1 '等待都抬起
H = Myhour : M = Myminute
Call Settime(> 'myhour , Myminute >
Bsecond = 0 '重置时间 Myhour = H : Myminute = M
End If
End If
'==============闹钟响============ If Alarmhour = Myhour And Alarmminute = Myminute Then
If Alarmhour <> 0 And Alarmminute <> 0 Then
If Enalarm = 0 Then '若按键退出,防止在本分钟内重复播放 Call Playmusic
Enalarm = 1
End If
End If
End If
Loop
End
Sub Disptime(h As Byte , M As Byte>
If Timeflash = 0 Or Timeflash = 2 Then 'TIMEFLASH是时间闪烁设定特征值
P = Mm Mod 10
P = P + 1
Portc = &B00010
Portd = Leddig(p> '显示分钟个位 Waitms 5
S = Mm / 10
P = Fix(s>
P = P + 1
Portc = &B00100
Portd = Leddig(p> '显示分钟十位 Waitms 5
End If
If Timeflash = 1 Or Timeflash = 2 Then
P = Hh Mod 10
P = P + 1
Portc = &B01000
Portd = Leddig(p> '显示小时个位 If Dotflash = 1 Then '闪烁两个点 Set Portd.7
Else
Reset Portd.7
End If
Waitms 5
S = Hh / 10
P = Fix(s>
P = P + 1
Portc = &B10000
Portd = Leddig(p> '显示小时十位 Waitms 5
End If
End Sub
Sub Settime(>
Do
If Pinb.1 = 0 Then '设置小时 Waitms 200
H = H + 1
If H > 23 Then '左键加,右键减。
H = 0
End If
End If
If Pinb.2 = 0 Then
Waitms 200
If H = 0 Then
H = 24
End If
H = H - 1
End If
If Dotflash = 1 Then '设定小时闪烁
Timeflash = 0
Else
Timeflash = 2
End If
Call Disptime(h , M>
If Pinb.0 = 0 Then '按中间键转换为设置分钟 Do
Loop Until Pinb.0 = 1
Waitms 200
Do
If Pinb.1 = 0 Then '左键加,右键减。
Waitms 200
M = M + 1
If M > 59 Then
M = 0
End If
End If
If Pinb.2 = 0 Then
Waitms 200
If M = 0 Then
M = 60
End If
M = M - 1
End If
If Dotflash = 1 Then
Timeflash = 1
Else
Timeflash = 2
End If
Call Disptime(h , M>
If Pinb.0 = 0 Then ' 退出
Loop Until Pinb.0 = 1
Waitms 200
Timeflash = 2
Exit Sub
End If
Loop
End If
Loop
End Sub
Sub Countdown(> '倒计时子程序 '以分为单位 Readeeprom Set_minutes , 0 '本程序设置最大值为99分钟 '可根据自己喜好自行修改 Portc = 0
Portd = 1
Countmark = 1
Do
Portc.2 = 0
Portc.1 = 1
P = Set_minutes Mod 10 '取余显示个位 P = P + 1
Portd = Leddig(p>
Waitms 10
Portc.1 = 0
S = Set_minutes / 10 '取整显示十位 S = Fix(s>
Portc.2 = P
P = S + 1
Portd = Leddig(p>
Waitms 10
If Pinb.1 = 0 Then
Waitms 200
Set_minutes = Set_minutes + 1
If Set_minutes > 99 Then
Set_minutes = 1
End If
Writeeeprom Set_minutes , 0
End If
If Pinb.2 = 0 Then
Waitms 200
Set_minutes = Set_minutes - 1
If Set_minutes = 0 Then
Set_minutes = 99
End If
Writeeeprom Set_minutes , 0
End If
If Pinb.0 = 0 Then '按中间键退出切换到时间模式 Do
Loop Until Pinb.0 = 1
Countmark = 0
Exit Sub
End If
If Set_minutes = 0 Then
K = 1
Call Playmusic '到时后音乐闹响
Readeeprom Set_minutes , 0
Portc = 0
K = 0
End If
Loop
End Sub
Sub Playmusic(> '闹向音乐子程序 For N = 1 To 100
Restore Musicdata
Do
Read I : I = I * 3 '调整两参数使音乐听起来舒服些 Read P : S = P * 0.32 : P = Fix(s>
If I = 0 And P = 0 Then
Exit Do
End If
If Pinb.1 = 0 Or Pinb.2 = 0 Then '按左或右键退出 Do
Loop Until Pinb.1 = 1 And Pinb.2 = 1
Waitms 200
Exit For
End If
Sound Portc.0 , I , P
Waitms 100
If Countmark = 0 Then
Call Disptime(myhour , Myminute>
End If
Loop
Wait 2
Next N
End Sub
Tim1_isr: '中断程序 0.5秒
' Tcnt1h = &H0B '3036
' Tcnt1l = &HDC
Counter1 = &H0BDC
Dotflash = Not Dotflash
Timecounter = Not Timecounter
If Timecounter = 0 Then
Incr Bsecond
If Bsecond = 60 Then
If Countmark = 1 Then
Decr Set_minutes
End If
Bsecond = 0
Myminute = Myminute + 1
If Myminute = 60 Then
Myhour = Myhour + 1
Myminute = 0
End If
If Myhour = 24 Then
Myhour = 0
End If
Enalarm = 0
End If
End If
If K = 1 Then
Portc.1 = Not Portc.1
Portc.2 = Not Portc.2
Portc.3 = Not Portc.3
Portc.4 = Not Portc.4
End If
Return
Leddata: 'LED数字码 (0-9> Data &B11000000 , &B11111001 , &B10100100 , &B10110000 , &B10011001
Data &B10010010 , &B10000010 , &B11111000 , &B10000000 , &B10010000
Musicdata: '两只老虎的音乐,摘自张茂清那本书P166 Data 100% , 956% , 100% , 865% , 100% , 759% , 100% , 956%
Data 100% , 956% , 100% , 865% , 100% , 759% , 100% , 956%
Data 100% , 759% , 100% , 717% , 200% , 638%
Data 100% , 759% , 100% , 717% , 200% , 638%
Data 50% , 638% , 50% , 568% , 50% , 638% , 50% , 717%
Data 100% , 759% , 100% , 956%
Data 50% , 638% , 50% , 568% , 50% , 638% , 50% , 717%
Data 100% , 759% , 100% , 956%
Data 100% , 865% , 100% , 1276% , 200% , 956%
Data 100% , 865% , 100% , 1276% , 200% , 956% , 0% , 0%。