#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);//”_”空格
}
void Delay(unsigned int x)
{
unsigned int t;
t=x;
while(t–);
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/257093.html