小弟使用stm32F429的HAL库,但是初始化ADS1256的时候,先写入寄存器,再读出寄存器,发现卡死在while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_RXNE)==RESET)这条语句,请各位大哥不吝赐教!#include "all.h"#include "clock.h"#include "led.h"#include "systick_delay.h"#include "bitband.h"#define DRDY PCin(10) SPI_HandleTypeDef spi1_init;void DRAY_init(){ __HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_Initstruct; GPIO_Initstruct.Mode=GPIO_MODE_INPUT; GPIO_Initstruct.Pin=GPIO_PIN_10; GPIO_Initstruct.Pull=GPIO_PULLUP; GPIO_Initstruct.Speed=GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC,&GPIO_Initstruct);}void spi1_WriteREG(uint8_t reg,uint8_t data){ while(DRDY); while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_TXE)==RESET) { HAL_SPI_Transmit(&spi1_init,®,1, 1000); } while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_TXE)==RESET) { HAL_SPI_Transmit(&spi1_init,0x00,1, 1000); } while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_TXE)==RESET) { HAL_SPI_Transmit(&spi1_init,&data,1,1000); }}uint8_t spi1_ReadREG(uint8_t reg){ while(DRDY); uint8_t temp=0x00; while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_TXE)==RESET) { HAL_SPI_Transmit(&spi1_init,®,1, 1000); } while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_TXE)==RESET) { HAL_SPI_Transmit(&spi1_init,0x00,1, 1000); } while(__HAL_SPI_GET_FLAG(&spi1_init,SPI_FLAG_RXNE)==RESET) { HAL_SPI_Receive(&spi1_init,&temp,1, 1000); } printf("%x
",temp); return temp;}void spi_init(){ __HAL_RCC_SPI1_CLK_ENABLE();
spi1_init.Instance=SPI1; spi1_init.Init.Mode=SPI_MODE_MASTER; spi1_init.Init.Direction=SPI_DIRECTION_2LINES; spi1_init.Init.DataSize=SPI_DATASIZE_8BIT; spi1_init.Init.CLKPolarity=SPI_POLARITY_HIGH; spi1_init.Init.CLKPhase=SPI_PHASE_2EDGE; spi1_init.Init.NSS=SPI_NSS_SOFT; spi1_init.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_256; spi1_init.Init.FirstBit=SPI_FIRSTBIT_MSB; spi1_init.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE; spi1_init.Init.CRCPolynomial=7; spi1_init.Init.TIMode=SPI_TIMODE_DISABLE; HAL_SPI_Init(&spi1_init); __HAL_SPI_ENABLE(&spi1_init);}void spi_gpio_init(){ __HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef gpio_spi_Init;
gpio_spi_Init.Pin=GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; gpio_spi_Init.Mode=GPIO_MODE_AF_PP; gpio_spi_Init.Pull=GPIO_PULLUP; gpio_spi_Init.Speed=GPIO_SPEED_FREQ_HIGH; gpio_spi_Init.Alternate=GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA,&gpio_spi_Init);}void ADS1256_init(){ while(DRDY); spi1_WriteREG(0x50,0x02); spi1_WriteREG(0x51,0x01); spi1_WriteREG(0x52,0x00); spi1_WriteREG(0x53,0x03); spi1_WriteREG(0x54,0x00); if(0x02!=spi1_ReadREG(0x10)) { printf("error
"); } if(0x01!=spi1_ReadREG(0x11)) { printf("error
"); } if(0x00!=spi1_ReadREG(0x12)) { printf("error
"); } if(0x03!=spi1_ReadREG(0x13)) { printf("error
"); } if(0x00!=spi1_ReadREG(0x14)) { printf("error
"); }}int main(){ HAL_Init(); SystemClock_Config(); DRAY_init(); spi_gpio_init(); spi_init(); ADS1256_init(); while(1) { }}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>