小弟最近刚接触STM32,在贵站下载了STM32的不完全手册,在PWM的程序上进行了一定的更改,程序如下:(实现的是TIM2的CH3通道的PWM调制)
#include <stm32f10x_lib.h>
#include "sys.h"
#include "delay.h"
#define LED1 PBout(10) // PB10
#define LED0_PWM_VAL TIM2->CCR3
void Timerx_Init(u16 arr,u16 psc);
void PWM_Init(u16 arr,u16 psc);
unsigned char dir;
unsigned int led0pwmval;
unsigned char a;
int main(void)
{
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
RCC->APB2ENR|=1<<3; //使能PORTB时钟
dir=1;
led0pwmval=0;
PWM_Init(900,0);
while(1)
{
delay_ms(10000);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>300)dir=0;
if(led0pwmval==0)dir=1;
LED0_PWM_VAL=led0pwmval;
}
}
void PWM_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<0; //TIM2 时钟使能
GPIOB->CRH&=0XFFFFF0FF; //PB10 输出
GPIOB->CRH|=0X00000B00; //复用功能输出
GPIOB->ODR|=1<<10; //PB10上拉
TIM2->EGR|=0X01; //重新初始化计数器
TIM2->ARR=arr; //设定计数器自动重装值
TIM2->PSC=psc; //预分频器不分频
TIM2->CCMR2|=0x0078; //CH3 PWM2 模式 CH3 预装载使能
TIM2->CCER|=0x0100; //CH3 输出使能
TIM2->CR1=0x0080; //ARPE 使能
TIM2->CR1|=0x01; //使能定时器2
}
现在无法实现PWM的调制,希望大家帮我看看参考下!谢谢! |