Using the Serial Peripheral Interface (SPI) for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
Freescale Semiconductor
11-3
void SPI_Init (void) {
SPI2BR = 0x75; // Select the highest baud rate prescaler divisor and the
// highest baud rate divisor
SPI2C1 = 0xD0; // SPI Interrupt enable, system enable and master mode selected
SPI2C2 = 0x00; // Different pins for data input and data output
}
This is the main function, descibed above are the called functions, and all the interrupts are enabled.
Within the endless loop a byte is sent by SPI and the next byte is sent after a delay. For detailed information
about the SPI module, refer to the QE MCU reference manual. It can be found at www.freescale.com.
void main(void) {
UINT8 counter = 0;
MCU_Init(); // Function that initializes the MCU
GPIO_Init(); // Function that initializes the Ports of the MCU
SPI_Init(); // Function that initializes the SPI module
EnableInterrupts; // enable interrupts
for(;;) {
delay(60000); // Delay function
while (!SPI2S_SPTEF && !PTDD_PTDD3); // Wait until transmit buffer is empty
PTDD_PTDD3 = 0; // Slave Select set in low
SPI2D = counter; // Put in SPI buffer a data to send
PTED = counter; // Display the counter value on LEDs
+; // Increment counter
} // loop forever
// please make sure that you never leave this function
}
NOTE
This is the SPI interrupt service routine. This routine is used when a byte is
sent by the slave to the master.
void interrupt VectorNumber_Vspi2 SPI_ISR(void) {
// SPI interrupt vector number = 12 (S08)
// SPI interrupt vector number = 74 (V1)
UINT8 temp;
while (PTDD_PTDD0); // Wait for clock to return no default
PTDD_PTDD3 = 1; // Set Slave Select high
temp = SPI2S; // Clear register flag
temp = SPI2D; // Read data register to clear receive flag
}
11.2.1.2
SPI Slave Project
The project SPI_Slave configures the SPI module in slave mode. The main functions are:
•
main — Waits for the SPI interrupt to occur.
•
MCU_Init – MCU initialization, watchdog disable and the SPI clock module enabled.
•
GPIO_Init – Configure PTE port as output.
•
SPI_Init – SPI module configuration.
•
SPI_ISR — Display the received data in PTE port
The firmware for this project is similar to the SPI_master project. The differences are, the device is
configured as slave and only Receives a byte and displays it on the PTE port.