在主函数只需对读取到的数值进行相应的转换输出即可。
#include"stm32f4xx.h"
#include"sys.h"
#include
static GPIO_InitTypeDef GPIO_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static ADC_InitTypeDef ADC_InitStructure;
static ADC_CommonInitTypeDef ADC_CommonInitStructure;
void delay_ms(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168000)-1; // 配置计数值(168000)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
}
SysTick->CTRL = 0; // Disable SysTick
}
void delay_us(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168)-1; // 配置计数值(168000)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
}
SysTick->CTRL = 0; // Disable SysTick
}
void usart1_init(uint32_t baud)
{
//端口A硬件是能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//串口硬件是能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//PF9应交连接到串口
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//配置串口
USART_InitStructure.USART_BaudRate = baud;//波特率
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(USART1, &USART_InitStructure);
//配置中断触发方式
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置终端优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能串口工作
USART_Cmd(USART1, ENABLE);
}
void adc_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//使能ADC
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//ADC_CommonInitTypeDef ADC_CommonInitStructure;
//配置端口模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE);//使能内部温度传感器
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立工作模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;//分屏
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;//不需要映射数据
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//精度
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//单通道,否则为多通道
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//不断的转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//不需要内部脉冲出发
// ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐存储
ADC_InitStructure.ADC_NbrOfConversion = 1;//几个转换通道
ADC_Init(ADC1,&ADC_InitStructure);
//通道链接PA5
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_56Cycles);
ADC_Cmd(ADC1,ENABLE);
}
struct __FILE{int handle;};
FILE __stdout;
FILE __stdin;
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
u16 Get_Adc(u8 ch)
{
//设置指定 ADC 的规则组通道,一个序列,采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles );
ADC_SoftwareStartConv(ADC1); //使能指定的 ADC1 的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次 ADC1 规则组的转换结果
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0; u8 t;
for(t=0;t
{
temp_val+=Get_Adc(ch); delay_ms(5);
}
return temp_val/times;
}
short Get_Temprate(void)
{
u32 adcx; short result;
double temperate;
adcx=Get_Adc_Average(ADC_Channel_16,20); //读取通道 16,20 次取平均
temperate=(float)adcx*(3.3/4096); //电压值
temperate=(temperate-0.76)/0.0025+25; //转换为温度值
result=temperate*=100; //扩大 100 倍.
return result;
}
int main(void)
{
uint16_t adc_val,adc_v,temp;
//波特率为115200
usart1_init(115200);
printf("ADC testrn");
adc_Init();
ADC_SoftwareStartConv(ADC1);//启动ADC1转换
while(1)
{
//等待转换完毕
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
adc_val=ADC_GetConversionValue(ADC1);
adc_v=adc_val*3300/4095;
printf("电压为:%dmvrn",adc_v);
printf("剩余点亮%d%% rn",adc_v/33);
delay_ms(1000);
temp=Get_Temprate();
printf("temp=%d.%drn",temp/100,temp%100);
}
}
void USART1_IRQHandler(void)
{
uint8_t d=0;
//判断标志位i
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
{
d=USART_ReceiveData(USART1);
//
if(d==0x01)PFout(10)=0;
if(d==0xf1)PFout(10)=1;
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
//清空标志位
}
(10)=0; if(d==0xf1)PFout(10)=1; USART_ClearITPendingBit(USART1,USART_IT_RXNE); } //清空标志位}
在主函数只需对读取到的数值进行相应的转换输出即可。
#include"stm32f4xx.h"
#include"sys.h"
#include
static GPIO_InitTypeDef GPIO_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static ADC_InitTypeDef ADC_InitStructure;
static ADC_CommonInitTypeDef ADC_CommonInitStructure;
void delay_ms(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168000)-1; // 配置计数值(168000)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
}
SysTick->CTRL = 0; // Disable SysTick
}
void delay_us(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168)-1; // 配置计数值(168000)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
}
SysTick->CTRL = 0; // Disable SysTick
}
void usart1_init(uint32_t baud)
{
//端口A硬件是能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//串口硬件是能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF ;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//PF9应交连接到串口
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//配置串口
USART_InitStructure.USART_BaudRate = baud;//波特率
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(USART1, &USART_InitStructure);
//配置中断触发方式
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置终端优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能串口工作
USART_Cmd(USART1, ENABLE);
}
void adc_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//使能ADC
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//ADC_CommonInitTypeDef ADC_CommonInitStructure;
//配置端口模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE);//使能内部温度传感器
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立工作模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;//分屏
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;//不需要映射数据
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//精度
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//单通道,否则为多通道
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//不断的转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//不需要内部脉冲出发
// ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐存储
ADC_InitStructure.ADC_NbrOfConversion = 1;//几个转换通道
ADC_Init(ADC1,&ADC_InitStructure);
//通道链接PA5
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_56Cycles);
ADC_Cmd(ADC1,ENABLE);
}
struct __FILE{int handle;};
FILE __stdout;
FILE __stdin;
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
u16 Get_Adc(u8 ch)
{
//设置指定 ADC 的规则组通道,一个序列,采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles );
ADC_SoftwareStartConv(ADC1); //使能指定的 ADC1 的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次 ADC1 规则组的转换结果
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0; u8 t;
for(t=0;t
{
temp_val+=Get_Adc(ch); delay_ms(5);
}
return temp_val/times;
}
short Get_Temprate(void)
{
u32 adcx; short result;
double temperate;
adcx=Get_Adc_Average(ADC_Channel_16,20); //读取通道 16,20 次取平均
temperate=(float)adcx*(3.3/4096); //电压值
temperate=(temperate-0.76)/0.0025+25; //转换为温度值
result=temperate*=100; //扩大 100 倍.
return result;
}
int main(void)
{
uint16_t adc_val,adc_v,temp;
//波特率为115200
usart1_init(115200);
printf("ADC testrn");
adc_Init();
ADC_SoftwareStartConv(ADC1);//启动ADC1转换
while(1)
{
//等待转换完毕
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
adc_val=ADC_GetConversionValue(ADC1);
adc_v=adc_val*3300/4095;
printf("电压为:%dmvrn",adc_v);
printf("剩余点亮%d%% rn",adc_v/33);
delay_ms(1000);
temp=Get_Temprate();
printf("temp=%d.%drn",temp/100,temp%100);
}
}
void USART1_IRQHandler(void)
{
uint8_t d=0;
//判断标志位i
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
{
d=USART_ReceiveData(USART1);
//
if(d==0x01)PFout(10)=0;
if(d==0xf1)PFout(10)=1;
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
//清空标志位
}
(10)=0; if(d==0xf1)PFout(10)=1; USART_ClearITPendingBit(USART1,USART_IT_RXNE); } //清空标志位}
举报