PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : mmc-karte mit bascom



robbifan
24.06.2007, 20:02
' Declare here you SPI-Mode
' using HW-SPI: cMMC_Soft = 0
' not using HW_SPI: cMMC_Soft = 1

Const cMMC_Soft = 0

#IF cMMC_Soft = 0

' --------- Start of Section for HW-SPI ----------------------------------------

' define Chip-Select Pin
Config PinB.4 = Output ' define here Pin for CS of MMC/SD Card
Mmc_cs Alias PortB.4
Set Mmc_cs

' Define here SS Pin of HW-SPI of the CPU (f.e. Pinb.0 on M128)
' If an other Pin than SS is used for MMC_SS, SS must be set to OUTPUT and high for proper work of SPI
' otherwise AVR starts SPI-SLAVE if SS-Pin is INPUT and goes to LOW
Config Pinb.4 = Output ' define here Pin of SPI SS
Spi_ss Alias PortB.4
Set Spi_ss ' Set SPI-SS to Output and High por Proper work of
' SPI as Master

' HW-SPI is configured to highest Speed
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1
' Spsr = 1 ' Double speed on ATMega128
Spiinit ' Init SPI

' --------- End of Section for HW-SPI ------------------------------------------

#ELSE ' Config here SPI pins, if not using HW SPI

' --------- Start of Section for Soft-SPI --------------------------------------

' Chip Select Pin => Pin 1 of MMC/SD
Config PinB.3 = Output
MMC_CS Alias PortB.3
Set MMC_CS

' MOSI - Pin => Pin 2 of MMC/SD
Config PinB.5 = Output
Set PinB.5
MMC_PortMOSI Alias PortB
bMMC_MOSI Alias 5

' MISO - Pin => Pin 7 of MMC/SD
Config PinB.6 = Input
MMC_PortMISO Alias PinB
bMMC_MISO Alias 6

' SCK - Pin => Pin 5 of MMC/SD
Config PinB.7 = Output
Set PinB.7
MMC_PortSCK Alias PortB
bMMC_SCK Alias 7

' --------- End of Section for Soft-SPI ----------------------------------------

#ENDIF

' ========== End of user definable range =======================================


' Error
Const Cperrdrivereset = 225 ' Error response Byte at Reset command
Const Cperrdriveinit = 226 ' Error response Byte at Init Command
Const Cperrdrivereadcommand = 227 ' Error response Byte at Read Command
Const Cperrdrivewritecommand = 228 ' Error response Byte at Write Command
Const Cperrdrivereadresponse = 229 ' No Data response Byte from MMC at Read
Const Cperrdrivewriteresponse = 230 ' No Data response Byte from MMC at Write
Const Cperrdrive = 231
Const Cperrdrivenotsupported = 232 ' return code for DriveGetIdentity, not supported yet

Waitms 1 ' Wait some time before initialising MMC/SD
Dim Gbdriveerror As Byte ' General Driver Error register
Dim Gbdriveerrorreg As Byte ' Driver load Error-Register of HD in case of error
Dim Gbdrivestatusreg As Byte ' Driver load Status-Register of HD on case of error
Dim Gbdrivedebug As Byte
$lib "MMC.LIB" ' link driver library
$EXTERNAL _mmc
Gbdriveerror = Driveinit() ' Init MMC/SD Card



hallo, wenn ich auf hardware gehe und für den avr portb4 nehme , kommt eine fehlermeldung :

' define Chip-Select Pin
Config PinB.4 = Output ' define here Pin for CS of MMC/SD Card
Mmc_cs Alias PortB.4
Set Mmc_cs

' Define here SS Pin of HW-SPI of the CPU (f.e. Pinb.0 on M128)
' If an other Pin than SS is used for MMC_SS, SS must be set to OUTPUT and high for proper work of SPI
' otherwise AVR starts SPI-SLAVE if SS-Pin is INPUT and goes to LOW
Config Pinb.4= Output ' define here Pin of SPI SS
Spi_ss Alias PortB.4
Set Spi_ss ' Set SPI-SS to Output and High por Proper work of
' SPI as Master

' HW-SPI is configured to highest Speed
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1
' Spsr = 1 ' Double speed on ATMega128
Spiinit ' Init SPI

uwe44k
29.03.2008, 22:09
hallo,
ist das problem jetzt scho gelöst?
Ich versuche es gerade mit einen ATMega 16.
Ich komme aber auch nicht wirklich weiter.

Gruß Uwe

sechsrad
29.03.2008, 22:31
les dir den code durch. er läuft mit dem atmega16. stell die mhz ein und schau wo du deine sd-karte angeklemmt hast und änders das dementsprechend. sector 0-2 wird beschrieben und gelesen.
bei mir läuft es 100%tig.




$regfile = "m16def.dat"
$framesize = 32
$swstack = 32
$hwstack = 64
$crystal = 8000000
$baud = 19200

Declare Sub Mc_init()
Declare Sub Mc_write()
Declare Sub Mc_read()

