找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8374|回复: 2
打印 上一主题 下一主题
收起左侧

stm32 ADC单通道连续采样并用串口发送电压值程序

[复制链接]
跳转到指定楼层
楼主
ID:86860 发表于 2015-7-26 02:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"

void Delay(unsigned int x);//延时
void GPIO_Configuration(void);//IO口配置
void UART_Init(void); //串口初始化
void UART2_PutChar(u8 ch);  //串口发送数据
void ADC_Configuration(void);  //ADC配置
void Read_ADC_Result(void);  //ADC读取结果



int main(void)
{
   SystemInit();
   unsigned int t;
   GPIO_Configuration();
   UART_Init();
   ADC_Configuration();
   while(1)
   {Read_ADC_Result();
    for(t=0;t<100;t++)
    {Delay(65530);}
   }
}


void GPIO_Configuration(void)
{
   GPIO_InitTypeDefGPIO_InitStructure;
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  //GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,ENABLE);
   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;
   GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
   GPIO_Init(GPIOA,&GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3;
   GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
   GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
   GPIO_Init(GPIOA,&GPIO_InitStructure);
}

void UART_Init(void)
{
   
   
    USART_InitTypeDefUSART_InitStructure;
   USART_InitStructure.USART_BaudRate = 9600;
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
   USART_InitStructure.USART_StopBits = USART_StopBits_1;
   USART_InitStructure.USART_Parity = USART_Parity_No;
   USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
   USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx;

    USART_Init(USART2,&USART_InitStructure);
   // USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);
   // USART_ITConfig(USART2,USART_IT_TXE, ENABLE);
    USART_Cmd(USART2,ENABLE);
}

void UART2_PutChar(u8 ch)
{
  USART_SendData(USART2,ch);
  while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)== RESET);
}


void ADC_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  //定义结构体
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1,ENABLE);//IO口使能设置
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;  //ADC输入管脚PC0
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;//模拟输出
  GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  
  ADC_InitTypeDef ADC_InitStructure;
  ADC_DeInit(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);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);//使用ADC1的通道10,采样顺序为1,采样周期为239.5周期
  ADC_Cmd(ADC1,ENABLE);//使能ADC1
  ADC_ResetCalibration(ADC1);//重置校准ADC1寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//获取ADC重置校准寄存器的状态
ADC_StartCalibration(ADC1);//开始ADC1的校准状态
while(ADC_GetCalibrationStatus(ADC1));//等待校准完成
  ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使能ADC1的软件转换启动功能
  
}


void Read_ADC_Result()
{
    unsigned chari,a,b,c,d;
    unsigned short result;
   
    for(i=0;i<8;i++)
    {
      ADC_SoftwareStartConvCmd(ADC1,ENABLE);//启动转换
      while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//等待转换结束
      result += ADC_GetConversionValue(ADC1);
    }
   
   
    result = result >>3;
    result = (unsignedint)(((unsigned long)result)*3300>>12);//转换为数字电压值
    a = result/1000;
    b =(result-a*1000)/100;
    c =(result-a*1000-b*100)/10;
    d =(result-a*1000-b*100-c*10);
   UART2_PutChar(0x56);//'v'
   UART2_PutChar(0X3D);//"="
    UART2_PutChar(a+48);
   UART2_PutChar(0x2e);//"."
    UART2_PutChar(b+48);
    UART2_PutChar(c+48);
    UART2_PutChar(d+48);
   UART2_PutChar(0x20);//"_"空格
}




void Delay(unsigned int x)
{
unsigned int t;
t=x;
while(t--);
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:102803 发表于 2016-2-22 18:35 | 只看该作者
很好,谢谢楼主!
回复

使用道具 举报

板凳
ID:186936 发表于 2017-10-28 23:51 | 只看该作者
你好,源工程可以借鉴一下吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表