打印

AD和外部触发不能同时进行吗

[复制链接]
1445|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
EVERBELL1|  楼主 | 2013-9-5 11:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"
#include"stm32f10x_tim.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
#include"delay.h"
#include "stdio.h"
#include "i2c.h"
#include "ZLG7290.h"
/***PA6 PA7 PB0 PB1Êä³ö4·PWM²¨*/
/***PA0 PA1 Êä³ö4·PWM²¨*/
uint16_t CCR1_Val = 100;
uint16_t CCR2_Val = 200;
uint16_t CCR3_Val = 300;
uint16_t CCR4_Val = 400;
uint16_t CCR5_Val = 300;
uint16_t CCR6_Val = 400;
/***LEDµÆ£¬ÏÔʾ²¿·Ö*****/
#define ld0  (1<<8)         //P24         PA8
#define ld1  (1<<2)         //P25         PD2
#define s3  (1<<0)         //P24         PA8
#define s4  (1<<1)         //P25         PD2
#define jizhun 0x0800//ÉèÖûù×¼µçѹ
#define        led0(x) GPIOA->ODR=(GPIOA->ODR&~ld0)|(x?ld0:0)
#define        led1(x) GPIOD->ODR=(GPIOD->ODR&~ld1)|(x?ld1:0)
#define SWT3(x)        GPIOA->ODR=(GPIOA->ODR&~s3)|(x?s3:0)
#define SWT4(x)        GPIOA->ODR=(GPIOA->ODR&~s4)|(x?s4:0)
#define  DMAbufsize  4
#define ADC1_DR_Address    ((u32)0x4001244C)  //ADC DR¼Ä´æÆ÷»ùµØÖ·
vu16 ADC_ConvertedValue[4];//ADC½á¹ûÔÚÄÚ´æÖеĵØÖ·
       
void RCC_Configuration(void);         //ʱÖÓÅäÖú¯ÊýÉùÃ÷
void GPIO_Configuration(void);         //GPIOÅäÖú¯ÊýÉùÃ÷
void NVIC_Configuration(void);         //ǶÌ×ÏòÁ¿ÖжÏÅäÖú¯ÊýÉùÃ÷
void ADC_Configuration(void);         //ADÅäÖú¯ÊýÉùÃ÷
void DMA_Configuration(void);         //DMAÅäÖú¯ÊýÉùÃ÷
void TIM3_Configuration(void) ;
void TIM2_Configuration(void) ;
void EXTI_Config(void);
void delay(long z)
{
  for(;z>0;z--);
}

int main(void)
{
  RCC_Configuration();
GPIO_Configuration();
  NVIC_Configuration();
  EXTI_Config();
    DMA_Configuration();
    ADC_Configuration();
           TIM3_Configuration();

    delay(0xffff);       

  delay_init();
  IIC_Init();
  ZLG7290INTRST_Init();
             // TIM2_Configuration();

  while (1)
  {

  }
}
void TIM2_Configuration(void)
{
  /* TIM2 configuration ------------------------------------------------------*/
  /* Time Base configuration  */
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;                  //¶Ô
          TIM_OCInitTypeDef  TIM_OCInitStructure;       
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = (8000-1);         
  TIM_TimeBaseStructure.TIM_Prescaler = 9;      
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  
  /* TIM2 channel2 configuration in PWM mode */
  TIM_OCStructInit(&TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;               
  TIM_OCInitStructure.TIM_Pulse = 1200;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  
  TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);

  TIM_ARRPreloadConfig(TIM2,ENABLE);

  /* TIM2 counter enable */
  TIM_Cmd(TIM2, ENABLE);  
}


