11 Using TMR2 as Alternate SPI Clock
The Timer 2 peripheral can be used as an alternative clock for the MSSP peripheral and can be used in the SPI clock configuration.
For example, to create a 10 kHz SPI clock, modify the Timer Period to 50 µs. This corresponds to 20 kHz since the SPI uses TMR2output/2 as clock, meaning that 20 kHz / 2 = 10 kHz frequency.
The following code examples will present how to set up the TMR2 peripheral and be used as clock source for the SPI configured as Master communicating to two Slave devices, alternatively.
To achieve the functionality described by the use case, the following actions will have
to be performed:
- System clock initialization
- TMR2 initialization
- SPI1 initialization
- PPS initialization
- Port initialization
- Slave control functions
- Data exchange function