PIC16F877A单片机SPI学习
PIR1 Register
The PIR1 register contains the individual flag bits for the peripheral
interrupts.
PIR1 REGISTER
PSPIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF
SSPIF:Synchronous Serial Port Interrupt Flag bit
1 = The SSP interrupt condition has occurred and must be cleared in software
before returning from the interrupt Service Routine. The condition that will
set this are:
SPI-A transmission/reception has taken,place.
I2C Slave - A transmission/reception has taken place.
I2C Master
0 = No SSP interrupt condition has occurred.
SSPCON1: MSSP CONTROL REGISTER1
WCOL SSPOV SSPEN CKP SSPM3 SSPM2 SSPM1 SSPM0
SMP:Sample bit
SPI Master mode:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
SPI Slave mode:
SMP must be cleared when SPI is used in Slave mode.
CKE:SPI Clock Select bit
1 = Transmit occurs on transition from active to Idle clock state
0 = Transmit occurs on transition from Idle to active clock state
BF:Buffer Full Status bit(Recevie mode only)
1 = Receive complete,SSPBUF is full
0 = Receive not complete,SSPBUF is empty
SSPSR寄存器我们是不能直接使用的。
SPI的工作方式可以从两个方面来看,一个是数据的传送,一个是数据的接收。送出数据的时候,只要将数据写入SSPBUF即可;在数据传送完成之后,SSPIF中断标志位会置1。接收的时间就比较复杂了。当有数据从SDI引脚进入单片机之后,会被存往往到SSPSR寄存器中,当完整的8个位接收完毕之后,这个字节的数据就会自动的放到SSPBUF中,一旦SSPBUF中放入新的数据,SSPCON寄存器中的BF位就会自动的设定为1,表示SSPBUF已经填入了新的值,也表示数据的接收已完成。
由于PIC16F877A通过SDO发送数据的同时,会通过SDI读入数据,当1字节的发送完成时,刚好接收1字节到SSPBUF,这时SSPBUF满,BF被置1,故可通过BF标志判断1字节是否发送完成。