我正在尝试将我的程序移动到hal并且我的SPI仅接收255.我忘了设置什么?
阅读功能:
uint8_t addressMsb[1] = { READ_ADDRESS | RTD_MSB_ADDRESS };
uint8_t addressLsb[1] = { READ_ADDRESS | RTD_LSB_ADDRESS };
uint8_t Temp_Msb[1];
uint8_t Temp_Lsb[1];
...
PT_CS1_LOW;
HAL_SPI_Transmit(&hspi1, (uint8_t*) addressMsb, 1, 10);
HAL_SPI_Receive(&hspi1, (uint8_t*) Temp_Msb, 1, 10);
PT_CS1_HIGH;
for(int i = 0; i<10;i++);
PT_CS1_LOW;
HAL_SPI_Transmit(&hspi1, (uint8_t*) addressLsb, 1, 10);
HAL_SPI_Receive(&hspi1, (uint8_t*) Temp_Lsb, 1, 10);
PT_CS1_HIGH;
...
设定:
void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
...
__HAL_RCC_SPI1_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
我知道这不是硬件故障,因为这个电路板上的自制功能起作用。 (频率和设置相同)
旧功能:
if (!((SPI1)->CR1 & SPI_CR1_SPE)){
while(((SPI1)->SR & (SPI_SR_TXE | SPI_SR_RXNE)) == 0 || (SPI1)->SR & SPI_SR_BSY))SPI_WAIT(SPIx);
SPI1->DR = data;
while(((SPI1)->SR & (SPI_SR_TXE | SPI_SR_RXNE)) == 0 || ((SPI1)->SR & SPI_SR_BSY))SPI_WAIT(SPIx);
return SPI1->DR;
}
答案 0 :(得分:0)
嘲笑我的错误, 如果有人需要一个MAX31865的工作示例,你可以把代码放在那里......我把我的电脑和我的硬件电源关闭了5分钟,现在即使用HAL也能正常工作。很可疑它确实与旧程序一起工作,并且使用HAL并且重新启动后都工作......但是现在我知道HAL代码原则上没有错。
READ_ADDRESS = 0x00
RTD_MSB_ADDRESS = 0x01
RTD_LSB_ADDRESS = 0x02