#include "pwm.h"
#include "sys.h"
#include "usart.h"
#include "stdio.h"
#include "delay.h"
int main(void)
{
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
uart_init(72,9600); //串口初始化
 WM_Init(10000,143,5000,8000,7500,500);
}
#include "pwm.h"
//tim1-pwm输出初始化
//2012.11.11
void PWM_Init(u16 arr,u16 psc,u16 ccr1,u16 ccr2,u16 ccr3,u16 ccr4)
{
RCC->APB2ENR|=1<<2; //端口A时钟使能
RCC->APB2ENR|=1<<11; //TIM1时钟使能
GPIOA->CRH&=0XFFFF0000;//PA8|PA9|PA10|PA11输出
GPIOA->CRH|=0X0000BBBB;//服用功能输出
GPIOA->ODR=1<<8;
GPIOA->ODR=1<<9;
GPIOA->ODR=1<<10;
GPIOA->ODR=1<<11;//PA8\9\10\11上拉
TIM1->ARR=arr-1;//设定计数器自动重装值
TIM1->SC=psc;//预分频器不分频
TIM1->CCMR1|=7<<4; //CH1 PWM2模式
TIM1->CCMR1|=7<<12; //CH2 PWM2模式
TIM1->CCMR2|=7<<4; // CH3 PWM2模式
TIM1->CCMR2|=7<<12; // CH4 PWM2模式
TIM1->CCMR1|=1<<3; //CH1预装载使能
TIM1->CCMR1|=1<<11; //CH2预装载使能
TIM1->CCMR2|=1<<3; //CH3预装载使能
TIM1->CCMR2|=1<<11; //CH4预装载使能
TIM1->CR1|=1<<7; //自动重装载预装载使能
//ARPE使能
TIM1->CR1&=0<<4; //计数器向上计数
TIM1->CCER|=3<<0; //OC1 输出使能
TIM1->CCER|=3<<4; //OC2 输出使能
TIM1->CCER|=3<<8; //OC3 输出使能
TIM1->CCER|=3<<12; //OC4 输出使能
TIM1->BDTR|=1<<15; //开启OC和OCN输
TIM1->CCR1=ccr1; //设定占空比
TIM1->CCR2=ccr2;
TIM1->CCR3=ccr3;
TIM1->CCR4=ccr4;
TIM1->EGR|=1<<0; //重新初始化计数器
TIM1->CR1|=0x01; //使能定时器1
} |