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

📄 push.c

📁 89c58接受PC窜口目标信号
💻 C
字号:
/*prj push*/
/*下料部分的主控程序:
1.通过串口接受目标位置信号及其他控制指令
2.通过外中断0接收来自编码器的步进信号
*/
/*描述:
1.需要飞利浦89p58(32k)
2.11.0592Mhz晶体
3.用于smd型机(qb-s-1)
*/
#include "reg51.h"
#include "stdio.h"
#include "math.h"
#include "spio.h"
#include "inc.h"
#include "hopp.h"
#include "stdlib.h"
#include "memio.h"
#include "intrins.h"

#define INBUF_LEN 16  
unsigned char inbuf1[INBUF_LEN];  //buffer length
bit           read_flag=0;    //find a valid directive , read_flag was set!
unsigned int  Rev_len;   //pointer

/*ToPush*/
/*0 - NewBie*/
unsigned char ToPush[30];
unsigned char b_tb_check = 1;  //check the tb...Oh....


/************************************Initialize**********************************/
/*starting with initialize() is good!                                           */
void initialize()
{
	P0=0xff;
	P1=0xff;
	P2=0xff;
	P3=0xff;

	ToP0(0x00);
	ToP1(0x00);
	ToP2(0x00);
	
	ToPush[0] = 25;
	ToPush[1] = 25;
	ToPush[2] = 25;
	ToPush[3] = 25;
	ToPush[4] = 25;
	ToPush[5] = 25;
	ToPush[6] = 25;
	ToPush[7] = 25;
	ToPush[8] = 25;
	ToPush[9] = 25;
	ToPush[10] = 25;
	ToPush[11] = 25;
	ToPush[12] = 25;
	ToPush[13] = 25;
	ToPush[14] = 25;
	ToPush[15] = 25;
	ToPush[16] = 25;
	ToPush[17] = 25;
	ToPush[18] = 25;
	ToPush[19] = 25;
	ToPush[20] = 25;
	ToPush[21] = 25;
	ToPush[22] = 25;
	ToPush[23] = 25;
	ToPush[24] = 25;
	ToPush[25] = 25;
	ToPush[26] = 25;
	ToPush[27] = 25;
	ToPush[28] = 25;
	ToPush[29] = 25;

	IE    = 0x93;    /*clear all interrupt enable bit*/
	SCON  = 0x50;  		/*serial port in mode 1*/
	PCON |= 0x80;    	/*double the generated band!*/ 
	TMOD  = 0x21;  		/*timer0 mode 1, timer1 mode 2*/
//	ES = 1;	
//	EX0 = 1;
//	ET0 = 1;
	/*ES = 1;   serial port interrupt enabled!*/
	/*ex0 = 1;  external interrupy 0 enabled*/
 	/*ex1 = 0;  external interrupt 1 disabled*/
	/*et0 = 1;  timer 0  enabled!*/
	/*et1 = 0;  timer 1 disabled!*/

	/* if timer1 served the serial port , timer1 should be disabled!"*/
	
	/*ES=1;*/
	
	IT0 = 1;    /*low level triger*/
	IT1 = 1;    /*Low level triger*/
	PS=1;       /*serial port high pro*/  
	
	TH1=0xfd; /*timer1 reload data */
	/*data=0x100-(1000*11.0592)/(384*19.2)=fd
	/* TH for TL to Timer*/
	TR1=1;    /*timer1 trigger!	serial port can receive data*/
	TR0 = 0;  /*timer0 stopped!*/
	led0 = 0;
	led1 = 0;

}

