/** * 軟件功能: PWM輸出實驗* */#include "stm32f10x.h"#include "delay.h"void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM3_Configuration(u16 arr,u16 psc);/*函數: int main(void)功能: main主函數參數: 無返回: 無/int main(void){u8 led_direction=1;u16 led_brightness=0;RCC_Configuration();GPIO_Configuration();TIM3_Configuration(499,71); delay_init(72);GPIO_ResetBits(GPIOB,GPIO_Pin_5); while(1){delay_ms(10);if(1==led_direction) led_brightness++;else led_brightness--;if(led_brightness>499) led_direction=0;if(led_brightness==0) led_direction=1;TIM_SetCompare2(TIM3, led_brightness);} }/*函數: void RCC_Configuration(void)功能: 復位和時鐘控制 配置參數: 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit(); //復位RCC外部設備寄存器到默認值RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時鐘準備好if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經準別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時鐘RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08) //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //允許 GPIOB、AFIO端口復用時鐘RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //時鐘使能}/*函數: void GPIO_Configuration(void)功能: GPIO配置參數: 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結構體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); //PB用于輸出控制LED燈}void TIM3_Configuration(u16 arr,u16 psc) //TIM3定時器配置 {TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//端口映射,參考STM32中文手冊P119和庫函數手冊P132 PB4TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數的預分頻值TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置時鐘分割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure); //根據指定的參數初始化TIMx的時間基數單位//((1+71[TIM_Prescaler] )/72M)*(1+499[TIM_Period] )=500us /* Configures the TIM3 in PWM Mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;TIM_OC2Init(TIM3, & TIM_OCInitStructure); //此處用比較 2 寄存器,對應main函數 ,也可設置為 比較 1 寄存器TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //預裝載器使能//ARR預裝載緩沖器使能 TIM_ARRPreloadConfig(TIM3, ENABLE);TIM_Cmd(TIM3, ENABLE); //使能TIMx}
評論