主函数文件
#include "stm32f10x.h"
#include "adc.h"
const unsigned char table[10] = {'0','1','2','3','4','5','6','7','8','9'};
// 软件延时
void Delay(__IO u32 nCount)
{
for(; nCount != 0; nCount--);
}
void USART_GPIO_CONFIG()
{
GPIO_InitTypeDef GPIO_InitStructure;
//SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_MODE_CONFIG()
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
void disp_adc(u16 res)
{
u8 wan, qian, bai, shi, ge;
wan = res / 10000;
qian = res %10000 /1000;
bai = res %1000 / 100;
shi = res %100 / 10;
ge = res %10;
USART_SendData(USART1 , table[wan]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , table[qian]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , table[bai]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , table[shi]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , table[ge]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , '\t');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}
void disp_adc2(u16 d)
{
float temp = d*3.3/4096;
unsigned char s = (unsigned char)(temp * 10);
USART_SendData(USART1 , table[s/10]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , '.');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , table[s%10]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1 , '\t');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}
int main(void)
{
u16 res;
/* config the sysclock to 72M */
SystemInit();
/* USART1 config */
USART_GPIO_CONFIG();
USART_MODE_CONFIG();
/* enable adc1 and config adc1 to dma mode */
ADC1_Init();
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (1)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
res = ADC_GetConversionValue(ADC1);
disp_adc2(res);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
}
}
|