#include<reg52.h>
sbit motor_1=P2^4;
sbit motor_2=P2^5;
sbit motor_3=P2^6;
sbit motor_4=P2^7;
static int count;
static int dc1,dc2,dc3,dc4;
void timer0_init()
{
TMOD=0x11; //1ms
TH0=0Xfc;
TL0=0X66;
TR0=1;
}
void int_Init()
{
ET0=1;
EA=1;
}
void time0_int() interrupt 1
{
count++;
if(1000==count)
{
count=0;
}
}
void pwm_motor_1(unsigned int dc1)
{
if(count<(dc1*100))
motor_1=1;
else
motor_1=0;
}
void pwm_motor_2(unsigned int dc2)
{
if(count<(dc2*100))
motor_2=1;
else
motor_2=0;
}
void pwm_motor_3(unsigned int dc3)
{
if(count<(dc3*100))
motor_3=1;
else
motor_3=0;
}
void pwm_motor_4(unsigned int dc4)
{
if(count<(dc4*100))
motor_4=1;
else
motor_4=0;
}
void main()
{
timer0_init();
int_Init();
while(1)
{
dc1=1;
dc2=0;
dc3=10;
dc4=0;
pwm_motor_1(dc1);
pwm_motor_2(dc2);
pwm_motor_3(dc3);
pwm_motor_4(dc4);
}
}