/******************* PUSH service routine ********************/
/**all data was pushed ahead,and the toPush[0] was set to 25 */
void StepIn(void) interrupt 0 using 3
{
	int i = 0;
	unsigned char T[3] = {0,0,0};
	led0 = 0;
	/************************/
	/* if tro on close it*/
	/*Light LED0*/
	
	/*all data was pushed ahead*/
	ToPush[29] = ToPush[28];
	ToPush[28] = ToPush[27];
	ToPush[27] = ToPush[26];
	ToPush[26] = ToPush[25];
	ToPush[25] = ToPush[24];
	ToPush[24] = ToPush[23];
	ToPush[23] = ToPush[22];
	ToPush[22] = ToPush[21];
	ToPush[21] = ToPush[20];
	ToPush[20] = ToPush[19];
	ToPush[19] = ToPush[18];
	ToPush[18] = ToPush[17];
	ToPush[17] = ToPush[16];
	ToPush[16] = ToPush[15];
	ToPush[15] = ToPush[14];
	ToPush[14] = ToPush[13];
	ToPush[13] = ToPush[12];
	ToPush[12] = ToPush[11];
	ToPush[11] = ToPush[10];
	ToPush[10] = ToPush[9];
	ToPush[9] = ToPush[8];
	ToPush[8] = ToPush[7];
	ToPush[7] = ToPush[6];
	ToPush[6] = ToPush[5];
	ToPush[5] = ToPush[4];
	ToPush[4] = ToPush[3];
	ToPush[3] = ToPush[2];
	ToPush[2] = ToPush[1];
	ToPush[1] = ToPush[0];
	
	
	/*toPush[0] was set to 25*/
	/*25 means not available*/
	ToPush[0] = 25;
	
	/* set the data to 0~7,8~15,16~23*/
	/*1-8*/
	for(i=7;i>0;i--)
	{
		if(ToPush[i+4] == i+1)
		{ 				
			T[0] += 1;
		}
		T[0] *= 2;
	}
	
	if(ToPush[i+4] == i+1)
	{ 				
		T[0] += 1;
	}
	/*9-16*/
	for(i=15;i>8;i--)
	{
		if(ToPush[i+4] == i+1)
		{ 				
			T[1] += 1;
		}
		T[1] *= 2;
	}
	if(ToPush[i+4] == i+1)
	{ 				
		T[1] += 1;
	}
	/*17-24*/
	
	for(i=23;i>16;i--)
	{
		if(ToPush[i+4] == i+1)
		{ 				
			T[2] += 1;
		}
		T[2] *= 2;
	}
	
	if(ToPush[i+4] == i+1)
	{ 				
		T[2] += 1;
	}
	/*check tb state before the push*/
	if(tb==0 || b_tb_check == 0)
	{
		ToP0(T[0]);
		ToP1(T[1]);
		ToP2(T[2]);
	}
	else
	{
		err = 0;
		_nop_();
		_nop_();
		err = 1;
	}
	
	/*************************/	
	TH0=0x00;
	TL0=0x00;
	TR0=1;   
	/******start timer0*******/
}


/***************** timer -0 service routine*******************/
/* timer0 will be retrigered untill the time reach 0.6s*******/
/*delay routine that to restore the small boxes*/

void Timer0(void) interrupt 1 using 3
{
	static int counter = 0;
	
	counter++;
	
	if(counter >= 8)
	{
		/*************************/	
		TR0=0;   
		/******End timer0 *******/
		counter = 0;
		ToP0(0);
		ToP1(0);
		ToP2(0);
		led0=1;
		/*shut down LED0*/
	}
} 


/*************the serial port Interrupt*********************/
/********************* U A R T *****************************/
/*serial into the mcu*/

void spi(void) interrupt 4 using 3
{
	if(RI)
	{
		RI = 0;
		inbuf1[Rev_len]=SBUF;
		if(inbuf1[Rev_len]=='\n')
		{
			inbuf1[Rev_len+1] = '\0';
			read_flag=1;
		}
		Rev_len++;
	}
	return;
}	 

/************************* M A I N **************************/   
/*response the serial port interrupt                        */
/*including other interrupts                                */

