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

📄 sci.c

📁 是用于开关磁阻电机的位置伺服控制的C语言源代码,基于CCS的开发环境.希望可以对你有所帮助.
💻 C
📖 第 1 页 / 共 2 页
字号:
 	 PADATDIR=0x0701;
 	 key_state=1;
 	}
 }  
/*************************************/
/* ISR for A/D and keyboard interrupt */
void c_int6() 
{
 unsigned int sysint_vector;
 IFR=0x0020;             			   /* Clear interrupt flags */
 sysint_vector=SYSIVR;   			   /* read interrupt vector address offset */
 if (sysint_vector=0x0001)        /* if keyboard(XINT1) interrupt */
 	{
 	 XINT1CR=XINT1CR | 0x8000; 
 	 keyboard_ISR(); 
 	} 
}


/* initiating parameter and register */
void init_SRM()
{
 velocity_value=0;
 velocity_dotbit=4;
 position_value=position=0;
 position_dotbit=4;
 input_value=0; 
 input_dotbit=4;
 display_times=0;
 TX_times=0;
 display_order=1;
 sign_state=1;
 key_state=0;
}  
/* initialize SCI */
 void SCI_init() 
{SCICCR=0x0017;                 /* 1 stop bit,no odd check,8 data bits,
                                   asynchronism mode,idle line */
 SCICTL1=0x0013;                /* inner clock,enable recive and transmit,disable 
                                   receive erro int ,sleep and woke */
 SCICTL2=0x0002;                /* enable recive int and disable transmit int */
 SCIHBAUD=0x0000;     
 SCILBAUD=0x0040;               /* Baud rate=19200 kbps */
 SCIPC2=0x0022;                 /* enable recive port and transmit port */
 SCICTL1=0x0033;                /* let SCI return from reset state */
 IFR=0xFFFF;                    /* clear all pending int flags */
 IMR=0x0001;                    /* unmask int level INT1 */
                                /* enable all unmasked int */
}
/*******************************************************************/
void disable_interrupts()
{
 asm(" SETC INTM");
}

/************************************************************************/
void enable_interrupts() 
{
 IFR  = 0xffff;       /* Clear pending interrupts */
 IFRA = 0xffff;
 IFRB = 0xffff;
 IFRC = 0xffff;
 IMR  = 0x000e;       /* Enable CPU Interrupts:INT 2,3,4 */
 IMRA = 0x0080;       /* Enable timer 1 period  interrupts */
 IMRB = 0x0010;       /* Enable temer 3 period interrupts */
 IMRC = 0x0001;       /* Enable CAP1 interrupts*/
 asm(" CLRC INTM");   /* Global interrupt enable */
}

/* display data on LCD */
void display(display_value,dot_bit)
unsigned int display_value;       	   /* the value want to be displayed */
int dot_bit;                      	   /* dot bit */
{
 unsigned int bit_value[4];
 int x; 
 port1=0;
 if (display_value>=2000)
 	port1=port1|0x40; 
 bit_value[3]=display_value%10;        /* the lowest bit data */
 bit_value[2]=(display_value/10)%10;
 bit_value[1]=(display_value/100)%10; 
 bit_value[0]=display_value/1000;      /* the highest bit data */
 if (bit_value[0]==1)
 	port1=port1|0x02; 
 if (sign_state)
 	port1=port1|0x01;
 x=dot_bit;
 if (x!=4) 
 	port1=port1|(1<<(x+1));               /* show radix point */
 port2=bit_value[1];
 port3=bit_value[2];
 port4=bit_value[3];
 if ((dot_bit==3)&&(bit_value[1]==0)&&(bit_value[0]==0))
 	port3=0x0f;
                  
                  
}
 
/* ***************************************************************** */
/* The main program  
/* ***************************************************************** */

