Board logo

标题: M8做的简易电子节拍器 [打印本页]

作者: yuchengze    时间: 2016-10-20 14:37     标题: M8做的简易电子节拍器

regfile = "m8def.dat" $crystal = 8000000
$hwstack = 32
$swstack = 10
$framesize = 40
Config Portd = Output                                       '0-6接LED A-G
Config Portb = Output                                       '0,1,2分别接LED共阳8,9,12    5接sound
Config Adc = Single , Prescaler = Auto , Reference = Avcc   '2接电位器
Config Timer1 = Timer , Prescale = 256                      '8-1us  64-8us 256-32us  1024-128us
'(
若要x拍 / 分钟,每拍的时间tp = 60 / X (秒) = 60000000 / X(微秒)
需要n个计数,n = Tp / 32
Counter1 = 65535 - N
')
On Timer1 Tim1_isr
Dim J As Integer
Dim V As Word , Oldv As Word , Count As Word
Dim S As Single
Dim Bpm As Byte , Idx As Byte
Dim Ss As String * 3 , Sled As String * 1
Osccal = &HB3
Start Adc
Enable Timer1
Enable Interrupts
Portb = 0
Portd = 0
Oldv = 0
Bpm = 30
Counter1 = 3035
Sound Portb.5 , 15 , 220
Do
   V = 0
   For J = 1 To 50
      V = V + Getadc(2)
   Next J
   S = V / 50
   V = Round(s)
   J = V - Oldv
   J = Abs(j)
   If J > 4 Then
      Oldv = V
      '计算节拍数  取值范围 30-230拍/分钟
      S = V / 1023
      S = S * 200
      S = S + 30
      Bpm = Round(s)
      '计算counter1
      S = 60000000 / Bpm
      S = S / 32
      V = Round(s)
      Count = 65535 - V
   End If
   '显示节拍数
   Ss = Str(bpm)
   Ss = Format(ss , "000")
   '显示个位
   Sled = Mid(ss , 3 , 1)
   Idx = Val(sled)
   Portd = Lookup(idx , Leddata )
   Portb.0 = 1
   Portb.1 = 0
   Portb.2 = 0
   Waitms 5
   '显示十位
   Sled = Mid(ss , 2 , 1)
   Idx = Val(sled)
   Portd = Lookup(idx , Leddata)
   Portb.0 = 0
   Portb.1 = 1
   Portb.2 = 0
   Waitms 5
   '显示百位
   Sled = Mid(ss , 1 , 1)
   Idx = Val(sled)
   If Idx = 0 Then
         Portb = 0
      Else
         Portd = Lookup(idx , Leddata)
         Portb.0 = 0
         Portb.1 = 0
         Portb.2 = 1
   End If
Loop
End

Tim1_isr:
   Counter1 = Count
   Sound Portb.5 , 20 , 220
Return

'LED数字 (0-9)
Leddata:
Data &B11000000 , &B11111001 , &B10100100 , &B10110000 , &B10011001
Data &B10010010 , &B10000010 , &B11111000 , &B10000000 , &B10010000

关键字:M8  电子节拍器




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0