Dim Dout As Byte
Dim Din As Byte
Dim A As Word
Dim Cmd(6) As Byte
Dim Buffer(515) As Byte
Dim Tmp As Long
Dim Sector As Long

Mcs Alias Portb.4
Mosi Alias Portb.5
Miso Alias Pinb.6
Sck Alias Portb.7

Config Pinb.4 = Output
Config Pinb.5 = Output
Config Pinb.6 = Input
Config Pinb.7 = Output

Waitms 255

Buffer(1) = &HFE
Buffer(514) = &HFF
Buffer(515) = &HFF

For A = 2 To 513
Buffer(a) = 14
Next
Call Mc_init()
Sector = 0
Call Mc_write()

For A = 2 To 513
Buffer(a) = 15
Next
Call Mc_init()
Sector = 1
Call Mc_write()

For A = 2 To 513
Buffer(a) = 16
Next
Call Mc_init()
Sector = 2
Call Mc_write()

Print
Print
Wait 1

' auslesen
Wait 1
Call Mc_init()
Sector = 0
Call Mc_read()

For A = 2 To 30
Print "wert: " ; Buffer(a)
Waitms 50
Next

'auslesen
Wait 1
Call Mc_init()
Sector = 1
Call Mc_read()

For A = 2 To 30
Print "wert: " ; Buffer(a)
Waitms 50
Next

'auslesen
Wait 1
Call Mc_init()
Sector = 2
Call Mc_read()

For A = 2 To 30
Print "wert: " ; Buffer(a)
Waitms 50
Next

End

Sub Mc_init()

Set Mcs

Dout = &HFF
For A = 0 To 10
Shiftout Mosi , Sck , Dout , 1 , 8 , 0
Next

Anfang:
Reset Mcs

Cmd(1) = &H40
Cmd(2) = 0
Cmd(3) = 0
Cmd(4) = 0
Cmd(5) = 0
Cmd(6) = &H95
For A = 1 To 6
Shiftout Mosi , Sck , Cmd(a) , 1 , 8 , 0
Next

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Shiftin Miso , Sck , Din , 1 , 8 , 0
If Din <> 1 Then
Goto Anfang
End If
Print "idle-modus " ; Din

Anfang1:
Reset Mcs

Cmd(1) = &H41
Cmd(2) = 0
Cmd(3) = 0
Cmd(4) = 0
Cmd(5) = 0
Cmd(6) = &HFF
For A = 1 To 6
Shiftout Mosi , Sck , Cmd(a) , 1 , 8 , 0
Next

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Shiftin Miso , Sck , Din , 1 , 8 , 0
If Din <> 0 Then
Goto Anfang1
End If
Print "betriebs-modus " ; Din

Set Mcs
End Sub

Sub Mc_read()
Set Mcs

Cmd(1) = &H51
Cmd(2) = 0
Cmd(3) = 0
Cmd(4) = 0
Cmd(5) = 0
Cmd(6) = &HFF

Shift Sector , Left , 9

Tmp = Sector And &HFF000000
Shift Tmp , Right , 24
Cmd(2) = Tmp

Tmp = Sector And &H00FF0000
Shift Tmp , Right , 16
Cmd(3) = Tmp

Tmp = Sector And &H0000FF00
Shift Tmp , Right , 8
Cmd(4) = Tmp

Anfang3:
Reset Mcs

For A = 1 To 6
Shiftout Mosi , Sck , Cmd(a) , 1 , 8 , 0
Next

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Shiftin Miso , Sck , Din , 1 , 8 , 0
If Din <> 0 Then
Goto Anfang3
End If
Print "read-modus " ; Din

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0
Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Shiftin Miso , Sck , Din , 1 , 8 , 0
If Din <> &HFE Then
Goto Ende
End If

For A = 2 To 515
Shiftin Miso , Sck , Buffer(a) , 1 , 8 , 0
Next

Ende:
Set Mcs
End Sub

Sub Mc_write()

Set Mcs

Cmd(1) = &H58
Cmd(2) = 0
Cmd(3) = 0
Cmd(4) = 0
Cmd(5) = 0
Cmd(6) = &HFF

Shift Sector , Left , 9

Tmp = Sector And &HFF000000
Shift Tmp , Right , 24
Cmd(2) = Tmp

Tmp = Sector And &H00FF0000
Shift Tmp , Right , 16
Cmd(3) = Tmp

Tmp = Sector And &H0000FF00
Shift Tmp , Right , 8
Cmd(4) = Tmp

Anfang2:
Reset Mcs

For A = 1 To 6
Shiftout Mosi , Sck , Cmd(a) , 1 , 8 , 0
Next

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Shiftin Miso , Sck , Din , 1 , 8 , 0
If Din <> 0 Then
Goto Anfang2
End If
Print "write-modus " ; Din

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

For A = 1 To 515
Shiftout Mosi , Sck , Buffer(a) , 1 , 8 , 0
Waitus 10
Next

Dout = &HFF
Shiftout Mosi , Sck , Dout , 1 , 8 , 0

Set Mcs
End Sub