ABL electronic PIC Microcontrollers PIC18 User Manual
mikroC - C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
231
MikroElektronika: Development tools - Books - Compilers
page
Library Example
The following program tests MMC FAT routines. It creates 5 different files in the root of MMC
card, and fills them with some data. You can check the file dates which should be different.
card, and fills them with some data. You can check the file dates which should be different.
char
FAT_ERROR[20] = "FAT16 not found";
char
file_contents[50] = "XX MMC/SD FAT16 library by Anton Rieckert";
char
filename[14] = "MIKRO00xTXT";
// File names
unsigned short
tmp, character, loop;
long
i, size;
void
main() {
PORTB = 0;
TRISB = 0;
Usart_Init(19200);
// Set up USART for reading the files
if
(!Mmc_Fat_Init(&PORTC, 2)) {
// Try to find the FAT
tmp = 0;
while
(FAT_ERROR[tmp])
Usart_Write(FAT_ERROR[tmp++]);
}
for
(loop = 1; loop <= 5; loop++) {
// We want 5 files on our MMC card
filename[7] = loop + 64;
// Set number 1, 2, 3, 4 or 5
Mmc_Fat_Assign(&filename);
// If file not found, create new file
Mmc_Fat_Rewrite();
// Clear the file, start with new data
file_contents[0] = loop / 10 + 48;
file_contents[1] = loop % 10 + 48;
Mmc_Fat_Write(file_contents, 41);
// Write data to the assigned file
Mmc_Fat_Append();
// Add more data to file
Mmc_Fat_Write(file_contents, 41);
// Write data to file
Delay_ms(200);
}
// Now if we want to add more data to those same files
for
(loop = 1; loop <= 5; loop++) {
filename[7] = loop + 64;
Mmc_Fat_Assign(&filename);
// Assign a file
Mmc_Fat_Append();
Mmc_Set_File_Date(2005,6,21,10,loop,0);
Mmc_Fat_Write(" for mikroElektronika 2005\r\n", 30);
Mmc_Fat_Append();
Mmc_Fat_Write(file_contents, 41);
Mmc_Fat_Reset(&size);
// To read file, returns file size
for
(i = 1; i <= size; i++) {
// Write whole file to USART
Mmc_Fat_Read(&character);
Usart_Write(character);
}
Delay_ms(200);
}
}
//~!