我说一个IO模拟PWM把。我用的是新塘的8051
首先定时器初始化
void TIMER_INIT (void)
{
TIMER0_MODE1_ENABLE;
clr_T0M;
u8TH0_Tmp = (65536-1000)/256;
u8TL0_Tmp = (65536-1000)%256;
TH0 = u8TH0_Tmp;
TL0 = u8TL0_Tmp;
set_ET0; //enable Timer0 interrupt
set_EA; //enable interrupts
set_TR0; //Timer0 run
}
定时器中断
void Timer0_ISR (void) interrupt 1 //interrupt address is 0x000B
{
TH0 = u8TH0_Tmp;
TL0 = u8TL0_Tmp;
timecnt++;
if(timecnt>=100)
timecnt=0;
if(dat>timecnt)
P00=1;
else
P00=0
}
主函数改变dat的值就会改变占空比了,0-100。
我说一个IO模拟PWM把。我用的是新塘的8051
首先定时器初始化
void TIMER_INIT (void)
{
TIMER0_MODE1_ENABLE;
clr_T0M;
u8TH0_Tmp = (65536-1000)/256;
u8TL0_Tmp = (65536-1000)%256;
TH0 = u8TH0_Tmp;
TL0 = u8TL0_Tmp;
set_ET0; //enable Timer0 interrupt
set_EA; //enable interrupts
set_TR0; //Timer0 run
}
定时器中断
void Timer0_ISR (void) interrupt 1 //interrupt address is 0x000B
{
TH0 = u8TH0_Tmp;
TL0 = u8TL0_Tmp;
timecnt++;
if(timecnt>=100)
timecnt=0;
if(dat>timecnt)
P00=1;
else
P00=0
}
主函数改变dat的值就会改变占空比了,0-100。
举报