今天需要拷贝一份赛点资源包里面提供的液晶驱动例程,并修改名称为ADC练习。
?
下面简单介绍一下ADC:
量程:指ADC所能输入模拟信号的类型和电压范围,即参考电压。信号类型包括单极性和双极性,蓝桥杯嵌入式G431开发板的输入电压范围是0~3.3V。
转换位数:量化过程中的量化位数n。蓝桥杯嵌入式G431开发板我习惯使用的位数是12位。
分辨率:ADC能够分辨的模拟信号最小变化量。
公式:分辨率 = 量程/
则蓝桥杯嵌入式G431开发板的分辨率就是3.3/4096 = 0.0008056
接着来看原理图部分:
?蓝桥杯嵌入式G431开发板上有两个比赛会考的ADC,分别接在PB15和PB12口,通过调节可调电阻让开发板读取到不同的电压并在屏幕上显示。
下面直接用STM32CubeMX打开拷贝过来的HAL_06_LCD.ioc文件进入到配置界面。
?点击PB12就可以直接配置为ADC1的11通道,同样点击PB15可以直接选择配置为ADC2的15通道。
点击Analog找到ADC1的通道11,按照图中进行配置,⑤为12位数据,⑥为右对齐,⑦为采样时间,时间越长数据越精确,时间越短数据质量越低。
?同样对ADC2的15通道进行相同的配置,完成后设置一下工程即可重新生成代码。下面附上编写完成的main代码,其中:
HAL_ADC_Start(&hadc1);为ADC启动函数
HAL_ADC_PollForConversion(&hadc1, 2);设置通道和采集超时时间 adc = HAL_ADC_GetValue(&hadc1);获取采样值 HAL_ADC_Stop(&hadc1);采样完成后停止,降低功耗
使用到了sprintf函数,如果不太了解的话可以去学习一下。
#include "main.h"
#include "adc.h"
#include "gpio.h"
#include "stdio.h"
void SystemClock_Config(void);
uint16_t R38_GET(void)
{
uint16_t adc=0;
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 2);
adc = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
return adc;
}
uint16_t R37_GET(void)
{
uint16_t adc=0;
HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 2);
adc = HAL_ADC_GetValue(&hadc2);
HAL_ADC_Stop(&hadc2);
return adc;
}
int main(void)
{
char str[64];
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line3,(unsigned char *)" LCD TEST ");
LCD_DisplayStringLine(Line1,(unsigned char *)" ");
LCD_DisplayStringLine(Line2,(unsigned char *)" ");
LCD_DisplayStringLine(Line0,(unsigned char *)" ");
LCD_DisplayStringLine(Line4,(unsigned char *)" ");
LCD_DisplayStringLine(Line5,(unsigned char *)" ");
LCD_DisplayStringLine(Line6,(unsigned char *)" ");
LCD_DisplayStringLine(Line7,(unsigned char *)" ");
LCD_DisplayStringLine(Line8,(unsigned char *)" ");
LCD_DisplayStringLine(Line9,(unsigned char *)" ");
while (1)
{
sprintf((char *)str, " R37: %.2f", R37_GET()*3.3/4096);
LCD_DisplayStringLine(Line6,(unsigned char *)str);
HAL_Delay(100);
sprintf((char *)str, " R38: %.2f", R38_GET()*3.3/4096);
LCD_DisplayStringLine(Line7,(unsigned char *)str);
HAL_Delay(100);
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
?
|