初级会员

- 积分
- 141
- 金钱
- 141
- 注册时间
- 2016-5-12
- 在线时间
- 49 小时
|
1金钱
#include "pbdata.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
int main(void)
{
u8 led_fx=1;
u16 led_dt=180;
RCC_Configuration(); //Ïμí3ê±Öó3õê¼»ˉ
GPIO_Configuration();//¶Ë¿ú3õê¼»ˉ
TIM3_Configuration();//¶¨ê±Æ÷oípwmÅäÖÃ
while(1)
{
delay_ms(10);
if(led_fx==1)
{
led_dt++;
}
else
{
led_dt--;
}
if(led_dt>190) led_fx=0;
if(led_dt==0) led_fx=1;
//led_dt=180;
TIM_SetCompare1(TIM3,led_dt);
TIM_SetCompare2(TIM3,led_dt);
TIM_SetCompare3(TIM3,led_dt);
TIM_SetCompare4(TIM3,led_dt);
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_1|GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
//¶¨ê±Æ÷3õê¼»ˉ
TIM_TimeBaseStruct.TIM_Period=199;//3õÖμ199
TIM_TimeBaseStruct.TIM_Prescaler=7199;//Ô¤·ÖÆμ
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòéÏ
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
//pwm 3õê¼»ˉ
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //???,????????
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE);
}
|
|