⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 luyaji.c

📁 电动机控制程序
💻 C
字号:
//--------------------------------------------------------------
//	Filename :  PLC8I6O.C
//	Language :  C for AT89C2051
//	Revision :  1.1
//  Initial Date :  2003/12/16

#include <AT892051.h>

#define uchar  unsigned char

#define keyPressed   0
#define keyReleased  1
#define ON           0
#define OFF          1
#define True		 1
#define False		 0

//signal input/output
//sbit _tmrCounting      = P3^3;   // Timer counting signal 
sbit _TR1Counting      = P3^4;    // TR1 counting signal
sbit _TR2Counting      = P3^3;   // TR2 counting signal
sbit _sOnceReachBottom = P3^0;	
sbit _stoothMatched    = P3^1;
sbit _sinserted		   = P1^0;
sbit _sEmergencyStop   = P1^3;	//Emergency stop

sbit _ctrlBUZZER       = P1^7;
sbit _uPControl        = P1^2;

//extern func. declaration
extern void		Init_Sys_Timer(void);
extern void		msDelay(unsigned int timer_count);
extern void     Enable_Timer(char timer_name, unsigned int timer_count);
extern void		Disable_Timer(char timer_name);
extern uchar 	STimerStillCounting(uchar idxStimer);

// Func declaration
//uchar _detectTimerCounting(void);
uchar _TimerCounting(char timer);

//------------------------------------------------------------
void main(void)
{       
  bit fInsertedAlready, fOnceReachBottom;
  bit fStopRolling;
  unsigned char stage;

  Init_Sys_Timer();
  msDelay(500);		// delay for 500ms
  stage = 0;

  while (1) {	// endless loop
    switch ( stage ) {
	  case 0: while ( _TimerCounting(2)); //wait until timer2 counting start                       
			  msDelay(300);
			  _uPControl  = OFF;
	  		  fInsertedAlready = False;
	 		  fOnceReachBottom = False;
	 		  fStopRolling = False;

	 		  while ( _TimerCounting(1) ) { //wait until timer1 start counting
			  	if ( !_sinserted )
				 	fInsertedAlready = True;
			  	if ( !_sOnceReachBottom)
				 	fOnceReachBottom = True;
				if ( !_sEmergencyStop)
					break;							
	 		  } // end while
 			  stage = 1;
			  break;	
	   case 1: //After 4.5s elapsed
	 		   if ( fInsertedAlready && fOnceReachBottom ) {
					if ( _stoothMatched) //still not match 			
			   			fStopRolling = True;	 
	 		   } else if ( fInsertedAlready )
				    fStopRolling = True;
 			   if ( fStopRolling ) { //stop rolling here
					_uPControl  = ON;
					_ctrlBUZZER = ON;
					stage = 2;
			   } else {
			   		_uPControl  = OFF;
					msDelay(1000);
					stage = 0;
			   } // end else
			   if ( !_sEmergencyStop) {
			   		msDelay(500);
					stage = 0;	
			   } // end if
			   break;
	   case 2: // got problem -- buzzer on and wait 
	   		   stage = 3;
			   if ( !fOnceReachBottom)
			   		while ( _sOnceReachBottom)
						if ( !_sEmergencyStop ) {
							msDelay(500);
							stage = 0;
							break;
						} // end if		   		
			   break;
	   case 3: // wait until tooth matched	 	
			   while ( _stoothMatched && _sEmergencyStop);//wait until tooth matched
			   _uPControl = OFF;		//stop control
			   _ctrlBUZZER = OFF;
			   msDelay(100);
			   stage = 0;
			   break;
	   default: stage = 0;
				break;		 
     } // end switch
  } // end while	
  
} // end main

//**************************************************/
uchar _TimerCounting(char timer)
{     // Return code: <>0 --- Not Counting
      //			  = 0 --- counting 
   switch(timer){
           case 2: if (_TR2Counting)
                    	return ( 0x01 );
                   msDelay(10);
                   return ( _TR2Counting );
           		   break;
           case 1: if (_TR1Counting )
                     	return ( 0x01 );                     
                   msDelay(10);
                   return ( _TR1Counting );
           		   break;           
   }//end switch
} // end func.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -