我尝试使用 SPI 通信连接 Nucleo-F302R8 和高精度 AD/DA 波共享板。虽然这样做我无法传输或接收数据。我使用 spi3 进行通信,并为芯片选择引脚提供了 ai/o。spi_clk 引脚不产生任何时钟脉冲。我尝试通过环回调试 spi,发现在 mosi 引脚上发生了接收,但时钟又是空的(使用数字示波器检查。我是这个环境的新手,需要很大的帮助。我已经分享了我的代码片段Spi配置:2lines,8bit,极性低,相位1edge,msb在前
void adc_write(uint8_t address, uint8_t value)
{
uint8_t data[3];
data[0] = 0x50|address;
data[1] = 0x01;
data[2] = value;
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi3, data,receive, 3, 50);
//DWT_Delay(10);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
}
void adc_write_command(uint8_t command)
{
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi3,&command,receive,1,50);
//DWT_Delay(10);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
}
int adc_read()
{
uint8_t datacon;
uint32_t convert = 0x00;
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
adc_write_command(0x01); //read
for(int i=0;i<3;i++)
{
HAL_SPI_TransmitReceive(&hspi3,0x00,&datacon,1,50);
convert |= datacon;
convert<<=8;
}
//DWT_Delay(10);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
return convert;
}
float volt(uint32_t data2)
{
float v;
v =(float) data2*0.000000596046; // 8388607 = +5.000v reference voltage
return v;
}
void adc_read_regis(uint8_t address1)
{
uint8_t data1[3];
data1[0] = 0x50|address1;
data1[1] = 0x01;
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi3, data1,receive, 2, 50);
HAL_SPI_TransmitReceive(&hspi3,0x00, &data1[2],1,50);
//DWT_Delay(10);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
}
void adc_reset()
{
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, RESET_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
HAL_GPIO_WritePin(GPIOC, RESET_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
}
void adc_init()
{
adc_write(0x00, 0x03); //status
adc_write(0x02, 0x20); //adcon
adc_write(0x03, 0x82); //drate 100sps
adc_read_regis(0x00); //verification
adc_write_command(0xF0); //selfcab of adc
adc_write(0x01, 0x0F); //mux for channel 0 positive
adc_write_command(0xFC); //sync
adc_write_command(0x00); //wake up
HAL_Delay(500);
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t adc_data_ch0, adc_data_ch1;
float voltage_ch0, voltage_ch1;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI3_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
MX_GPIO_Init();
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
adc_reset();
HAL_Delay(500);
MX_SPI3_Init();
HAL_Delay(500);
adc_write_command(0xFE); //reset to power up values
adc_write_command(0xFC); //sync
adc_write_command(0x00); //wake up
//DWT_Delay(10);
adc_init();
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET); //confirm configuration
MX_USART2_UART_Init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_RESET);
while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
adc_write(0x01, 0x1F); //mux for ch1 positive channel
adc_write_command(0xFC); //sync
adc_write_command(0x00); //wake up
adc_data_ch0 = adc_read();
voltage_ch0 = volt(adc_data_ch0);
HAL_Delay(100);
while(HAL_GPIO_ReadPin(DRDY_GPIO_Port,DRDY_Pin));
adc_write(0x01, 0x0F); //mux for ch0 positive channel
adc_write_command(0xFC); //sync
adc_write_command(0x00); //wake up
adc_data_ch1 = adc_read();
voltage_ch1 = volt(adc_data_ch1);
//frequency_in_ch1 = freq(voltage_in_ch1);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOC, CHIP_SELECT_Pin, GPIO_PIN_SET);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}