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

📄 main.c

📁 F206串口收发程序,内含16进制和字符数据的相互转换
💻 C
字号:
asm("icr     .set   0ffech ");
asm("pmst    .set   0ffe4h "); 
asm("sdtr    .set   0fff0h ");
asm("adtr    .set   0fff4h");
asm("aspcr   .set   0fff5h");
asm("iosr    .set   0fff6h");
asm("brd     .set   0fff7h");
asm("ifr     .set   0006h");
asm("imr     .set   0004h"); 
asm("wsgr    .set   0fffch "); 

volatile unsigned int *imr = (volatile unsigned int * )0x0004;
volatile unsigned int *greg = (volatile unsigned int * )0x0005;
volatile unsigned int *ifr = (volatile unsigned int * )0x0006;

ioport unsigned port0ffe4;   /*PMST*/
#define pmst port0ffe4

ioport unsigned port0fff7;    /*BRD*/  /*identify capslock mode*/
#define brd port0fff7

ioport unsigned port0fff6;    /*iosr*/
#define iosr port0fff6

ioport unsigned port0fff5;    /*aspcr*/
#define aspcr port0fff5         

ioport unsigned port0ffe8;     /*clk*/
#define clk port0ffe8
                                      
ioport unsigned port0ffec;     /*icr*/
#define icr port0ffec                                      

ioport unsigned port0fffc;	/*wsgr*/
#define wsgr port0fffc;

ioport unsigned port0fff4;      /*adtr*/
#define adtr port0fff4;
/**************************************************************/

unsigned int sendbuff[22]={0};   /*串口发送缓冲区*/   

unsigned int answer[22]={0};

unsigned int recbuff[6]={0};

unsigned int recdata[4]={0};

unsigned int sendready[4]={0};

unsigned int senddata[1]={0};

unsigned int rdpoint=0;

unsigned int sdpoint=0;

unsigned int sendnum=0;

unsigned int sendenable=4;

unsigned int ready=0;

unsigned int recing=0;

unsigned int recnum=0; 

unsigned int sendanswer=0; 

unsigned int sendansweragain=1;

static unsigned int recach[4][4]={{0,0}}; 
static unsigned int recach4[4][4]={{0,0}}; 
static unsigned int recach3[3][4]={{0,0}};

void proA();
void proB();
void proAnswer();
void hexascii(unsigned int *a,unsigned int b[4] );
void asciihex(unsigned int b[4],unsigned int *a );

/**************************************************************/ 
void asciihex(unsigned int b[4],unsigned int *a )
 {char i,ch; 
  *a=0;  
 for (i=0;i<4;i++)
  { ch=b[i]; 
    if( ((ch>='0')&& (ch<='9')) || ((ch>='A')&&(ch<='F')) )
	{
	
   switch(ch){
  case 0x30: ch=0; break;
  case 0x31: ch=1; break;
  case 0x32: ch=2; break;
  case 0x33: ch=3; break;
  case 0x34: ch=4; break;
  case 0x35: ch=5; break;
  case 0x36: ch=6; break;
  case 0x37: ch=7; break;
  case 0x38: ch=8; break;
  case 0x39: ch=9; break;
  case 0x41: ch=10; break;
  case 0x42: ch=11; break;
  case 0x43: ch=12; break;
  case 0x44: ch=13; break;
  case 0x45: ch=14; break;
  case 0x46: ch=15; break;
               } ;
 *a=*a+(ch<<(3-i)*4); 
    }
       
    else *a=0;   
    }
}

void hexascii(unsigned int *a,unsigned int b[4] )	
{                               
unsigned char ch;     
unsigned int i;
	for (i=0;i<4;i++)
	{ ch= *a>>((3-i)*4);
		ch=ch & 0x0f;
    switch(ch){
  case 0: ch=0x30; break;
  case 1: ch=0x31; break;
  case 2: ch=0x32; break;
  case 3: ch=0x33; break;
  case 4: ch=0x34; break;
  case 5: ch=0x35; break;
  case 6: ch=0x36; break;
  case 7: ch=0x37; break;
  case 8: ch=0x38; break;
  case 9: ch=0x39; break;
  case 10: ch=0x41; break;
  case 11: ch=0x42; break;
  case 12: ch=0x43; break;
  case 13: ch=0x44; break;
  case 14: ch=0x45; break;
  case 15: ch=0x46; break;
           } ;
		b[i]=ch;
	}
}  

void sciinitial()
{	
    	aspcr = 0xe08f;   /* rec interrupt   send  only = 0xe10fh*/
	
	iosr = 0x0ff0;
	
	brd = 0x0082;

}

void proA()
{int i;
	asciihex(recdata,senddata);
  	                       
	senddata[0]=senddata[0]-4369;   /*senddata-'1111'*/
  	
	hexascii(senddata,sendready);	
	
	aspcr=0x0e18f;
	  for(i=0,sendnum=0;i<4;i++,sendnum++)
		{sendbuff[sendnum]=sendready[i];
	 
		}
		sendnum=0,sendenable=4;	
 	port0fff4=sendbuff[sendnum];/*adtr*/ 
        	sendnum++;
        	sendenable--; 
  return;              
}

