Application Example

24 Mar

Application Example
The example that follows uses the Microchip PIC18F4550 microcontroller circuit introduced in Chapter 4. The code shows how to detect a MultiMediaCard, initialize communications, and read and write data in the card’s storage media.

Detecting and Selecting a Card
As the circuit in Chapter 4 showed, firmware can use spare port bits to control a MultiMediaCard’s CS input and monitor a card connector’s card-detect (CD) and write-protect (WP) pins. The host brings CS low to enable communications in SPI mode. The card-detect and write-protect pins connect to switches in the card connector, with one switch terminal connected to the ground pin and the other terminal brought out to the CD or WP connector pin. The card-detect switch is open when no card is present and closed when a card is present. The write-protect switch is open if the card’s write-protect tab is in the write-protect position or the card isn’t present. The switch is closed if the tab is in the write-enable position or the card doesn’t have a write-protect tab. Only full-size SD Cards have write-protect tabs, though full-size adapters with tabs enable other card types to provide a write-protect signal.

Hirose DM1 series sockets are an example of SD-Card/MultiMediaCard sockets that have card-detect and write-protect switches. To monitor the state of a switch, connect it to a port pin with a pull up. The port pin is high when the switch is open and low when the switch is closed.

On the PIC18F4550, each I/O port has three registers. A TRIS register sets the direction of the port’s bits (0 = output, 1 = input). Firmware can use the Port and LAT registers to read and write to the port. Reading the Port register returns the current state of the port’s pins. Firmware can write to the port by writing to either the Port or LAT register. Writing to the Port register latches the value to the port pins. The LAT register simplifies writes to individual bits. The register always contains the last value written to the port. To change a single bit on the port, firmware can write to the bit in the register, and the write operation causes the chip to write the register’s contents,
including the just-written bit, to the port pins.

In Chapter 4’s circuit, CS connects to PORTB, bit 3 on the microcontroller. The card-detect pin connects to PORTB, bit 4, and the write-protect pin connects to PORTA, bit 4. The code below provides application-specific names for the port bits and their direction bits. The Microchip C18 compiler files p18f4550.asm and p18f4550.h define the locations of the registers.

Random Posts

No comments yet

Leave a Reply

You must be logged in to post a comment.