不懂51HEI论坛里有没有人发过,我是从别的地方拿来的分享给大家。
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "pwm.h"
- int main(void)
- { u16 led0pwmval=150;
- u16 led1pwmval=150;
- u16 led2pwmval=150;
- u16 led3pwmval=150;
- u8 dir=1;
- u16 math=0;
- delay_init(); //延时函数初始化
- LED_Init(); //初始化与LED连接的硬件接口
- TIM3_PWM_Init(899,0);//不分频。PWM频率=72000/(899+1)=80Khz
- while(1)
- {
- delay_ms(10);
- math++;
- if(math)
- {
- delay_ms(10);
- if(dir)
- {led0pwmval++;led1pwmval++;led2pwmval++;led3pwmval++;}
- else {led0pwmval--;led1pwmval--;led2pwmval--;led3pwmval--;}
- if(led0pwmval>600||led1pwmval>600||led2pwmval>600||led3pwmval>600) dir=0;
- if(led0pwmval==0||led1pwmval==0||led2pwmval==0||led3pwmval==0) dir=1;
- math=0;
- }
- TIM_SetCompare1(TIM3,led0pwmval);
- TIM_SetCompare2(TIM3,led1pwmval);
- TIM_SetCompare3(TIM3,led2pwmval);
- TIM_SetCompare4(TIM3,led3pwmval);
- }
- }
复制代码
所有资料51hei提供下载:
TIM3四路 PWM输出实验.rar
(279.67 KB, 下载次数: 91)
|