if(HAL_ADC_GetState(&hadc)==HAL_ADC_STATE_EOC)
{
Delay(0x00FFFF);
//uwConvertedValue = HAL_ADC_GetValue(&hadc);
uwConvertedValue = hadc.Instance->DR;
/* Convert the result from 16 bit value to the voltage dimsension */
/* Vref = 3.3 V */
uwInputVoltage = (uwConvertedValue * 3300) / 0xFFF0;
}
while (1)
{
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
/* USER CODE END 4 */
static void Error_Handler(void)
{
while(1)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
volitale?
AD模块就是怎么也不好用,程序可以编译通过,但是就是采不到模拟端口的值,下面是我的main函数(单步调试时,ADC_ISR始终为0,ADC_DR始终为0)
/**main.c**/
#include "LMP91000.h"
#include<stdio.h>
#include"stm32l0xx_hal_rcc_ex.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC_Init(void);
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount); //延时函数
static void Error_Handler(void);
/* ADC handle declaration */
ADC_HandleTypeDef hadc;
/* ADC channel configuration structure declaration */
ADC_ChannelConfTypeDef sConfig;
/* Converted value declaration */
uint32_t uwConvertedValue;
/* Input voltage declaration */
uint32_t uwInputVoltage;
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
I2C_GPIO1_Config();
I2C_GPIO2_Config();
LMP91000_PortInit(1,0);
Delay(0X00FFFF);
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = DISABLE;
// hadc.Init.OversamplingMode = ENABLE;
// hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128;
// hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3;
// hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
hadc.Init.ScanDirection = ADC_SCAN_DIRECTION_UPWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = EOC_SINGLE_CONV;
hadc.Init.Overrun = OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = ENABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoOff = DISABLE;
HAL_ADC_Init(&hadc);
/* Initialize ADC peripheral according to the passed parameters */
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/* ### -2 - Channel configuration ######################################## */
/* Select Channel 0 to be converted */
sConfig.Channel = ADC_CHANNEL_0;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* ### - 3 - Start calibration ############################################ */
if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
}
/* ### - 4 - Start conversion ############################################# */
if (HAL_ADC_Start(&hadc) != HAL_OK)
{
Error_Handler();
}
HAL_ADC_PollForConversion(&hadc, 10); //等待转换结束
if(HAL_ADC_GetState(&hadc)==HAL_ADC_STATE_EOC)
{
Delay(0x00FFFF);
//uwConvertedValue = HAL_ADC_GetValue(&hadc);
uwConvertedValue = hadc.Instance->DR;
/* Convert the result from 16 bit value to the voltage dimsension */
/* Vref = 3.3 V */
uwInputVoltage = (uwConvertedValue * 3300) / 0xFFF0;
}
while (1)
{
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA9(KEY1) PA3(MENB_O2)*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA0(AN_O2_1) PA6(L1_VOLT) AD模拟输入*/
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/********************************************************************************************
延时函数void Delay(__IO uint32_t nCount)
*******************************************************************************************/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
/* USER CODE END 4 */
static void Error_Handler(void)
{
while(1)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/