Compact Flash Library
The Compact Flash Library provides routines for accessing data on Compact Flash card (abbr. CF further in text). CF cards are widely used memory elements, commonly used with digital cameras. Great capacity and excellent access time of only a few microseconds make them very attractive for the microcontroller applications.
In CF card, data is divided into sectors. One sector usually comprises 512 bytes. Routines for file handling, the Cf_Fat
routines, are not performed directly but successively through 512B buffer.

- Routines for file handling can be used only with FAT16 file system.
- Library functions create and read files from the root directory only.
- Library functions populate both FAT1 and FAT2 tables when writing to files, but the file data is being read from the FAT1 table only; i.e. there is no recovery if the FAT1 table gets corrupted.
- If MMC/SD card has Master Boot Record (MBR), the library will work with the first available primary (logical) partition that has non-zero size. If MMC/SD card has Volume Boot Record (i.e. there is only one logical partition and no MBRs), the library works with entire card as a single partition. For more information on MBR, physical and logical drives, primary/secondary partitions and partition tables, please consult other resources, e.g. Wikipedia and similar.
- Before writing operation, make sure not to overwrite boot or FAT sector as it could make your card on PC or digital camera unreadable. Drive mapping tools, such as Winhex, can be of great assistance.
Library Dependency Tree

External dependencies of Compact Flash Library
The following variables must be defined in all projects using Compact Flash Library: | Description : | Example : |
---|---|---|
dim CF_Data_Port as byte sfr external |
Compact Flash Data Port. | dim CF_Data_Port as byte at PORTD |
dim CF_RDY as sbit sfr external |
Ready signal line. | dim CF_RDY as sbit at RB7_bit |
dim CF_WE as sbit sfr external |
Write Enable signal line. | dim CF_WE as sbit at LATB6_bit |
dim CF_OE as sbit sfr external |
Output Enable signal line. | dim CF_OE as sbit at LATB5_bit |
dim CF_CD1 as sbit sfr external |
Chip Detect signal line. | dim CF_CD1 as sbit at RB4_bit |
dim CF_CE1 as sbit sfr external |
Chip Enable signal line. | dim CF_CE1 as sbit at LATB3_bit |
dim CF_A2 as sbit sfr external |
Address pin 2. | dim CF_A2 as sbit at LATB2_bit |
dim CF_A1 as sbit sfr external |
Address pin 1. | dim CF_A1 as sbit at LATB1_bit |
dim CF_A0 as sbit sfr external |
Address pin 0. | dim CF_A0 as sbit at LATB0_bit |
dim CF_RDY_direction as sbit sfr external |
Direction of the Ready pin. | dim CF_RDY_direction as sbit at TRISB7_bit |
dim CF_WE_direction as sbit sfr external |
Direction of the Write Enable pin. | dim CF_WE_direction as sbit at TRISB6_bit |
dim CF_OE_direction as sbit sfr external |
Direction of the Output Enable pin. | dim CF_OE_direction as sbit at TRISB5_bit |
dim CF_CD1_direction as sbit sfr external |
Direction of the Chip Detect pin. | dim CF_CD1_direction as sbit at TRISB4_bit |
dim CF_CE1_direction as sbit sfr external |
Direction of the Chip Enable pin. | dim CF_CE1_direction as sbit at TRISB3_bit |
dim CF_A2_direction as sbit sfr external |
Direction of the Address 2 pin. | dim CF_A2_direction as sbit at TRISB2_bit |
dim CF_A1_direction as sbit sfr external |
Direction of the Address 1 pin. | dim CF_A1_direction as sbit at TRISB1_bit |
dim CF_A0_direction as sbit sfr external |
Direction of the Address 0 pin. | dim CF_A0_direction as sbit at TRISB0_bit |
Library Routines
- Cf_Init
- Cf_Detect
- Cf_Enable
- Cf_Disable
- Cf_Read_Init
- Cf_Read_Byte
- Cf_Write_Init
- Cf_Write_Byte
- Cf_Read_Sector
- Cf_Write_Sector
Routines for file handling:
- Cf_Fat_Init
- Cf_Fat_QuickFormat
- Cf_Fat_Assign
- Cf_Fat_Reset
- Cf_Fat_Read
- Cf_Fat_Rewrite
- Cf_Fat_Append
- Cf_Fat_Delete
- Cf_Fat_Write
- Cf_Fat_Set_File_Date
- Cf_Fat_Get_File_Date
- Cf_Fat_Get_File_Date_Modified
- Cf_Fat_Get_File_Size
- Cf_Fat_Get_Swap_File
The following routine is for the internal use by compiler only:
- Cf_Issue_ID_Command
Cf_Init
Prototype |
sub procedure Cf_Init() |
---|---|
Returns |
Nothing. |
Description |
Initializes ports appropriately for communication with CF card. |
Requires |
Global variables :
|
Example |
dim Cf_Data_Port as byte at PORTD CF_RDY as sbit at RB7_bit CF_WE as sbit at LATB6_bit ' for writing to output pin always use latch (PIC18 family) CF_OE as sbit at LATB5_bit ' for writing to output pin always use latch (PIC18 family) CF_CD1 as sbit at RB4_bit CF_CE1 as sbit at LATB3_bit ' for writing to output pin always use latch (PIC18 family) CF_A2 as sbit at LATB2_bit ' for writing to output pin always use latch (PIC18 family) CF_A1 as sbit at LATB1_bit ' for writing to output pin always use latch (PIC18 family) CF_A0 as sbit at LATB0_bit ' for writing to output pin always use latch (PIC18 family) CF_RDY_direction as sbit at TRISB7_bit CF_WE_direction as sbit at TRISB6_bit CF_OE_direction as sbit at TRISB5_bit CF_CD1_direction as sbit at TRISB4_bit CF_CE1_direction as sbit at TRISB3_bit CF_A2_direction as sbit at TRISB2_bit CF_A1_direction as sbit at TRISB1_bit CF_A0_direction as sbit at TRISB0_bit ' end of cf pinout 'Init CF Cf_Init() |
Cf_Detect
Prototype |
sub function CF_Detect() as byte |
---|---|
Returns |
|
Description |
Checks for presence of CF card by reading the |
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' Wait until CF card is inserted: while (Cf_Detect() = 0) nop wend |
Cf_Enable
Prototype |
sub procedure Cf_Enable() |
---|---|
Returns |
Nothing. |
Description |
Enables the device. Routine needs to be called only if you have disabled the device by means of the Cf_Disable routine. These two routines in conjunction allow you to free/occupy data line when working with multiple devices. |
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' enable compact flash Cf_Enable() |
Cf_Disable
Prototype |
sub procedure Cf_Disable() |
---|---|
Returns |
Nothing. |
Description |
Routine disables the device and frees the data lines for other devices. To enable the device again, call Cf_Enable. These two routines in conjunction allow you to free/occupy data line when working with multiple devices. |
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' disable compact flash Cf_Disable() |
Cf_Read_Init
Prototype |
sub procedure Cf_Read_Init(dim address as longword, dim sector_count as byte) |
---|---|
Returns |
Nothing. |
Description |
Initializes CF card for reading. Parameters :
|
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' initialize compact flash for reading from sector 590 Cf_Read_Init(590, 1) |
Cf_Read_Byte
Prototype |
sub function CF_Read_Byte() as byte |
---|---|
Returns |
Returns a byte read from Compact Flash sector buffer. ![]() unsigned return value is cleared.
|
Description |
Reads one byte from Compact Flash sector buffer location currently pointed to by internal read pointers. These pointers will be autoicremented upon reading. |
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. CF card must be initialized for reading operation. See Cf_Read_Init. |
Example |
' Read a byte from compact flash: dim data as byte ... data = Cf_Read_Byte() |
Cf_Write_Init
Prototype |
sub procedure Cf_Write_Init(dim address as longword, dim sectcnt as byte) |
---|---|
Returns |
Nothing. |
Description |
Initializes CF card for writing. Parameters :
|
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' initialize compact flash for writing to sector 590 Cf_Write_Init(590, 1) |
Cf_Write_Byte
Prototype |
sub procedure Cf_Write_Byte(dim data_ as byte) |
---|---|
Returns |
Nothing. |
Description |
Writes a byte to Compact Flash sector buffer location currently pointed to by writing pointers. These pointers will be autoicremented upon reading. When sector buffer is full, its content will be transfered to appropriate flash memory sector. Parameters :
|
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. CF card must be initialized for writing operation. See Cf_Write_Init. |
Example |
dim data_ as byte ... data = 0xAA Cf_Write_Byte(data) |
Cf_Read_Sector
Prototype |
sub procedure Cf_Read_Sector(dim sector_number as longword, dim byref buffer as byte[512]) |
---|---|
Returns |
Nothing. |
Description |
Reads one sector (512 bytes). Read data is stored into buffer provided by the Parameters :
|
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' read sector 22 dim data as array[512] of byte ... Cf_Read_Sector(22, data) |
Cf_Write_Sector
Prototype |
sub procedure Cf_Write_Sector(dim sector_number as longword, dim byref buffer as byte[512]) |
---|---|
Returns |
Nothing. |
Description |
Writes 512 bytes of data provided by the Parameters :
|
Requires |
The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init. |
Example |
' write to sector 22 dim data as array[512] of byte ... Cf_Write_Sector(22, data) |
Cf_Fat_Init
Prototype |
sub function Cf_Fat_Init() as byte |
---|---|
Returns |
|
Description |
Initializes CF card, reads CF FAT16 boot sector and extracts data needed by the library. |
Requires |
Nothing. |
Example |
init the FAT library if (Cf_Fat_Init() = 0) then ... end if |
Cf_Fat_QuickFormat
Prototype |
sub function Cf_Fat_QuickFormat(dim byref cf_fat_label as string[11]) as byte |
---|---|
Returns |
|
Description |
Formats to FAT16 and initializes CF card. Parameters :
![]()
|
Requires |
Nothing. |
Example |
'--- format and initialize the FAT library if ( Cf_Fat_QuickFormat('mikroE') = 0) then ... end if |
Cf_Fat_Assign
Prototype |
sub function Cf_Fat_Assign(dim byref filename as char[12], dim file_cre_attr as byte) as byte |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
|||||||||||||||||||||||||||
Description |
Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied over the assigned file. Parameters :
![]() |
|||||||||||||||||||||||||||
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. |
|||||||||||||||||||||||||||
Example |
' create file with archive attribut if it does not already exist Cf_Fat_Assign('MIKRO007.TXT',0xA0) |
Cf_Fat_Reset
Prototype |
sub procedure Cf_Fat_Reset(dim byref size as longword) |
---|---|
Returns |
Nothing. |
Description |
Opens currently assigned file for reading. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
dim size as longword ... Cf_Fat_Reset(size) |
Cf_Fat_Read
Prototype |
sub procedure Cf_Fat_Read(dim byref bdata as byte) |
---|---|
Returns |
Nothing. |
Description |
Reads a byte from currently assigned file opened for reading. Upon function execution file pointers will be set to the next character in the file. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. File must be open for reading. See Cf_Fat_Reset. |
Example |
dim character as byte ... Cf_Fat_Read(character) |
Cf_Fat_Rewrite
Prototype |
sub procedure Cf_Fat_Rewrite() |
---|---|
Returns |
Nothing. |
Description |
Opens currently assigned file for writing. If the file is not empty its content will be erased. |
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. The file must be previously assigned. See Cf_Fat_Assign. |
Example |
' open file for writing Cf_Fat_Rewrite() |
Cf_Fat_Append
Prototype |
sub procedure Cf_Fat_Append() |
---|---|
Returns |
Nothing. |
Description |
Opens currently assigned file for appending. Upon this function execution file pointers will be positioned after the last byte in the file, so any subsequent file writing operation will start from there. |
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
' open file for appending Cf_Fat_Append() |
Cf_Fat_Delete
Prototype |
sub procedure Cf_Fat_Delete() |
---|---|
Returns |
Nothing. |
Description |
Deletes currently assigned file from CF card. |
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
' delete current file Cf_Fat_Delete() |
Cf_Fat_Write
Prototype |
sub procedure Cf_Fat_Write(dim byref fdata as byte[512], dim data_len as word) |
---|---|
Returns |
Nothing. |
Description |
Writes requested number of bytes to currently assigned file opened for writing. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. File must be open for writing. See Cf_Fat_Rewrite or Cf_Fat_Append. |
Example |
dim file_contents as array[42] of byte ... Cf_Fat_Write(file_contents, 42) ' write data to the assigned file |
Cf_Fat_Set_File_Date
Prototype |
sub procedure Cf_Fat_Set_File_Date(dim year as word, dim month as byte, dim day as byte, dim hours as byte, dim mins as byte, dim seconds as byte) |
---|---|
Returns |
Nothing. |
Description |
Sets the date/time stamp. Any subsequent file writing operation will write this stamp to currently assigned file's time/date attributs. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. File must be open for writing. See Cf_Fat_Rewrite or Cf_Fat_Append. |
Example |
Cf_Fat_Set_File_Date(2005,9,30,17,41,0) |
Cf_Fat_Get_File_Date
Prototype |
sub procedure Cf_Fat_Get_File_Date(dim byref year as word, dim byref month as byte, dim byref day as byte, dim byref hours as byte, dim byref mins as byte) |
---|---|
Returns |
Nothing. |
Description |
Reads time/date attributes of currently assigned file. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
dim year as word month, day, hours, mins as byte ... Cf_Fat_Get_File_Date(year, month, day, hours, mins) |
Cf_Fat_Get_File_Date_Modified
Prototype |
sub procedure Cf_Fat_Get_File_Date_Modified(dim byref year as word, dim byref month as byte, dim byref day as byte, dim byref hours as byte, dim byref mins as byte) |
---|---|
Returns |
Nothing. |
Description |
Retrieves the last modification date/time of the currently assigned file. Parameters :
|
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
dim year as word month, day, hours, mins as byte ... Cf_Fat_Get_File_Date_Modified(year, month, day, hours, mins) |
Cf_Fat_Get_File_Size
Prototype |
sub function Cf_Fat_Get_File_Size() as longword |
---|---|
Returns |
Size of the currently assigned file in bytes. |
Description |
This function reads size of currently assigned file in bytes. |
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. File must be previously assigned. See Cf_Fat_Assign. |
Example |
dim my_file_size as longword ... my_file_size = Cf_Fat_Get_File_Size() |
Cf_Fat_Get_Swap_File
Prototype |
sub function Cf_Fat_Get_Swap_File(dim sectors_cnt as longint, dim byref filename as string[11], dim file_attr as byte) as longword |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
|||||||||||||||||||||||||||
Description |
This function is used to create a swap file of predefined name and size on the CF media. If a file with specified name already exists on the media, search for consecutive sectors will ignore sectors occupied by this file. Therefore, it is recommended to erase such file if it exists before calling this function. If it is not erased and there is still enough space for a new swap file, this function will delete it after allocating new memory space for a new swap file. The purpose of the swap file is to make reading and writing to CF media as fast as possible, by using the Cf_Read_Sector() and Cf_Write_Sector() functions directly, without potentially damaging the FAT system. The swap file can be considered as a "window" on the media where the user can freely write/read data. Its main purpose in the library is to be used for fast data acquisition; when the time-critical acquisition has finished, the data can be re-written into a "normal" file, and formatted in the most suitable way. Parameters:
![]() |
|||||||||||||||||||||||||||
Requires |
CF card and CF library must be initialized for file operations. See Cf_Fat_Init. |
|||||||||||||||||||||||||||
Example |
program '-------------- Try to create a swap file with archive atribute, whose size will be at least 1000 sectors. ' If it succeeds, it sends the No. of start sector over USART dim size as longword ... main: ... size = Cf_Fat_Get_Swap_File(1000, "mikroE.txt", 0x20) if size then UART1_Write(0xAA) UART1_Write(Lo(size)) UART1_Write(Hi(size)) UART1_Write(Higher(size)) UART1_Write(Highest(size)) UART1_Write(0xAA) end if end. |
Library Example
The following example demonstrates various aspects of the Cf_Fat16 library :
- Creation of new file and writing down to it;
- Opening existing file and re-writing it (writing from start-of-file);
- Opening existing file and appending data to it (writing from end-of-file);
- Opening a file and reading data from it (sending it to USART terminal);
- Creating and modifying several files at once;
program CF_Fat16_Test ' set compact flash pinout dim Cf_Data_Port as byte at PORTD CF_RDY as sbit at RB7_bit CF_WE as sbit at LATB6_bit ' for writing to output pin always use latch (PIC18 family) CF_OE as sbit at LATB5_bit ' for writing to output pin always use latch (PIC18 family) CF_CD1 as sbit at RB4_bit CF_CE1 as sbit at LATB3_bit ' for writing to output pin always use latch (PIC18 family) CF_A2 as sbit at LATB2_bit ' for writing to output pin always use latch (PIC18 family) CF_A1 as sbit at LATB1_bit ' for writing to output pin always use latch (PIC18 family) CF_A0 as sbit at LATB0_bit ' for writing to output pin always use latch (PIC18 family) CF_RDY_direction as sbit at TRISB7_bit CF_WE_direction as sbit at TRISB6_bit CF_OE_direction as sbit at TRISB5_bit CF_CD1_direction as sbit at TRISB4_bit CF_CE1_direction as sbit at TRISB3_bit CF_A2_direction as sbit at TRISB2_bit CF_A1_direction as sbit at TRISB1_bit CF_A0_direction as sbit at TRISB0_bit ' end of cf pinout FAT_TXT as string[20] file_contents as string[50] filename as string[14] ' File names character as byte loop_, loop2 as byte size as longword Buffer as byte[512] '-------------- Writes string to USART sub procedure Write_Str(dim byref ostr as byte[2] ) dim i as byte i = 0 while ostr[i] <> 0 UART1_Write(ostr[i]) Inc(i) wend UART1_Write($0A) end sub '-------------- Creates new file and writes some data to it sub procedure Create_New_File filename[7] = "A" Cf_Fat_Set_File_Date(2005,6,21,10,35,0) ' Set file date & time info 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 loop_=1 to 90 ' We want 5 files on the MMC card PORTC = loop_ file_contents[0] = loop_ div 10 + 48 file_contents[1] = loop_ mod 10 + 48 Cf_Fat_Write(file_contents, 38) ' write data to the assigned file UART1_Write(".") next loop_ end sub '-------------- Creates many new files and writes data to them sub procedure Create_Multiple_Files for loop2 = "B" to "Z" UART1_Write(loop2) ' this line can slow down the performance filename[7] = loop2 ' set filename Cf_Fat_Set_File_Date(2005,6,21,10,35,0) ' Set file date & time info 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 loop_ = 1 to 44 file_contents[0] = loop_ div 10 + 48 file_contents[1] = loop_ mod 10 + 48 Cf_Fat_Write(file_contents, 38) ' write data to the assigned file next loop_ next loop2 end sub '-------------- Opens an existing file and rewrites it sub procedure Open_File_Rewrite filename[7] = "C" ' Set filename for single-file tests Cf_Fat_Assign(filename, 0) Cf_Fat_Rewrite for loop_ = 1 to 55 file_contents[0] = byte(loop_ div 10 + 48) file_contents[1] = byte(loop_ mod 10 + 48) Cf_Fat_Write(file_contents, 38) ' write data to the assigned file next loop_ end sub '-------------- Opens an existing file and appends data to it ' (and alters the date/time stamp) sub procedure Open_File_Append filename[7] = "B" Cf_Fat_Assign(filename, 0) Cf_Fat_Set_File_Date(2009, 1, 23, 17, 22, 0) Cf_Fat_Append file_contents = " for mikroElektronika 2010" ' Prepare file for append file_contents[26] = 10 ' LF Cf_Fat_Write(file_contents, 27) ' Write data to assigned file end sub '-------------- Opens an existing file, reads data from it and puts it to USART sub procedure Open_File_Read filename[7] = "B" Cf_Fat_Assign(filename, 0) Cf_Fat_Reset(size) ' To read file, sub procedure returns size of file while size > 0 Cf_Fat_Read(character) UART1_Write(character) ' Write data to USART Dec(size) wend end sub' '-------------- Deletes a file. If file doesn"t exist, it will first be created ' and then deleted. sub procedure Delete_File filename[7] = "F" Cf_Fat_Assign(filename, 0) Cf_Fat_Delete end sub '-------------- Tests whether file exists, and if so sends its creation date ' and file size via USART sub procedure Test_File_Exist(dim fname as byte) dim fsize as longint year as word month_, day, hour_, minute_ as byte outstr as string[11] filename[7] = "B" ' uncomment this line to search for file that DOES exists ' filename[7] = "F" ' uncomment this line to search for file that DOES NOT exist if Cf_Fat_Assign(filename, 0) <> 0 then '--- file has been found - get its date Cf_Fat_Get_File_Date(year,month_,day,hour_,minute_) UART1_Write_Text(" created: ") WordToStr(year, outstr) UART1_Write_Text(outstr) ByteToStr(month_, outstr) UART1_Write_Text(outstr) WordToStr(day, outstr) UART1_Write_Text(outstr) WordToStr(hour_, outstr) UART1_Write_Text(outstr) WordToStr(minute_, outstr) UART1_Write_Text(outstr) '--- file has been found - get its modified date Cf_Fat_Get_File_Date_Modified(year, month_, day, hour_, minute_) UART1_Write_Text(" modified: ") WordToStr(year, outstr) UART1_Write_Text(outstr) ByteToStr(month_, outstr) UART1_Write_Text(outstr) WordToStr(day, outstr) UART1_Write_Text(outstr) WordToStr(hour_, outstr) UART1_Write_Text(outstr) WordToStr(minute_, outstr) UART1_Write_Text(outstr) '--- get file size fsize = Cf_Fat_Get_File_Size LongIntToStr(fsize, outstr) Write_Str(outstr) else '--- file was not found - signal it UART1_Write(0x55) Delay_ms(1000) UART1_Write(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 sub function for details if (size <> 0) then LongIntToStr(size, fat_txt) Write_Str(fat_txt) for i=0 to 4999 Cf_Write_Sector(size, Buffer) size = size+1 UART1_Write(".") next i end if end sub '-------------- Main. Uncomment the sub function(s) to test the desired operation(s) main: FAT_TXT = "FAT16 not found" file_contents = "XX CF FAT16 library by Anton Rieckert" file_contents[37] = 10 ' newline filename = "MIKRO00xTXT" ADCON1 = ADCON1 or 0x0F ' Configure pins as digital I/O TRISC = 0 ' we will use PORTC to signal test end PORTC = 0 UART1_Init(19200) ' Set up USART for file reading delay_ms(100) UART1_Write_Text("Start") UART1_Write(10) UART1_Write(13) ' --- Init the FAT library ' --- use Cf_Fat_QuickFormat instead of init routine if a format is needed if Cf_Fat_Init() = 0 then '--- test sub functions '----- test group #1 Create_New_File() Create_Multiple_Files() '----- test group #2 Open_File_Rewrite() Open_File_Append() Delete_File '----- test group #3 Open_File_Read() Test_File_Exist("F") M_Create_Swap_File() '--- Test termination UART1_Write(0xAA) else UART1_Write_Text(FAT_TXT) end if '--- signal end-of-test UART1_Write(10) UART1_Write(13) UART1_Write_Text("End") end.
HW Connection
Pin diagram of CF memory card
What do you think about this topic ? Send us feedback!