#include "timer.h"
void TIM3_PWM_Init(u16 pwm_fre,u8 ch1_duty,u8 ch2_duty)
{
u16 arr;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
arr = 1000000 / pwm_fre;
TIM_TimeBaseInitStructure.TIM_Period = arr - 1; //¼ÆêyÖμ
TIM_TimeBaseInitStructure.TIM_Prescaler = 71; //·ÖÆμÏμêy 72·ÖÆμ
TIM_TimeBaseInitStructure.TIM_CounterMode = 0x0; //ÏòéϼÆêy
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0; //2»·ÖÆμ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (arr - 1) * ch1_duty / 100;
TIM_OC1Init( TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (arr - 1) * ch2_duty / 100;
TIM_OC2Init( TIM3, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd( TIM3,ENABLE);
}
#include "stm32f10x.h"
#include "stdio.h"
#include "lcd.h"
#include "rtc.h"
#include "timer.h"
//Main Body
int main(void)
{
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
SysTick_Config(SystemCoreClock/1000);
TIM3_PWM_Init(5000,10,30);
while(1)
{
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>