⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rr1050 ver10 pir.bas

📁 AVR PWM Audio player from FAT16 MMC version11
💻 BAS
📖 第 1 页 / 共 2 页
字号:
'**************************** COMPILER INSTRUCTIONS ****************************

$crystal = 11059200
$regfile = "m8def.dat"
$baud = 57600

'********************************** ALIASES ************************************


' MMC
Cs Alias Portc.3
Clk Alias Portc.0
Mosi Alias Portc.2
Miso Alias Pinc.1

' Audio output
Ppwm Alias Portb.1

' Mute
Mute Alias Portd.4

' Tipke
Tipka Alias Pind.5

'PIR
Pir Alias Pind.6


'****************************** HARDWARE CONFIG ********************************

' MMC
Config Pinc.3 = Output
Config Pinc.0 = Output
Config Pinc.2 = Output
Config Pinc.1 = Input
Portc.1 = 1
Cs = 1

' Audio output
Config Pinb.1 = Output
Portb.1 = 0

' Tipka
Config Pind.5 = Input

' mute
Config Pind.4 = Output
Mute = 1

'PIR
Config Pind.6 = Input

' I2C config
Config Scl = Portc.5
Config Sda = Portc.4
Config I2cdelay = 30


'configurates watchdog timer
Config Watchdog = 256
Start Watchdog


' Timer for randomization
Config Timer0 = Timer , Prescale = 1024
On Ovf0 Randomizetimer
Enable Interrupts
Enable Timer0
Start Timer0


' Timer for PWM output
Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Down


On Ovf1 Changepwm
Disable Timer1
Stop Timer1
Pwm1a = 0
Pwm1b = 0


'**************************** VARIABLE DECLARATIONS ****************************

Dim Startid As Eram Byte

' za MMC protokol
Dim Mcmd As Byte
Dim Maddr As Long
Dim Mdata As Byte
Dim Mresp As Byte                                           'MMC response type1
Dim Mmccid As Word                                          'part of MMC CID
Dim Mdat(512) As Byte                                       'Matrica for data
Dim Readsector As Long                                      'adresa sektora
Dim Mmcrestarted As Bit                                     'MMC is inserted

' file sistem var's
Dim Filesystemtype As Byte                                  'type of file system
Dim Brsector As Long                                        'boot rector
Dim Fatno As Byte                                           'no of fat tables
Dim Maxroot As Word                                         'max root directory entries
Dim Sectorsperfat As Word                                   'sectors per fat table
Dim Sectorspercluster As Byte
Dim Reservedsector As Word                                  'reserved sector size
Dim Fatsector As Long                                       'FAT tables
Dim Dirsector As Long                                       'directory entries
Dim Datasector As Long                                      'data start
Dim Systemfound As Bit
Dim Bpointer As Word                                        'buffer pointer
Dim Filename As String * 8                                  'filename
Dim Fileextension As String * 3                             'fileextension
Dim Filesector As Long
Dim Filesize As Long
Dim Eofsector As Long
Dim Eofbyte As Word
Dim Filefound As Bit

'random file
Dim Randomfilenumber As Byte
Randomfilenumber = Rnd(0)                                   'to fix bascom bug

'time variables
Dim Ts As Byte
Dim Tm As Byte
Dim Th As Byte
Dim Ty As Byte
Dim Tw As Byte

'alarm variables
Dim Noalarms As Byte
Dim Tas(50) As Byte
Dim Tam(50) As Byte
Dim Tah(50) As Byte
Dim Taw(50) As Byte

'play variables
Dim Parttoread As Bit
Dim Partloaded As Bit
Dim Playend As Bit

'temporary variables
Dim Tempstring As String * 8
Dim Tempword As Word
Dim Templong As Long
Dim Tempbyte As Byte
Dim Q As Word                                               'za petlje
Dim W As Word

'****************************** SUB DECLARATION ********************************

Declare Sub Mmcinit
Declare Sub Mmccmd
Declare Sub Mmccheck
Declare Sub Mmcget1stpart
Declare Sub Mmcget2ndpart
Declare Sub Mmcstartread
Declare Sub Mmcread
Declare Sub Mmcwrite

Declare Sub Getsysteminfo
Declare Sub Getfileinfo
Declare Sub Getendoffile

Declare Sub Getalarms
Declare Sub Checkalarm
Declare Sub Gettimetoset
Declare Sub Getrandomfile

Declare Sub Playfile

Declare Sub Settime
Declare Sub Gettime

Declare Function Getlong(byval Pocetak As Word ) As Long
Declare Function Clustertosector(byval Cluster As Word) As Long

Declare Sub Printbuffer


'***************************** MAIN PROGRAM ************************************

Waitms 100
Reset Watchdog

'Print "Atmel started"

' main loop

