结合板载的电位器和STM32的AD转换功能,测量电位器两端的电压变化,并通过LED的数量提示AD转化的电压值的大小,电压越大点亮的LED越多。推荐用adcx,0~4095,分成8个区间,每个区间5120~511,LED0512~1023,LED0、LED11024~1535,LED0、LED1、LED2依次类推不推荐用temp,小数,最大值3.3,写出所有代码
时间: 2023-09-29 09:10:55 浏览: 41
以下是基于STM32的AD转换功能和板载电位器实现LED数量提示的代码:
```c
#include "stm32f10x.h"
void ADC1_Init(void);
void GPIO_Init(void);
void LED_Display(uint16_t value);
int main(void)
{
ADC1_Init(); // 初始化ADC1
GPIO_Init(); // 初始化GPIO
uint16_t ad_value;
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // 启动ADC转换
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); // 等待转换结束
ad_value = ADC_GetConversionValue(ADC1); // 读取转换结果
LED_Display(ad_value); // 根据AD转换值点亮LED
}
}
void ADC1_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_71Cycles5);
}
void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void LED_Display(uint16_t value)
{
uint8_t led_num = value / 512;
switch (led_num)
{
case 0:
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
break;
case 1:
GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1);
GPIO_ResetBits(GPIOB, GPIO_Pin_2 | GPIO_Pin_3);
break;
case 2:
GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
break;
case 3:
GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
break;
default:
break;
}
}
```
此代码中,我们使用了ADC1,将其配置为单次转换模式,采样通道为ADC_Channel_0,采样时间为71.5个时钟周期。通过循环读取ADC转换值,并根据转换值的大小点亮一定数量的LED。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)