Table 7.6: Protected area and data area sizes
| Model | Protected area (blocks) | User area (blocks) |
|---|---|---|
| SDSDB-16 | 352 | 28,800 |
| SDSDB-32 | 736 | 59,776 |
| SDSDJ-64 | 1,376 | 121,856 |
| SDSDJ-128 | 2,624 | 246,016 |
| SDSDJ-256 | 5,376 | 494,080 |
| SDSDJ-512 | 10,240 | 940,864 |
| SDSDJ-1024 | 20,480 | 1,983,744 |
1 block = 512 bytes.
Data can be written to or read from any sector of the card using raw sector access methods. In general, SD card data is structured as a file system and two DOS-formatted partitions are placed on the card: the user area and the security protected area. The size of each area is shown in Table 7.7. For example, in a 1GB card, the size of the security protected area is 519 sectors (1 sector is 512 bytes), and the size of the user data area is 1,982,976 sectors.
Table 7.7: Size of the security protected area and the user area in a DOS-formatted card
| Model | Protected area (sectors) | User area (sectors) |
|---|---|---|
| SDSDB-16 | 39 | 28,704 |
| SDSDB-32 | 45 | 59,680 |
| SDSDJ-64 | 57 | 121,760 |
| SDSDJ-128 | 95 | 245,824 |
| SDSDJ-256 | 155 | 493,824 |
| SDSDJ-512 | 275 | 990,352 |
| SDSDJ-1024 | 519 | 1,982,976 |
1 sector = 512 bytes.
A card can be inserted and removed from the bus without any damage. This is because all data transfer operations are protected by cyclic redundancy check (CRC) codes, and any bit changes caused by inserting or removing a card can easily be detected. SD cards typically operate with a supply voltage of 2.7V. The maximum allowed power supply voltage is 3.6V. If the card is to be operated from a standard 5.0V supply, a voltage regulator should be used to drop the voltage to 2.7V.
Using an SD card requires the card to be inserted into a special card holder with external contacts (see Figure 7.6) so connections are easily made to the required card pins.
Figure 7.6: SD card holder
7.2 mikroC Language SD Card Library Functions
The mikroC language provides an extensive set of library functions to read and write data to SD cards (and also MultiMediaCards, MMC). Data can be written to or read from a given sector of the card, or the file system on the card can be used for more sophisticated applications.
The following library functions are provided:
• Mmc_Init (initialize the card)
• Mmc_Read_Sector (read one sector of data)
• Mmc_Write_Sector (write one sector of data)
• Mmc_Read_Cid (read CID register)
• Mmc_Read_Csd (read CSD register)
• Mmc_Fat_Init (initialize FAT)
• Mmc_Fat_QuickFormat (format the card to FAT16)
• Mmc_Fat_Assign (assign the file we will be working with)
• Mmc_Fat_Reset (reset the file pointer; opens the currently assigned file for reading)
• Mmc_Fat_Rewrite (reset the file pointer and clear assigned file; opens the assigned file for writing)
• Mmc_Fat_Append (move file pointer to the end of assigned file so new data can be appended to the file)
• Mmc_Fat_Read (read the byte the file pointer points to)
• Mmc_Fat_Write (write a block of data to the assigned file)
• Mmc_Set_File_Date (write system timestamp to a file)
• Mmc_Fat_Delete (delete a file)
• Mmc_Fat_Get_File_Date (read file timestamp)
• Mmc_Fat_Get_File_Size (get file size in bytes)
• Mmc_Fat_Get_Swap_File (create a swap file)
In the remainder of this chapter we will look at some SD-card and PIC18 microcontroller-based projects.
PROJECT 7.1 — Read CID Register and Display on a PC Screen
In this project a SD card is interfaced to a PIC18F452-type microcontroller. The serial output port of the microcontroller is connected to the serial input port (e.g., COM1) of a PC. The microcontroller reads the contents of the card CID register and sends this data to the PC so it can be displayed on the PC screen.
Figure 7.7 shows the block diagram of the project.
Figure 7.7: Block diagram of the project
The circuit diagram of the project is shown in Figure 7.8. The SD card is inserted into a card holder and then connected to PORTC of a PIC18F452 microcontroller through 2.2K and 3.3K resistors, using the following pins:
• Card CS to PORTC pin RC2
• Card CLK to PORTC pin RC3
• Card DO to PORTC pin RC4
• Card DI to PORTC pin RC5
Figure 7.8: Circuit diagram of the project
According to the SD card specifications, when the card is operating with a supply voltage of VDD = 3.3V, the input-output pin voltage levels are as follows:
• Minimum produced output HIGH voltage, VOH = 2.475V
• Maximum produced output LOW voltage, VOL = 0.4125V
• Minimum required input HIGH voltage, VIH = 2.0625
• Maximum input HIGH voltage, VIH = 3.6V
• Maximum required input LOW voltage, VIL = 0.825V
Although the output produced by the card (2.475V) is sufficient to drive the input port of a PIC microcontroller, the logic HIGH output of the microcontroller (about 4.3V) is too high for the SD card inputs (maximum 3.6V). Therefore, a potential divider is set up at the three inputs of the SD card using 2.2K and 3.3K resistors. This limits the maximum voltage at the inputs of the SD card to about 2.5V: