智能车制作
标题:
pwm控制led亮度问题求助!!!
[打印本页]
作者:
风起天阑
时间:
2016-8-2 10:44
标题:
pwm控制led亮度问题求助!!!
我用的是mc9s12xs128芯片,pwm占空比最开始设置的100%,通过定时器逐渐降低控制led亮度降低。
求大神帮忙看看哪里出错了,灯亮度没变,旁边的4位数码管还亮了。
#include <hidef.h>
#include "derivative.h"
#define LEDCPU PORTK_PK4
#define LEDCPU_dir DDRK_DDRK4
#define BUS_CLOCK 32000000
#define OSC_CLOCK 16000000
unsigned int p=100;
void INIT_PLL(void)//初始化锁相环
{
CLKSEL &= 0x7f;
PLLCTL &= 0x8F;
CRGINT &= 0xDF;
SYNR = 0x43;
REFDV = 0x81;
PLLCTL =PLLCTL|0x70;
asm NOP;
asm NOP;
while(!(CRGFLG&0x08));
CLKSEL |= 0x80;
}
void init_pwm(void)//初始化pwm
{
PWMPOL_PPOL1= 1;
PWMPRCLK = 0x22;
PWMSCLA = 0x04;
PWMCLK =0x02;
PWMCAE = 0x00;
PWMPER1 = 100;
PWMDTY1 = 100;
PWME_PWME1 = 1;
}
void init_tim(void) //初始化定时器
{
TSCR1_TFFCA = 1;
TSCR1_TEN = 1;
TIOS = 0xff;
TCTL1 = 0x00;
TCTL2 = 0x00;
TIE = 0x01;
TSCR2 = 0x07;
TFLG1 = 0xff;
TFLG2 = 0xff;
}
void delay() //延时
{
unsigned int i,j;
for(i=0;i<2;i++)
for(j=0;j<5000;j++);
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED//计时中断
interrupt void scan(void)
{
if(TFLG1_C0F == 1)
{
TFLG1_C0F = 1;
TC0 = TCNT + 1250;
if(p>1) {
p--;
PWMDTY01=p;
}
else
p=100;
}
}
#pragma CODE_SEG DEFAULT
void main(void) //主函数
{
DisableInterrupts;
INIT_PLL();
init_pwm();
LEDCPU_dir=1;
LEDCPU=0;
EnableInterrupts;
TFLG1_C0F = 1;
TC0 = TCNT + 1250;
for(;;){
}
}
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2