#include "stm32f10x.h"void Delay(unsigned int x);//延時(shí)
void GPIO_Configuration(void);//IO口配置
void UART_Init(void); //串口初始化
void UART2_PutChar(u8 ch); //串口發(fā)送數(shù)據(jù)
void ADC_Configuration(void); //ADC配置
void Read_ADC_Result(void); //ADC讀取結(jié)果
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; //定義結(jié)構(gòu)體
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_ADC1,ENABLE);//IO口使能設(shè)置
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;//工作在獨(dú)立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//工作在單通道模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//單次模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//轉(zhuǎn)換由軟件而不是外部觸發(fā)啟動(dòng)
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//數(shù)據(jù)右對(duì)齊
ADC_InitStructure.ADC_NbrOfChannel = 1;//通道數(shù)目
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);//使用ADC1的通道10,采樣順序?yàn)?,采樣周期為239.5周期
ADC_Cmd(ADC1,ENABLE);//使能ADC1
ADC_ResetCalibration(ADC1);//重置校準(zhǔn)ADC1寄存器
while(ADC_GetResetCalibrationStatus(ADC1));//獲取ADC重置校準(zhǔn)寄存器的狀態(tài)
ADC_StartCalibration(ADC1);//開(kāi)始ADC1的校準(zhǔn)狀態(tài)
while(ADC_GetCalibrationStatus(ADC1));//等待校準(zhǔn)完成
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能ADC1的軟件轉(zhuǎn)換啟動(dòng)功能
}
void Read_ADC_Result()
{
unsigned char i,a,b,c,d;
unsigned short result;
for(i=0;i<8;i++)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//啟動(dòng)轉(zhuǎn)換
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//等待轉(zhuǎn)換結(jié)束
result += ADC_GetConversionValue(ADC1);
}
result = result >> 3;
result = (unsigned int)(((unsigned long)result)*3300>>12);//轉(zhuǎn)換為數(shù)字電壓值
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--);
}
評(píng)論