中级会员
 
- 积分
- 359
- 金钱
- 359
- 注册时间
- 2016-10-31
- 在线时间
- 50 小时
|
发表于 2016-11-1 11:27:36
|
显示全部楼层
/**************************adc.c文件**********************************/
#include "adc.h"
#include "delay.h"
////////////////////////
//初始化ADC
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
//先初始化ADC1通道5 IO口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA5 通道5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
//RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1复位
//RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //复位结束
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=2j1Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_10b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;//1个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
}
//获得ADC值
//ch: @ref ADC_channels
//通道值 0~16取值范围为:ADC_Channel_0~ADC_Channel_16
//返回值:转换结果
u16 Get_Adc(u8 ch)
{
//设置指定ADC的规则组通道,一个序列,采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles ); //ADC1,ADC通道,480个周期,提高采样时间可以提高精确度
ADC_SoftwareStartConv(ADC1); //使能指定的ADC1的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
}
//获取通道ch的转换值,取times次,然后平均
//ch:通道编号
//times:获取次数
//返回值:通道ch的times次转换结果平均值
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
/***********************adc.h文件**********************************/
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
//////////
void Adc_Init(void); //ADC通道初始化
u16 Get_Adc(u8 ch); //获得某个通道值
u16 Get_Adc_Average(u8 ch,u8 times);//得到某个通道给定次数采样的平均值
#endif
/*************************main.c文件**************************/
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "adc.h"
#include "timer.h"
//ADC转换实验
//
#define true 1
#define false 0
int BPM; //心率值 每分钟跳动多少次 // used to hold the pulse rate
int Signal; //采样的值,即adc值 // holds the incoming raw data
int IBI = 600; //两次心跳间隔时间 单位 毫秒 // holds the time between beats, must be seeded!
unsigned char Pulse = false; //脉冲 // true when pulse wave is high, false when it's low
unsigned char QS = false; //QS为真时候表示一次真正的心跳 // becomes true when Arduoino finds a beat.
int rate[10]; //该数组用于保存最后10次IBI的值 // array to hold last ten IBI values
unsigned long sampleCounter = 0; //脉搏跳动启动时间 // used to determine pulse timing
unsigned long lastBeatTime = 0; //结束时间,即IBI的时间 // used to find IBI
int P =512; //用于统计峰值 // used to find peak in pulse wave, seeded
int T = 512; //用于统计低谷值 // used to find trough in pulse wave, seeded
int thresh = 512; //峰值和低谷分界时间 // used to find instant moment of heart beat, seeded
int amp = 100; //震幅长度 // used to hold amplitude of pulse waveform, seeded
int Num; //时间
unsigned char firstBeat = true; // used to seed rate array so we startup with reasonable BPM
unsigned char secondBeat = false; // used to seed rate array so we startup with reasonable BPM
//用于确定心跳时间 IBI
int main(void)
{
u16 adcx;
// float temp;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //初始化LCD接口
Adc_Init(); //初始化ADC
POINT_COLOR=RED;
TIM3_Int_Init(2000-1,84-1); //2ms
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"ADC TEST");
LCD_ShowString(30,90,200,16,16,"UPLOOKING");
LCD_ShowString(30,110,200,16,16,"2015/5/6");
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(30,130,200,16,16,"ADC1_CH5_VAL:");
LCD_ShowString(30,150,200,16,16,"ADC1_CH5_VOL:0.000V"); //先在固定位置显示小数点
while(1)
{
//adcx=Get_Adc_Average(ADC_Channel_5,20);//获取通道5的转换值,20次取平均
//adcx = Get_Adc(ADC_Channel_5);
//LCD_ShowxNum(134,130,adcx,4,16,0); //显示ADCC采样后的原始值
//temp=(float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
//adcx=temp; //赋值整数部分给adcx变量,因为adcx为u16整形
//LCD_ShowxNum(134,150,adcx,1,16,0); //显示电压值的整数部分,3.1111的话,这里就是显示3
//temp-=adcx; //把已经显示的整数部分去掉,留下小数部分,比如3.1111-3=0.1111
//temp*=1000; //小数部分乘以1000,例如:0.1111就转换为111.1,相当于保留三位小数。
//LCD_ShowxNum(150,150,temp,3,16,0X80); //显示小数部分(前面转换为了整形显示),这里显示的就是111.
//和上位机用串口通信,
//S ADC的值用于绘制心率的图像
//B 用于计算心率值
//Q 用于计算IBI的值
printf("S%d\r\n", Signal);
if (QS == true)
{
printf("B%d\r\n", BPM);
printf("Q%d\r\n", IBI);
//sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
// sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
QS = false; // reset the Quantified Self flag for next time
}
LED0=!LED0;
//printf("adcx = %d\r\n", adcx);
delay_ms(20);
}
}
/*我用的是STM32F4的,
希望有所帮助 */ |
|