void ADC1_conf(void)
{
ADC_ChannelConfTypeDef sConfig;
HAL_ADC_Init(&hadc1);
/**Configure the global features of the ADC (Clock, Resolution, Data,Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE; //upward
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START; //software trigger
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; //rising edge
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //< Data right alignment
hadc1.Init.NbrOfConversion = 2; /* This parameter must be a number between Min_Data = 1 and Max_Data = 16.
Note: This parameter must be modified when no conversion is on going on regular group (ADC disabled,
or ADC enabled without continuous mode or external trigger that could launch a conversion). */
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5; // Sampling time 12.5 ADC clock cycles at ADC resolution 12 bits.
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
void ADC1_conf(void)
{
HAL_Init(); // HAL Init
/**Configure the global features of the ADC (Clock, Resolution, Data,Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE; //upward
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START; //software trigger
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; //rising edge
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //< Data right alignment
hadc1.Init.NbrOfConversion = 2; /* This parameter must be a number between Min_Data = 1 and Max_Data = 16.
Note: This parameter must be modified when no conversion is on going on regular group (ADC disabled,
or ADC enabled without continuous mode or external trigger that could launch a conversion). */
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5; // Sampling time 12.5 ADC clock cycles at ADC resolution 12 bits.
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
评分
查看全部评分
参考人家是怎么弄的,看看自己有没有漏掉什么代码规则,初始化代码设置有没有漏写
#include "stm32l4xx.h"
#include "stm32l476g_discovery.h"
UART_HandleTypeDef huart2;
GPIO_InitTypeDef GPIO_InitStruct;
void configure_system_clock(void);
int main(void)
{
HAL_Init(); // HAL Init
configure_system_clock(); // Configure Clock
__GPIOD_CLK_ENABLE(); // Enable GPIOD clock
__USART2_CLK_ENABLE(); // Enable USART2 Clock
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
HAL_UART_Init(&huart2);
后面就不粘贴了,可以参考下
评分
查看全部评分
#include "stm32l4xx.h"
#include "stm32l476g_discovery.h"
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
void ADC1_conf(void)
{
ADC_ChannelConfTypeDef sConfig;
HAL_ADC_Init(&hadc1);
/**Configure the global features of the ADC (Clock, Resolution, Data,Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE; //upward
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START; //software trigger
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; //rising edge
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //< Data right alignment
hadc1.Init.NbrOfConversion = 2; /* This parameter must be a number between Min_Data = 1 and Max_Data = 16.
Note: This parameter must be modified when no conversion is on going on regular group (ADC disabled,
or ADC enabled without continuous mode or external trigger that could launch a conversion). */
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5; // Sampling time 12.5 ADC clock cycles at ADC resolution 12 bits.
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
void ADC1_conf(void);
评分
查看全部评分
评分
查看全部评分
见5楼,
在ADC.H中声明该函数
void ADC1_conf(void);
在.c中定义函数,在.h中声明函数。
在要调用ADC1_conf()函数的.c文件中,把ADC.H包含进来,#include ADC.H
套路就是这么个套路,c语言功底,好好学习一下
评分
查看全部评分
//#include "stm32l476g_discovery.h"
#include "adc.h"
#include "stm32l4xx_hal_adc.h"
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
ADC_ChannelConfTypeDef sConfig;
void ADC1_conf(void)
{
HAL_Init(); // HAL Init
/**Configure the global features of the ADC (Clock, Resolution, Data,Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE; //upward
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START; //software trigger
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; //rising edge
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //< Data right alignment
hadc1.Init.NbrOfConversion = 2; /* This parameter must be a number between Min_Data = 1 and Max_Data = 16.
Note: This parameter must be modified when no conversion is on going on regular group (ADC disabled,
or ADC enabled without continuous mode or external trigger that could launch a conversion). */
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5; // Sampling time 12.5 ADC clock cycles at ADC resolution 12 bits.
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
这一部分是adc.h
#define MX_ADC1_Init
/* Definition for USARTx Pins */
#include "stm32l4xx.h"
#include "stm32l476g_discovery.h"
void ADC1_conf(void);
//void HAL_ADC_Init(ADC_HandleTypeDef* hadc);
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc);
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc);
#define USARTx_TX_PIN GPIO_PIN_6
#define USARTx_TX_GPIO_PORT GPIOB
#define USARTx_TX_AF GPIO_AF7_USART1
#define USARTx_RX_PIN GPIO_PIN_7
#define USARTx_RX_GPIO_PORT GPIOB
#define USARTx_RX_AF GPIO_AF7_USART1
评分
查看全部评分