void TIM3_Configuration(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;                  //¶Ô
          TIM_OCInitTypeDef  TIM_OCInitStructure;       

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);          //¿ªÆôTIM3ʱÖÓ
         TIM_DeInit(TIM3);                                                                          //³õʼ»¯TIM3       
  TIM_TimeBaseStructure.TIM_Period=1000;                        /* ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ(¼ÆÊýÖµ) */
                                                                                                        /* ÀÛ¼Æ TIM_Period¸öƵÂʺó²úÉúÒ»¸ö¸üлòÕßÖÐ¶Ï */
                                                                                                //ARR=1000;
  TIM_TimeBaseStructure.TIM_Prescaler= 3;                   /* ʱÖÓÔ¤·ÖƵÊý   ÀýÈ磺ʱÖÓƵÂÊ=72MHZ/(ʱÖÓÔ¤·ÖƵ+1) */
  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;         /* ²ÉÑù·ÖƵ */
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned1;        /* ÖÐÑë¶ÔÆëģʽ1¼ÆÊýģʽ */
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//  TIM_ClearFlag(TIM2, TIM_FLAG_Update);                    /* Çå³ýÒç³öÖжϱêÖ¾ */
//  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
       
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIMÊä³ö±È½Ï¼«ÐÔ¸ß

          /* PWM1 Mode configuration: Channel 1 */                                  //TIMµÄͨµÀ2
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 400;                                  //ÉèÖÃÕ¼¿Õ±È³õÖµ
                                                                                                                  //Õ¼¿Õ±ÈDUTY=CCR2_val/TIM_Period
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);//ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæ

  /* PWM1 Mode configuration: Channel 2 */                                  //TIMµÄͨµÀ2
   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 600;                                  //ÉèÖÃÕ¼¿Õ±È³õÖµ
                                                                                                                  //Õ¼¿Õ±ÈDUTY=CCR2_val/TIM_Period
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
   TIM_ARRPreloadConfig(TIM3, ENABLE);//ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæ
  /* PWM1 Mode configuration: Channel 3 */                                  //TIMµÄͨµÀ3 PB1
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 400;                                //Õ¼¿Õ±ÈDUTY=CCR3_val/TIM_Period
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);//ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæ
  /* PWM1 Mode configuration: Channel 4 */                                 //TIMµÄͨµÀ3
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 600;                          //Õ¼¿Õ±ÈDUTY=CCR4_val/TIM_Period

  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM3, ENABLE);//ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæÆ÷
  TIM_Cmd(TIM3, ENABLE);       
}

void ADC_Configuration(void)
{
  ADC_InitTypeDef ADC_InitStructure;//ADC³õʼ»¯½á¹¹ÌåÉùÃ÷

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//¶ÀÁ¢×ª»»Ä£Ê½
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//¿ªÆôɨÃèģʽ
//  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//¹Ø±ÕÁ¬Ðøת»»Ä£Ê½(ÒòΪÊÇÓÉTIM2À´´¥·¢µÄ)
//  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//  ADC_SoftwareStartConvCmd(ADC1, ENABLE);//µ÷ÓÃADCµÄÈí¼þ´¥·¢¿âº¯Êý!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//¹Ø±ÕÁ¬Ðøת»»Ä£Ê½(ÒòΪÊÇÓÉTIM2À´´¥·¢µÄ)
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;//¶¨ÒåÁËʹÓÃÍⲿ´¥·¢À´Æô¶¯¹æÔòͨµÀµÄÄ£Êýת»»//ÏÖÔÚΪѡÔñ¶¨Ê±Æ÷ 2 µÄ²¶»ñ±È½Ï 2 ×÷Ϊת»»Íⲿ´¥·¢
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//¹æ¶¨ÁË ADC Êý¾ÝÏò×ó±ß¶ÔÆ뻹ÊÇÏòÓұ߶ÔÆë ÏÖΪÓÒ
  ADC_InitStructure.ADC_NbrOfChannel = 4; //¹æ¶¨ÁË˳Ðò½øÐйæÔòת»»µÄ ADC ͨµÀµÄÊýÄ¿(1-16)(ÒòΪÓÐÒ»¸öµçѹÁ½¸öµçÁ÷ͬʱת»»)
  ADC_Init(ADC1, &ADC_InitStructure);//¸ù¾Ý ADC_InitStruct ÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉè ADCx µÄ¼Ä´æÆ÷

  
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5);//²ÉÑùµçѹ
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_239Cycles5);//
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_239Cycles5);//²ÉÑùÄ£¿é1µçÁ÷I1
  ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_239Cycles5);//²ÉÑùÄ£¿é2µçÁ÷I2
  //ÉèÖÃÖ¸¶¨ ADC µÄ¹æÔò×éͨµÀ£¬ÉèÖÃËüÃǵÄת»¯Ë³ÐòºÍ²ÉÑùʱ¼ä

  ADC_DMACmd(ADC1, ENABLE);//ʹÄÜ»òÕßʧÄÜÖ¸¶¨µÄ ADC µÄ DMA ÇëÇó
  ADC_Cmd(ADC1, ENABLE);        //ʹÄÜ»òÕßʧÄÜÖ¸¶¨µÄ ADC
  ADC_ResetCalibration(ADC1);  //ÖØÐÂУ׼
  while(ADC_GetResetCalibrationStatus(ADC1));  //µÈ´ýÖØÐÂУ׼Íê³É
  ADC_StartCalibration(ADC1);  //¿ªÊ¼Ð£×¼
  while(ADC_GetCalibrationStatus(ADC1));//µÈ´ýУ׼Íê³É
  ADC_ExternalTrigConvCmd(ADC1, ENABLE); //Á¬Ðøת»»¿ªÊ¼,ADCͨ¹ýDMA·½Ê½²»¶Ï¸üÐÂRAMÇø
}

