很奇怪的软件问题:
当主程序 while(1)循环中添加了if (value==0x40)这行语句之后,SPI通讯的时钟莫名其妙的变成了125KHZ,我在Init_Spi ();函数中设定的SPI通讯时钟为4M,去掉if (value==0x40)这行语句之后,SPI的SCK时钟频率就恢复为4M,请教各位大侠,使用的单片机为PIC16F1503,SPI的SCK时钟频率只有修改寄存器SSP1CON1才可以更改,在while(1)循环中添加了if (value==0x40)怎么会出现这样的问题?
#include <pic.h>
#include "spi.h"
#define IRCF 0X7A
#define READ_REG 0x00
#define STATUS_RF 0x07
U8 Spi_Write (U8 data)
{
U8 databyte;
PIR1 &= ~SSP1IF1 ;// clear SSP interrupt bit
SSP1BUF = data ;
while(!(PIR1&SSP1IF1)); // Wait for interrupt flag to go high indicating transmission is co
databyte = SSP1BUF; // Read the incoming data byte
return (databyte);
}
U8 Spi_Read(void)
{
U8 databyte;
PIR1 &= ~SSP1IF1 ; // Clear SSP interrupt bit
SSP1BUF = 0; // Write dummy data byte to the buffer to initiate transmission
while(!(SSP1STAT&BF1)); // Wait for interrupt flag to go high indicating transmission is complete
databyte = SSP1BUF; // Read the incoming data byte
return (databyte);
}
U8 Spi_RD_Reg (U8 reg)
{
U8 temp = 0;
CSN_ENABLE ;
Spi_Write (reg);
temp = Spi_Read();
CSN_DISABLE;
return (temp) ;
}
U8 rx_array[5]={0};
U8 i;
void main (void)
{
U8 value =0;
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&CLKOUTEN_OFF&BOREN_ON&CP_OFF&MCLRE_ON);
__CONFIG(LVP_OFF);
OSCCON |= IRCF ; // 内部时钟16M
TRISA &= (~BIT4) ;
LATA &= ~BIT4 ;
Init_Spi ();
Init_NRF24L01();
while (1)
{
value =Spi_RD_Reg (READ_REG+STATUS_RF);
if (value==0x40)
LATA |= BIT4 ;
}
}
|