您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 基于STM32F103VET6的PWM呼吸灯
Õâ¸ö³ÌÐòÎÒÇײâ¹ýÁË£¬ÎÒÓõÄÊǷܶ·°æ¿ª·¢°åV5.0º¯Êý¹Ì¼þ¿âÓõÄ3.5#includestm32f10x.h#includestm32f10x_gpio.hvoidRCC_Configuration(void);voidTIM_GPIO_Config(void);voidPWM_Mode_Config(void);intmain(void){u8led_fx=1;//µÆÁÁµÄ·½Ïò£¬ÓÐÁÁµ½°µ£¬ºÍ°µµ½ÁÁ£¬ºôÎüµÆ¾ÍÊÇÕâÑù½¥±äµÄu16led_dt=0;RCC_Configuration();TIM_GPIO_Config();PWM_Mode_Config();while(1){delay_nms(10);if(led_fx==1)//{led_dt++;}else{led_dt--;}if(led_dt100)//ÏÂÃæ³ÌÐòµÄ¶¨Ê±ÖÜÆÚÊÇ100£¬ËùÒÔled_dt¼Óµ½100¾Í×Ô¼õµ½0£¬Ò²¾ÍÊÇpwmÊä³ö¸ß/µÍµçƽ³ÖÐøʱ¼äµÄ³¤¶Ì£¬led_fx=0;if(led_dt==0)led_fx=1;TIM_SetCompare1(TIM4,led_dt);//°Ñled_dtµÄÖµË͵½¶¨Ê±Æ÷4µÄCCR¼Ä´æÆ÷ÖиıäÕ¼¿Õ±È£¬¾ÍÊǺóÃæÌáµ½µÄÌø±äÖµpulse}}voidTIM_GPIO_Config(void)//ÅäÖõÄÊÇPB6¶Ë¿ÚµÄ¸´ÓÃÍÆÍìÊä³ö£¬ÒòΪÊä³öPWM²¨{GPIO_InitTypeDefGPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);}voidRCC_Configuration(void){SystemInit();//³õʼ»¯ÏµÍ³Ê±ÖÓRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);//ʹÄܶ˿ÚBʱÖӺ͸´Óù¦ÄÜʱÖÓRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//ʹÄܶ¨Ê±Æ÷ʱÖÓ}voidPWM_Mode_Config(void){u32CCR2_Val;TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;TIM_TimeBaseStructure.TIM_Prescaler=1;//Ô¤·ÖƵÆ÷Ϊ1¾ÍÊǶ¨Ê±Æ÷µÄ¹¤×÷ƵÂÊΪ36M£¬·ñÔò(Ϊ0»òÕßÆäËûµÄÊýʱ,TIMclock=72M/(pres+1))Ϊ72MTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//ÏòÉϼÆÊýTIM_TimeBaseStructure.TIM_Period=99;//36M/(99+1)=360KTIM_TimeBaseStructure.TIM_ClockDivision=0x0;TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//¨°?¦Ì?TIMx_CNTTIMx_CCR1¨º¡À¨ª¡§¦Ì¨¤1?a?TD¡ì¦Ì???¡ê?¡¤??¨°?a¨®DD¡ì¦Ì???TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//ÔÊÐíÊä³öTIM_OCInitStructure.TIM_Pulse=CCR2_Val;//Ìø±äÖµ£¬µ±¼ÆÊýÆ÷µÄֵСÓÚ´Ëֵʱ£¬Êä³öµçƽ¼«ÐÔ·¢ÉúÌø±ä£¬¼´¾ö¶¨Õ¼¿Õ±ÈTIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//СÓÚÉèÖõÄÌø±äֵʱÊä³öµÍµçƽTIM_OC1Init(TIM4,&TIM_OCInitStructure);//³õʼ»¯²¶»ñ±È½Ï¼Ä´æÆ÷TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//ʹÄܲ¶»ñ±È½ÏÖØ×°ÔØTIM_ARRPreloadConfig(TIM4,ENABLE);//ʹÄÜÖØ×°ÔØ£¬¿ÉÒÔÁ¬ÐøÊä³öTIM_Cmd(TIM4,ENABLE);//ʹÄܶ¨Ê±Æ÷}
本文标题:基于STM32F103VET6的PWM呼吸灯
链接地址:https://www.777doc.com/doc-6189304 .html