【AT32F421 Review】+ ADC Sampling and SPI_OLED Display
[Copy link]
This article will evaluate the hardware SPI and 12-bit ADC of AT32F421. First, the hardware SPI will be presented in the form of SPI_OLED, and the ADC will be tested by testing the actual voltage.
The OLED I chose is the common 0.96-inch OLED on the market. It supports I2C and SPI interfaces. I will use the AT32 hardware SPI to drive it and use it as a display for the voltage value measured by the ADC.
First of all, because there are many people using 0.96-inch OLEDs on the market, some developers or merchants have written universal drivers for them. In order to improve development efficiency and avoid reinventing the wheel, I chose a driver from a certain park, which provides a lot of routines, and I modified them based on it.
First, create three C files such as SPI.c, SPI_OLED.c, ADC.c and their corresponding h header files in the HARDWARE folder. Then add at32f4xx_spi.c, at32f4xx_adc.c and at32f4xx_dma.c in FWLIB as shown in the figure below
The second step is to copy the analog SPI driver of a certain park to SPI_OLED.c, and
OLED_WR_Byte , which is the function of writing a byte, is modified as shown in the figure below, and the software simulated transmission process is replaced by the hardware SPI sending function SPI_I2S_TxData.
Because there are DC and RST pins on SPI_OLED, I use PA6 and PA4 to replace them respectively, as shown in the figure below.
Other driver functions do not need to be modified.
The third step is to configure the hardware SPI of AT32F421. First, add the following function in SPI.c so that it can be called in the main function (refer to the Simplex_Interrupt example in AT_START_F421 in the official BSP). It should be noted here that, first, we need to use SPI, configure GPIO, configure the SPI corresponding clock and GPIO corresponding clock, and configure the SPI parameters; second, according to the timing diagram of the SPI interface of the SSD1306 driver chip used by the 0.96-inch OLED, the initial clock level can be high or low, and then transmit data on the rising edge, so I configure the initial level to low through SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW; and then through SPI_InitStructure.SPI_CPHA = SPI_CPHA_1EDGE; configure it as the first edge change transmission , that is, the rising edge. (Here I would like to add that it may be my misunderstanding of the timing diagram. When CPOL and CPHA are not configured as the above configuration, it seems to be drivable after the experiment, but my suggestion is that it is best to configure it as the above configuration). The specific configuration is shown in the figure below.
The fourth step is to configure ADC. Please refer to the ADC1_DMA example in AT_START_F421 in the official BSP. I will only talk about the key points here. Please see the attachment for the specific configuration. The measured ADC data here is transferred to a variable array through DMA, through DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)ADC_RegularConvertedValueTab;
DMA_InitStructure.DMA_BufferSize = 1;
Move the measured voltage to the ADC_RegularConvertedValueTab array,
I will then test the ADC measuring the external voltage and the internal reference voltage in turn.
The following is to measure the external voltage value of channel 1 of ADC1, which is the external value of PA1
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239_5);
//Test channel 1 PA1
The following can measure the internal reference voltage
ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,1,ADC_SampleTime_239_5); //Test internal reference voltage
Remember to add the following sentence , which is the function that enables ADC to measure the internal temperature sensor and reference voltage
/* Enables Temperature Sensor and Vrefint Channel */
ADC_TempSensorVrefintCtrl(ENABLE);
Finally , write the following function in the main program. The microcontroller updates the actual voltage value collected at a refresh rate of 1 second. A conversion is required here: ADC_RegularConvertedValueTab[0]*3282/4096.0; the 3282 in this statement means that the working voltage of the microcontroller measured by the multimeter is 3282mv. The voltage value may be different in different situations, so this needs to be modified.
Finally, let's show the actual measurement results . First, measure the reference voltage inside the microcontroller. According to the data sheet, this reference voltage should be around 1.20V.
The actual measurement is 1.206V, which meets the standard
Next, we tested different voltage values and compared them with a calibrated 4.5-digit multimeter. The ADC of AT32F421 is still very reliable and has high accuracy.
This time the ADC and SPI test of AT32F421 is completed
|