|
主要是STM32103系列的應(yīng)用,適合新手。非喜勿噴。
單片機源程序如下:
- #include"PWM.h"
- void TIM3_CH1_PWM_Init(u16 pre,u16 psc)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;//端口
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//推挽輸出
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//輸出速度
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//映射到PC6
-
- TIM_TimeBaseInitStructure.TIM_Period=pre;//自動重載值
- TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
- TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;//預(yù)分頻系數(shù)(1分頻)
- TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//計數(shù)模式
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
-
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OC1Init(TIM3,&TIM_OCInitStructure);
-
- TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3,ENABLE);
-
- TIM_Cmd(TIM3,ENABLE);
-
- }
復(fù)制代碼
- #include"stm32f10x.h"
- #include"led.h"
- #include"system.h"
- #include"SysTick.h"
- #include"PWM.h"
- int main()
- {
- u16 i=0;
- u8 fx=0;
- SysTick_Init(72);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- LED_Init();
- TIM3_CH1_PWM_Init(500,72-1);//ARR頻率2K
- while(1)
- {
- if(fx==0)
- {
- i++;
- if(i==300)
- {
- fx=1;
- }
- }
- else
- {
- i--;
- if(i==0)
- {
- fx=0;
- }
- }
- TIM_SetCompare1(TIM3,i);
- delay_ms(10);
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
實驗13PWM呼吸燈.rar
(283.36 KB, 下載次數(shù): 55)
2018-11-22 01:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|