Книга: Advanced PIC Microcontroller Projects in C

PROJECT 7.3 — Using the Card Filing System

PROJECT 7.3 — Using the Card Filing System

The hardware of this project is the same as for Project 7.1 (i.e., as shown in Figure 7.8). In this project, a file called MYFILE55.TXT is created on the SD card. String “This is MYFILE.TXT” is written to the file initially. Then the string “This is the added data…” is appended to the file. The program then reads the contents of the file and sends the string “This is MYFILE.TXT. This is the added data…” to the UART, enabling the data to be displayed on the PC screen when HyperTerminal is run.

The program listing of the project is given in Figure 7.12 (program SD3.C). At the beginning of the program the UART is initialized to 2400 baud. Then the SPI bus and the FAT file system are initialized as required by the library. The program then creates file MYFILE55.TXT by calling library function Mmc_Fat_Assign with the arguments as the filename and the creation flag 0x80, which tells the function to create a new file if the file does not exist. The filename should be in “filename.extension” format, though it is also possible to specify an eight-digit filename and a three-digit extension with no “.” between them, as the “.” will be inserted by the function. Other allowed values of the creation flag are given in Table 7.8. Note that the SD card must have been formatted in FAT16 before we can read or write to it. Most new cards are already formatted, but we can also use the Mmc_Fat_QuickFormat function to format a card.

/**************************************************************
                         SD CARD PROJECT
                         ===============
In this project a SD card is connected to PORTC as follows:
CS  RC2
CLK RC3
DO  RC4
DI  RC5
In addition, a MAX232 type RS232 voltage level converter chip
is connected to serial output port RC6.
The program opens a file called MYFILE55.TXT on the SD card
and writes the string "This is MYFILE.TXT." to this file. Then
the string "This is the added data..." is appended to this file.
The program then sends the contents of this file to the UART.
Author: Dogan Ibrahim
Date:   August 2007
File:   SD3.C
**************************************************************/
char filename[] = "MYFILE55TXT";
unsigned char txt[] = "This is the added data...";
unsigned short character;
unsigned long file_size,i;
void main() {
 //
 // Configure the serial port
 //
 Usart_Init(2400);
 //
 // Initialize the SPI bus
 //
 Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE,
  CLK_IDLE_LOW, LOW_2_HIGH);
 //
 // Initialize the SD card bus
 //
 while(Mmc_Init(&PORTC,2));
 //
 // Initialize the FAT file system
 //
 while(Mmc_Fat_Init(&PORTC,2));
 //
 // Create the file (if it doesn’t exist)
 //
 Mmc_Fat_Assign(&filename, 0x80);
 //
 // Clear the file, start with new data
 //
 Mmc_Fat_Rewrite();
 //
 // Write data to the file
 //
 Mmc_Fat_Write("This is MYFILE.TXT.",19);
 //
 // Add more data to the end...
 //
 Mmc_Fat_Append();
 Mmc_Fat_Write(txt,sizeof(txt));
 //
 // Now read the data and send to UART
 //
 Mmc_Fat_Reset(file_size);
 for(i=0; i<file_size; i++) {
  Mmc_Fat_Read(&character);
  Usart_Write(character);
 }
 for(;;); // wait here forever
}


Figure 7.12: Program listing of the project

Table 7.8: Mmc_Fat_Assign file creation flags

Flag Description
0x01 Read only
0x02 Hidden
0x04 System
0x08 Volume label
0x10 Subdirectory
0x20 Archive
0x40 Device (internal use only, never found on disk)
0x80 File creation flag. If file does not exist and this flag is set, a new file with the specified name will be created.

The file is cleared (if it is not already empty) using function call Mmc_Fat_Rewrite, and then the string “This is MYFILE.TXT” is written to the file by calling library function Mmc_Fat_Write. Note that the size of the data to be written must be specified as the second argument of this function call. Then Mmc_Fat_Append is called and the second string “This is the added data…” is appended to the file. Calling function Mmc_Fat_Reset sets the file pointer to the beginning of the file and also returns the size of the file. Finally, a for loop is set up to read each character from the file using the Mmc_Fat_Read function call, and the characters read are sent to the UART with the Usart_Write function call.

A snapshot of the screen with the HyperTerminal running is shown in Figure 7.13.


Figure 7.13: Snapshot of the screen

Оглавление книги


Генерация: 0.049. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз