您的位置 首页 汽车

STM32单片机学习(6) PWM输出试验

/***软件功能:PWM输出实验**/#includestm32f10x.h#includedelay.hvoidRCC_Configuration(void);voidGPIO_Configurat

/*

*	* 软件功用:	 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;                    //界说外部高速晶体发动状况枚举变量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);            //装备体系时钟 = PLL时钟while(RCC_GetSYSCLKSource() != 0x08)                  //查看PLL时钟是否作为体系时钟{}}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}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/265263.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部