完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
****************************************************************************************/
#include "pwm.h" #define ARR ((uint8_t)499) static void Delay(u16 nCount); /************************************************************************** * 函数名:PWM_Init * 描述 :PWM初始化 * 输入 :无 * * 输出 :无 * 返回 :无 * 调用 :外部调用 *************************************************************************/ void PWM_Init(void) { TIM3_PSCR =0x04; /* Set the Autoreload value */ TIM3_ARRH = (uint8_t)(ARR >> 8); TIM3_ARRL = (uint8_t)(ARR); /*TIM2 Frequency=16M/16/(499+1)=2K*/ /* PWM1 Mode configuration: Channel1 TIM2 Channel1 duty cycle = [TIM2_CCR1/(TIM2_ARR + 1)] * 100 = 50%*/ #if TIM3_Channel==TIM3_Channel1 /*TIM2 Frequency = TIM2 counter clock/(ARR + 1) */ TIM3_OC1Init(TIM3_OCMODE_PWM1, TIM3_OUTPUTSTATE_ENABLE,250, TIM3_OCPOLARITY_HIGH); #elif TIM3_Channel==TIM3_Channel2 TIM3_OC2Init(TIM3_OCMODE_PWM2, TIM3_OUTPUTSTATE_ENABLE,500, TIM3_OCPOLARITY_HIGH); #endif TIM3_CR1 |= (uint8_t)MASK_TIM3_CR1_CEN; } /************************************************************************** * 函数名:TIM3_OC1Init * 描述 :TIM3 第一通道PWM初始化 * 输入 :TIM3_OCMode 常用参数TIM3_OCMODE_PWM1:0x60 ,TIM3_OCMODE_PWM2:0x70 TIM3_OutputState TIM3_OUTPUTSTATE_ENABLE:0x11 TIM3_Pulse 占空比 TIM3_OCPolarity TIM3_OCPOLARITY_HIGH:0x00 ,TIM3_OCPOLARITY_LOW:0x22 * * 输出 :无 * 返回 :无 * 调用 :外部调用 *************************************************************************/ void TIM3_OC1Init(uint8_t TIM3_OCMode,uint8_t TIM3_OutputState,uint16_t TIM3_Pulse,uint8_t TIM3_OCPolarity ) { /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */ TIM3_CCER1 &= (uint8_t)(~( MASK_TIM3_CCER1_CC1E | MASK_TIM3_CCER1_CC1P)); /* Set the Output State & Set the Output Polarity */ TIM3_CCER1 |= (uint8_t)((uint8_t)(TIM3_OutputState & MASK_TIM3_CCER1_CC1E ) | (uint8_t)(TIM3_OCPolarity & MASK_TIM3_CCER1_CC1P)); /* Reset the Output Compare Bits & Set the Ouput Compare Mode */ TIM3_CCMR1 = (uint8_t)((uint8_t)(TIM3_CCMR1 & (uint8_t)(~(uint8_t)MASK_TIM3_CCMR1_OC1M)) | (uint8_t)TIM3_OCMode); /* Set the Pulse value */ TIM3_CCR1H = (uint8_t)(TIM3_Pulse >> 8); TIM3_CCR1L = (uint8_t)(TIM3_Pulse); TIM3_CCMR1 |= (uint8_t)MASK_TIM3_CCMR1_OC1PE; } /************************************************************************** * 函数名:TIM3_OC2Init * 描述 :TIM3 第二通道PWM初始化 * 输入 :TIM3_OCMode 常用参数TIM3_OCMODE_PWM1:0x60 ,TIM3_OCMODE_PWM2:0x70 TIM3_OutputState TIM3_OUTPUTSTATE_ENABLE:0x11 TIM3_Pulse 占空比 TIM3_OCPolarity TIM3_OCPOLARITY_HIGH:0x00 ,TIM3_OCPOLARITY_LOW:0x22 * * 输出 :无 * 返回 :无 * 调用 :外部调用 *************************************************************************/ void TIM3_OC2Init(uint8_t TIM3_OCMode,uint8_t TIM3_OutputState,uint16_t TIM3_Pulse,uint8_t TIM3_OCPolarity ) { /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */ TIM3_CCER1 &= (uint8_t)(~(MASK_TIM3_CCER1_CC2E | MASK_TIM3_CCER1_CC2P)); /* Set the Output State & Set the Output Polarity */ TIM3_CCER1 |= (uint8_t)((uint8_t)(TIM3_OutputState & MASK_TIM3_CCER1_CC2E ) | (uint8_t)(TIM3_OCPolarity & MASK_TIM3_CCER1_CC2P)); /* Reset the Output Compare Bits & Set the Ouput Compare Mode */ TIM3_CCMR2 = (uint8_t)((uint8_t)(TIM3_CCMR1 & (uint8_t)(~(uint8_t)MASK_TIM3_CCMR2_OC2M )) | (uint8_t)TIM3_OCMode); /* Set the Pulse value */ TIM3_CCR2H = (uint8_t)(TIM3_Pulse >> 8); TIM3_CCR2L = (uint8_t)(TIM3_Pulse); TIM3_CCMR2 |= (uint8_t)MASK_TIM3_CCMR2_OC2PE; } void SetTIM3_PWM_Frequency(uint16_t TIM3_Period) { /* Set the Autoreload value */ TIM3_ARRH = (uint8_t)(TIM3_Period >> 8); TIM3_ARRL = (uint8_t)(TIM3_Period); } void SetTIM3_PWM_DutyCycle( uint16_t TIM3_Pulse) { #if TIM3_Channel==TIM3_Channel1 /* Set the Pulse value */ TIM3_CCR1H = (uint8_t)(TIM3_Pulse >> 8); TIM3_CCR1L = (uint8_t)(TIM3_Pulse); #elif TIM3_Channel==TIM3_Channel2 TIM3_CCR2H = (uint8_t)(TIM3_Pulse >> 8); TIM3_CCR2L = (uint8_t)(TIM3_Pulse); #endif } void TestPWM_LED(void) { u16 Duty_Val; for(Duty_Val=0;Duty_Val<499;Duty_Val++) { SetTIM3_PWM_DutyCycle(Duty_Val); Delay(0x3fff); } } static void Delay(u16 nCount) { /* Decrement nCount value */ while (nCount != 0) { nCount--; } } |
|
相关推荐
|
|
飞凌嵌入式ElfBoard ELF 1板卡-CAN编程示例之开发板测试
654 浏览 0 评论
该问题是用APP给芯海科技的CST92F25芯片发指令是出现的
2242 浏览 1 评论
771 浏览 0 评论
1530 浏览 1 评论
2276 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 02:40 , Processed in 0.547909 second(s), Total 68, Slave 51 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号