void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;  //DMA³õʼ»¯½á¹¹ÌåÉùÃ÷

  DMA_DeInit(DMA1_Channel1);   //ĬÈϳõʼ»¯
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA¶ÔÓ¦ÍâÉèµÄ»ùµØÖ·
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//ÄÚ´æ´æ´¢»ùµØÖ·
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//DMAת»»Ä£Ê½ÎªSRCģʽÓÉÍâÉè°áµ½ÄÚ´æ
  DMA_InitStructure.DMA_BufferSize = 4;//DMA»º´æ´óС
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//½ÓÊÕÒ»´ÎÊý¾ÝºóÉ豸µØÖ·½ûÖ¹ºóÒÆ
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//¿ªÆô½ÓÊÕÒ»´ÎÊý¾ÝºóÄ¿±êÄÚ´æµØÖ·ºóÒÆ
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//¶¨ÒåÍâÉèÊý¾Ýλ¿íΪ16λ
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//DMA°áÒÆÊý¾Ýλ¿í16λ
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//ת»»Ä£Ê½,Ñ­»·×ª´æģʽ
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMAÓÅÏȼ¶¸ß
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //H2Hģʽ½ûÓÃ
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);//¸ù¾ÝDMA_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯DMAµÄͨµÀx¼Ä´æÆ÷

  DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);         //DMA´«ËÍÍê³ÉÖжÏ
  DMA_Cmd(DMA1_Channel1, ENABLE); //ʹÄÜ»òÕßʧÄÜÖ¸¶¨µÄͨµÀ x
}

void GPIO_Configuration(void)         //ÉèÖÃPA(6)PA(7)ΪÍÆÍìÊä³öģʽ
{                                                                 //        PB(0)PB(1)
  GPIO_InitTypeDef GPIO_InitStructure;  //PC(1,2,3,4)ΪģÄâÊäÈëģʽ
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA
                                                                                  |RCC_APB2Periph_GPIOB
                                                                                  |RCC_APB2Periph_GPIOC
                                                                                   |RCC_APB2Periph_GPIOD, ENABLE);                                                         
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;       
  GPIO_Init(GPIOA, &GPIO_InitStructure);
//          GPIOA->CRH&=0XFFFFFFF0;
//        GPIOA->CRH|=0X00000003;//PA8 ????            
//    GPIOA->ODR|=1<<8;      //PA8 ???
//        GPIOA->CRL&=0XFFFFFF00;
//        GPIOA->CRL|=0X00000033;//PA0PA1 ????            
//    GPIOA->ODR|=1<<0;      //PA8 ???
//        GPIOA->ODR|=1<<1;      //PA8 ???       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;               
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        //ADC²ÉÑùÊäÈ룬ÓÃÄ£ÄâÊäÈëģʽ
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIOD->CRL&=0XFFFFF0FF;
        GPIOD->CRL|=0X00000300;//PD.2????
        GPIOD->ODR|=1<<2;      //PD.2???
}

/*ʱÖÓÅäÖü°Ïà¹ØÍâÉèʱÖÓʹÄÜ*/
void RCC_Configuration(void)
{   
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3  , ENABLE);
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
}
void EXTI_Config(void)
{
        EXTI_InitTypeDef EXTI_InitStructure;
        EXTI_StructInit(&EXTI_InitStructure);
                                        
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);        //¹Ü½ÅÑ¡Ôñ
        EXTI_ClearITPendingBit(EXTI_Line4);

        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
        EXTI_InitStructure.EXTI_Line = EXTI_Line4 ;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitStructure);
}

/*ÖжÏÅäÖÃ*/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

        #ifdef  VECT_TAB_RAM
        NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
    #else
        NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    #endif
}
/*DMAÅäÖÃ*/

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif
main函数处将TIM2CONFIG 引用后就可以正常工作(EXTI4连着键盘 可正常中断控制)
不将TIM2引用就不能正常工作 怎么回事呢
沙发
EVERBELL1|  楼主 | 2013-9-5 11:46 | 只看该作者
乱码部分是注释 不要在意
我开启了EXTI4的外部按键中断
ADC由TIM2倒时触发

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