Device 16F72
Xtal 4
'For RS-232 Communication
Declare Serial_Baud 9600
Declare Rsout_Mode true
Declare Rsin_Mode true
Declare Rsout_Pin PORTB.6
Declare Rsin_Pin PORTB.7
Output PORTB
Output PORTC.2 'CS
Output PORTC.3 'SCK of micro
Output PORTC.5 'SDO of micro
Input PORTC.4 'SDI of micro
Symbol SSPEN = SSPCON.5
Symbol CS = PORTC.2
Symbol SMP = SSPSTAT.7
Symbol CKE = SSPSTAT.6
Symbol CKP = SSPCON.4
Symbol BF = SSPSTAT.0
'Using clock = f/16 for 4 MHz (Crystal Frequency)
SSPCON.0=1
SSPCON.1=0
SSPCON.2=0
SSPCON.3=0
Dim kk As Byte 'for data
Dim cnt As Byte
Dim rd As Byte
GoTo main
'To send data to the Memory Card
spi_write:
SSPBUF=kk
While BF = 0
Wend
Return
'To read data from the memory card
spi_read:
SSPBUF=kk
While BF = 0
Wend
rd = SSPBUF
Return
'Initilization of memory card
mmc_init:
kk=$FF
'Sending 0xFF for the clock
For cnt = 1 To 50
GoSub spi_write
Next cnt
CS = 0 'Chip select low and now send command
DelayMS 50
GoSub spi_write
kk=$40 'For command 0-> 0x40
GoSub spi_write
kk=$0 'Sending 00 00 00 00
GoSub spi_write
GoSub spi_write
GoSub spi_write
GoSub spi_write
kk=$95 'sending CRC value -> 0x95
GoSub spi_write
Return
main:
cnt=0
CS = 1
SSPEN = 1
SMP = 1
CKE = 1
CKP = 0
RSOut "for command o",13,10
GoSub mmc_init
cnt=0
kk=$FF 'Sending last byte and reading the response. 0x01 -> success
GoSub spi_read
Hmm:
If rd = 1 Then
RSOut "Command 0 executed",13,10
ElseIf cnt<1000 Then
GoSub spi_read
cnt=cnt+1
GoTo Hmm
EndIf
RSOut "Failed!!",13,10
End