Coffee
Joined: 10 Feb 2015 Posts: 9
|
PIC18F8722 SPI Port using Timer2 |
Posted: Tue Feb 10, 2015 3:31 pm |
|
|
I'm posting this little test program because I couldn't find anything that dealt with my situation specifically. I found a couple of erratas that concerned me about using Timer2 for a SPI port clock. I also found a couple of postings that indicated there were some problems using Timer2 for the a SPI port clock on certain PIC controllers. With a little research I determined that those postings didn't really apply to my specific application.
Anyway, while this demo is trivial, it does work. It shows that Timer2 can be used with PIC18F8722 SPI1 using the PICC compiler as documented.
This has a SPI clock rate of 1.6 msecs which is as slow as I could get using a 20 MHz processor clock.
Code: |
#include <main.h>
/************************************************************
Quick test program for the PIC18F8722 controller SPI1 port.
SPI1 using hardware MASTER
Mode 0
Using Timer2 for spi_clk
CCS: PCWH Compiler Version 5.040
Hardware: CCS PIC18F8722 development board
************************************************************/
// SPI modes borrowed from CCSinfo.com forum posts
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
void main()
{
// initialize
setup_timer_2(T2_DIV_BY_16,0xff,1);
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_T2);
output_LOW(PIN_A1); // my SPI_LOAD Pin
// run test
while(TRUE)
{
spi_write(0xA3); // something to write
//SPI_LOAD assert
output_high(PIN_A1);
delay_ms(1);
output_low(PIN_A1);
}
}
|
Many thanks to this forum for all the useful postings. _________________ Thank you,
Bill |
|