Information Technology Reference
In-Depth Information
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 indi-
vidual 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 com-
piler files p18f4550.asm and p18f4550.h define the locations of the regis-
ters.
#define SDC_CS LATBbits.LATB3
#define SDC_CS_DIR TRISB3
#define MEDIA_CD RB4
#define MEDIA_CD_DIR TRISB4
#define MEDIA_WD RA4
#define MEDIA_WD_DIR TRISA4
The SocketInitialize function initializes a MultiMediaCard socket by setting
the direction of the card-detect, CS, and write-protect bits and setting CS
high to deselect the card:
void SocketInitialize(void)
{
MEDIA_CD_DIR = INPUT;
SDC_CS_DIR = OUTPUT;
MEDIA_WD_DIR = INPUT;
SDC_CS = 1;
}
The DetectSDCard function returns the state of the card-detect pin:
int DetectSDCard(void)
{
if (MEDIA_CD)
return 0; // Card not present.
else
return 1; // Card is present.
}
Search WWH ::




Custom Search