新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32單片機(jī)學(xué)習(xí)(6) PWM輸出實(shí)驗(yàn)

STM32單片機(jī)學(xué)習(xí)(6) PWM輸出實(shí)驗(yàn)

作者: 時(shí)間:2016-11-19 來(lái)源:網(wǎng)絡(luò) 收藏
/*
*	* 軟件功能:	 PWM輸出實(shí)驗(yàn)* */#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);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無(wú)返回: 無(wú)/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);}   }/*函數(shù): void RCC_Configuration(void)功能: 復(fù)位和時(shí)鐘控制 配置參數(shù): 無(wú)返回: 無(wú)/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus;                    //定義外部高速晶體啟動(dòng)狀態(tài)枚舉變量RCC_DeInit();                                    //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值RCC_HSEConfig(RCC_HSE_ON);                       //打開(kāi)外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等待外部高速時(shí)鐘準(zhǔn)備好if(HSEStartUpStatus == SUCCESS)                  //外部高速時(shí)鐘已經(jīng)準(zhǔn)別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開(kāi)啟FLASH預(yù)讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時(shí)鐘起振之后FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延時(shí)RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)時(shí)鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)鐘==AHB時(shí)鐘RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)鐘==AHB1/2時(shí)鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //配置PLL時(shí)鐘 == 外部高速晶體時(shí)鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE);                                   //使能PLL時(shí)鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL時(shí)鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //配置系統(tǒng)時(shí)鐘 = PLL時(shí)鐘while(RCC_GetSYSCLKSource() != 0x08)                  //檢查PLL時(shí)鐘是否作為系統(tǒng)時(shí)鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //允許 GPIOB、AFIO端口復(fù)用時(shí)鐘RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //時(shí)鐘使能}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無(wú)返回: 無(wú)/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;        //定義GPIO初始化結(jié)構(gòu)體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//復(fù)用輸出 GPIO_Init(GPIOB, &GPIO_InitStructure); 	   //PB用于輸出控制LED燈}void TIM3_Configuration(u16 arr,u16 psc)	  //TIM3定時(shí)器配置   {TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//端口映射,參考STM32中文手冊(cè)P119和庫(kù)函數(shù)手冊(cè)P132  PB4TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值	TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時(shí)鐘分割:TDTS = Tck_timTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上計(jì)數(shù)模式TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位//((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 寄存器,對(duì)應(yīng)main函數(shù) ,也可設(shè)置為	 比較 1 寄存器TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  	//預(yù)裝載器使能//ARR預(yù)裝載緩沖器使能  TIM_ARRPreloadConfig(TIM3, ENABLE);TIM_Cmd(TIM3, ENABLE);  //使能TIMx}


關(guān)鍵詞: STM32單片機(jī)PWM輸出

評(píng)論


技術(shù)專區(qū)

關(guān)閉