1.png (212.03 KB, 下載次數: 59)
下載附件
2023-7-31 09:59 上傳
單片機源程序如下:
- #include "stm32f10x.h" // Device header
- #include "Delay.h"
- #include "OLED.h"
- #include "AD.h"
- uint16_t ADValue;
- float Voltage;
- int main(void)
- {
- OLED_Init();
- AD_Init();
-
- OLED_ShowString(1, 1, "ADValue:");
- OLED_ShowString(2, 1, "Volatge:0.00V");
-
- while (1)
- {
- ADValue = AD_GetValue();
- Voltage = (float)ADValue / 4095 * 3.3;
-
- OLED_ShowNum(1, 9, ADValue, 4);
- OLED_ShowNum(2, 9, Voltage, 1);
- OLED_ShowNum(2, 11, (uint16_t)(Voltage * 100) % 100, 2);
-
- Delay_ms(100);
- }
- }
復制代碼- #include "stm32f10x.h" // Device header
- void AD_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
-
- ADC_InitTypeDef ADC_InitStructure;
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
-
- ADC_Cmd(ADC1, ENABLE);
-
- ADC_ResetCalibration(ADC1);
- while (ADC_GetResetCalibrationStatus(ADC1) == SET);
- ADC_StartCalibration(ADC1);
- while (ADC_GetCalibrationStatus(ADC1) == SET);
- }
- uint16_t AD_GetValue(void)
- {
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
- return ADC_GetConversionValue(ADC1);
- }
復制代碼
Keil代碼下載:
程序.7z
(176.7 KB, 下載次數: 14)
2023-8-1 15:01 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|