N76E003是有PWM模块的。新手只接触过没有PWM模块的。有模块的话怎么简化下面的代码呢
unsigned char HighRH = 0;
unsigned char HighRL = 0;
unsigned char LowRH = 0;
unsigned char LowRL = 0;
unsigned long PeriodCnt = 0;
unsigned char T1RH = 0;
unsigned char T1RL = 0;
void ConfigPWM(unsigned int fr,unsigned char dc);
void ConfigTimer1(unsigned int ms);
void ConfigTimer1(unsigned int ms);
void main ()
{
EA = 1;
P1 = 0X01;
P1M1=0x00;
P1M2=0x0f;
ConfigPWM(100,10);
ConfigTimer1(50);
while(1);
}
void ConfigPWM(unsigned int fr,unsigned char dc)
{
unsigned int high,low;
PeriodCnt = (16000000/12) / fr;
high = (PeriodCnt*dc)/100;
low = PeriodCnt - high;
high = 65536 - high + 12;
low = 65536 - low +12;
HighRH = (unsigned char)(high >> 8);
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low >> 8);
LowRL = (unsigned char)low;
TMOD &= 0XF0;
TMOD |= 0X01;
TH0 = HighRH;
TL0 = HighRL;
ET0 = 1;
TR0 = 1;
P0 = 0X01;
}
void ConfigTimer1(unsigned int ms)
{
unsigned long tmp;
tmp = 16000000/12;
tmp = (tmp*ms)/1000;
tmp = 65536 - tmp;
tmp = tmp + 12;
T1RH = (unsigned char)(tmp>>8);
T1RL = (unsigned char) tmp;
TMOD &= 0X0F;
TMOD |= 0X10;
TH1 = T1RH;
TH1 = T1RL;
ET1 = 1;
TR1 = 1;
}
void AdjustDuyCycle(unsigned char dc)
{
unsigned int high,low;
high = (PeriodCnt*dc)/100;
low = PeriodCnt - high;
high = 65536 - high + 12;
low = 65536 - low +12;
HighRH = (unsigned char)(high>>8);
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low>>8);
LowRL = (unsigned char)low;
}
void InterruptTimer0() interrupt 1
{
if(P1 == 1)
{
TH0 = LowRH;
TL0 = LowRL;
P1 = 0X00;
}
else
{
TH0 = HighRH;
TL0 = HighRL;
P1 = 0X01;
}
}
void InterruptTimer1() interrupt 3
{
static bit dir = 0;
static unsigned char index = 0;
unsigned char table[13]=
{5,18,30,41,51,60,68,75,81,86,90,93,95};
TH1 = T1RH;
TL1 = T1RL;
AdjustDuyCycle(table[index]);
if (dir == 0)
{
index++;
if(index >= 12)
{
dir = 1;
}
}
else
{
index--;
if (index == 0)
{
dir = 0;
}
}
} |