我在使用QE128的SPI2模块时,用的是中断方式收发,在初始时将SPIxC1寄存器的SPTIE位(SPI Transmit Interrupt Enable)置1。但无数据传输时,程序始终会由于SPIxS寄存器的SPTEF位(SPI Transmit Buffer Empty Flag)置位而触发中断。中断里我每次都会读取SPIxS寄存器来清除该标志,但又会被程序自动置位。请问有用过SPI中断的朋友指导下,谢谢!附上程序如下:
void SPI2_Init(void)
{
SPI2C1 = 0x00;
SPI2C2 = 0x00;
// SPIE=1; SPE=1; SPTIE=1; MSTR=1; CPOL=1; CPHA=1; SSOE=0; LSBFE=0
SPI2C1 = 0xFC;
// bit7=0; bit6=0; bit5=0; MODFEN=0; BIDIROE=1; bit2=0; SPISWAI=1; SPC0=0
SPI2C2 = 0x0A;
SPI2BR = 0x22;
}
void SPI2_Interrupt(void)
{
uint8 tmp;
uint8 tda;
tmp=SPI2S;
tda=SPI2D;
if(tmp & 0x80) //SPRF=1, SPI Read Buffer Full Flag
{
if(rbufc) {
SPI2_SerFlag |= CHAR_IN_RX;
rbufc--;
*rbuf++ = tda;
SPI2D = 0xFF; //Dummy Byte
if(rbufc == 0) {
CS_CLR; //Chip Select high
SPI2_SerFlag &= ~CHAR_IN_RX;
}
}
}
else if(tmp & 0x20) //SPTEF=1, SPI Transmit Buffer Empty Flag
{
if(tbufc) { //sending data
SPI2_SerFlag |= CHAR_IN_TX;
tbufc--;
SPI2D = tbuf[tbufc];
}
else {
if(tbuf[0] != 0xFF) //Don't wait for reading data
CS_CLR; //Chip Select high
SPI2_SerFlag &= ~CHAR_IN_TX;
}
}
} |