yuanjingjie 发表于 2010-6-22 21:38:19

请教马老师BASCOM-AVR利用AVR-DOS库读写SD卡,U盘,CF卡等外部存储的原理

最近在BASCOM的官方网站上看到一个用BASCOM程序写的MP3资料,里面的程序不是很懂
http://cache.amobbs.com/bbs_upload782111/files_30/ourdev_563294OM1RNA.jpg
实物图 (原文件名:MP3BOARD.jpg)

http://cache.amobbs.com/bbs_upload782111/files_30/ourdev_563295SPPUAH.jpg
原理图 (原文件名:MP3SCH.jpg)




程序如下,希望马老师能帮忙解释一下
'///////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////
'
' MP_3_PLAYER with VS1001k FOR MP3.FILES FORMAT Name 8.3 FAT16 AVR-DOS 5.5
' data 14-06-2007 versione BASCOM 1.8.11.3AVR-DOS 5.5
' written by GIOVANNI DE LUCA
' HTTP://delucagiovanni.interfree.it             robot applications and others
' HTTP://WWW.LNS.INFN.IT                         nuclear phisics applications
' HTTP://www.youtube.com/tubincolo               my video mobile-robots
' deluca@lns.infn.it                           my e-mail
' to control it's used serial port com1 and pul "UP" to change songs
' to make test I used 64Mb MMC with 20 songs in the MMC
' but I thing that it's possible read bigger MMC memory without problem
'
'///////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////

$prog &HFF , &H4F , &HC5 , &HFF                           'FUSE BITS generated
$regfile = "M128def.dat"                                    'IT'S POSSIBLE USE DIFFERENT AVR-CHIPS
$crystal = 14745600                                       'BUT this NOT TESTED
$baud = 115200
'-------------------------------------------------------------------------------
Config Timer1 = Timer , Prescale = 64
Config Clock = Soft
Enable Interrupts
Enable Timer1
Enable Urxc                                                 'abilita rs232
On Timer1 Timer_1
On Urxc Rs_232                                              'salto per rs232
Config Date = Dmy , Separator = .
'-------------------------------------------------------------------------------
Avanti Alias Ping.0
   Led Alias Portg.1
Vs_miso Alias Pinc.0
Vs_mosi Alias Portc.1
Vs_clock Alias Portc.2
   Xcs Alias Portc.3
Xreset Alias Portc.4
    Dreq Alias Pinc.5
   Bsync Alias Portc.6
'-------------------------------------------------------------------------------
Config Avanti = Input                                       'pulsante avanti
Config Led = Output                                       'led
Config Vs_miso = Input                                    'serial input vs-1001
Config Vs_mosi = Output                                     'serial out   vs-1001
Config Vs_clock = Output                                    'serial clock vs-1001
Config Xcs = Output                                       'VS Chip Select
Config Xreset = Output                                    'VS Reset
Config Dreq = Input                                       'VS Data Request
Config Bsync = Output                                       'VS Stream Request
'-------------------------------------------------------------------------------
Declare Sub Vs_init()
Declare Sub Soft_reset()
Declare Sub Vs_write(byval Vs_address As Byte , Byval Data1 As Byte , Byval Data2 As Byte)
Declare Function Vs_read(byval Vs_address As Byte) As Word
Declare Sub Send_mp3(byval Filename As String)
'-------------------------------------------------------------------------------
Const Vs_rd = &B0000_0011                                 'Write command vs1001
Const Vs_wr = &B0000_0010                                 'Read commandvs1001
Const Vs_mode = 0
Const Vs_status = 1
Const Vs_clockf = 3
Const Vs_decode_time = 4
Const Vs_audata = 5
Const Vs_volume = 11
'-------------------------------------------------------------------------------
Dim B As Byte
Dim I As Integer
Dim J As Long
Dim Curbyte As Long
Dim S As String * 20
Dim Sample_rate As Word
Dim Bit_rate As Word
Dim Is_stereo As Word
Dim Audata As Word
Dim Filename As String * 11
Dim Mmc_data(512) As Byte                                 '512 byte
Dim Tempo_new As Word
Dim Tempo_old As Word
Dim Flag_read As Bit
Dim Flag_play As Bit
Dim Comando As String * 10
Dim Mode_low As Byte
Dim Mode_hig As Byte
Dim Volume As Word
Dim Volume_low As Byte
Dim Volume_hig As Byte
'-------------------------------------------------------------------------------
$Include "Config_MMC.bas"                                 'include mmc

