OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2765|回复: 0

求助,关于tim3输出pwm

[复制链接]

6

主题

14

帖子

0

精华

初级会员

Rank: 2

积分
59
金钱
59
注册时间
2018-4-1
在线时间
9 小时
发表于 2018-4-3 11:12:13 | 显示全部楼层 |阅读模式
#include "pwm.h"
#include "stm32f10x.h"
#include "sys.h"
#include "led.h"


void TIM3_PWM_Init(u16 psc,u16 arr)
{
        GPIO_InitTypeDef GPIO_InitStructure;   //¶¨òåGPIO½á11ìå
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;   //¶¨òåTIM3õê¼»ˉ½á11ìå
        TIM_OCInitTypeDef TIM_OCInitStructure;
       
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , 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_11;
//        GPIO_InitStructure.GPIO_Speed =   GPIO_Speed_50MHz;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        TIM_TimeBaseStructure.TIM_Period = arr;
        TIM_TimeBaseStructure.TIM_Prescaler =psc;
        TIM_TimeBaseStructure.TIM_ClockDivision = 0;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
       
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
       
       
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;
        TIM_OC1Init(TIM3, &TIM_OCInitStructure);
        TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
       
       
        //TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;
        //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);
       
        //TIM_CtrlPWMOutputs(TIM1,ENABLE);
        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
       
        TIM_ARRPreloadConfig(TIM3, ENABLE);
       
        TIM_Cmd(TIM3, ENABLE);
}

#include "stm32f10x.h"
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "pwm.h"


void main(void)
{
        u8 dir=1;
        u16 ledpwmval=0;
        delay_init();
        LED_Init();
        TIM3_PWM_Init(0,899);
        while(1)
        {
                delay_ms(10);
                if(dir==1)
                {ledpwmval++;
                }
                else
                {
                       
                        ledpwmval--;
                }
                if(ledpwmval>300) {
                        dir=0;
                }
                        if(ledpwmval==0) dir=1;
                TIM_SetCompare1(TIM3,ledpwmval);
                TIM_SetCompare2(TIM3,ledpwmval);
        }
}

我是想用tim3的ch1和ch2输出两路pwm控制两个LED的亮度的,但是一个灯都点不亮。。。求大佬指点

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-8 22:52

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表