结合板载的电位器和STM32的AD转换功能,测量电位器两端的电压变化,并通过LED的数量提示AD转化的电压值的大小,电压越大点亮的LED越多。 推荐用adcx,04095,分成8个区间,每个区间512 0511,LED0 5121023,LED0、LED1 10241535,LED0、LED1、LED2 依次类推 不推荐用temp,小数,最大值3.3 帮我写一个实现这个功能的工程代码。(开发板为ministm32f103rc)其包main.c文件和.h文件和.c文件
时间: 2023-09-22 15:07:34 浏览: 138
以下是基于STM32的AD转换功能实现测量电位器两端的电压变化,并通过LED的数量提示AD转化的电压值的大小的代码。
main.c 文件:
```c
#include "stm32f10x.h"
#include "led.h"
#include "adc.h"
int main(void)
{
uint16_t adValue;
LED_Init();
ADC_Init();
while(1)
{
adValue = ADC_GetConversionValue(ADC1);
LED_Display(adValue);
}
}
```
adc.h 文件:
```c
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
void ADC_Init(void);
uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
#endif
```
adc.c 文件:
```c
#include "adc.h"
void ADC_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 使能 ADC1 和 GPIOC 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
// 配置 ADC1 的输入引脚 PC5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// ADC1 初始化
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// 配置 ADC1 的通道 15,采样时间为 55.5 周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_55Cycles5);
// 使能 ADC1
ADC_Cmd(ADC1, ENABLE);
// 开启 ADC1 的 DMA 传输
ADC_DMACmd(ADC1, ENABLE);
// 开启 ADC1 的软件触发转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
{
// 等待转换完成
while(ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET);
// 返回 ADC1 最近一次转换的结果
return ADC_GetConversionValue(ADCx);
}
```
led.h 文件:
```c
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
void LED_Init(void);
void LED_Display(uint16_t adValue);
#endif
```
led.c 文件:
```c
#include "led.h"
#define LED0_PIN GPIO_Pin_0
#define LED1_PIN GPIO_Pin_1
#define LED2_PIN GPIO_Pin_2
#define LED3_PIN GPIO_Pin_3
#define LED4_PIN GPIO_Pin_4
#define LED5_PIN GPIO_Pin_5
#define LED6_PIN GPIO_Pin_6
#define LED7_PIN GPIO_Pin_7
#define LED_PORT GPIOB
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能 GPIOB 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 配置 LED 引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
// 初始化 LED 灯,全部关闭
GPIO_SetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
}
void LED_Display(uint16_t adValue)
{
uint8_t ledCount = 0;
// 根据 AD 值计算需要点亮的 LED 数量
if(adValue >= 0 && adValue < 512)
{
ledCount = 0;
}
else if(adValue >= 512 && adValue < 1024)
{
ledCount = 1;
}
else if(adValue >= 1024 && adValue < 1536)
{
ledCount = 2;
}
else if(adValue >= 1536 && adValue < 2048)
{
ledCount = 3;
}
else if(adValue >= 2048 && adValue < 2560)
{
ledCount = 4;
}
else if(adValue >= 2560 && adValue < 3072)
{
ledCount = 5;
}
else if(adValue >= 3072 && adValue < 3584)
{
ledCount = 6;
}
else if(adValue >= 3584 && adValue < 4096)
{
ledCount = 7;
}
// 根据需要点亮的 LED 数量点亮相应的 LED
switch(ledCount)
{
case 0:
GPIO_SetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 1:
GPIO_ResetBits(LED_PORT, LED0_PIN);
GPIO_SetBits(LED_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 2:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN);
GPIO_SetBits(LED_PORT, LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 3:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN);
GPIO_SetBits(LED_PORT, LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 4:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN);
GPIO_SetBits(LED_PORT, LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 5:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
GPIO_SetBits(LED_PORT, LED5_PIN | LED6_PIN | LED7_PIN);
break;
case 6:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN);
GPIO_SetBits(LED_PORT, LED6_PIN | LED7_PIN);
break;
case 7:
GPIO_ResetBits(LED_PORT, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN);
GPIO_SetBits(LED_PORT, LED7_PIN);
break;
default:
break;
}
}
```
阅读全文