Do
 Mainloop:
 Mmccheck
 If Mmcrestarted = 1 Then
  Reset Watchdog
  Getsysteminfo
   Reset Watchdog
  'Getalarms
  ' Reset Watchdog
  Mmcrestarted = 0                                          '
 End If

 Reset Watchdog
 If Pir = 0 Then
   Getrandomfile
   Reset Watchdog
   Playfile
   Reset Watchdog
 End If
 If Tipka = 0 Then
  'Reset Watchdog
  'Gettimetoset
  Reset Watchdog
  Getrandomfile
  Reset Watchdog
  Playfile
  Reset Watchdog
 End If
 Reset Watchdog
 Waitms 50
 'Checkalarm
Loop

End                                                         'end program



'******************************** CHANGE PWM ***********************************

' on timer1 (PWM) interrupt
' changes pwm value to next value from buffer

Changepwm:
  Pwm1a = Mdat(bpointer)
  Bpointer = Bpointer + 1
  If Bpointer = 513 Then                                    '513
    Bpointer = 1
    Parttoread = 1
    Partloaded = 0
  End If
  If Bpointer = 257 Then
    Parttoread = 0
    Partloaded = 0
  End If
Return


'******************************* RANDOMIZE TIMER *******************************

' on timer0 interrupt
' changes random number for better randomization
Randomizetimer:
 ___rseed = ___rseed + 1
Return


'****************************** PLAY FILE **************************************

Sub Playfile
 'play file written in filename, fileextension
 Getfileinfo
 If Filefound = 0 Then
  Do
  Loop
 End If

 Readsector = Filesector

 Mmcread
 Playend = 0
 Bpointer = 1
 Partloaded = 1

 Reset Watchdog
 Reset Timer1
 Enable Timer1
 Start Timer1
 Mute = 0

 Do
  If Partloaded = 0 Then                                    'if part needs to be readed
   If Parttoread = 0 Then                                   'if part0 is need to be readed
     Readsector = Readsector + 1
     Mmcget1stpart
     Partloaded = 1
     Reset Watchdog
   Else                                                     'if part1 is need to be readed
     Mmcget2ndpart
     Partloaded = 1
     Reset Watchdog
   End If
  End If
  If Readsector => Eofsector Then
     Mute = 1
     Waitms 50
     Reset Watchdog
     Waitms 50
     Stop Timer1
     Disable Timer1
     Playend = 1
  End If

 Loop Until Playend = 1
 Reset Watchdog
 If Parttoread = 0 Then
   Mmcget2ndpart                                            'to get left bytes
 End If
End Sub


'******************************* GET RANDOM FILE *******************************

Sub Getrandomfile
 '1st entry - no of files (0-255)
 'entry format:
 'NNNNNNNN
 'N - short filename format
 Randomfilenumber = 0
 Filename = "DATOTEKE"
 Fileextension = "CFG"
 Getfileinfo
 Tempbyte = 0
 If Filefound = 1 Then
  Readsector = Filesector - 1

  Do
   Reset Watchdog
   Readsector = Readsector + 1
   Mmcread
   If Readsector < Eofsector Then
    Tempword = 512
   Else
    Tempword = Eofbyte
   End If
   Tempword = Tempword - 1
   For Q = 1 To Tempword Step 8
    Incr Tempbyte                                           'limit to 255 files
    If Tempbyte = 1 Then
      Randomfilenumber = Rnd(mdat(1))
      Randomfilenumber = Randomfilenumber + 2
    End If

    If Tempbyte = Randomfilenumber Then
     Filename = ""
     Fileextension = "RAW"
     For W = 0 To 7
      Bpointer = Q + W
      If Mdat(bpointer) <> &H20 Then
       Filename = Filename + Chr(mdat(bpointer))
      End If
     Next W
     Filename = Ucase(filename)
    End If

   Next Q
  Loop Until Readsector => Eofsector
 End If

End Sub


'*********************************** CHECK ALARM *******************************

Sub Checkalarm
 Gettime
  For Q = 1 To Noalarms
   If Tah(q) = Th Then
    If Tam(q) = Tm Then
     If Tas(q) = Ts Then
      ' Print "Alarm at:";
      ' Print Hex(th) ; ":" ; Hex(tm) ; ":" ; Hex(ts)
       Getrandomfile
       Playfile
     End If
    End If
   End If
  Next Q
End Sub



'********************************** GET ALARMS *********************************

Sub Getalarms
 'entry format:
 'SMHWNNXX
 'S - seconds BCD
 'M - minutes BCD
 'H - hours BCD
 'W - Weekday (Pon Uto Sri Cet Pet Sub Ned Praznik)
 'NN - first file in "files.cfg" to play
 'XX - reserved for future use

 Noalarms = 0
 Filename = "ALARM"
 Fileextension = "CFG"
 Getfileinfo
 If Filefound = 1 Then
 Readsector = Filesector - 1
 Do
  Reset Watchdog
  Readsector = Readsector + 1
  Mmcread
  If Readsector < Eofsector Then
    Tempword = 512
   Else
    Tempword = Eofbyte

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -