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

PROJECT 7.2 — Read/Write to SD Card Sectors

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

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


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