📄 driver.h
字号:
//////////////////////////////////////////////////////////////////////
//
// GD102 DOS驱动程序V1.0
// Writer: hehang
// 2003.3.28
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>
#include <math.h>
#include <iostream.h>
#define TRUE 1 // Windows下不需定义
#define FALSE 0 // Windows下不需定义
#define R_MAX 8000000 // 最大范围
#define R_MIN 16000 // 最小范围
#define M_MAX 500 // 最大倍率
#define M_MIN 1 // 最小倍率
#define V_MAX 8000 // 最大速度
#define V_MIN 1 // 最小速度
#define A_MAX 8000 // 最大加速度
#define A_MIN 1 // 最小加速度
#define AXIS_NUM 2 // 最大轴数
#define AXIS_X 0x1
#define AXIS_Y 0x2
#define AXIS_XY 0x3
#define AXIS_ALL 0x3
#define CW 0 // 顺圆插补
#define CCW 1 // 逆圆插补
#define INT_CEND 0x2000 // 定速结束发生中断
#define INT_CSTA 0x4000 // 定速开始发生中断
#define INT_DEND 0x8000 // 驱动结束发生中断
//////////////////////////////////////////////////////////////////////
// WR0 命令列表
#define SET_R 0x00 // 设置范围
#define SET_K 0x01 // 设置加加速度
#define SET_A 0x02 // 设置加速度
#define SET_D 0x03 // 设置减速度
#define SET_SV 0x04 // 设置初始速度
#define SET_V 0x05 // 设置驱动速度
#define SET_P 0x06 // 设置输出脉冲或插补终点
#define SET_DP 0x07 // 设置手动减速点
#define SET_C 0x08 // 设置圆弧圆心
#define SET_LP 0x09 // 设置逻辑计数器
#define SET_EP 0x0a // 设置实位计数器
#define SET_CP 0x0b // 设置COMP+寄存器
#define SET_CM 0x0c // 设置COMP-寄存器
#define SET_AO 0x0d // 设置加速计数器偏移
#define SET_NOP 0x0f // 轴切换
#define READ_LP 0x10 // 读逻辑位置计数器
#define READ_EP 0x11 // 读实位计数器
#define READ_CV 0x12 // 读当前驱动速度
#define READ_CA 0x13 // 读当前加减速度
#define DRIVE_P 0x20 // 正向定量驱动
#define DRIVE_N 0x21 // 负向定量驱动
#define DRIVE_CP 0x22 // 正向连续驱动
#define DRIVE_CN 0x23 // 正向连续驱动
#define DRIVE_PAUSE 0x24 // 暂停驱动
#define DRIVE_RESET 0x25 // 解除暂停驱动/清除结束状态
#define STOP_D 0x26 // 驱动减速停止
#define STOP_I 0x27 // 驱动立即停止
#define CMD_LINE 0x30 // 2轴直线插补
#define CMD_CW 0x32 // 顺圆插补
#define CMD_CCW 0x33 // 逆圆插补
#define CMD_BP 0x34 // 2轴位模式插补
#define CMD_STEP 0x3a // 单步插补
#define UNLOCK_BP 0x36 // 允许写BP寄存器
#define LOCK_BP 0x37 // 禁止写BP寄存器
#define STACK_BP 0x38 // BP数据堆栈
#define CLEAR_BP 0x39 // BP数据清除
#define ENABLE_D 0x3b // 减速有效
#define DISABLE_D 0x3c // 减速无效
#define CLEAR_INT 0x3d // 插补中断清除
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 板卡地址列表
#define BASE_ADR 0x280 // 板卡基地址
#define IRQ_NUM 0x3 // 中断号
#define IRQ_ADR 0x0b // 中断入口地址
#define WR0 0x0 // 命令寄存器
#define WR1 0x2 // 寄存器1方式
#define WR2 0x4 // 寄存器2方式
#define WR3 0x6 // 寄存器3方式
#define WR4 0x8 // 输出寄存器
#define WR5 0xa // 插补方式寄存器
#define WR6 0xc // 低位数据写寄存器
#define WR7 0xe // 高位数据写寄存器
#define RR0 0x0 // 主状态寄存器
#define RR1 0x2 // 寄存器1状态
#define RR2 0x4 // 寄存器2状态
#define RR3 0x6 // 寄存器3状态
#define RR4 0x8 // 寄存器1输入
#define RR5 0xa // 寄存器2输入
#define RR6 0xc // 低位数据读寄存器
#define RR7 0xe // 高位数据读寄存器
#define BP1P 0x4 // 1轴控制BP正方向数据寄存器
#define BP1M 0x6 // 1轴控制BP反方向数据寄存器
#define BP2P 0x8 // 2轴控制BP正方向数据寄存器
#define BP2M 0xa // 2轴控制BP反方向数据寄存器
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 主要数据结构及函数
#ifndef _DRIVER_H
#define _DRIVER_H
typedef struct
{
unsigned int wr1[AXIS_NUM];
unsigned int wr2[AXIS_NUM];
unsigned int wr3[AXIS_NUM];
unsigned int wr4;
unsigned int wr5;
} REGISTER; // 存放读写寄存器的值
typedef struct
{
float m[AXIS_NUM]; // 各轴倍率
long r[AXIS_NUM]; // 各轴范围
long k[AXIS_NUM]; // 各轴加加速
short ao[AXIS_NUM]; // 各轴加速计数器偏移设定
long cp[AXIS_NUM]; // 各轴COMP+寄存器数值
long cm[AXIS_NUM]; // 各轴COMP-寄存器数值
} SYSLIST; // 系统参数列表
typedef struct
{
short sv[AXIS_NUM]; // 各轴初始速度 2byte 1-8000
short v[AXIS_NUM]; // 各轴驱动速度 2byte 1-8000
short a[AXIS_NUM]; // 各轴加速度 2byte 1-8000
short d[AXIS_NUM]; // 各轴减速度 2byte 1-8000
long p[AXIS_NUM]; // 各轴输出脉冲数
long dp[AXIS_NUM]; // 各轴手动减速点
long c[AXIS_NUM]; // 各轴圆弧中心点
} DRVLIST; // 驱动参数列表
typedef struct
{
long ep[AXIS_NUM]; // 各轴当前实际位置脉冲数
long lp[AXIS_NUM]; // 各轴当前逻辑位置脉冲数
short cv[AXIS_NUM]; // 各轴当前速度
short ca[AXIS_NUM]; // 各轴当前加速度
} CSVLIST; // 当前状态变量列表
void InitialBoard(unsigned int adr = BASE_ADR);
void InitialAllData();
void UpdateSysData(short axis = AXIS_ALL);
void UpdateDrvData(short axis = AXIS_ALL);
void Command(short axis, unsigned int cmd);
void Reset();
void Stop(int mode = 0);
void ExitSystem();
void SetError(); // ADD:
int GetError();
void SetM(short axis, float fdata);
void SetR(short axis, long dwdata);
void SetK(short axis, unsigned int wdata);
void SetA(short axis, int wdata);
void SetD(short axis, int wdata);
void SetSV(short axis, int wdata);
void SetV(short axis, int wdata);
void SetP(short axis, long dwdata);
void SetDP(short axis, long dwdata);
void SetC(short axis, long dwdata);
void SetLP(short axis, long dwdata);
void SetEP(short axis, long dwdata);
void SetCP(short axis, long dwdata);
void SetCM(short axis, long dwdata);
void SetAO(short axis, int wdata);
long ReadLP(short axis);
long ReadEP(short axis);
int ReadCV(short axis);
int ReadCA(short axis);
void _OUTW(unsigned int adr, unsigned int wdata);
unsigned int _INW(unsigned int adr);
void SetWR1(short axis, unsigned int wdata);
void SetWR2(short axis, unsigned int wdata);
void SetWR3(short axis, unsigned int wdata);
void SetWR4(unsigned int wdata);
void SetWR5(unsigned int wdata);
void SetWR67(long dwdata);
void EnableWR1(short axis, unsigned int ctrl, int bEnabled = TRUE);
void EnableWR2(short axis, unsigned int ctrl, int bEnabled = TRUE);
void EnableWR3(short axis, unsigned int ctrl, int bEnabled = TRUE);
void EnableWR4(unsigned int ctrl, int bEnabled = TRUE);
void EnableWR5(unsigned int ctrl, int bEnabled = TRUE);
unsigned int GetRR0();
unsigned int GetRR1(short axis);
unsigned int GetRR2(short axis);
unsigned int GetRR3(short axis);
unsigned int GetRR4();
unsigned int GetRR5();
SYSLIST& GetSysList();
DRVLIST& GetDrvList();
CSVLIST& GetCurStatus(short axis = AXIS_ALL);
long JerkToK(short axis, long dwdata);
short AccToA(short axis, long wdata);
short SpeedToV(short axis, long wdata);
long KToJerk(short axis, long dwdata);
long AToAcc(short axis, short wdata);
long VToSpeed(short axis, short wdata);
void HookInt(void interrupt (*NewInt)(...), unsigned int irq = IRQ_ADR);
void UnhookInt();
void WaitForStop(short axis);
void WaitForNext();
void SingleAxisDrive(short axis, long p, long sv = NULL, long v = NULL,
long a = NULL, long d = NULL, long k = NULL, float m = NULL);
void ConstSpeedDrive(short axis, long v, long p = NULL);
void Line2D(long p1, long p2, long sv = NULL, long v = NULL, long a = NULL, long d = NULL, short dp = NULL);
void Circle2D(short mode, long c1, long c2, long p1, long p2, long sv = NULL, long v = NULL,
long a = NULL, long d = NULL, short dp = NULL);
#endif
//////////////////////////////////////////////////////////////////////
// 常用的设置函数
#define SetIntMode(axis, bit) { EnableWR1(axis, bit); } // 设置中断模式
#define ClearInt() { _OUTW(WR0, 0x3d); outportb(0x20,0x20); } // 清除中断
#define EnableManu(axis) { EnableWR3(axis, 0x1); } // 手动减速
#define EnableDecAcc(axis) { EnableWR3(axis, 0x2); } // 独立减速度
#define EnableSCurve(axis) { EnableWR3(axis, 0x4); } // S曲线加/减速
#define EnableVRing(axis) { EnableWR3(axis, 0x40); } // 计数器可变环
#define EnableStatusOut(axis) { EnableWR3(axis, 0x80); } // 输出驱动状态
#define EnableCSpeed() { EnableWR5(0x100); } // 两轴匀速插补(注:R2 = 1.414 R1)
#define ShiftPulseOut(axis) { EnableWR2(axis, 0x40); } // 切换到1脉冲工作方式
#define ShiftEPCount(axis) { EnableWR1(axis, 0x80); } // 切换实位计数器的增减
//
// CMEE of UESTC CopyRight (C) 2003
//
//////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -