烟台培训学校
烟台建筑培训
您当前的位置:主页 > 机械电子 > 机电资讯 >

烟台西门子plc培训讲解独立式键盘控制步进电机实验

时间:2016-05-27 15:31 来源:未知 点击:
#includereg51.h //包含51单片机寄存器定义的头文件 sbit S1=P1^4; //将S1位定义为P1.4引脚 sbit S2=P1^5; //将S2位定义为P1.5引脚 sbit S3=P1^6; //将S3位定义为P1.6引脚 unsigned char keyval; //储存按键值 unsign
#include<reg51.h>       //包含51单片机寄存器定义的头文件
sbit S1=P1^4;           //将S1位定义为P1.4引脚
sbit S2=P1^5;           //将S2位定义为P1.5引脚
sbit S3=P1^6;           //将S3位定义为P1.6引脚
unsigned char keyval;   //储存按键值
unsigned char ID;       //储存功能标号
/                                                
函数功能:软件消抖延时(约50ms)
                                                  /
void delay(void)
 {
   unsigned char i,j;
       for(i=0;i<150;i++)
         for(j=0;j<100;j++)
           ;
 }
/                                               
函数功能:步进电机转动延时,延时越长,转速越慢
                                                 /
void motor_delay(void)    
{
   unsigned int i;
    for(i=0;i<2000;i++)
         ;
}
/                                                
函数功能:步进电机正转
                                                 /
void forward( )
  {
          P0=0xfc;           //P0口低四位脉冲1100
              motor_delay();  
              P0=0xf6;           //P0口低四位脉冲0110
          motor_delay();
              P0=0xf3;           //P0口低四位脉冲0011
          motor_delay();
              P0=0xf9;          //P0口低四位脉冲1001
          motor_delay();
  }
/                                               
函数功能:步进电机反转
                                                 /
void backward()
  {
          P0=0xfc;            //P0口低四位脉冲1100
              motor_delay();
              P0=0xf9;           //P0口低四位脉冲1001
              motor_delay();
              P0=0xf3;           //P0口低四位脉冲0011
              motor_delay();
              P0=0xf6;           //P0口低四位脉冲0110
              motor_delay();
  }
/                                               
函数功能:步进电机停转
                                                 /
void stop(void)
{
      P0=0xff ;          //停止输出脉冲
}
/                                                
函数功能:主函数
                                                  /
void main(void)          
{
  TMOD=0x01;               //使用定时器T0的模式1
  EA=1;                    //开总中断
  ET0=1;                   //定时器T0中断允许
  TR0=1;                   //启动定时器T0
 TH0=(65536-500)/256;      //定时器T0赋初值,每计数200次(217微秒)发送一次中断请求
 TL0=(65536-500)%256;      //定时器T0赋初值
  keyval=0;                //按键值初始化为0,什么也不做
  ID=0;
    while(1)
          {                
                     switch(keyval)           //根据按键值keyval选择待执行的功能
                       {
                              case 1:forward();   //按键S1按下,正转
                                       break;
                              case 2:backward();  //按键S2按下 ,反转
                                       break;
                              case 3:stop();     //按键S3按下,停转
                                       break;                                
                             }               
        }
}
/                                                
函数功能:定时器T0的中断服务子程序
                                                  /
void Time0_serve(void) interrupt 1 using 1
{
   TR0=0;                       //关闭定时器T0
   if((P1&0xf0)!=0xf0)          //第一次检测到有键按下
         {
                  delay();              //延时一段时间再去检测
                      if((P1&0xf0)!=0xf0)   //确实有键按下
                            {
                               if(S1==0)        //按键S1被按下
                               keyval=1;
                        if(S2==0)        //按键S2被按下
                                    keyval=2;
                                   if(S3==0)        //按键S3被按下
                                    keyval=3;                    
                       }
               }   
  TH0=(65536-200)/256;         //定时器T0的高8位赋初值
  TL0=(65536-200)%256;         //定时器T0的低8位赋初值
  TR0=1;                       //启动定时器T0
}
 

会计开课

更多>>

电脑开课

更多>>

建筑开课

更多>>

外语开课

更多>>

Copyright © 2012烟台邦文教育   版权所有  鲁ICP备13018239号-2
关闭窗口
点击这里给我发消息 邦文-张老师
邦文--李老师
邦文--郭老师