标题: 电机的正反转 [打印本页]

作者: 青春胜过年华    时间: 2016-9-20 15:44
标题: 电机的正反转
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
void delayms(uint);
sbit pwm=P2^7;
uchar aa=1,bb=0,cc=0,dd=0,ee=1;
void main()
{
TMOD=0X01;
TH0=(65536-1000)/256;
TL0=(65536-1000)/256;
EA=1;
ET0=1;
TR0=1;
while(1);
}

void key()          //按键检测处理函数
{
uchar temp;
P3=0xfe;
temp=P3;
temp=(temp&0xf0);
if(temp!=0xf0)
{
  delayms(10);
  if(temp!=0xf0)
   {
    temp=P3;  //消抖
        switch(temp)
        {
          case 0xee:
           TR0=~TR0;
           break;
          case 0xde:
          if(cc==0)
           aa+=10;
           if(aa>100)
            aa=1;
       break;
        }
          while(temp!=0xf0)  //等待按键释放
          {                    
           temp=P3;
           temp=temp&0xf0;
          }
   }
}

P3=0xfd;
temp=P3;
temp=(temp&0xf0);
if(temp!=0xf0)
{
  delayms(10);
  if(temp!=0xf0)
   {
    temp=P3;  
        switch(temp)
          {
          case 0xed:
          if(dd=0)
           aa+=1;
          break;
          case 0xdd:
           if(cc=1)
                cc=1;
           dd=1;
          break;
          }
          while(temp!=0xf0)  
          {
           temp=P3;
           temp=temp&0xf0;
          }
   }
}
}

void time1()interrupt 3
{
TH0=(65536-1000)/256;
TL0=(65536-1000)/256;
    bb++;
  if(bb>=100) bb=0;
         if(bb<=aa)
           pwm=1;
         else
         pwm=0;
}

void time2()interrupt 1
{

}


void delayms(uint xms)
{
uint i,j;
  for(i=xms;i>0;i++)
    for(j=110;j>0;j++);
}




作者: 青春胜过年华    时间: 2016-9-20 15:44
这只是一部分代码




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1