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

📄 stepper.c

📁 在JX44B0教学实验箱进行步进电机控制的实验 个人觉得很实用
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************/
/*                                                                          */
/* FILE NAME                                      VERSION                   */
/*                                                                          */
/* STEPPER.C                                            1.0                 */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*     JX44B0(S3C44B0X)步进电机实验                                         */
/*                                                                          */
/*                                                                          */
/* DATA STRUCTURES                                                          */
/*                                                                          */
/* FUNCTIONS :                                                              */
/*     在JX44B0教学实验箱进行步进电机控制的实验                             */
/*                                                                          */
/* DEPENDENCIES                                                             */
/*     JX44B0-1                                                             */
/*     JX44B0-2                                                             */
/*     JX44B0-3                                                             */
/*                                                                          */
/*                                                                          */
/* NAME:                                                                    */
/* REMARKS:                                                                 */
/*                                                                          */
/*								Copyright (C) 2003 Wuhan CVTECH CO.,LTD     */
/****************************************************************************/

/****************************************************************************/
/* 学习JX44B0实验箱中的步进电机控制方法:                                   */
/****************************************************************************/

/* 包含文件 */
#include "44b.h"
#include "lcdlib.h"
#include "hzk16.h"
#include "ascii16x8.h"

#define STEP_INTERVALE				0x40
#define STEPER_VALUE_MIN 			0x40
#define STEPER_VALUE_MAX 			0x1000

#define STEPER_MOTOR_ENABLE         1
#define STEPER_MOTOR_DISABLE        0
#define STEPER_MOTOR_CLOCKWISE      0        
#define STEPER_MOTOR_ANTICLOCKWISE  1 

#define true						1
#define false						0

#define STEPER_ENABLE				0x01
#define STEPER_SINGLE				0x02
#define STEPER_REVERSE				0x04

#define MIN_STEPER_PLUSE_FREQ		0x300
#define MAX_STEPER_PLUSE_FREQ		0x100000

#define SET_STEPER_ENABLE(X)						\
{ 													\
	if(X)											\
		steper_control_mask |= (1<<3); 				\
	else 											\
		steper_control_mask &= ~(1<<3);				\
	*(unsigned char *)0x0a000006 = steper_control_mask; \
}
		
#define SET_STEPER_REVERSE(X)						\
{ 													\
	if(X)steper_control_mask |= (1<<2); 			\
	else steper_control_mask &= ~(1<<2);			\
	*(unsigned char *)0x0a000006 = steper_control_mask; \
}

#define GEN_PLUSE_HIGH()			{rPDATC |= (1<<11);}
#define GEN_PLUSE_LOW()				{rPDATC &= (~(1<<11));}

typedef (*ISR_ROUTINE_ENTRY)(void);

char key_get_char(int row, int col);
void init_interrupt_handler(unsigned int irq_handler);
void install_isr_handler(int irq_no, void* irq_routine);

void timer1_isr(void);
void IsrIRQ() __attribute__ ((interrupt("IRQ")));

static int steper_control_mask = 0xf8;

int  step_status;				/* 当前步进电机的工作状态 */
int  step_mode;					/* 模式:单步或循环,正反转 */
int  step_freq;					/* 步进频率 */

/*****************************************************************************
// Function name	: key_get_char
// Description	    : 根据键盘行列键值查询键
// Return type		: char
// Argument         : int row
// Argument         : int col
*****************************************************************************/
char key_get_char(int row, int col)
{
	char key;
	
	switch( row )
	{
	case 0:
		if(col == 0x0b ) key = '0';
		else if(col == 0x0e) key = '7';
		else if(col == 0x07) key = '4';
		else if(col == 0x0d) key = '1';
		break;
	case 1:
		if(col == 0x0b ) key = 'A';
		else if(col == 0x0e) key = '8';
		else if(col == 0x07) key = '5';
		else if(col == 0x0d) key = '2';
		break;
	case 2:
		if(col == 0x0b ) key = 'B';
		else if(col == 0x0e) key = '9';
		else if(col == 0x07) key = '6';
		else if(col == 0x0d) key = '3';
		break;
	case 3:
		if(col == 0x0b ) key = 'C';
		else if(col == 0x0e) key = 'D';
		else if(col == 0x07) key = 'E';
		else if(col == 0x0d) key = 'F';
		break;
	default:
		break;
	}
	return key;
}




/* 数码管显示码表,请参考led实验 */
unsigned char seg7table[16] = 
{
    /* 0       1       2       3       4       5       6      7*/
    0xc0,   0xf9,   0xa4,   0xb0,   0x99,   0x92,   0x82,   0xf8,

    /* 8       9      A        B       C       D       E      F*/
    0x80,   0x90,   0x88,   0x83,   0xc6,   0xa1,   0x86,   0x8e,
};

