直接上代码吧
void USART_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //开启串口时钟 //设置usart2 tx引脚工作模式PA2 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); //设置usart2 rx引脚工作模式PA3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //usart2 工作模式配置 USART_InitStructure.USART_BaudRate = 19200; //波特率 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_Cmd(USART2, ENABLE); //使能 } //发送函数 void USART_SendString(int8_t *str) { uint8_t index = 0; do { USART_SendData(USART2,str[index]); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET); index++; } while(str[index] != 0); //¼ì²é×Ö·û´®½áÊø±êÖ¾ }
参考博文点击此处
在配置引脚输入模式时配置为模拟输入模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
引脚对应关系
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel, u8 Rank, u8 ADC_SampleTime)
该函数的参数ADC_Channel,Rank是代表什么?
这个在你多通道采集和DMA应用时会有很大的作用,比如你需要多通道采集,你设置每个通道的采集顺序其实就是用这个变量来做的,假设你定义channle1 的rank=1,channle2 的rank=2,那么对应你在DMA缓存空间的变量数组AdcDMA[0] 就i是channle1的转换结果,AdcDMA[1]就是通道2的转换结果。
code:
void ADC_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //PB0-ADC channel 8 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); // ADC1 工作模式配置 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //µ¥´Îת»» 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_8, 1, ADC_SampleTime_13Cycles5); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); } //读函数 float Read_ADC(void) { float ADC_VALUE; ADC_SoftwareStartConvCmd(ADC1,ENABLE); Delay_Ms(5); ADC_VALUE = ADC_GetConversionValue(ADC1)*3.30/0xfff; return ADC_VALUE; } //main函数中的片段 while(1){ //200ms采样一次 if(ADC_Flag){ ADC_Flag = 0; adc_temp = Read_ADC(); sprintf(string,"%s%.3f","ADC Value:",adc_temp); LCD_DisplayStringLine(Line7,string); }
很草率,还会再看一遍的><