STM8S单片机 定时器TIM1 设置输出PWM没有输出,只能输出一个固定的PWM。求解! 15
#include<iostm8s003f3.h>#include<intrinsics.h>voidCLK_Init(){CLK_CKDIVR=0x18;//16M/8=...
#include<iostm8s003f3.h>
#include<intrinsics.h>
void CLK_Init()
{
CLK_CKDIVR = 0x18; // 16M/8 =2M;
}
void DelayMS(unsigned int ms)
{
unsigned int i;
while(ms != 0)
{
for(i = 0; i < 200;i++)
{
for(i = 0; i < 150;i++);
}
ms--;
}
}
void Tim1_Config(void)
{
TIM1_PSCRH =199/256; //必须先高8位再低8位 update:999
TIM1_PSCRL = 199%256; //技术时钟= 2MHz/(199+1)= 10KHz 计数周期为0.1ms
TIM1_CCMR3 |=0x60; //output mode1 PWM 强制为有效电平强制OC2REF为高
TIM1_CCER2 |= 0x01; //捕获使能
TIM1_ARRH =100/256;
TIM1_ARRL = 100%256; //脉冲周期 = 10ms
TIM1_CCR3H = 0;
TIM1_CCR3L = 0;
TIM1_BKR =0x80; //TIM1必须写,TIM2,3没有此寄存器
TIM1_CR1 =0x01; //启动计数
}
void PWM1_Config(unsigned int duty)
{
TIM1_CCR3H = duty/256;
TIM1_CCR3L = duty%256;
}
unsigned int id;
main()
{
CLK_Init();
Tim1_Config();
while(1)
{
for (id = 0; id <=100; id++)
{
PWM1_Config(id);
DelayMS(10);
}
for (id = 100; id > 0;id--)
{
PWM1_Config(id);
DelayMS(10);
}
}
} 展开
#include<intrinsics.h>
void CLK_Init()
{
CLK_CKDIVR = 0x18; // 16M/8 =2M;
}
void DelayMS(unsigned int ms)
{
unsigned int i;
while(ms != 0)
{
for(i = 0; i < 200;i++)
{
for(i = 0; i < 150;i++);
}
ms--;
}
}
void Tim1_Config(void)
{
TIM1_PSCRH =199/256; //必须先高8位再低8位 update:999
TIM1_PSCRL = 199%256; //技术时钟= 2MHz/(199+1)= 10KHz 计数周期为0.1ms
TIM1_CCMR3 |=0x60; //output mode1 PWM 强制为有效电平强制OC2REF为高
TIM1_CCER2 |= 0x01; //捕获使能
TIM1_ARRH =100/256;
TIM1_ARRL = 100%256; //脉冲周期 = 10ms
TIM1_CCR3H = 0;
TIM1_CCR3L = 0;
TIM1_BKR =0x80; //TIM1必须写,TIM2,3没有此寄存器
TIM1_CR1 =0x01; //启动计数
}
void PWM1_Config(unsigned int duty)
{
TIM1_CCR3H = duty/256;
TIM1_CCR3L = duty%256;
}
unsigned int id;
main()
{
CLK_Init();
Tim1_Config();
while(1)
{
for (id = 0; id <=100; id++)
{
PWM1_Config(id);
DelayMS(10);
}
for (id = 100; id > 0;id--)
{
PWM1_Config(id);
DelayMS(10);
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询