/*****************************************************************************
// Function name	: lcd_disp_hzk16
// Description	    : 在LCD的(x,y)坐标处以colour颜色显示s中的汉字
// Return type		: void
// Argument         : int x : x坐标
// Argument         : int y : y坐标
// Argument         : char *s : 待显示字符串
// Argument         : int colour : 显示颜色
*****************************************************************************/
void lcd_disp_hzk16(int x,int y,char *s,int colour)
{
	char buffer[32];							/* 32字节的字模缓冲区		*/
 	int i,j,k;
 	unsigned char qh,wh;
 	unsigned long location;

 	while(*s)
  	{
   		qh=*s-0xa0;								/* 计算区码					*/
   		wh=*(s+1)-0xa0;							/* 计算位码					*/
   		location=(94*(qh-1)+(wh-1))*32L;		/* 计算字模在文件中的位置	*/
   		memcpy(buffer, &hzk16[location], 32);	/* 获取汉字字模				*/
		for(i=0;i<16;i++)						/* 每一行					*/
		{
    		for(j=0;j<2;j++)					/* 一行两个字节				*/
			{
     			for(k=0;k<8;k++)				/* 每个字节按位显示			*/
				{
      				if(((buffer[i*2+j]>>(7-k)) & 0x1) != 0)
       					lcd_put_pixel(x+8*(j)+k,y+i,colour); /* 显示一位	*/
				}
			}
		}
   		s+=2;												/* 下一个汉字	*/
   		x+=16;												/* 汉字间距		*/
  	}
}
/*****************************************************************************
// Function name	: lcd_disp_ascii16x8
// Description	    : 在LCD的(x,y)坐标处以colour颜色显示s中的ASCII字符
// Return type		: void
// Argument         : int x : x坐标
// Argument         : int y : y坐标
// Argument         : char *s : 待显示字符串
// Argument         : int colour : 显示颜色
*****************************************************************************/
void lcd_disp_ascii16x8(int x,int y,char *s,int colour)
{
	char buffer[32];							/* 32字节的字模缓冲区		*/
 	int i,j,k;
 	unsigned char qh,wh;
 	unsigned long location;

 	while(*s)
  	{
   		qh=*s-0xa0;								/* 计算区码					*/
   		wh=*(s+1)-0xa0;							/* 计算位码					*/
   		location=(94*(qh-1)+(wh-1))*32L;		/* 计算字模在文件中的位置	*/
   		memcpy(buffer, &arcii16x8[location], 32);	/* 获取汉字字模				*/
		for(i=0;i<16;i++)						/* 每一行					*/
		{
    		for(j=0;j<1;j++)					/* 一行两个字节				*/
			{
     			for(k=0;k<8;k++)				/* 每个字节按位显示			*/
				{
      				if(((buffer[i*2+j]>>(7-k)) & 0x1) != 0)
       					lcd_put_pixel(x+8*(j)+k,y+i,colour); /* 显示一位	*/
				}
			}
		}
   		s+=1;												/* 下一个汉字	*/
   		x+=8;												/* 字符间距		*/
  	}
}
/********************************************************************
// Function name	: stepper_control
// Description	    : 步进电机使能控制
// Return type		: void
// Argument         : int benable
//     STEPER_MOTOR_ENABLE   -- stepper enable
//     STEPER_MOTOR_DISABLE  -- stepper disable
*********************************************************************/
void stepper_control(int benable) 
{   
	if(benable == STEPER_MOTOR_ENABLE) 
	{                                
		steper_control_mask |= (1<<3); 
		*(unsigned char *)0x0a000006 = steper_control_mask; 
	}                                
	else                             
	{                                
		steper_control_mask &= (~(1<<3));  
		*(unsigned char *)0x0a000006 = steper_control_mask; 
	}           
}

/********************************************************************
// Function name	: stepper_set_direct
// Description	    : 步进电机方向控制
// Return type		: void
// Argument         : int direct
//     STEPER_MOTOR_CLOCKWISE      -- clockwise direction
//     STEPER_MOTOR_ANTICLOCKWISE  -- anticlockwise direction
*********************************************************************/
void stepper_set_direct(int direct)
{
	if(direct == STEPER_MOTOR_CLOCKWISE)
	{
		steper_control_mask |= (1<<2);
		*(unsigned char *)0x0a000006 = steper_control_mask;
	}
	else
	{
		steper_control_mask &= (~(1<<2));
		*(unsigned char *)0x0a000006 = steper_control_mask;
	}
}

/*****************************************************************************
// Function name	: delay
// Description	    : 延时子程序
// Return type		: void
// Argument         : count,延时的数值
*****************************************************************************/
void delay( int count )
{
	for( ; count>0; count--);
}

/*****************************************************************************
// Function name	: Main
// Description	    : 步进电机驱动程序
//     键盘定义
//     1(正反转) 2(使能)   3        UP(加速)
//     4         5         6        DOWN(减速)
//     7         8         9        Cancel(退出)

⌨️ 快捷键说明

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