找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1756|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32F103C8T6 AD單通道程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:685250 發(fā)表于 2023-7-31 09:51 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式


單片機源程序如下:
  1. #include "stm32f10x.h"                  // Device header
  2. #include "Delay.h"
  3. #include "OLED.h"
  4. #include "AD.h"

  5. uint16_t ADValue;
  6. float Voltage;

  7. int main(void)
  8. {
  9.         OLED_Init();
  10.         AD_Init();
  11.         
  12.         OLED_ShowString(1, 1, "ADValue:");
  13.         OLED_ShowString(2, 1, "Volatge:0.00V");
  14.         
  15.         while (1)
  16.         {
  17.                 ADValue = AD_GetValue();
  18.                 Voltage = (float)ADValue / 4095 * 3.3;
  19.                
  20.                 OLED_ShowNum(1, 9, ADValue, 4);
  21.                 OLED_ShowNum(2, 9, Voltage, 1);
  22.                 OLED_ShowNum(2, 11, (uint16_t)(Voltage * 100) % 100, 2);
  23.                
  24.                 Delay_ms(100);
  25.         }
  26. }
復(fù)制代碼
  1. #include "stm32f10x.h"                  // Device header

  2. void AD_Init(void)
  3. {
  4.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  5.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  6.         
  7.         RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  8.         
  9.         GPIO_InitTypeDef GPIO_InitStructure;
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  11.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  12.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  14.         
  15.         ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
  16.         
  17.         ADC_InitTypeDef ADC_InitStructure;
  18.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  19.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  20.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  21.         ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  22.         ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  23.         ADC_InitStructure.ADC_NbrOfChannel = 1;
  24.         ADC_Init(ADC1, &ADC_InitStructure);
  25.         
  26.         ADC_Cmd(ADC1, ENABLE);
  27.         
  28.         ADC_ResetCalibration(ADC1);
  29.         while (ADC_GetResetCalibrationStatus(ADC1) == SET);
  30.         ADC_StartCalibration(ADC1);
  31.         while (ADC_GetCalibrationStatus(ADC1) == SET);
  32. }

  33. uint16_t AD_GetValue(void)
  34. {
  35.         ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  36.         while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  37.         return ADC_GetConversionValue(ADC1);
  38. }
復(fù)制代碼

Keil代碼下載: 程序.7z (176.7 KB, 下載次數(shù): 14)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表