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

📄 main0502b.c

📁 是用于开关磁阻电机的位置伺服控制的C语言源代码,基于CCS的开发环境.希望可以对你有所帮助.
💻 C
📖 第 1 页 / 共 2 页
字号:
 	 case 0x0e: column=4; 
 	            break;
 	 case 0x0d: column=3;
 	 			break;
 	 case 0x0b: column=2;
 	 			break;
     case 0x07: column=1;
     			break;
     default  : column=17;
    }
 scan_out=0x0f7;
 port0=scan_out;            /* scan keyboard */
 for(i=0;i<1000;i++);
 scan_in=port5; 
 while ((scan_in & 0x0f)==0x0f)
 	{
 	 scan_out=scan_out>>1;
 	 port0=scan_out;
 	 for(i=0;i<1000;i++);  
 	 scan_in=port5;
 	 row--;
 	 if (row==0)
 	 	{
 	 	 row=17;
 	 	 break;
 	 	}
 	}
 key_NO=4*(row-1)+column; 
 port0=0x00;
 x2=port5;
 while ((x2&0x0f)!=0x0f)
 	{
 	x2=port5;
 	port0=0x00;
 	}
 for(i=0;i<5000;i++);
 x2=port5;
 while ((x2&0x0f)!=0x0f)
 	{
 	x2=port5;
 	port0=0x00;
 	} 
 switch(key_NO)
 	{  
     case 1  :
     case 2  :
     case 3  :
     case 4  :
     case 5  : 
     case 6  :
     case 7  : 
     case 8  :
     case 9  :
     case 10 :  if (input_state==FALSE)                 /* deal with data keyboard */
                	break;
                if (dot_state)       
                	input_dotbit--;
                input_value=input_value*10+key_NO-1;
                inputdata_num++;             
                break;
     
     case 11 : if (input_state==FALSE)                  /* deal with radix point keyboard */
     			   break;
     		   if (dot_state)
     		   	   break;
     		   if (inputdata_num!=0)
     		      {
     		       input_dotbit=3; 
     		       dot_state=TRUE;
     		       break;
     		      }    		      
     		       
     case 12 : if (sign_state)                         /* deal with sign keyboard */
               	   sign_state=FALSE;
               else
                   sign_state=TRUE;
               break;  
               
               /***********************/
               /* code for denote the reverse state */
               /***********************/                 
                                
     case 13 : disable_interrupts();
               init_SRM();
               input_state=TRUE;
               /*  Enable timer 1 period  interrupts */
               IFR  = 0xffff;                      /* Clear pending interrupts */
               IFRA = 0x7fff;
               IFRB = 0xffff;
               IMR  = 0x0022;                      /* Enable CPU Interrupts:INT 2,3,6 */
               IMRA = 0x0080;                      /* Enable timer 1 period  interrupts */
               IMRB = 0x0;                         /* Enable timer 3 period */
               IMRC = 0x0;
               asm(" CLRC INTM");   /* Global interrupt enable */           
               PBDATDIR = 0x0701; 	  /* outputs IOPA0–IOPA2, IOPA0 set high */
               break;
     case 14 : display_order++;
               if (display_order==4)
                   display_order=1;
     		   break;
     case 15 : disable_interrupts(); 
               display_order=1;
               enable_interrupts();
               drive_SRM();
               start_state=TRUE; 
               break;
     case 16 :                    
               /* stop SRM */ 
               /*IMRB = 0x0;            /* Disable timer 3 period */
               /*IMRC = 0x0;            /* Disnable CAP1~3 interrupts*/
               disable_interrupts();
               eventmgr_init();
               init_SRM();
               /*  Enable timer 1 period  interrupts */
               IFR  = 0xffff;                      /* Clear pending interrupts */
               IFRA = 0x7fff;
               IFRB = 0xffff;
               IMR  = 0x0022;                      /* Enable CPU Interrupts:INT 2,3,6 */
               IMRA = 0x0080;                      /* Enable timer 1 period  interrupts */
               IMRB = 0x0;                         /* Enable timer 3 period */
               IMRC = 0x0;
               asm(" CLRC INTM");   /* Global interrupt enable */           
               PBDATDIR = 0x0703; 	  /* outputs IOPA0–IOPA2, IOPA0 set high */
               start_state=FALSE;
               revert_state=FALSE;
               break;
 	 default : break;               
 	}
 }	
}  
/*************************************/

