请大家帮忙看下,我的这个ADC配置是否有问题,我要采集一段采样值逐渐下降的信号。但是目前ADC采样的话是会存在两秒钟采样值相同的结果。while中是简单些的测量,可能比较乱,还请见谅。
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 10 $
* $Date: 15/06/16 7:23p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Convert ADC channel 0 in Single mode and print conversion results.
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano100Series.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "lcd.h"
#include "uart.h"
#include "sys.h"
#include "clk.h"
#include "LCDLIB.h"
#define LCD_ALPHABET_NUM 7
#define LCD_DIGIT_NUM 4
uint32_t u32Result;
uint32_t times_us=0,time_delay=0;
unsigned int jidi=0,jieguo=0;
unsigned int shuzu[33]={0};
unsigned int resul[3]={0};
unsigned int i=0;
unsigned char cCountI , cCountJ ;
unsigned int k ,n;
unsigned int sum;
unsigned int m;
unsigned char delay_time=0;
volatile uint8_t u8ADF,u8ADF1,u8ADF2;
static unsigned char test=0;
void ADC_IRQHandler(void);
void ADC_IRQHandler(void)
{
uint32_t u32Flag;
// Get ADC conversion finish interrupt flag
u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT);
if(u32Flag & ADC_ADF_INT)
u8ADF = 1;
ADC_CLR_INT_FLAG(ADC, u32Flag);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Set HCLK source form HXT and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC,CLK_HCLK_CLK_DIVIDER(1));
/* Enable external 12MHz HXT, 32KHz LXT and HIRC */
CLK_EnableXtalRC( CLK_PWRCTL_LXT_EN_Msk | CLK_PWRCTL_HIRC_EN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady( CLK_CLKSTATUS_LXT_STB_Msk | CLK_CLKSTATUS_HIRC_STB_Msk);
CLK_SetCoreClock(1000000);
/* Enable IP clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(TMR0_MODULE);
/* Enable ADC clock */
CLK_EnableModuleClock(ADC_MODULE);
CLK->APBCLK |= CLK_APBCLK_LCD_EN;
/* Select IP clock source */
CLK_SetModuleClock(UART0_MODULE,CLK_CLKSEL1_UART_S_HIRC,CLK_UART_CLK_DIVIDER(1));
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HIRC, 0);
CLK->CLKSEL1 &= ~CLK_CLKSEL1_LCD_S_Msk;
CLK->CLKSEL1 |= (0x0 << CLK_CLKSEL1_LCD_S_LXT);// Clock source from external 12 MHz or 32 KHz crystal clock
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PA multi-function pins for UART0 RXD and TXD */
SYS->PA_H_MFP &= ~(SYS_PA_H_MFP_PA14_MFP_Msk|SYS_PA_H_MFP_PA15_MFP_Msk);
SYS->PA_H_MFP |= (SYS_PA_H_MFP_PA14_MFP_UART0_RX|SYS_PA_H_MFP_PA15_MFP_UART0_TX);
/* Set PB multi-function pins for Clock Output */
SYS->PB_H_MFP = ( SYS->PB_H_MFP & ~SYS_PB_H_MFP_PB12_MFP_Msk ) | SYS_PB_H_MFP_PB12_MFP_CKO;
SYS->PB_H_MFP &= ~SYS_PB_H_MFP_PB8_MFP_Msk;
SYS->PB_H_MFP |= SYS_PB_H_MFP_PB8_MFP_TMR0_EXT;
/* Set PA.0 multi-function pin for ADC channel 0 */
SYS->PD_L_MFP = (SYS->PD_L_MFP & ~SYS_PD_L_MFP_PD2_MFP_Msk) | SYS_PD_L_MFP_PD2_MFP_ADC_CH10;
//SYS->PE_L_MFP = (SYS->PE_L_MFP & ~ SYS_PE_L_MFP_PE0_MFP_Msk ) | SYS_PE_L_MFP_PE0_MFP_GPE0;
/* Disable PA.0 digital input path */
SYS->Int_VREFCTL = 0x00000F07;
PD->OFFD |= (((1<<1)|(1<<2)) << GP_OFFD_OFFD_Pos);
SystemCoreClockUpdate();
/* Lock protected registers */
SYS_LockReg();
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
UART_Open(UART0, 9600);
}
void TMR0_IRQHandler(void)
{
times_us=1;
// 清除中断标志
TIMER_ClearIntFlag(TIMER0);
}
int32_t main (void)
{
SYS_Init();
/* Init UART0 for printf */
UART0_Init();
GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
GPIO_SetMode(PE, BIT6, GPIO_PMD_OUTPUT);
GPIO_SetMode(PC, BIT8, GPIO_PMD_OUTPUT);
GPIO_SetMode(PE, BIT3, GPIO_PMD_OUTPUT);
PE3=0;
// Enable channel 0
ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_SINGLE, ADC_CH_10_MASK);
/* ADC工作在单端single模式,并使能通道0准备采样 */
// Power on ADC
ADC_POWER_ON(ADC);
/* 使用AVDD电压作为参考*/
ADC_SET_REF_VOLTAGE(ADC,ADC_REFSEL_INT_VREF);
/* 使能ADC 中断,转换完成将发生中断*/
ADC_EnableInt(ADC, ADC_ADF_INT);
NVIC_EnableIRQ(ADC_IRQn);
/* 初始化Timer0,周期模式,每秒发生一次中断*/
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 100);
/* 使能Timer0中断 */
TIMER_EnableInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
// 启动 Timer 0
TIMER_Start(TIMER0);
PA3=0;
PE6=1;
PC8=0;
while(1)
{
if(times_us==1)//每10ms进入一次
{
PE6=0; //LED灭
for(m=250;m>0;m--);//等待硬件开关
u8ADF = 0;
ADC_START_CONV(ADC);
while (u8ADF == 0);
jidi = ADC_GET_CONVERSION_DATA(ADC, 10);//测出灯灭的ADC值
PE6=1; //LED亮
for(m=250;m>0;m--);//等待硬件开关
u8ADF = 0;
ADC_START_CONV(ADC);
while (u8ADF == 0);
jieguo = ADC_GET_CONVERSION_DATA(ADC, 10); //灯亮的LED值
if(jieguo>jidi)
{
jieguo=jieguo-jidi;
}
else
{
jieguo=1;
}
shuzu[i]=jieguo;//存储32次的ADC值
PE6=0;
i++;
times_us=0;
}
if(i==33)
{
for(cCountI = 0 ; cCountI < 32 ; cCountI ++)//排序
{
for(cCountJ = cCountI+1 ; cCountJ < 32 ; cCountJ ++)
{
if(shuzu[cCountI] < shuzu[cCountJ] )
{
k = shuzu[cCountI] ;
shuzu[cCountI] = shuzu[cCountJ] ;
shuzu[cCountJ] = k ;
}
}
}
sum = 0 ;
for(cCountI = 4 ; cCountI < 28 ; cCountI++)
{
sum += shuzu[cCountI] ;
}
sum = (unsigned int)(sum / (24)); // 按照大小排序后,取中间24的数的平均值
resul[test] =sum;
test++;
if(test==3)//此时是一秒,输出一次结果
{
// sum=(unsigned int)(shuzu1[0]+shuzu1[1]+shuzu1[2])/3;
printf("%5d",resul[1]);
test=0;
}
i=0; //次数清零
times_us=0; //时间清零
}
}
}
|