10/17/2008

AT89S5X PWM

使用上一篇所寫的TIME
做一PWM使LED漸亮漸暗
如拉長DUTY超過30ms 會發現led有抖動



/*
First ver 1.0
Brian @Taiwan
*/
#include "AT89X52.H"

//for time 0 pwm test 30ms

void sys_init(void);

int time_ms;
int time_1s;

void main(void)
{
char duty;
char i;

sys_init();

time_ms=0;
time_1s=0;
P0_1=1;
TR0=1;
i=1;
duty=0;

while(1)
{
if(time_1s <30)
{
if(time_1s < i)
P0_1=0;
else
P0_1=1;
}
else
{
time_1s=0;
if(i>30)
duty=~duty;
if(i<=0)
duty=~duty;
if(duty)
i--;
else
i++;
}
}
}

void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
}

void Time0_int(void)interrupt 1
{
// TIME 0 MODE2 AUTORELOAD USER INTERRUPT
// 1000 = 0.1S(100ms), 10000 = 1S(1000ms), 100=0.01S(10ms) ,10=0.001S(1ms)
time_ms++;
if(time_ms==10)
{
time_ms=0;
time_1s++;
}
}

沒有留言: