用的是TIM1 中间对齐方式
单片机源程序如下:
- /*边沿对齐PWM CH1输出*/
- /*注意:LCD R/W引脚需要硬件接地 若要软件接地则GPIO初始化内定义*/
- #include"iostm8s208mb.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- void delay(uint z) //约1ms延时
- {
- uint i,j;
- while (z--)
- {
- for(i=0;i<50;i++)
- for(j=0;j<20;j++);
- }
- }
- void CH1_PWM_SET(ulong SET_CH1,float Duty_CH1)//改变占空比
- {
- float a;
- a=Duty_CH1*SET_CH1;
- TIM1_CCR1H=((uint)(a))/256;
- TIM1_CCR1L=((uint)(a))%256;
- TIM1_CCMR1|=0x60;
- TIM1_CCER1&=0xfd;
- TIM1_CCER1|=0x01;
- TIM1_OISR|=0x01;
- }
- void CH2_PWM_SET(ulong SET_CH2,float Duty_CH2)//改变占空比
- {
- float a;
- a=Duty_CH2*SET_CH2;
- TIM1_CCR2H=((uint)(a))/256;
- TIM1_CCR2L=((uint)(a))%256;
- TIM1_CCMR2|=0x60;
- TIM1_CCER1&=0xdf;
- TIM1_CCER1|=0x10;
- TIM1_OISR|=0x04;
- }
- void CH3_PWM_SET(ulong SET_CH3,float Duty_CH3)//改变占空比
- {
- float a;
- a=Duty_CH3*SET_CH3;
- TIM1_CCR3H=((uint)(a))/256;
- TIM1_CCR3L=((uint)(a))%256;
- TIM1_CCMR3|=0x60;
- TIM1_CCER2&=0x3d;
- TIM1_CCER2|=0x01;
- TIM1_OISR|=0x10;
- }
- void TIM1_PWM_SET(ulong PWM_SET)//改变周期
- {
- // PWM_SET=(PWM_SET/2); //改变和边沿对齐一样的频率
- TIM1_ARRH=PWM_SET/256;
- TIM1_ARRL=PWM_SET%256;
- TIM1_CR1|=0x60;
- CH1_PWM_SET(PWM_SET,0.2);
- CH2_PWM_SET(PWM_SET,0.4);
- CH3_PWM_SET(PWM_SET,0.6);
- TIM1_CR1|=0x01;
- TIM1_BKR|=0x80;
- }
- void main(void)
- {
- CLK_CKDIVR=0x00;
- delay(10);
- TIM1_PWM_SET(16000);
- while(1);
- }
复制代码
所有资料51hei提供下载:
中间对齐TIM1PWM输出.rar
(273.98 KB, 下载次数: 26)
|