微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32单片机学习(6) PWM输出实验

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

时间:11-19 来源:互联网 点击:
/*
*	* 软件功能:	 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函数 ,也可设置为	             

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top