打印

请大家帮看一下

[复制链接]
1959|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jan2008|  楼主 | 2009-8-27 10:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
IO, pi, TI, ST, gp
为什么当AD采集到大值时,定时中断跳进的时间就会缩短很多。平常时跳进定时中断时,一般情况能FFT,9到10次,但是当AD采集大数值时,FFT只有1次了啊。

MAIN:文件
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#define ADC1_DR_Address     0x4001244C
vu16 ADCConvertedValue;
#define NPT 512
long lBUFIN[NPT];         /* Complex input vector */
long lBUFOUT[256];        /* Complex output vector */
ErrorStatus HSEStartUpStatus;
ADC_InitTypeDef           ADC_InitStructure;
TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
TIM_OCInitTypeDef         TIM_OCInitStructure;
DMA_InitTypeDef           DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
   
/* Private functions ---------------------------------------------------------*/
int main(void)
{
#ifdef DEBUG
  debug();
#endif
  /* System Clocks Configuration */
  RCC_Configuration();
      
  /* NVIC configuration */
  NVIC_Configuration();
  /* Configure the GPIO ports */
  GPIO_Configuration();
/* DMA1 Channel5 configuration ----------------------------------------------*/
  GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7);
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&lBUFIN[0];
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize =512;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  DMA_ClearITPendingBit(DMA1_IT_TC1);
  DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
  /* Enable DMA1 Channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC1, &ADC_InitStructure);
  /* ADC1 RegularChannelConfig Test */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_1Cycles5);
  /* TIM1 configuration ------------------------------------------------------*/
  /* Time Base configuration */
  TIM_DeInit(TIM2);
//  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 0x0FFF0;         
  TIM_TimeBaseStructure.TIM_Prescaler = 0x06FF;      
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* Channel1 Configuration in PWM mode */
  TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
  /* Enable TIM1 */  
  TIM_Cmd(TIM2, ENABLE);
  /* Enable TIM1 outputs */
  ADC_DMACmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
  ADC_ResetCalibration(ADC1);
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  while(!DMA_GetITStatus(DMA1_IT_TC1));
  DMA_ClearITPendingBit(DMA1_IT_TC1);

  while (1)
  {
  }      
}
/******************************************************************************

void RCC_Configuration(void)
{

  RCC_DeInit();

  RCC_HSEConfig(RCC_HSE_ON);

  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
   
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
   
    FLASH_SetLatency(FLASH_Latency_2);

  
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
    RCC_PCLK2Config(RCC_HCLK_Div4);

    RCC_PCLK1Config(RCC_HCLK_Div2);

    RCC_ADCCLKConfig(RCC_PCLK2_Div8);
   
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);
        RCC_PLLCmd(ENABLE);

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  /* Enable GPIOA, GPIOC, ADC1 and TIM1 Periph clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOB|
                         RCC_APB2Periph_ADC1 |RCC_APB2Periph_AFIO, ENABLE);
}
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Configure TIM1 Channel1 output */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  /* Configure ADC Channel14 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
#ifdef  VECT_TAB_RAM  
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
#ifdef  DEBUG

stm32f10x_it文件:
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include <math.h>
#include "stm32_dsp.h"
#include "table_fft.h"
#define NPT 512
//extern u16 TableFFT[];
extern long lBUFIN[NPT];         /* Complex input vector */
extern long lBUFOUT[256];        /* Complex output vector */
long aBUFIN[256];
long bBUFIN[256];
long lBUFMAG[256];
u8 nuM;
u8 duM;

******************************************************************************
void DMA1_Channel1_IRQHandler(void)
{
  int i,j=0;
   s32 lX,lY;
//   s32 d1;
   while(DMA_GetITStatus(DMA1_IT_TC1))
  {
    ADC_SoftwareStartConvCmd(ADC1, DISABLE);
    ADC_DMACmd(ADC1, DISABLE);
    DMA_Cmd(DMA1_Channel1, DISABLE);
    ADC_Cmd(ADC1, DISABLE);
    for(i=0;i<512;i=i+2)
    {
      aBUFIN[j++]=lBUFIN[i];
    }
    j=0;
    cr4_fft_256_stm32(lBUFOUT, aBUFIN, 256);    //每格在133Hz
//  u32 i;
  for (i=0; i < 256; i++)
  {
    lX= (lBUFOUT[i]<<16)>>16; /* sine_cosine --> cos */
    lY= (lBUFOUT[i] >> 16);   /* sine_cosine --> sin */   
    {
      float X=  128*((float)lX)/32768;
      float Y = 128*((float)lY)/32768;
      float Mag = sqrt(X*X+ Y*Y)/256;   // 先平方和,再开方
      lBUFMAG[i] = (u32)(Mag*65536);   //
    }   
  }
  if((lBUFMAG[11]+lBUFMAG[12]+lBUFMAG[13]+lBUFMAG[14]+lBUFMAG[15]+lBUFMAG[16])>120)
    nuM++;
    for(i=1;i<512;i=i+2)
    {
      bBUFIN[j++]=lBUFIN[i];
    }
    cr4_fft_256_stm32(lBUFOUT, bBUFIN, 256);
  for (i=0; i < 256; i++)
  {
    lX= (lBUFOUT[i]<<16)>>16; /* sine_cosine --> cos */
    lY= (lBUFOUT[i] >> 16);   /* sine_cosine --> sin */   
    {
      float X=  128*((float)lX)/32768;
      float Y = 128*((float)lY)/32768;
      float Mag = sqrt(X*X+ Y*Y)/256;   // 先平方和,再开方
      lBUFMAG[i] = (u32)(Mag*65536);   //
    }   
  }
//    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  duM++;
    DMA_ClearITPendingBit(DMA1_IT_TC1);
    DMA_Cmd(DMA1_Channel1, ENABLE);
    ADC_DMACmd(ADC1, ENABLE);
    ADC_Cmd(ADC1, ENABLE);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  }
}

/*******************************************************************************/
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
    if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_7)==1)
      GPIO_ResetBits(GPIOB,GPIO_Pin_7);
    else
      GPIO_SetBits(GPIOB,GPIO_Pin_7);
    if(nuM>=1)
      GPIO_ResetBits(GPIOB,GPIO_Pin_5);
    else
      GPIO_SetBits(GPIOB,GPIO_Pin_5);
    nuM=0;
    duM=0;
  }
}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

26

帖子

0

粉丝