If Gbdriveerror <> 0 Then End
$Include "Config_AVR-DOS.BAS"                               'include avr-dos

B = Initfilesystem(1)                                       'init file system
If B <> 0 Then Print "Errore MMC"                           'se errore
'-------------------------------------------------------------------------------
Bsync = 0
Xreset = 0
Xcs = 1
Portg.0 = 1                                                 'res pull up
Flag_play = 0                                             'sta suonando ?
Flag_read = 1                                             'gia letto ?
'-------------------------------------------------------------------------------
'--------------------------PROGRAM START -------------------------------------
'-------------------------------------------------------------------------------
Print "Dir *.mp3"                                           'dir mp3
Print
'-------------------------------------------------------------------------------
'---------------------- crea una lista dei brani mp3 presenti sulla MMC --------
'-------------------------------------------------------------------------------
Open "Lista.txt" For Output As #9                           'create file.list in MMC
S = Dir( "*.mp3")
While Len(s) > 0
   Print S ; "" ; Filedate() ; "" ; Filetime() ; "" ; Filelen()
   Print #9 , S                                             'memorizza la lista
   S = Dir()
Wend
Close #9
'-------------------------------------------------------------------------------
Print "VS1001 Init"
Vs_init                                                   'reset hardware e inizializza
'-------------------------------------------------------------------------------
'--------------------------- MAIN PROGRAMM -------------------------------------
'-------------------------------------------------------------------------------
Do
Open "Lista.txt" For Input As #9                        'OPEN LA LISTA
      While Eof(#9) = 0
         Line Input #9 , S                              'READ filename Mp3
         Call Send_mp3(s)                                 'play mp3
      Wend
Close #9
Loop
End
'-------------------------------------------------------------------------------
'---------------------------- TIMER 1 ------------------------------------------
'-------------------------------------------------------------------------------
Timer_1:
   If Bsync = 0 And Flag_play = 1 And Flag_read = 1 Then'per leggere inforamzioni
      Flag_read = 0
      Audata = Vs_read(vs_audata)
      Sample_rate = Audata And &H1E00
      Rotate Sample_rate , Right , 9
      Bit_rate = Audata And &H1FF
      Is_stereo = Audata And &H8000
      Rotate Is_stereo , Right , 15
      Print Sample_rate ; "," ; Bit_rate ; " Kbs," ; Is_stereo       'print informations
   End If

   If Bsync = 0 And Flag_play = 1 And Flag_read = 0 Then
      Tempo_new = Vs_read(vs_decode_time)
             If Tempo_new <> Tempo_old Then Print Tempo_new ; " Sec"
      Tempo_old = Tempo_new
   End If
Return
'-------------------------------------------------------------------------------
'---------------------- RS 232 -------------------------------------------------
'-------------------------------------------------------------------------------
Rs_232:
      Comando = Inkey()
      If Comando = "1" Then
          Mode_low.7 = 1
          Bsync = 0
          Vs_write Vs_mode , &H0 , Mode_low               'no loudness
      End If
      If Comando = "2" Then
          Mode_low.7 = 0
          Bsync = 0
          Vs_write Vs_mode , &H0 , Mode_low               'loudness
      End If
      If Comando = "3" Then
          Mode_low.1 = 0
          Bsync = 0
          Vs_write Vs_mode , &H0 , Mode_low               'play normale
      End If
      If Comando = "4" Then
          Mode_low.1 = 1
          Bsync = 0
          Vs_write Vs_mode , &H0 , Mode_low               'play veloce
      End If
      If Comando = "5" And Volume_low < 250 Then
         Volume_hig = Volume_hig + 5
         Volume_low = Volume_low + 5
         Bsync = 0
         Vs_write Vs_volume , Volume_hig , Volume_low       'volume down
      End If
      If Comando = "6" And Volume_low > 0 Then
         Volume_hig = Volume_hig - 5
         Volume_low = Volume_low - 5
         Bsync = 0
         Vs_write Vs_volume , Volume_hig , Volume_low       'volume up
      End If
      Comando = ""
Return
'-------------------------------------------------------------------------------
'------------------------------ MP3 SUBROUTINES --------------------------------
'-------------------------------------------------------------------------------
Function Vs_read(byval Vs_address As Byte) As Word
Xcs = 0                                                   'Xcs low
   B = Vs_rd
   Shiftout Vs_mosi , Vs_clock , B , 1
   Shiftout Vs_mosi , Vs_clock , Vs_address , 1
   B = 0
   Shiftin Vs_miso , Vs_clock , B , 1
   Vs_read = B
   Shift Vs_read , Left , 8
   B = 0
   Shiftin Vs_miso , Vs_clock , B , 1
   Vs_read = Vs_read + B
Xcs = 1                                                   'Xcs high
End Function
'-------------------------------------------------------------------------------
'-------------------------------- WRITE ROUTINE --------------------------------
'-------------------------------------------------------------------------------
Sub Vs_write(byval Vs_address As Byte , Byval Data1 As Byte , Byval Data2 As Byte)
Xcs = 0
   B = Vs_wr
   Shiftout Vs_mosi , Vs_clock , B , 1
   Shiftout Vs_mosi , Vs_clock , Vs_address , 1
   Shiftout Vs_mosi , Vs_clock , Data1 , 1
   Shiftout Vs_mosi , Vs_clock , Data2 , 1
Xcs = 1
End Sub
'-------------------------------------------------------------------------------
'------------------------------- RESETTA E INIZILIZZA --------------------------
'-------------------------------------------------------------------------------
Sub Vs_init()
Xreset = 0
    Waitms 5
Xreset = 1
    Waitms 10

Do : Led = Dreq : Loop Until Dreq = 1
Vs_write Vs_mode , &H0 , &H4                              'reset

Do : Led = Dreq : Loop Until Dreq = 1
Mode_low.7 = 1
Vs_write Vs_mode , &H0 , Mode_low                         'bass

Do : Led = Dreq : Loop Until Dreq = 1
Vs_write Vs_clockf , &H0 , &H0                            'clockf24,xx MHz

Do : Led = Dreq : Loop Until Dreq = 1
Vs_write Vs_volume , &H0 , &H0                            'volume =0 massimo

End Sub
'-------------------------------------------------------------------------------
'--------------------------- SOFT RESET VS1001 ---------------------------------
'-------------------------------------------------------------------------------
Sub Soft_reset
Do : Led = Dreq : Loop Until Dreq = 1                     'wait Dreq
Vs_write Vs_mode , &H0 , &H4                              'reset
End Sub
'-------------------------------------------------------------------------------
'---------------------------- SEND FILE MP3 ------------------------------------
'-------------------------------------------------------------------------------
Sub Send_mp3(byval Filename As String)
Print "Play : " ; Filename
Soft_reset                                                'resetta vs1001
Flag_read = 1                                             'per leggere una volta sample rate etc
Flag_play = 0                                             'inizia a leggere dopo che è stato letto il primo pacchetto

Open Filename For Binary As #10                           'apri il file mp3 e play song

Do : Led = Dreq : Loop Until Dreq = 1                     'wait Dreq

While Eof(#10) = 0
      Get #10 , Mmc_data(1) , , 512                     'preleva 512 byte
      Do : Led = Dreq : Loop Until Dreq = 1               'wait Dreq

      For I = 1 To 16
            For J = 1 To 32
            Bsync = 1
                Shiftout Vs_mosi , Vs_clock , Mmc_data(curbyte) , 1
            Bsync = 0
            Incr Curbyte
            Next J
       Do : Led = Dreq : Loop Until Dreq = 1                'wait Dreq
       Next I                                             'first 32
    Curbyte = 1                                             'after 1616x32=512 byte
    If Avanti = 0 Then Goto Esci                            'pulsante up
    Flag_play = 1                                           'playing primo pacchetto
Wend

Esci:
Flag_play = 0                                             'stop play
Close #10
For I = 1 To 2048                                       'send 2048 zeros to clear buffer
    B = 0
    Shiftout Vs_mosi , Vs_clock , B , 1                     'send 2048 byte
Next I
Print "MP3 End"                                           'end song

Waitms 100                                                'aspetta 100
End Sub
'-------------------------------------------------------------------------------

machao 发表于 2010-6-23 20:27:00

如果不学习掌握基本的知识,比如SPI接口、SD卡协议,MP3格式等,当然是看不懂代码了。

yuanjingjie 发表于 2010-6-24 10:08:10

首先要感谢马老师白忙之中回复我的帖子,我用BASCOM写单片机也有3年左右了,最近看到这个程序,觉得它有研究的价值,之所以不懂才来这请教马老师,希望马老师给我讲讲
页: [1]
查看完整版本: 请教马老师BASCOM-AVR利用AVR-DOS库读写SD卡,U盘,CF卡等外部存储的原理