打印
[STM32F2]

出拥有的全部积分请教如何初始化B口的1脚作为模拟输入,8.9脚作为PWM输出。

[复制链接]
1066|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanshon|  楼主 | 2014-12-28 13:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //
         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_OUT;//GPIO_Mode_Out_PP;//;
        GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       
GPIO_Init(GPIOB, &GPIO_InitStructure);       
这样可以吗?
沙发
mmuuss586| | 2014-12-28 18:46 | 只看该作者

积分又没啥用;

使用特权

评论回复
板凳
mmuuss586| | 2014-12-28 19:45 | 只看该作者
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
PWM输出改成这样;

使用特权

评论回复
地板
wanshon|  楼主 | 2014-12-29 07:25 | 只看该作者
mmuuss586 发表于 2014-12-28 19:45
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
PWM输出改成这样; ...

stm32f2没有 GPIO_MODE_AF_PP这个选项,我担心的是两次使用GPIO_Init(GPIOB, &GPIO_InitStructure);后一次会不会把前一次的GPIO_mode冲掉。

使用特权

评论回复
5
wanshon|  楼主 | 2014-12-29 07:37 | 只看该作者
GPIO_Configuration();       
NVIC_Configuration4();
  TIM4_Configuration();
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  TIM_ARRPreloadConfig(TIM4, DISABLE);

  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);   
        TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_Update);
  TIM_Cmd(TIM4, ENABLE);        
main(1)
{
}
void NVIC_Configuration4(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
        TIM_DeInit(TIM4);
  TIM_TimeBaseStructure.TIM_Period = (10-1); //100K
  TIM_TimeBaseStructure.TIM_Prescaler = (60-1);
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
        /* Output Compare Active Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM??2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //??????
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//??????
TIM_OCInitStructure.TIM_Pulse = 5; //????
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //????
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //??????
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init(TIM2,&TIM_OCInitStructure); //??1
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //??TIMx?CCR3????????

TIM_ARRPreloadConfig(TIM4, ENABLE); //??TIMx?ARR????????
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE); //??TIMx??
        TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;       
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);        

//PB
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //
         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//GPIO_Mode_Out_PP;//;
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
        //GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       
GPIO_Init(GPIOB, &GPIO_InitStructure);       
       

       
}
void TIM4_IRQHandler(void)
{
        /*if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
TIM_SetCompare2(TIM4, TIM_GetCapture2(TIM4) + 5);
}*/

        if(TIM_GetITStatus(TIM4, TIM_IT_Update))            //????update????  
    {  
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);     //??update??????  
                        test_TIM4++;
    }  
        flag_time=1;}
这是原程序,怎么波形就出不来呢?

使用特权

评论回复
6
mmuuss586| | 2014-12-29 20:32 | 只看该作者
wanshon 发表于 2014-12-29 07:25
stm32f2没有 GPIO_MODE_AF_PP这个选项,我担心的是两次使用GPIO_Init(GPIOB, &GPIO_InitStructure);后一 ...

呵呵,肯定有啊;
我这段就是从F2的例程复制过来的;
不过可能和你的库不一样吧;

使用特权

评论回复
7
白丁野老| | 2014-12-30 21:00 | 只看该作者
我也来学习一下

使用特权

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

本版积分规则

4

主题

12

帖子

0

粉丝