void proB()
{ int i;
	asciihex(recdata,senddata);
  	                       
	senddata[0]=senddata[0]+4369;   /*senddata+'1111'*/
  	
	hexascii(senddata,sendready);	
	
	aspcr=0x0e18f;
	  for(i=0,sendnum=0;i<4;i++,sendnum++)
		{sendbuff[sendnum]=sendready[i];
	 
		}
		sendnum=0,sendenable=4;	
 	port0fff4=sendbuff[sendnum];/*adtr*/ 
        	sendnum++;
        	sendenable--; 
  return;              
}               

void proAnswer()
{int k;
 
 sendenable=22;
 answer[0]='Y';  answer[1]='o';      answer[2]='u';    answer[3]='r';
 answer[4]=' ';      answer[5]='i';      answer[6]='n';    answer[7]='p';
 answer[8]='u';       answer[9]='t';      answer[10]=' ';  answer[11]='i';
 answer[12]='s';      answer[13]=' ';     answer[14]='i';      
 answer[15]='n';           answer[16]='v';      answer[17]='a';     
 answer[18]='l';      answer[19]='i';      answer[20]='d';  answer[21]='!';
 for(k=0,sendnum=0;k<22;k++,sendnum++)
 {sendbuff[sendnum]=answer[k];}
  sendnum=0;
 aspcr=0x0e10f;sendanswer=1;
 port0fff4=sendbuff[sendnum];
 sendnum++;
 sendenable--;
 return;
}
 
/*************************************************************************/
main()
{	int i;
	
	asm(" setc intm ");                          /*清除所有中断*/
  	asm(" clrc cnf");                            /*B0分配到数据空间*/ 
    	asm(" LDP #0H");
    
    	pmst=0x04;             /*PON=1.DON=0*/
    	clk=0x00;              /*CLKOUT1 enable*/    
	icr=0x00;              /*REST value*/
     	asm(" splk  #0e40h,64h");        
     	asm(" nop");
     	asm(" out  64h,wsgr");                      /*等待状态产生器控制寄存器*/              
    	
    	*greg = 0x0000;              /*all memory in local memory*/
	*ifr = 0xffff;
    	
	sciinitial(); 
    	asm(" clrc INTM");
     	*imr = 0x0026;  /*open TXRXINT TINT INT2/3 interrupt*/  
	


        asm(" splk #(_sendbuff),63h");
       
	while(1)
	
	{}
}


/***************************************************************************/
void interrupt recive()
{ static  int judgeDR;
  static int judgeTHRE; 
  int i;
  
  judgeDR=(iosr&0x0100);  /*judgeTHRE=(iosr&0x0800);  */


if(sendanswer==1)
{ aspcr=0x0e10f;
	if(sendenable!=0)
 		{     
 		port0fff4=sendbuff[sendnum];/*adtr*/ 
        sendnum++;
        sendenable--; 
        aspcr=0xe10f;  
           goto end;
	}        
       else
       {sendanswer=0; 
       sendansweragain=0; 
       aspcr=0x0e08f  ;    
       goto end ;                   
       } 
}  
 
else 
{if (judgeDR==0x0000)                /*THRE=1*/
 {      aspcr=0x0e10f;
	if(sendenable!=0)
 		{     
 		port0fff4=sendbuff[sendnum];/*adtr*/ 
        sendnum++;
        sendenable--; 
        aspcr=0xe10f;
        }        
       else
       {  
       aspcr=0x0e08f  ;    
       *ifr=0x0020 ;                   
       }
 }
 
   
else

 {	if (recing==1)
 		{
		recbuff[recnum]=adtr;/*adtr*/   
 		recbuff[recnum]=recbuff[recnum]&0x00ff;
 		recdata[rdpoint]=recbuff[recnum];
		rdpoint++;
		if(recbuff[recnum]=='&')
 				{recing=0;recnum=0;rdpoint=0;
				goto end;}
			else
                		{recnum++;
 		 		goto end;} 
		} 
 		 
 		
 	else 
		{
		if(ready==1)
			{ ready=0;
			recbuff[recnum]=adtr;/*adtr*/   
 			recbuff[recnum]=recbuff[recnum]&0x00ff;
 			if(recbuff[recnum]=='A')
				{
				 proA();
				}
			else if(recbuff[recnum]=='B')
				{
				 proB();
				}
			else if(recbuff[recnum]!='#')
				{ aspcr=0x0e00f;
				 proAnswer();
			
				 }
			}
		else
			{
			 recbuff[recnum]=adtr;
			 recbuff[recnum]=recbuff[recnum]&0x00ff;
			 if(recbuff[recnum]=='#')
				{
				 ready=1;
				 recnum=0;
				 goto end;
				}
			 else if(recbuff[recnum]=='$')
				{
				 recing=1;
				 recnum=0;
				 goto end;		
				}
			 else
			    {aspcr=0x0e00f;
			    proAnswer();}
			
			 }
        }
}
}
end: 
 	*ifr=0x0020 ;                   
 	return;
   
} 

⌨️ 快捷键说明

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