miscellaneous
order
This article introduces the adc acquisition and SD card storage based on STM32F4
laboratory
The requirement of the seniors is to use STM32 to collect Adc value and store it in SD, and then use Matlab to analyze the frequency of FFT (the first in the world)
brief introduction
ST (Italian French semiconductor) has launched an ARM ® Cortex based Gamma -The STM32F4 Series High-Performance microcontroller with M4 as the core adopts NVM technology of 90nm and ART (it is said that it can operate the 28 nm tool FPGA of the learned in the future).
/Here's some atom
Called resource: Adc
Analog to digital converter is A/D converter, referred to as ADC, usually refers to an electronic component that transforms analog signal into digital signal.
Calling peripheral: SD card
Compared with u disk and flash, SD card is a large capacity storage device suitable for single chip microcomputer system. It has large capacity selection scale (tens of M to tens of G), simple replacement and convenient movement. It is the first choice for large capacity external memory of single chip microcomputer.
Implementation process
Adc
The corresponding relationship between ADC channel and pin can be found in the data manual of STM32F4, where ADC1 channel 5 is used
Initialize IO to analog first
__HAL_RCC_ADC1_CLK_ENABLE(); //Enable ADC1 clock __HAL_RCC_GPIOA_CLK_ENABLE(); //Turn on GPIOA clock GPIO_Initure.Pin=GPIO_PIN_5; //PA5 GPIO_Initure.Mode=GPIO_MODE_ANALOG; //simulation GPIO_Initure.Pull=GPIO_NOPULL; //Without up and down HAL_GPIO_Init(GPIOA,&GPIO_Initure); //
Initialize ADC to set ADC frequency division coefficient, resolution, mode, scanning mode, alignment and other information
In the HAL library, the ADC is initialized by the function HAL_ADC_Init, which is declared as:
HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc);
The inlet parameter hadc is ADC_HandleTypeDef structure pointer type, which is defined as:
//Structure ADC_HandleTypeDef typedef struct { ADC_TypeDef *Instance; //ADC1/ ADC2/ ADC3 ADC_InitTypeDef Init; //Initializing structure variables __IO uint32_t NbrOfCurrentConversionRank; //Current conversion sequence DMA_HandleTypeDef *DMA_Handle; //DMA mode use HAL_LockTypeDef Lock; __IO HAL_ADC_StateTypeDef State; __IO uint32_t ErrorCode; }ADC_HandleTypeDef; //Structure ADC_InitTypeDef typedef struct { uint32_t ClockPrescaler; //Division coefficient 2 / 4 / 6 / 8 division ADC_CLOCK_SYNC_PCLK_DIV4 uint32_t Resolution; //Resolution 12 / 10 / 8 / 6 bit: ADC_RESOLUTION_12B uint32_t DataAlign; //Alignment: left or right: ADC_DATAALIGN_RIGHT uint32_t ScanConvMode; //Scan mode DISABLE uint32_t EOCSelection; //Whether the EOC flag is set to DISABLE uint32_t ContinuousConvMode; //Turn on continuous conversion mode or single conversion mode DISABLE uint32_t DMAContinuousRequests;//Enable DMA request continuous mode or single mode DISABLE uint32_t NbrOfConversion; //How many conversions 1 are there in the rule sequence uint32_t DiscontinuousConvMode;//Discontinuous sampling mode DISABLE uint32_t NbrOfDiscConversion; //Number of discontinuous sampling channels 0 uint32_t ExternalTrigConv; //External trigger mode ADC_SOFTWARE_START uint32_t ExternalTrigConvEdge; //External trigger edge }ADC_InitTypeDef;
After setting the above information, you can turn on the AD converter (through ADC_CR2 register control)
HAL_ADC_Start(&ADC1_Handler); //Turn on ADC
Configure channel, read channel ADC value
After completing the above steps, the ADC is ready. Next, set the channel for rule sequence 1, and start the ADC conversion. After the conversion, read the conversion result value.
The functions that set the regular sequence channel and sampling period are:
HAL_StatusTypeDef HAL_ADC_ConfigChannel( ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig);
Needless to say, the first parameter is sConfig, which is the second entry parameter
It's ADC_ChannelConfTypeDef structure pointer type, which is defined as follows:
typedef struct { uint32_t Channel; //ADC channel uint32_t Rank; //The number of transformations in regular channels uint32_t SamplingTime; //Sampling time uint32_t Offset; //Standby, not used yet }ADC_ChannelConfTypeDef; //example ADC1_ChanConf.Channel= ADC_CHANNEL_5; //Channel 5 ADC1_ChanConf.Rank=1; //First sequence, sequence 1 ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_480CYCLES; //Sampling time ADC1_ChanConf.Offset=0; HAL_ADC_ConfigChannel(&ADC1_Handler,&ADC1_ChanConf); //Channel configuration
After configuring the channel and enabling the ADC, the next step is to read the ADC value. The query mode is adopted to read (the latest period end time is urgent and DMA will be tried next time), so it is necessary to wait for the last conversion to end. This procedure HAL library provides a special function HAL_ADC_PollForConversion, the function is defined as:
HAL_StatusTypeDef HAL_ADC_PollForConversion( ADC_HandleTypeDef* hadc, uint32_t Timeout);
After the last conversion, the next step is to read the ADC value. The function is:
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc);
SD card
Through today's study, I mainly understand the use of SD card and file system FATFS. For the specific implementation principle, please refer to the following blog explanation
https://blog.csdn.net/vca821/article/details/80738151?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1
https://blog.csdn.net/nulidehahafr/article/details/84146148
Hal library SD card supports source file stm32f4xx_hal_sd.c and stm32f4xx_II_sdmmc.h and corresponding header file stm32f4xx_hal_sd.h and stm32f4xx_II_sdmmc.h
In order to really apply SD card, we must use file system management and FATFS to manage SD card to read and write SD card files.
FatFs is a general file system (FAT/exFAT) module, which is used to implement FAT file system in small embedded system. FatFs components are written in accordance with ANSI C(C89), completely separated from the disk I/O layer, so they do not depend on the hardware platform. It can be embedded in microcontrollers with limited resources, such as 8051, PIC, AVR, ARM, Z80, RX, etc., without any modification.
FATFS is a free and open source module of FAT file system. It is written in standard C language, so it has good independence of hardware platform.
The source code of FATFS can be found in http://elm-chan.org/fsw/ff/00index_e.html Download.
Function f of application layer_ open f_ read f_ write f_ Close and so on. It can be as simple as reading / writing files on a PC. (refer to main function code for specific function application)
Main function code
The core code is as follows
f_opendir((u8 *)"0:/"); //Open Directory delay_ms(5); //Delay 5ms (happy is good f_open((u8 *)"xdpnb.txt",2); //Open the file in the directory (mode 2 r +) (Xdp Niubi for(t = 0; t < 2048; t++){ //2048 samples adcx=Get_Adc_Average(ADC_CHANNEL_5,1);//Get the conversion value of Adc channel 5 and average it once rui[t] = adcx; //Stored in Rui array } for(t = 0; t < 2048; t++){ //In order to ensure the quality of sampling, the collection and storage processes are distinguished myitoa(rui[t],tem,10); //Call custom number to character function to save in temporary array f_write((u8 *)tem,0X4); //Write array length 4 to SD card f_write((u8 *)"\r\n",0X2); //Line break (for Matlab parsing } f_close(); //Close file!
(Ps: close the file after opening)
The analog adc value is the sine wave generated by the laboratory signal source
Specific experimental phenomena and principles
TerayTech
Off line digital signal processing (1) -- read the TXT file with Matlab and do FFT analysis
Off line digital signal processing (2) -- using Matlab to analyze the data collected by STM32F4 ADC
(about to take over 28nm)Xilinx ZYNQ learning notes (I) -- using PS to read and write SD card