51单片机仿真例程-PWM直流电动机

通过单片机输出PWM波,控制占空比实现控制电动机的转速,方向,启停。

代码:

#include
#include	 
#define uint unsigned int
#define uchar unsigned char
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
sbit K_up = P3^3;
sbit K_down = P3^4;
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^2;
sbit MA= P1^0;
sbit MB= P1^1;
bit FX=0;
unsigned int PwmH,Pwm;
unsigned char i;
void Timer0Interrupt (void) interrupt 1
{
	i++;
	if(FX==0){
	if(i==PwmH){
	 MA=0;
	 }
	if(i==Pwm)
	{
	  i=0;
	  MA=1;
	 }
   	}
	else {
	 if(i==PwmH){
	 MB=0;
	 }
	if(i==Pwm)
	{
	  i=0;
	  MB=1;
	 }



	}
  
}
void delay5ms(void)
{
  int time;
  for(time=0;time<500;time++);
}
void InitTimer(void)
{
  TMOD = 0x02;
  TH0=56;
  TL0=56;
  EA=1;
  ET0=1;  //KAI ZHONGDUAN
  TR0=1; //QIDONG DINGSHI QI
}
void main(void)
{
 i=0;
 PwmH=

你可能感兴趣的:(单片机,单片机)