#include "adc.h"
void adc_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitTypeDef GPIO_Instruction;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_Instruction.GPIO_Pin=GPIO_Pin_9;
GPIO_Instruction.GPIO_Mode=GPIO_Mode_AF;
GPIO_Instruction.GPIO_OType=GPIO_OType_PP;
GPIO_Instruction.GPIO_Speed=GPIO_Fast_Speed;
GPIO_Instruction.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_Instruction);
GPIO_Instruction.GPIO_Pin=GPIO_Pin_10;
GPIO_Instruction.GPIO_Mode=GPIO_Mode_AF;
GPIO_Instruction.GPIO_OType=GPIO_OType_PP;
GPIO_Instruction.GPIO_Speed=GPIO_Medium_Speed;
GPIO_Instruction.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_Instruction);
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
// 时钟为fpclk x分频
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
// 禁止DMA直接访问模式
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
// 采样时间间隔
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitTypeDef ADC_InitStruction;
ADC_InitStruction.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruction.ADC_ScanConvMode = DISABLE;
ADC_InitStruction.ADC_ContinuousConvMode = DISABLE;
// ADC_InitStruction.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruction.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStruction.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruction.ADC_NbrOfConversion =1;
ADC_Init(ADC1, &ADC_InitStruction);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_3Cycles);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void usart(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=115200;
USART_InitStructure.USART_HardwareFlowControl=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//
// NVIC_Configuration();
USART_Cmd(USART1, ENABLE);
// USART_SendData(USART1,14);
}
///重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
int fputc(int ch, FILE f)
{
/ 发送一个字节数据到串口DEBUG_USART */
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
///重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
int fgetc(FILE f)
{
/ 等待串口输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
这是adc的文件,先把usart也放进去了,发现如果把void usart整体函数写到adc前面串口只会发一个。
int main(void)
{
Init_GPIO();
adc_init();
usart();
while(1)
{
ADC_SoftwareStartConv(ADC1);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
{
}
ADC_Value[0]=ADC_GetConversionValue(ADC1);
AD=(float)ADC_Value[0]/4096*(float)3.3;
printf("\n \r %f",AD);
}
}