#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit out1=P0^0; sbit out2=P0^1; sbit out3=P0^2; sbit out4=P0^3; bit pwm0;bit pwm1; bit o1,o2,o3,o4; uchar G_pwm_num0=50;//(占空比为50%),这两个参数的最大值为100,因为下面count<=0 uchar G_pwm_num1=50; uchar count; void time_init() { TMOD=0x21; EA=1; TH1=156; TL1=156; TR1=1; ET1=1; ET0=1; TH0=0; TL0=0; } void time1() interrupt 3 { count++; if(count<G_pwm_num0) pwm0=1; else pwm0=0; if(count<G_pwm_num1) pwm1=1; else pwm1=0; if(count==100) //这里的占空比是这样算的(G_pwm_num0/100,G_pwm_num1/100) { count=0; } else _nop_(); out1=o1&&pwm0;//left+ out2=o2&&pwm0; out3=o3&&pwm1;//right+ out4=o4&&pwm1; } void main() { time_init(); G_pwm_num0=50;//占空比可自行定义大小 o1=1; o2=0;//当两者都为0时,停止 G_pwm_num1=50; o3=1; o4=0; while(1); } |