我是STM8的初学者,想用stm8s003f3S的定时器1的输出比较3通道产生一个PWM信号来实现LED由灭到亮的效果,但实际效果只有长亮的状态 请大侠指点一二!
#include "stm8s.h"
void PWM_Init2(unsigned long int pwm)
{
/*不分频,向上计数,TIM1_ARR=16000,重复计数器为0 */
TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 16000, 0);
TIM1_OC3Init(TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE,
pwm,
TIM1_OCPOLARITY_HIGH,
TIM1_OCNPOLARITY_HIGH,
TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_SET);
//PWM输出使能
TIM1_CtrlPWMOutputs(ENABLE);
//定时器开始计数
TIM1_Cmd(DISABLE);
}
void delayms(int x)
{ int m,n;
for(m=x;m>0;m--)
for(n=110;n>0;n--);
}
void main( void )
{
//unsigned long int i,j=0;
CLK->CKDIVR = 0X00; //系统工作在HSI 16M
while(1)
{
long int i;
for(i=0;i<16000;i=i+22)
{
PWM_Init2(i);
delayms(6000);
delayms(6000);
delayms(6000);
}
}
} |