关于f407输出两路 pwm dac 的疑惑

2019-07-20 16:12发布

本帖最后由 爱奔跑的IT 于 2017-3-8 13:34 编辑

我之前做的输出一路pwm dac输出模拟电压已经成功。但是现在发现同样的方法再输出一路模拟电压却不能成功。代码如下,望有识之士能不吝赐教。谢谢。
以下是pwmdac.c文件。成功的是TIM9同样的方法用在TIM14不成功,换TIM12也是这样。
void TIM9_CH2_PWM_Init(u16 arr,u16 psc)
{        
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   
   
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE);      //TIM9ʱÖÓʹÄÜ   
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ʹÄ&#220ORTAʱÖÓ   
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //GPIOA3
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;    //ËÙ¶È100MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
    GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»&#175A3

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM9); //GPIOA3¸´ÓÃλ¶¨Ê±Æ÷9 AF3

    TIM_TimeBaseStructure.TIM_Prescaler=psc;  //¶¨Ê±Æ÷·ÖƵ
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
    TIM_TimeBaseStructure.TIM_Period=arr;   //×Ô¶¯ÖØ×°ÔØÖµ
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM9,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷9

    //³õʼ»¯TIM9 Channel1 PWMģʽ     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OC2Init(TIM9, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM9 OC2

    TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable);  //ʹÄÜTIM9ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷

  TIM_ARRPreloadConfig(TIM9,ENABLE);//ARPEʹÄÜ
   
    TIM_Cmd(TIM9, ENABLE);  //ʹÄÜTIM9
}
void TIM14_CH2_PWM_Init(u16 arr,u16 psc)
{        
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   
   
    RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);      //TIM9ʱÖÓʹÄÜ   
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); //ʹÄ&#220ORTAʱÖÓ   
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //GPIOA3
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;    //ËÙ¶È100MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
    GPIO_Init(GPIOG,&GPIO_InitStructure); //³õʼ»&#175A3

    GPIO_PinAFConfig(GPIOG,GPIO_PinSource4,GPIO_AF_TIM14); //GPIOA3¸´ÓÃλ¶¨Ê±Æ÷9 AF3

    TIM_TimeBaseStructure.TIM_Prescaler=psc;  //¶¨Ê±Æ÷·ÖƵ
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
    TIM_TimeBaseStructure.TIM_Period=arr;   //×Ô¶¯ÖØ×°ÔØÖµ
    TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷9

    //³õʼ»¯TIM14 Channel1 PWMģʽ     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OC2Init(TIM14, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM9 OC2

    TIM_OC2PreloadConfig(TIM14, TIM_OCPreload_Enable);  //ʹÄÜTIM9ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷

  TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEʹÄÜ
   
    TIM_Cmd(TIM14, ENABLE);  //ʹÄÜTIM9
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。