void main(void)
{   
	char dir;                       /*directive    */
	unsigned char op1,op2;          /*oprand 1 & 2 */
	unsigned char buf[8];           /* buffer temp */
	
	/*used for led flash*/
	/*sounds good!*/
	unsigned int cnt = 0;
	initialize();

	while(1)
	{
		/*if Rev_len >=12,the input command is ineffective*/
		/*clear it*/
		/*fi*/
		
		if(Rev_len > 4)
		{
			//directive over flow, clear them all!
			inbuf1[0] = 0;
			inbuf1[1] = 0;
			inbuf1[2] = 0;
			inbuf1[3] = 0;
			inbuf1[4] = 0;
			inbuf1[5] = 0;
			inbuf1[6] = 0;
			inbuf1[7] = 0;
			inbuf1[8] = 0;
			inbuf1[9] = 0;
			inbuf1[10] = 0;
			inbuf1[11] = 0;
			inbuf1[12] = 0;
			inbuf1[13] = 0;
			inbuf1[14] = 0;
			inbuf1[15] = 0;
			Rev_len = 0;
			read_flag = 0;
		}

		if(read_flag == 1)
		{
			led1 = 0;
			read_flag = 0;
			dir = inbuf1[0];
			op1 = inbuf1[1];
			op2 = inbuf1[2];

			switch(dir)
			{
			case 'r': 	/*read buffer*/
				/*r 12,12*/
				sscanf(&inbuf1[2],"%bu,%bu",&op1,&op2);
				ReadBuf(op1,buf,op2);
				send_string_com(buf,op2);
				break;

			case 'R': /*read from buf*/
				/*R23*/
				ReadBuf(op1,buf,op2); /*op1 = '2';address
										op2 = '3';data*/
				send_string_com(buf,op2);
				break;

			case 'w': /*write to mem:w 12,12,sdfa*/
				/*W 12,12,sadf*/
				sscanf(&inbuf1[2],"%bu,%bu,%s",&op1,&op2,buf);
				WriteBuf(op1,buf,op2);
				break;

			case 'W': /*write to mem:W 12,12,sdfa*/
				/*W11sadf*/
				WriteBuf(op1,inbuf1[3],op2);
				break;

			case 'l': /*Led state 0, 1*/
					  /*L 1,0*/
				sscanf(&inbuf1[2],"%bu,%bu",&op1,&op2);
				if(op1 == 0)
  			    {
					led0 = op2;
				}
				if(op1 == 1)
				{
					led1 = op2;
				}

				break;

			case 'L': 	/*Led state 0, 1*/
						/*4c0001*/
						/*led0 = 1*/
				if(op1 == 0)
  			    {
					led0 = op2;
				}
				if(op1 == 1)
				{
					led1 = op2;
				}
				break;

			case 'd': /*Destination,specify a state of tar*/
				/*d 12,1|0*/
				sscanf(&inbuf1[2],"%bu,%bu",&op1,&op2);
				sethopp(op1,op2);

				break;

			case 'D': /*Destination,specify a state of tar*/
				/*441201*/
				sethopp(op1,op2);
				break;
			case 'p':  /*push with a state*/
				/*p 12*/
				sscanf(&inbuf1[2],"%bu",&op1);
				ToPush[0]=op1;
				break;

			case 'P':  /*push with a state*/
				/*5012*/
				ToPush[0]=op1;
				break;
			case 't':  /*chang the tb(tuoban) Check State*/
				/*T 0*/
				sscanf(&inbuf1[2],"%bu",&op1);
				b_tb_check = op1;
				break;
			case 'T':  /*chang the tb(tuoban) Check State*/
				/*5400*/
				b_tb_check = op1;
				break;
			default:
				/*led1=0;*//*error occured!*/

				break;
			}
			//directive over, clear them all!

			inbuf1[0] = 0;
			inbuf1[1] = 0;
			inbuf1[2] = 0;
			inbuf1[3] = 0;
			inbuf1[4] = 0;
			inbuf1[5] = 0;
			inbuf1[6] = 0;
			inbuf1[7] = 0;
			inbuf1[8] = 0;
			inbuf1[9] = 0;
			inbuf1[10] = 0;
			inbuf1[11] = 0;
			Rev_len=0;
			led1 = 1;

		} //if return
	} //while return
	return;
}//main return 

⌨️ 快捷键说明

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