主函数文件
#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);
}
}
adc.c代码
#include "adc.h"
#define ADC1_DR_Address ((u32)0x4001244C)
__IO u16 ADC_ConvertedValue;
/*
* 函数名:ADC1_GPIO_Config
* 描述 :使能ADC1和DMA1的时钟,初始化PB.01
* 输入 : 无
* 输出 :无
* 调用 :内部调用
*/
static void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB.01 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure); // PB1,输入时不用设置速率
}
/* 函数名:ADC1_Mode_Config
* 描述 :配置ADC1的工作模式为MDA模式
* 输入 : 无
* 输出 :无
* 调用 :内部调用
*/
static void ADC1_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* ADC1 configuration */
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);
/* ADC1 regular channel11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
// ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
// ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
/*
* 函数名:ADC1_Init
* 描述 :无
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void ADC1_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}