我正在尝试将PIC18F25K22的MSSP模块配置为SPI主模式。我正在查看时序,并且整个传输过程中的时钟都不稳定。图片比文字更能说明问题。
发送一个比特后,时钟会缩短,并且每次不会缩短相同的时间。我以前没有使用过SPI,但是我在Wikipedia和其他资源上找到的图表从未显示过。我还连接了一个Arduino,但没有看到这种行为。我的代码是:
#pragma config FOSC = INTIO67 // Oscillator Selection bits (Internal oscillator block)
#pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator used directly)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config WDTEN = OFF // Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
void main(void)
{
OSCCON = 0b11100110;
spi_setup();
__delay_ms(10);
byte temp;
while (TRUE)
{
temp = spi_transfer(0x00);
temp = spi_transfer(0x01);
temp = spi_transfer(0x02);
temp = spi_transfer(0x03);
temp = spi_transfer(0x04);
temp = spi_transfer(0x05);
__delay_us(1);
}
}
void spi_setup(void)
{
SSP1STAT = 0b00000000;
SSP1STATbits.CKE = HIGH; // data transmitted on rising edge
SSP1CON1 = 0b00000000; // enable Master SPI mode
SSP1CON1bits.CKP1 = LOW; //clock idle state is low
//i2c bits, all don't matters for SPI, cleared just in case
SSP1CON3 = 0;
// baud rate generation
SSP1ADD = 0; //FCLOCK = 8Mhz /2 = 2Mhz
// configure pins for output/input as needed
SDI1 = INPUT;
SDO1 = OUTPUT;
SCK1 = OUTPUT;
SS1 = OUTPUT;
SSP1CON1bits.SSPEN1 = HIGH; // enable pins for serial mode
}
unsigned char spi_transfer(unsigned char data)
{
SS1_LAT = LOW; // select slave
PIR1bits.SSPIF = LOW;
SSP1BUF = data;
//while (!SSP1STATbits.BF); //wait for receive to complete
while( !PIR1bits.SSPIF );
SS1_LAT = HIGH; // deselect slave
PIR1bits.SSPIF = LOW; // clear interrupt
return SSP1BUF; //return data from the slave
}
(也是https://gist.github.com/stumpylog/5095250 )
有没有人遇到这个问题或对原因有建议?
我做了什么
最后,我无法使MSSP1模块正常工作。但是,将其更改为与MSSP2模块完全相同的代码,则不会出现此现象。我无法解释,但这解决了问题。