#include <reg52.h>
sbit LPWM0 = P1^2;
sbit LPWM1 = P1^3;
sbit RPWM0 = P1^4;
sbit RPWM1 = P1^6;
unsigned char flag1S=0;
//全局变量
unsigned int LPwm0Cnt=0;
unsigned int LPwm1Cnt=0;
unsigned int LPWM0Set=100;
unsigned int LPWM1Set=100;
unsigned int RPwm0Cnt=0;
unsigned int RPwm1Cnt=0;
unsigned int RPWM0Set=100;
unsigned int RPWM1Set=100;
void delay(int tms) //20ms
{
int i,j;
for(i=0;i<tms;i++)
{
for(j=0;j<5;j++);
}
}
void main()
{
TMOD=0x01; //定时器 0 工作在模式 1,16 位定时模式
TH0=(65535-100)/256; //定时 10us
TL0=(65535-100)%256;
ET0=1;//打开定时器0中断开关
EA=1;//打开总中断开关
TR0=1;//打开定时器
while(1)
{
LPWM0Set =0;
//LPWM1Set =25;
delay(5);//d20--103ms d10--53ms
LPWM0Set =25;
//LPWM1Set =0;
delay(5);
}
while(1)
{ //正转
LPWM0Set =0;
LPWM1Set =25;
RPWM0Set =0;
RPWM1Set =25;
delay(5);
LPWM0Set =0;
LPWM1Set =0;
RPWM0Set =0;
RPWM1Set =0;
delay(5);
LPWM0Set =95;
LPWM1Set =0;
RPWM0Set =95;
RPWM1Set =0;
delay(5);
LPWM0Set =0;
LPWM1Set =0;
RPWM0Set =0;
RPWM1Set =0;
delay(5);
}
}
/* 定时器0中断函数 */
void tm0_isr() interrupt 1 using 1
{
TH0=0xFF;//10us 进来一次中断
TL0=0xCC;
LPwm0Cnt++;
LPwm1Cnt++;
RPwm0Cnt++;
RPwm1Cnt++;
//决定PWM的周期,也就是频率,
if(LPwm0Cnt>=50)
{
LPwm0Cnt=0;
}
if(LPwm1Cnt>=50)
{
LPwm1Cnt=0;
}
//输出PWM
if(LPwm0Cnt<LPWM0Set)//1s=1000ms 20ms
{
LPWM0=1;
}
else
{
LPWM0=0;
}
//输出PWM
if(LPwm1Cnt<LPWM1Set)//1s=1000ms 20ms
{
LPWM1=1;
}
else
{
LPWM1=0;
}
//决定PWM的周期,也就是频率,
if(RPwm0Cnt>=50)
{
RPwm0Cnt=0;
}
if(RPwm1Cnt>=50)
{
RPwm1Cnt=0;
}
//输出PWM
if(RPwm0Cnt<RPWM0Set)//
{
RPWM0=1;
}
else
{
RPWM0=0;
}
//输出PWM
if(RPwm1Cnt<RPWM1Set)//
{
RPWM1=1;
}
else
{
RPWM1=0;
}
}
stc89c52,,双PWM
最新推荐文章于 2024-08-14 22:57:13 发布