微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ADC单通道连续采样并用串口发送电压值

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

时间:11-27 来源:互联网 点击:
#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_InitTypeDef GPIO_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_InitTypeDef USART_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 char i,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 = (unsigned int)(((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);//"_"空格

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top