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

📄 main.c

📁 GPIO-步进电机控制实验 熟悉s3c2410a的GPIO基本功能和设置方法。 掌握步进电机的驱动原理。 掌握如何利用GPIO功能控制步进电机
💻 C
字号:


#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h"
#include "mmu.h"
#include "2410iic.h"


void Isr_Init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
void Led_Display(int data);
void Step_Motor(U16 Motor_delay, S8 rev, U32 n);
void Test_Step_Motor(void);
//如果函数无参数,却又没写void,那么会出现编译warning:Deprecated declaration Test_Step_Motor()-give arg types.


void Step_Motor(U16 Motor_delay, S8 rev, U32 n)
{
	S8 i;
	
	if (rev == 0)
	{
	   for (;n>0;n--)
		{
		  for (i=0; i<12; i++)
		  {	rGPEDAT = 0xcfff; //一步转动7.5度
			Delay(Motor_delay);
			
			rGPEDAT = 0x9fff;
			Delay(Motor_delay);
			
			rGPEDAT = 0x3fff;
			Delay(Motor_delay);
			
			rGPEDAT = 0x6fff;
			Delay(Motor_delay);
		
		   }
		 }
	}
	else
	{
	   for (;n>0;n--)
	    {
		  for (i=0; i<12; i++)
	      {
			rGPEDAT = 0xcfff;
			Delay(Motor_delay);
			
			rGPEDAT = 0x6fff;
			Delay(Motor_delay);
			
			rGPEDAT = 0x3fff;
			Delay(Motor_delay);
			
			rGPEDAT = 0x9fff;
			Delay(Motor_delay);
	      }
	    }
	}
}


void Test_Step_Motor()
{
  S8 flag;
  U16 Motor_delay = 200;
  S8 reverse = 0;
  U32 num;
  
  Uart_Printf("\n Enter '+' to accelerate;\n");
  Uart_Printf("\n Enter '-' to decelerate.\n");
  Uart_Printf("\n Enter 't' to reverse.\n");

  Uart_Printf("\n Please enter parameter:\n");
  flag = Uart_Getch();  // 接收用户通过DNW传入的旋转参数:正转,反转.
  
  while(flag!= '0')  //注意,flag定义为char型,所以此处的零也应该是字符0.
  {
 		 Uart_Printf("\n Parameter entered is : %c \n", flag); //"%c"代表 字符.
 		 
         Uart_Printf("\n Please enter round number ......\n");
 	
 		 num = Uart_GetIntNum();   //note: 在DNW输入旋转的圈数之后,需要按回车才有效.
        
        	 
 	     Uart_Printf("\n rotate %d round \n", num);
 	 	
		   if (flag == 't')
  			{
  				reverse = 1;
  				Step_Motor(Motor_delay, reverse, num);
                reverse = 0;
  			}
  			else
  			{
  				if (flag == '+') Motor_delay -= 5;
  				if (flag == '-') Motor_delay += 5;
  				if (Motor_delay < 150)  Motor_delay = 150;
  				if (Motor_delay >2000) Motor_delay =2000;
  				Step_Motor(Motor_delay, reverse, num);

  			}
        Uart_Printf("\n Please enter parameter:\n");
        flag = Uart_Getch();
  	}
}

//===================================================================
void Main(void)
{

     
    MMU_Init();
   
    ChangeClockDivider(1,1);          // 1:2:4    

    ChangeMPllValue(0xa1,0x3,0x1);    // FCLK=202.8MHz  
    
    Port_Init();
    Isr_Init();

    Uart_Init(0,115200);
    Uart_Select(0);

    Uart_Printf("\n Step Motor TEST\n");
        
	rGPECON=((rGPECON)&(0x00ffffff))|(1<<30)|(1<<28)|(1<<26)|(1<<24);
	rGPEDAT=(rGPEDAT)&0x0fff;
	rGPEUP=(rGPEUP)|0xffff;   //disable the pull_up function
		
	while (1)
	{
    	Uart_Printf("\n S3C2410 test Step_Motor \n");
             
        Test_Step_Motor();
	}
}

//===================================================================
//Active is low.(LED On)
// GPF7  GPF6   GPF5   GPF4
//nLED_1 nLED2 nLED_4 nLED_3
void Led_Display(int data)
{
//    rGPFDAT = (rGPFDAT & 0xf) | !((data & 0xf)<<4);
    rGPFDAT = (rGPFDAT & ~(0xf<<4)) | ((~data & 0xf)<<4);    
}


//===================================================================
void Isr_Init(void)
{
    pISR_UNDEF  = (unsigned)HaltUndef;
    pISR_SWI    = (unsigned)HaltSwi;
    pISR_PABORT = (unsigned)HaltPabort;
    pISR_DABORT = (unsigned)HaltDabort;
    
    rINTMOD     = 0x0;                     //All=IRQ mode
    rINTMSK     = BIT_ALLMSK;              //All interrupt is masked.
    rINTSUBMSK  = BIT_SUB_ALLMSK;          //All sub-interrupt is masked. <- April 01, 2002 SOP

}

//===================================================================
void HaltUndef(void)
{
    Uart_Printf("Undefined instruction exception.\n");
    while(1);
}

//===================================================================
void HaltSwi(void)
{
    Uart_Printf("SWI exception.\n");
    while(1);
}

//===================================================================
void HaltPabort(void)
{
    Uart_Printf("Pabort exception.\n");
    while(1);
}

//===================================================================
void HaltDabort(void)
{
    Uart_Printf("Dabort exception.\n");
    while(1);
}

⌨️ 快捷键说明

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