Home > other >  STM32F407 using dma sampling adc + two voltage signals, abnormal serial printing
STM32F407 using dma sampling adc + two voltage signals, abnormal serial printing

Time:12-03

STM32F407 using dma sampling adc + two voltage signals, serial printing has been measured for the first time, then the same, in the simulation mode, reset once, will update to the current measured values, the value is correct, but is always keep the first print the value of the code is as follows:
Main function:
Int main (void)
{
Under-16 adcx;
Float temp;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//set the system interrupt priority group 2
Delay_init (168);//initialization time delay function
Uart_init (9600);//initialize serial port baud rate to 115200
LED_Init();//initialize leds
LCD_Init ();//initialize the LCD interface
BSP_Init ();
Adc1_start ();
While (1)
{

AD_Value [0]=(float) ADCDualConvertedValue [0]/4096 * 3.3;//get voltage value
AD_Value [1]=(float) ADCDualConvertedValue [1]/4096 * 3.3;//to get current value
Printf (" \ r \ n voltage measurement 1: % f V \ r \ n ", AD_Value [0]).
Printf (" \ r \ n voltage measurement for 2: % f V \ r \ n ", AD_Value [1]).
delay_ms(500);
}
}
BSP. C functions are as follows:
# include "BSP. H"
# include "sys. H"
//# include "adc. H"



/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* the INCLUDE FILES
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/

Volatile uint16_t ADCDualConvertedValue [2].
Float AD_Value [2].



/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* LOCAL FUNCTION as
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/

The static void HSE_SetSysClock (void);
Void ADC1_DMA_Config (void);
Void ADC1_GPIO_Config (void);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Func Name: BSP_Init
Descriptions: BSP initialization
Input para:
The Output para:
The Return value: function performs the results
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void BSP_Init (void)

{
HSE_SetSysClock ();

ADC1_GPIO_Config ();

Adc_Init ();

ADC1_DMA_Config ();




}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Func Name: HSE_SetSysClock
Descriptions: STM32F407 system clock configuration: SYSCLK=168 MHZ, APB1=42 MHZ, APB2=84 MHZ
Input para: none
The Output para: none
The Return value: none
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
The static void HSE_SetSysClock (void)
{
//uint32_t pllmul;
__IO uint32_t StartUpCounter=0, HSEStartUpStatus=0;

//initialize the RCC peripherals reset state, it is a must
RCC_DeInit ();

//can make HSE, open external crystals, 8 m
RCC_HSEConfig (RCC_HSE_ON);

//wait for HSE start stable
HSEStartUpStatus=RCC_WaitForHSEStartUp ();

//HSE successfully started
If (HSEStartUpStatus==SUCCESS)
{
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --//
//can make FLASH pre access buffer
FLASH_PrefetchBufferCmd (ENABLE);

FLASH_SetLatency (FLASH_Latency_5);
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --//

//AHB preassigned dividing frequency factor is set to 1, HCLK=SYSCLK
RCC_HCLKConfig (RCC_SYSCLK_Div1);

//APB2 preassigned dividing frequency factor is set to 1, PCLK2=HCLK
RCC_PCLK2Config (RCC_HCLK_Div2);

//APB1 preassigned dividing frequency factor is set to 1, PCLK1=HCLK
RCC_PCLK1Config (RCC_HCLK_Div4);

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set various frequency is mainly set up here -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --//
//set the PLL clock source to HSE, set the PLL frequency multiplication factor
//PLLCLK=8 MHZ * pllmul=168 MHZ
RCC_PLLConfig (RCC_PLLSource_HSE, 8336, 2, 4);

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -//

//open the PLL
RCC_PLLCmd (ENABLE);

//wait for PLL stable
While (RCC_GetFlagStatus (RCC_FLAG_PLLRDY)==RESET)
{
}

//when PLL is stable, the PLL clock switch to a system clock SYSCLK
RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
//RCC_SYSCLKConfig (RCC_SYSCLKSource_HSE);
//read clock switching state, make sure PLLCLK was selected as system clock
While (RCC_GetSYSCLKSource ()!=0 x08)//the doubt
{
}
}
The else
{//HSE open failure, the program will come here, the user can add the error code to handle here
//when the HSE open failure or malfunction, microcontroller will automatically set HSI to the system clock,
//HSI is the internal high speed clock, 8 MHZ
While (1)
{
}
}
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Func Name: ADC1_DMA_Config
Descriptions: using DMA function
Input para: none
The Output para: none
The Return value: none
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
The static void ADC1_DMA_Config (void)
{
DMA_InitTypeDef DMA_InitStructure;

/* DMA channel1 configuration */
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_DMA2, ENABLE);//can make DMA2 clock//enabling DMA transfer

DMA_DeInit (DMA2_Stream0);//20205.5.15

While (DMA_GetCmdStatus (DMA2_Stream0)!={}//DISABLE) wait for the DMA can be configured (2020.5.15)

DMA_InitStructure. DMA_Channel=DMA_Channel_0;
DMA_InitStructure. DMA_PeripheralBaseAddr=(u32) & amp; (ADC1 - & gt; DR);//ADC address
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related