
The example shows the connection of a compact flash (CF) memory card to a dsPIC30F micocontroller. The CF memory cards are often used as the memory elements in digital video cameras. The memory capacity is high, from 8MB up to 2GB even more, and the read/write time is typically of the order of µs. Application of CF memory cards in microcontroller systems is quite widespread.
In the CF memory cards data are split into sectors (usually of 512 bytes, in earlier models 256 bytes). Reading or writing is not performed directly byte after byte, but data are blocked per sectors through a 512 byte buffer. Fig. 13-7 shows the electrical connection of a CF memory card to a device of the dsPIC30F family. The realization is carried out by using the mikroC compiler for dsPIC30F microcontrollers. The example covers, writing and reading one byte from a CF memory card.
char buff[512];
//-------------- Init for dsPICPRO2
void Cf_Init_dsPICPRO2() {
Cf_Init(&PORTD,8,9,10, &PORTG,14, &PORTG,12, &PORTD,11, &PORTG,15, &PORTG,13, &PORTD);
}//~
void initCF() {
ADPCFG = 0xFFFF;
Cf_Init_dsPICPRO2();
//--- CD1 does not work on non-TTL inputs
//while (CF_Detect() == 0) ; // wait until CF card is inserted
//Delay_ms(500); // wait for a while until the card is stabilized
}//~
//--------------
void testBytes() {
unsigned int i, tmp;
//--- write some data
CF_Write_Init(620,1); // Initialize writing at sector address 620
// for 1 sector (byte)
Uart1_Write_Char('s'); // Notify that writing has started
Delay_ms(1000);
for (i=0; i<=511; i++) { // Write 512 bytes to sector 590
CF_Write_Byte(i);
}
Delay_ms(1000);
//--- read written data
CF_Read_Init(620,1); // Initialize read from sector address 620
Delay_ms(1000);
// for 1 sector (byte)
Cf_Read_Sector(620, buff);
for (i=0; i<=511; i++) { // Read 512 bytes from initialized sector
Uart1_Write_Char(buff[i]);
}
}//~
//-------------- Main program
void main() {
Uart1_Init(19200);
initCF();
testBytes();
}//~!