Using the Serial Peripheral Interface (SPI) for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
11-2
Freescale Semiconductor
11.2.1.1
SPI Master Project
The project SPI_Master configures the SPI module in master mode. The main functions are:
•
main — A byte is sent by SPI.
•
MCU_Init – MCU initialization, watchdog disable and the SPI clock module enabled.
•
GPIO_Init – Configure PTE port as output, configure PTD4 as output (
signal).
•
SPI_Init – SPI module configuration.
•
SPI_ISR — Clear module flags.
The following example describes the initialization code for the SPI module in master mode. Two boards
need to be connected. One board is configured as master, the other one works as slave. The firmware
configures the MCU as master and sends a count of 0 to 255 to the slave using the SPI module.
The code below executes the instructions to disable the watchdog, enable the Reset option and backgroud
pin. The System Option Register 1 (SOPT1) is used to configure the MCU. The SCGC1 and SCGC2 are
registers used for power saving consumption, here the bus clock to peripherals can be enabled or disabled.
In this example only the bus clock to the SPI2 module is active. The clocks to the other peripherals are
disabled.
void MCU_Init(void) {
SOPT1 = 0x23; // Watchdog disable. Stop Mode Enable. Background Pin
// enable. RESET pin enable
SCGC1 = 0x00; // Disable Bus clock to unused peripherals
SCGC2 = 0x02; // Bus Clock to the SPI2 module is enabled
}
This is the General Purpose Input/Output configuration. These code lines configure the pin directions for
the PTD port. The SPI protocol can communicate various slaves with one master. To communicate with a
specific slave the
signal must be low. The PTD3 pin is configured as output and the
signal must be
changed by software using a GPIO.
void GPIO_Init(void) {
PTDDD = 0x04; // The SS signal must be generated by software using a GPIO
PTEDD = 0xFF; // Configure PTE port as output
PTED = 0x00; // Put 0's in PTE port
}
This is the initialization code for Serial Peripheral Interface module used for the QE MCU. This
application configures the SPI module to work in master mode. The different pins are used for data input
and data output. To obtain a 15.625KHz bit rate it is necessary to do the following calculations:
256 = (Prescaler Divisor) x (Clock Rate Divider)
256 = 8*32
SS
SS
SS
(
)
)
(
Pr
4
625
.
15
Divider
escaler
MHz
kHz
=
256
625
.
15
4
=
kHz
MHz