给你个程序参考一下/*******************************************************************//* 程序名:PWM直流电机调速 *//* 晶振: MHz CPU型号:AT89C51 *//* 直流电机的PWM波控制,可以直接的调速从0到20级的调速 */ /*****************************************************************/ #include<>#define TH0_TL0 (65536-1000)//设定中断的间隔时长unsigned char count0 = 50;//低电平的占空比unsigned char count1 = 0;//高电平的占空比bit Flag = 1;//电机正反转标志位,1正转,0反转sbit Key_add=P2 ^ 0; //电机减速sbit Key_dec=P2 ^ 1; //电机加速sbit Key_turn=P2 ^ 2; //电机换向sbit PWM1=P2^6;//PWM 通道 1,反转脉冲sbit PWM2=P2^7;//PWM 通道 2,正转脉冲unsigned char Time_delay;/************函数声明**************/void Delay(unsigned char x);void Motor_speed_high(void);void Motor_speed_low(void);void Motor_turn(void);void Timer0_init(void);/****************延时处理**********************/void Delay(unsigned char x){ Time_delay = x; while(Time_delay != 0);//等待中断,可减少PWM输出时间间隔}/*******按键处理加pwm占空比,电机加速**********/void Motor_speed_high(void){ if(Key_add==0) { Delay(10); if(Key_add==0) { count0 += 5; if(count0 >= 100) { count0 = 100; } } while(!Key_add);//等待键松开 }}/******按键处理减pwm占空比,电机减速*****/void Motor_speed_low(void){ if(Key_dec==0) { Delay(10); if(Key_dec==0) { count0 -= 5; if(count0 <= 0) { count0 = 0; } } while(!Key_dec ); } }/************电机正反向控制**************/void Motor_turn(void){ if(Key_turn == 0) { Delay(10); if(Key_turn == 0) { Flag = ~Flag; } while(!Key_turn); }}/***********定时器0初始化***********/void Timer0_init(void){TMOD=0x01; //定时器0工作于方式1TH0=TH0_TL0/256;TL0=TH0_TL0%256;TR0=1;ET0=1;EA=1;}/*********主函数********************/void main(void){ Timer0_init(); while(1) { Motor_turn(); Motor_speed_high(); Motor_speed_low(); }}/**************定时0中断处理******************/void Timer0_int(void) interrupt 1 using 1{TR0 = 0;//设置定时器初值期间,关闭定时器TL0 = TH0_TL0 % 256;TH0 = TH0_TL0 / 256 ;//定时器装初值TR0 = 1;if(Time_delay != 0)//延时函数用{ Time_delay--; }if(Flag == 1)//电机正转{ PWM1 = 0; if(++count1 < count0) { PWM2 = 1; } else PWM2 = 0; if(count1 >= 100) { count1=0; }} else //电机反转 { PWM2 = 0; if(++count1 < count0) { PWM1 = 1; } else PWM1 = 0; if(count1 >= 100) { count1=0; } }}/************************************************void Delay(unsigned char x){Time_delay = x;while(Time_delay != 0);//等待中断,可减少PWM输出时间间隔}Delay(10);if(Time_delay != 0)//延时函数用{ Time_delay--; }