main()
{
 unsigned int TX_data;
 unsigned int RX_data;
 long velocity;
 int i;
 unsigned long delta_count;
 unsigned int MSB_count;
 unsigned int LSB_count;
 struct CAP_INT * p1;
 disable_interrupts();
 dsp_setup(); 
 init_SRM();
 eventmgr_init(); 
 enable_interrupts(); 
 SCI_init();
 for(;;)
	{
	 if (SCIRXST & 0x40)            /* data is received */
	 	{
	 	if (SCIRXST & 0x80)         /* error? */
	 		{
	 		for(;(SCICTL2 & 0x80)==0;);
            TX_data='e';                 /* transmit "err" */
            SCITXBUF=TX_data;
            for(;(SCICTL2 & 0x80)==0;);
            TX_data='r';               
            SCITXBUF=TX_data;
            for(;(SCICTL2 & 0x80)==0;);
            TX_data='r';               
            SCITXBUF=TX_data; 
	 		if (SCIRXST & 0x04)     /* if parity error */  
	 			{
	 			 for(;(SCICTL2 & 0x80)==0;);
                 TX_data='p';               
                 SCITXBUF=TX_data; 
	 			}       
	 		if (SCIRXST & 0x08)     /* if overtime error */  
	 			{
	 			 for(;(SCICTL2 & 0x80)==0;);
                 TX_data='o';               
                 SCITXBUF=TX_data; 
	 			}
	 		if (SCIRXST & 0x10)     /* if frame error */  
	 			{
	 			 for(;(SCICTL2 & 0x80)==0;);
                 TX_data='f';               
                 SCITXBUF=TX_data; 
	 			}
	 		else                   /* break error */  
	 			{
	 			 for(;(SCICTL2 & 0x80)==0;);
                 TX_data='b';               
                 SCITXBUF=TX_data; 
	 			}
	 		SCICTL2=0x33;
	 		}
	 	else
	 		{
	 		RX_data=SCIRXBUF;
	 		switch(RX_data)
	 			{
	 			case 'D': drive_SRM;                   /* drive SRM , clockwise */ 
	 			          RXdata_state=TRUE;
	 			          sign_state=FALSE;
	 			          break;
	 			case 'd': drive_SRM;                   /* drive SRM , anti-clockwise */ 
	 			          RXdata_state=TRUE;
	 			          sign_state=TRUE;
	 			          break;           
	 			case 'S': stop_SRM;                    /* stop SRM */
	 			          RXdata_state=FALSE;          
	 			          break;                   
	 			case 'C': PADATDIR=0x0701;             /* correct SRM */
	 			          RXdata_state=FALSE;          
	 			          break;     
	 			case 'R': for(;(SCICTL2 & 0x80)==0;); /* transmit data again */
                          SCITXBUF=TX_data; 
                          break;
                default : for(;(SCICTL2 & 0x80)==0;); /* unknown error */
                          TX_data='e';     
                          SCITXBUF=TX_data;
                          for(;(SCICTL2 & 0x80)==0;);
                          TX_data='r';     
                          SCITXBUF=TX_data;
                          for(;(SCICTL2 & 0x80)==0;);
                          TX_data='r';  
                          SCITXBUF=TX_data;
                          for(;(SCICTL2 & 0x80)==0;);
                          TX_data='u';
                          SCITXBUF=TX_data;
	 			}
	 		}
	 	}  
	if (TX_times>=50) 	     /* 10 ms */
		{
		TX_times=0;
		delta_count=0;
 	    for(i=1;i<=6;i++)
 	 		{
 	 		 p1=CAP_p;
 	 		 delta_count=delta_count+p1->delta_count;
 	 		 p1=p1->next;
 	 		}
 	 	delta_count=delta_count/6;
 	 	LSB_count=delta_count & 0x0f;
 	 	MSB_count=delta_count>>8;
 	 	for(;(SCICTL2 & 0x80)==0;);
        TX_data=MSB_count;                 /* transmit speed MSB */
        SCITXBUF=TX_data; 	 			
		for(;(SCICTL2 & 0x80)==0;);
        TX_data=LSB_count;                 /* transmit speed LSB */
        SCITXBUF=TX_data;         
		}
	if (display_times>=1000)               /* 200ms */
 		{
 	 	 display_times=0;
 	 	 switch(display_order)
 	 		{
 	 		 case 1: /* read velocity */
 	 		         if (delta_count==0)
 			         	velocity=0;   
 			         else
			         	velocity=VELOCITY_CONST*10/delta_count;
 	 		 		 velocity_value=(unsigned int)velocity;
 					 if (velocity_value%10)
					 	{
					 	velocity_value=velocity;
					 	velocity_dotbit=3;
						if (velocity_value>2000)
							{
							velocity_value=velocity/10;
							velocity_dotbit=4;
							}
						 }
					 else
						 {
						 velocity_value=velocity/10;
						 velocity_dotbit=4;
						 } 
	 			 	 display(velocity_value,velocity_dotbit);     
	 			 	 break;
 	 		 case 2: /* read position */
					 position_value=position;
 					 if (position_value%10)
					 	{
					 	position_value=position;
					 	position_dotbit=3;
					 	if (position_value>2000)
							{
							position_value=position/10;
							position_dotbit=4;
							}
					 	}
					 else
					 	{
					 	position_value=position;
					 	position_dotbit=4;
					 	}                  
	 		 		 display(position_value,position_dotbit); break; 
		     case 3: display(input_value,input_dotbit); break;
		     default: break; 
 		    }
		}	
	}
	           
}


⌨️ 快捷键说明

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