编写stm32f103c8t6利用AD采集电压信号并通过液晶屏输出
时间: 2023-07-15 07:11:18 浏览: 183
以下是基于STM32F103C8T6的AD采集电压信号并通过液晶屏输出的代码示例:
1. 首先,需要使用CubeMX配置引脚和ADC模块。将ADC通道1连接到PA1引脚。
2. 在main函数中初始化ADC,并设置为连续转换模式。
```c
/* ADC Initialization */
ADC_HandleTypeDef hadc1;
/* Function prototypes */
static void MX_ADC1_Init(void);
/* Main function */
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_LCD_Init();
/* Infinite loop */
while (1)
{
/* Start ADC conversion */
if (HAL_ADC_Start(&hadc1) == HAL_OK)
{
/* Wait for conversion to complete */
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
{
/* Read ADC value */
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
/* Convert ADC value to voltage */
float voltage = (3.3f * adc_value) / 4096.0f;
/* Print voltage to LCD */
char lcd_buffer[16];
snprintf(lcd_buffer, sizeof(lcd_buffer), "Voltage: %.2fV", voltage);
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)lcd_buffer, CENTER_MODE);
}
}
/* Delay for 1 second */
HAL_Delay(1000);
}
}
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/* Common config */
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* Configure ADC channel */
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
```
3. 在BSP_LCD.c文件中添加以下函数以支持字符串输出:
```c
/**
* @brief Displays a string on the LCD.
* @param Xpos: specifies the X position, can be one of the following values:
* - LINE(0), LINE(1), LINE(2), LINE(3), LINE(4), LINE(5), LINE(6) or LINE(7)
* @param Ypos: specifies the Y position, can be one of the following values:
* - CENTER_MODE or RIGHT_MODE
* @param pText: pointer to string to display on LCD
* @retval None
*/
void BSP_LCD_DisplayStringAt(uint16_t Xpos, uint16_t Ypos, uint8_t *pText, Text_AlignModeTypdef Mode)
{
uint16_t ref_column = 1, i = 0;
uint32_t size = 0;
/* Get the text size */
while (*(pText + i))
{
size += LCD_GetCharWidth(*(pText + i));
i++;
}
/* Set the reference column position */
if (Mode == CENTER_MODE)
{
ref_column = MAX_LINE_WIDTH / 2 - size / 2;
}
else if (Mode == RIGHT_MODE)
{
ref_column = MAX_LINE_WIDTH - size;
}
/* Send the string character by character on LCD */
while ((*pText != 0) & (((Xpos + size) <= MAX_LINE_WIDTH) | ((Ypos + 16) <= MAX_LINE_HEIGHT)))
{
/* Display one character on LCD */
BSP_LCD_DisplayChar(Xpos + ref_column, Ypos, *pText);
/* Increment pointers */
pText++;
size -= LCD_GetCharWidth(*(pText - 1));
ref_column += LCD_GetCharWidth(*(pText - 1));
}
}
```
这样就可以通过液晶屏输出采集到的电压信号了。注意,由于ADC采集的是模拟信号,需要进行数字转换才能得到实际电压值。在上述代码中,使用了3.3V作为参考电压,采样精度为12位。根据公式Vref = 3.3V,N = 12,ADC_value为采集到的ADC值,可以计算出实际电压值V = Vref * ADC_value / 2^N。
阅读全文