(二)单片机类: (1)数字电压表设计 设计要求: 1、设计一简易数字电压表, 要求可以测量直流 0~50V 和交流 0~36V 电压 值, 最小测量电压误差 50mV. 2、电压值由 3 位 LED 数码管显示, 两位整数, 一位小数;的程序
时间: 2024-03-22 15:40:30 浏览: 17
好的,以下是一个基于STM32单片机的数字电压表的程序示例,可以测量直流0~50V和交流0~36V电压值,最小测量电压误差50mV,并将电压值显示在3位LED数码管上:
```
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((u32)0x4001244C)
u16 ADC_ConvertedValue[2];
float voltage;
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
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);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
float Get_Voltage(void)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ADC_ConvertedValue[0] = ADC_GetConversionValue(ADC1);
voltage = (float)ADC_ConvertedValue[0] * 3.3 / 4096.0 * 10; // 10:电压分压比
return voltage;
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
while(1)
{
voltage = Get_Voltage();
printf("%.2fV\r\n", voltage);
// 将电压值分解成整数和小数部分
int integer_part = (int)voltage;
int decimal_part = (int)((voltage - integer_part) * 10);
// 将整数和小数部分转换成BCD码
int integer_bcd = ((integer_part / 10) << 4) | (integer_part % 10);
int decimal_bcd = decimal_part;
// 将BCD码分别输出到LED数码管上
GPIO_Write(GPIOA, integer_bcd << 8 | decimal_bcd);
}
}
```
在这个程序中,我们使用了STM32的ADC模块来测量电压值,并将测量得到的电压值转换为实际的电压值。然后,我们将电压值分解成整数和小数部分,并将它们转换成BCD码并输出到LED数码管上。
希望这个程序对你有所帮助。如果你有任何问题,请随时提出。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)