/*********************************************/ 
/* ISR for A/D and keyboard interrupt */
/* ISR for A/D and keyboard interrupt */
void c_int6() 
{
 unsigned int ADCfifo_data;
 int ADCfifo_status;
 IFR=0x0020;             			   /* Clear interrupt flags */
 ADCTRL1=0xc100;
 do {
	ADCfifo_data= ADCFIFO1;                /* read value */
	ADCfifo_status = ADCTRL2 & 0x0c0;   /* read status register, mask bits */
	} 
 while (ADCfifo_status != 0);
 if (ADCfifo_data>=IA_max)
 	{
 	if (ADC_channel==2)
 		PADATDIR=PADATDIR & 0x070e;
 	else if (ADC_channel==3)
 		PADATDIR=PADATDIR & 0x070d;
 	else if (ADC_channel==4)
 		PADATDIR=PADATDIR & 0x070b; 
 	} 
 else if (ADCfifo_data<=IA_min)
	{
 	if (ADC_channel==2)
 		PADATDIR=PADATDIR | 0x01;
 	else if (ADC_channel==3)
 		PADATDIR=PADATDIR | 0x02;
 	else if (ADC_channel==4)
 		PADATDIR=PADATDIR | 0x04; 
 	}  
 ADC_channel++;
 if (ADC_channel>4)
 	ADC_channel=2;
 IFRB=0x0ff;
 ADCTRL1=0xca00+(ADC_channel<<1);
}

/* 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];
 unsigned int x,port1_value; 
 port1_value=0; 
 if (display_value>=2000)
 	port1_value=port1_value|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_value=port1_value|0x02; 
 if (sign_state)
 	port1_value=port1_value|0x01;  
 x=dot_bit;
 if ((x>=1)&&(x<=3)) 
 	port1_value=port1_value|(1<<(x+1));     /* show radix point */
 port1=port1_value; 
 if ((dot_bit!=1)&&(dot_bit!=2)&&(bit_value[0]==0)&&(bit_value[1]==0))
 	port2=0x0f; 
 else
    port2=bit_value[1];
 if (((dot_bit==3)&&(bit_value[1]==0)&&(bit_value[0]==0))||((dot_bit==4)&&(bit_value[0]==0)&&(bit_value[1]==0)&&(bit_value[2]==0)))
 	port3=0x0f; 
 else
    port3=bit_value[2];
 port4=bit_value[3];
                  
}
/* refresh LCD */
void LCD_refresh() 
{
 unsigned long velocity,delta_count;
 int i;
 struct CAP_INT * p1; 
 if  (display_times>=1000)   
 	{
 	 display_times=0;
 	 switch (display_order)
 	 {
 	  case 1: /* read velocity */
 	 		  delta_count=0;
 	 		  for(i=1;i<=3;i++)
 	 		      {
 	 		       p1=CAP_p;
 	 		       delta_count=delta_count+p1->delta_count;
 	 		       p1=p1->next;
 	 		      } 
 	 		  if (delta_count==0)
 			      velocity=0;   
 			  else
			      velocity=VELOCITY_CONST*30/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; 
 	 }
	}		
}
/* ***************************************************************** */
/* The main program  
/* ***************************************************************** */

main()
{
 unsigned int INT1_status;
 disable_interrupts();
 dsp_setup();
 eventmgr_init();
 init_SRM();
 /*  Enable timer 1 period  interrupts */
 XINT1CR=0x0007;
 IFR  = 0xffff;                      /* Clear pending interrupts */
 IFRA = 0x7fff;
 IFRB = 0xffff;
 IMR  = 0x0022;                      /* Enable CPU Interrupts:INT 2,3,6 */
 IMRA = 0x0080;                      /* Enable timer 1 period  interrupts */
 IMRB = 0x0;                         /* Enable timer 3 period */
 IMRC = 0x0;
 asm(" CLRC INTM");   /* Global interrupt enable */           
 for(;;)
	{	
	port0=0x00;
 	INT1_status=XINT1CR; 
 	if (INT1_status & 0x8000)
 		{   
 		XINT1CR=0x0007;
 		keyboard_ISR();
 		}
 	if (CAP_A & CAP_B & CAP_C)
 		{
    	 ACTR=0x0fff;
         COMCON=0x4a57;
		}
	LCD_refresh();	
	}
}

⌨️ 快捷键说明

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