Выбрать главу

CRC = “1011100” binary (the LSB bit is always 1)

PROJECT 7.2 — Read/Write to SD Card Sectors

The hardware of this project is the same as for Project 7.1 (i.e., as shown in Figure 7.8). In this project, sector 10 of the SD card is filled with “C” characters, and then this sector is read and the card data is sent to the UART.

The program listing of this project is given in Figure 7.11 (program SD2.C). Two character arrays called data1 and data2, of 512 bytes each, are declared at the beginning of the program. Array data1 is loaded with character “C,” and the contents of this array are written to sector 10 of the SD card. Then the contents of sector 10 are read into character array data2 and sent to the UART, displaying 512 “C” characters on the PC screen. Normally, only one array is used to read and write to the SD card. Two arrays are used here to make it clear that what is sent to the UART is the card data, not the contents of array data1.

/**************************************************************

                        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 loads sector 10 of the SD card with character "C".

The contents of sector 10 is then read and sent to the UART,

displaying 512 "C" characters on the PC display.

Author: Dogan Ibrahim

Date:   August 2007

File:   SD2.C

**************************************************************/

unsigned char data1[512],data2[512];

unsigned int i;

unsigned short x;

void main() {

 //

 // Configure the serial port

 //

 Usart_Init(2400);

 //

 // Initialize the SD card

 // Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);

 //

 // Initialize the SD bus

 //

 while(Mmc_Init(&PORTC,2));

 //

 // Fill buffer with character "C"

 //

 for(i=0; i<512; i++) data1[i] = 'C';

 //

 // Write to sector 10

 //

 x = Mmc_Write_Sector(10, data1);

 //

 // Now read from sector 10 into data2 and send to UART

 //

 x = Mmc_Read_Sector(10,data2);

 for(i=0; i<400; i++) Usart_Write(data2[i]); // Send to UART

 for(;;);                                    // Wait here forever

}

Figure 7.11: Program listing of the project

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();

 //