本帖最后由 gameboy22 于 2015-7-26 22:26 编辑
最近用M0516调试NRF24L01,但是检测不到24L01;后来在线调试仿真,发现,向SPI1的TX0寄存器写数据的时候,写不进去,不知道是什么问题,我用的是SPI1.下面是相关源代码!
/*********************************************************/
/*SPI1初始化函数 */
/*********************************************************/
void SPI1_Init(void)
{
SPI1->CNTRL = SPI_CNTRL_MASTER_MODE | SPI_CNTRL_CLK_IDLE_LOW | SPI_CNTRL_MSB_FIRST | SPI_CNTRL_TX_RISING |
SPI_CNTRL_RX_RISING | SPI_CNTRL_TX_BIT_LEN(8);
SPI1->DIVIDER = (SPI1->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | SPI_DIVIDER_DIV(11);//HCLK / ((2+1)*2) = 8MHz
}
void GPIO_Init(void)
{
GPIO->DBNCECON = GPIO_DBNCECON_ICLK_ON | GPIO_DBNCECON_DBCLKSRC_HCLK | GPIO_DBNCECON_DBCLKSEL_32768;
P0->PMD = GPIO_PMD_PMD0_OUTPUT | GPIO_PMD_PMD1_OUTPUT | GPIO_PMD_PMD2_OUTPUT | GPIO_PMD_PMD3_OUTPUT
| GPIO_PMD_PMD6_QUASI | GPIO_PMD_PMD4_OUTPUT | GPIO_PMD_PMD7_OUTPUT | GPIO_PMD_PMD5_QUASI;
P1->PMD = GPIO_PMD_PMD6_INPUT | GPIO_PMD_PMD7_INPUT | GPIO_PMD_PMD2_OUTPUT | GPIO_PMD_PMD3_OUTPUT;
P2->PMD = GPIO_PMD_PMD0_OUTPUT | GPIO_PMD_PMD1_OUTPUT | GPIO_PMD_PMD2_OUTPUT | GPIO_PMD_PMD3_OUTPUT |
GPIO_PMD_PMD4_OUTPUT | GPIO_PMD_PMD5_OUTPUT | GPIO_PMD_PMD6_INPUT | GPIO_PMD_PMD7_OUTPUT;
P3->PMD = GPIO_PMD_PMD2_INPUT | GPIO_PMD_PMD3_INPUT | GPIO_PMD_PMD6_INPUT | GPIO_PMD_PMD7_INPUT;
P4->PMD = GPIO_PMD_PMD0_OUTPUT | GPIO_PMD_PMD1_OUTPUT | GPIO_PMD_PMD3_INPUT | GPIO_PMD_PMD2_OUTPUT |
GPIO_PMD_PMD4_OUTPUT | GPIO_PMD_PMD5_OUTPUT;
P3->PMD |= GPIO_PMD_PMD4_QUASI | GPIO_PMD_PMD5_OUTPUT;
GPIO_EnableInt(P3, 6, GPIO_INT_RISING);
GPIO_EnableInt(P3, 2, GPIO_INT_RISING);
GPIO_EnableInt(P1, 7, GPIO_INT_RISING);
NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);
NVIC_EnableIRQ(GPIO_P0P1_IRQn);
NVIC_EnableIRQ(EINT0_IRQn);
}
void SYS_Init(void)
{
SYS_UnlockReg();
SYSCLK->PWRCON |= SYSCLK_PWRCON_XTL12M_EN_Msk | SYSCLK_PWRCON_IRC10K_EN_Msk;
SYS_WaitingForClockReady( SYSCLK_CLKSTATUS_XTL12M_STB_Msk);
SYSCLK->CLKSEL0 = SYSCLK_CLKSEL0_HCLK_XTAL;
SYSCLK->PLLCON |= SYSCLK_PLLCON_PD_Msk;
SYSCLK->PLLCON = PLLCON_SETTING;
SYS_WaitingForClockReady(SYSCLK_CLKSTATUS_PLL_STB_Msk | SYSCLK_CLKSTATUS_XTL12M_STB_Msk | SYSCLK_CLKSTATUS_IRC10K_STB_Msk);
SYSCLK->CLKSEL0 = SYSCLK_CLKSEL0_STCLK_HCLK_DIV2 | SYSCLK_CLKSEL0_HCLK_PLL;
SYSCLK->APBCLK = SYSCLK_APBCLK_ADC_EN_Msk | SYSCLK_APBCLK_PWM67_EN_Msk | SYSCLK_APBCLK_PWM45_EN_Msk |
SYSCLK_APBCLK_PWM23_EN_Msk | SYSCLK_APBCLK_PWM01_EN_Msk | SYSCLK_APBCLK_UART0_EN_Msk |
SYSCLK_APBCLK_SPI1_EN_Msk | SYSCLK_APBCLK_I2C0_EN_Msk | SYSCLK_APBCLK_FDIV_EN_Msk |
SYSCLK_APBCLK_TMR3_EN_Msk | SYSCLK_APBCLK_TMR2_EN_Msk | SYSCLK_APBCLK_TMR1_EN_Msk |
SYSCLK_APBCLK_TMR0_EN_Msk | SYSCLK_APBCLK_WDT_EN_Msk;
SYSCLK->CLKSEL1 = SYSCLK_CLKSEL1_PWM23_HCLK | SYSCLK_CLKSEL1_PWM01_XTAL | SYSCLK_CLKSEL1_UART_PLL |
SYSCLK_CLKSEL1_TMR3_XTAL | SYSCLK_CLKSEL1_TMR2_XTAL | SYSCLK_CLKSEL1_TMR1_XTAL |
SYSCLK_CLKSEL1_TMR0_XTAL | SYSCLK_CLKSEL1_ADC_HCLK | SYSCLK_CLKSEL1_WDT_IRC10K;
SYSCLK->CLKSEL2 = SYSCLK_CLKSEL2_PWM67_HCLK | SYSCLK_CLKSEL2_PWM45_HCLK | SYSCLK_CLKSEL2_FRQDIV_HCLK;
SYSCLK->CLKDIV = SYSCLK_CLKDIV_ADC(7) |
SYSCLK_CLKDIV_UART(1) |
SYSCLK_CLKDIV_HCLK(1);
SystemCoreClockUpdate();
SYS->P0_MFP = SYS_MFP_P00_GPIO | SYS_MFP_P01_GPIO | SYS_MFP_P02_GPIO | SYS_MFP_P03_GPIO |
SYS_MFP_P04_GPIO/*SPISS1*/ | SYS_MFP_P05_MOSI_1 | SYS_MFP_P06_MISO_1 | SYS_MFP_P07_SPICLK1;
SYS->P1_MFP = SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_GPIO/*RXD1*/ | SYS_MFP_P13_GPIO/*TXD1*/ |
SYS_MFP_P14_AIN4 | SYS_MFP_P15_AIN5 | SYS_MFP_P16_GPIO | SYS_MFP_P17_GPIO;
SYS->P2_MFP = SYS_MFP_P20_GPIO | SYS_MFP_P21_GPIO | SYS_MFP_P22_GPIO | SYS_MFP_P23_GPIO |
SYS_MFP_P24_GPIO | SYS_MFP_P25_GPIO | SYS_MFP_P26_GPIO | SYS_MFP_P27_GPIO;
SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P32_GPIO | SYS_MFP_P33_GPIO |
SYS_MFP_P34_GPIO | SYS_MFP_P35_GPIO | SYS_MFP_P36_GPIO | SYS_MFP_P37_GPIO;
SYS->P4_MFP = SYS_MFP_P40_GPIO | SYS_MFP_P41_GPIO | SYS_MFP_P42_PWM2/*GPIO*/ | SYS_MFP_P43_GPIO |
SYS_MFP_P44_GPIO | SYS_MFP_P45_GPIO | SYS_MFP_P46_ICE_CLK | SYS_MFP_P47_ICE_DAT;
SYS_LockReg();
}
uint8_t SPIx_ReadWriteByte(uint8_t TxData)
{
uint8_t u8rx0_tmp = 0;
// _SPI_WRITE_TX_BUFFER0(SPI1, TxData); //发送一个byte
SPI1->TX[0] = TxData;
_SPI_SET_GO(SPI1); //启动SPI1传输
// while(SPI1 ->CNTRL & SPI_CNTRL_GO_BUSY);
while(_SPI_GET_BUSY_STATUS(SPI1));
u8rx0_tmp = _SPI_GET_RX0_DATA(SPI1) & 0xff;
return u8rx0_tmp; //返回收到的数据
}
上面的源码中,调试到SPI1->TX[0] = TxData;的时候,发现TX0寄存器根本就写不进去,不知道什么原因!
|