
The example shows the connection of a compact flash (CF) memory card to a dsPIC30F micocontroller. The CF memory cards are often used as the memory elements in digital video cameras. The memory capacity is high, from 8MB up to 2GB even more, and the read/write time is typically of the order of µs. Application of CF memory cards in microcontroller systems is quite widespread.
In the CF memory cards data are split into sectors (usually of 512 bytes, in earlier models 256 bytes). Reading or writing is not performed directly byte after byte, but data are blocked per sectors through a 512 byte buffer. Fig. 13-7 shows the electrical connection of a CF memory card to a device of the dsPIC30F family. The realization is carried out by using the mikroBasic compiler for dsPIC30F microcontrollers. The example covers, writing and reading one byte from a CF memory card.
program Cf_Fat16_Test
const
SWAP_FILE_MSG as string[15] = "Swap file at: "
dim
fat_txt as string[20]
file_contents as string[50]
filename as byte[14] ' File names
loop1, loop12 as word
caracter as byte
i, size as longint
Buffer as byte[512]
sub procedure Glob_Init
fat_txt = "FAT16 not found"
file_contents = "XX CF/SD FAT16 library by Anton Rieckert" + Chr(10) + Chr(0)
filename = "MIKRO00xTXT" ' File names
end sub
'M-M-M--------- Creates new file and writes some data to it
sub procedure M_Create_New_File
filename[7] = "A"
Cf_Fat_Assign(filename, 0xA0) ' Will not find file and then create file
Cf_Fat_Rewrite ' To clear file and start with new data
for loop1 = 1 to 99 ' We want 5 files on the MMC card
Uart1_Write_Char(".")
file_contents[0] = (loop1 div 10) + 48
file_contents[1] = (loop1 mod 10) + 48
' Uart1_Write_Text(file_contents)
Cf_Fat_Write(file_contents, 42) ' write data to the assigned file
next loop1
end sub'~
'M-M-M--------- Creates many new files and writes data to them
sub procedure M_Create_Multiple_Files
for loop12 = "B" to "Z"
Uart1_Write_Char(loop12) ' signal the progress
filename[7] = loop12 ' set filename
Cf_Fat_Assign(filename, 0xA0) ' find existing file or create a new one
Cf_Fat_Rewrite ' To clear file and start with new data
for loop1 = 1 to 44
file_contents[0] = (loop1 div 10) + 48
file_contents[1] = (loop1 mod 10) + 48
Cf_Fat_Write(file_contents, 42) ' write data to the assigned file
next loop1
next loop12
end sub'~
'M-M-M--------- Opens an existing file and rewrites it
sub procedure M_Open_File_Rewrite
filename[7] = "C"
Cf_Fat_Assign(filename, 0)
Cf_Fat_Rewrite
for loop1 = 1 to 55
file_contents[0] = loop1 div 10 + 64
file_contents[1] = loop1 mod 10 + 64
Cf_Fat_Write(file_contents, 42) ' write data to the assigned file
next loop1
end sub'~
'M-M-M--------- Opens an existing file and appends data to it
' (and alters the date/time stamp)
sub procedure M_Open_File_Append
filename[7] = "B"
Cf_Fat_Assign(filename, 0)
Cf_Fat_Set_File_Date(2005,6,21,10,35,0)
Cf_Fat_Append ' Prepare file for append
Cf_Fat_Write(" for mikroElektronika 2005n", 27) ' Write data to assigned file
end sub'~
'M-M-M--------- Opens an existing file, reads data from it and puts it to USART
sub procedure M_Open_File_Read
filename[7] = "B"
Cf_Fat_Assign(filename, 0)
Cf_Fat_Reset(size) ' To read file, sub procedure returns size of file
for i = 1 to size
Cf_Fat_Read(caracter)
Uart1_Write_Char(caracter) ' Write data to USART
next i
end sub'~
'M-M-M--------- Deletes a file. If file doesn't exist, it will first be created
' and then deleted.
sub procedure M_Delete_File
filename[7] = "F"
Cf_Fat_Assign(filename, 0)
Cf_Fat_Delete
end sub'~
'M-M-M--------- Tests whether file exists, and if so sends its creation date
' and file size via USART
sub procedure M_Test_File_Exist(dim fLetter as byte)
dim
fsize as longint
year as word
month1, day, hour1, minute1 as byte
outstr as byte[12]
filename[7] = fLetter
if (Cf_Fat_Assign(filename, 0) <> 0) then
'--- file has been found - get its date
Cf_Fat_Get_File_Date(year, month1, day, hour1, minute1)
WordToStr(year, outstr)
Uart1_Write_Text(outstr)
WordToStr(month1, outstr)
Uart1_Write_Text(outstr)
WordToStr(day, outstr)
Uart1_Write_Text(outstr)
WordToStr(hour1, outstr)
Uart1_Write_Text(outstr)
WordToStr(minute1, outstr)
Uart1_Write_Text(outstr)
'--- get file size
fsize = Cf_Fat_Get_File_Size
LongIntToStr(longint(fsize), outstr)
Uart1_Write_Text(outstr)
else
'--- file was not found - signal it
Uart1_Write_Char(0x55)
Delay_ms(1000)
Uart1_Write_Char(0x55)
end if
end sub'~
'-------------- Tries to create a swap file, whose size will be at least 100
' sectors (see Help for details)
sub procedure M_Create_Swap_File
dim i as word
for i=0 to 511
Buffer[i] = i
next i
size = Cf_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20)' see help on this function for details
if (size <> 0) then
LongIntToStr(size, fat_txt)
Uart1_Write_Text(fat_txt)
for i=0 to 4999
Cf_Write_Sector(size, Buffer)
Inc(size)
Uart1_Write_Char(".")
next i
end if
end sub'~
main:
'-------------- Main. Uncomment the function(s) to test the desired operation(s)
Glob_Init
'--- prepare PORTB for signalling
ADPCFG = 0xFFFF
PORTB = 0
TRISB = 0
'--- set up USART for the file read
Uart1_Init(19200)
Uart1_Write_Text("start")
'--- init the FAT library
'** dsPIC30F601x, dsPICPro3
' use fat16 quick format instead of init routine if a format is needed
if CF_fat_Init(PORTG, 0, 1, 2,
PORTG, 9,
PORTG, 8,
PORTG, 7,
PORTG, 6,
PORTG, 3,
PORTD) ' data port
= 0 then
'**
Delay_ms(500)
'--- Test start
PORTB = 0x0005
'--- Test routines. Uncomment them one-by-one to test certain features
M_Create_New_File
M_Create_Multiple_Files
Uart1_Write_Char(10)
M_Open_File_Rewrite
M_Open_File_Append
M_Delete_File
M_Create_Swap_File
Uart1_Write_Char(10)
M_Open_File_Read
Uart1_Write_Char(10)
M_Test_File_Exist("F") ' this file will not exist here
Uart1_Write_Char(10)
M_Test_File_Exist("B") ' this file will exist here
Uart1_Write_Char(10)
else
Uart1_Write_Text(fat_txt)
end if
'--- Test termination
PORTB = 0xFFFF
end.