STM32 driver ADC124S051 (HAL library) source code-upgraded version

  • MCU model: stm32f103c8t6;
  • ADC signal: ADC124S051, 12bit;
  • The sample code provided is to read the data of 3 channels;
  • The problem I encountered when using this chip: It is no problem to read only one channel. If continuous reading>=2 channels, the channel data will be staggered. For example, the code is written to read channel A, but it is actually channel B data. If it is a 4-channel continuous reading, it seems that A reads B, B reads C, C reads D, and D reads A, which is similar to this stagger.
  • After the code is written, I will actually read the data for debugging and correct the misplaced data.
  • The following is the source code:
  • In the HAL library mode, the "DATASIZE" of SPI is configured as 16bit. There is a problem. The parameter type of the SPI operation function HAL_SPI_TransmitReceive() in the HAL library is "uint8_t", but the byte length of the SPI configuration is 16bit. How to do?
  • There are two methods. One is to directly modify the HAL library package function and change the parameter type to uint16_t. I
    am confused when I change it. Try not to change the package source code. This method is not recommended; the other is to force the type, although the parameter The transfer is based on uint8_t, but the SPI function finally gives you the complete uint16_t type data
  • Variables should be defined as arrays
uint16_t channel1[1] = {
    
    0x0000 };	 //注意数据类型都是uint16_t
uint16_t channel2[1] = {
    
    0x0800 };
uint16_t channel3[1] = {
    
    0x1000};
uint16_t channel4[1] = {
    
    0x1800};
uint16_t x_index[1] = {
    
     0 };	//用来存放SPI读取到的数据
uint16_t x_cos[1] = {
    
     0 };
uint16_t x_sin[1] = {
    
     0 };
void Data_collection()
{
    
    	
    ......
	//--------------------------
	reg_CS1 = 0;
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&channel3, (uint8_t*)x_index, 1, 0xff);  	//强制类型转换(uint8_t*),这个函数最终返回给你的x_index还是完整的uint16_t类型的数据。
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&channel4, (uint8_t*)x_cos, 1, 0xff);     
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&channel1, (uint8_t*)x_sin, 1, 0xff); 	
	reg_CS1 = 1;
	......
	/*利用读到的数据x_index、x_cos、x_sin做一些事情*/
}

Guess you like

Origin blog.csdn.net/aqwtyyh/article/details/112999261