TMC配置程序
           
               
                    #include "iom32v.h"
#include <stddef.h>
#include "macros.h"
#include "stdio.h"
#include "stdlib.h"
#define uint  unsigned int
#define uchar unsigned char
#define ulong unsigned long
//SPI变量
#define ENN_262 PB3
#define CSN_262 PB4
#define SPI_MOSI PB5
#define SPI_MISO PB6
#define SPI_SCK PB7
#define BYTE3(var) *((uchar*)&var+3)
#define BYTE2(var) *((uchar*)&var+2)
#define BYTE1(var) *((uchar*)&var+1)
#define BYTE0(var) *((uchar*)&var)
#define TRUE 1
#define FALSE 0
#define MCLK 16000000
#define FIFO_SIZE 127
//#pragma interrupt_handler Timer0:10              //定时器/ 计数器0 溢出
#pragma interrupt_handler Uart_recv_interrupt:14  //串口接收中断函数声明
#pragma interrupt_handler Uart_send_interrupt:16  //
//#pragma interrupt_handler Timer0Interrupt:12
typedef struct
{
uchar Opcode;
uchar Type;
uchar Motor;
union
{
long Int32;
uchar Byte[4];
} Value;
}TTMCLCommand;
//TMCL reply
typedef struct
{
uchar Status;
timeout on t2 timer
uchar Opcode;
union
{
long Int32;
uchar Byte[4];
} Value;
}TTMCLReply;
//Global settings (in this example only RS232 settings)
typedef struct
{
uchar SerialBitrate;
uchar SerialModuleAddress;
uchar SerialHostAddress;
} TModuleConfig;
//Variables
uchar RecvFIFO[FIFO_SIZE];
uchar RecvWritePtr, RecvReadPtr;
uchar SendFIFO[FIFO_SIZE];
uchar SendWritePtr, SendReadPtr;
uchar          UARTCmd[9];
uchar          UARTCount;
uchar          TMCLCommandState;
uchar          TMCLSendFlag;
TTMCLCommand  ActualCommand;
TTMCLReply    ActualReply;
TModuleConfig  ModuleConfig;
uchar          TMCLReplyFormat;
uchar          SpecialReply[9];
uchar          outgoing[4],incoming[4];
uchar          UARTTimeout;
ulong Readback;
ulong SPIReadInt;
ulong SPIWriteInt;
//Global parameters (here only used for the RS232 interface)
TModuleConfig ModuleConfig=
{
0,      //RS232 bitrate (0=9600)
1,      //RS232 address
2      //RS232 reply address
};
//Motor configuration data
uchar  EEPROM[150][6];
uchar  k=0;  //eeprom计数
uchar  num=0;
uchar  ProgFlag=FALSE,DownLoadFlag=FALSE;
uint    i,j;
ulong  ActualVelocity,TargetVelocity,VelAdd,VelAccumulator,ActualAcceleration;                 
//Supported TMCL commands
#define TMCL_ROR  1
#define TMCL_ROL  2
#define TMCL_MST  3
#define TMCL_MVP  4
#define TMCL_SAP  5
#define TMCL_GAP  6
#define TMCL_SIO  14
#define TMCL_GIO  15
#define TMCL_DOWNLOAD 16
#define TMCL_OUTPUT  17
#define TMCL_INPUT  17
#define TMCL_GetVersion 136
//TMCL status codes
#define REPLY_OK 100
#define REPLY_CHKERR 1
#define REPLY_INVALID_CMD 2
#define REPLY_WRONG_TYPE 3
#define REPLY_INVALID_VALUE 4
#define REPLY_EEPROM_LOCKED 5
#define REPLY_CMD_NOT_AVAILABLE 6
#define REPLY_CMD_LOAD_ERROR 7
#define REPLY_WRITE_PROTECTED 8
#define REPLY_MAX_EXCEEDED 9
#define IDX_XTARGET 0x00
#define IDX_XACTUAL 0x02
#define IDX_VMIN 0x04
#def
           
               
                    ine IDX_VMAX 0x06
#define IDX_VTARGET 0x08
#define IDX_VACTUAL 0x0A
#define IDX_AMAX 0x0C
#define IDX_AACTUAL 0x0E
#define IDX_AGTAT_ALEAT 0x10
#define IDX_PMUL_PDIV 0x12
#define IDX_REFCONF_RM 0x14
#define IDX_IMASK_IFLAGS 0x16
#define IDX_PULSEDIV_RAMPDIV 0x18
#define IDX_DX_REFTOLERANCE 0x1A
#define IDX_XLATCHED 0x1C
//States of the command interpreter
#define TCS_IDLE  0
#define TCS_UART  1
#define TCS_UART_ERROR 2
#define        TMCL_RESET    20
/*******************************************
函数名称: Uart_init
功    能: 异步串口初始化
参    数: baud--设置的波特率
返回值  : 无
/********************************************/
void Uart_init(uint baud)                               
{
baud=MCLK/16/baud-1;                      //波特率最大为65K
UCSRB=0x00;
UCSRA=0x00;                                //控制寄存器清零
UCSRC=(1<<URSEL)|(0<<UPM0)|(3<<UCSZ0); //选择UCSRC,异步模式,禁止校验,1位停止位,8位数据位
UBRRL=baud;                               
UBRRH=baud>>8;                          //设置波特率
UCSRB=(1<<TXEN)|(1<<RXEN)|(1<<RXCIE);  //接收、发送使能,接收中断使能
SREG=(1<<7);                              //全局中断开放
DDRD|=0X02;                              //配置TX为输出(很重要)
}
/*******************************************
函数名称: Uart_recv_interrupt
功    能: 串口接受中断处理
参    数: NULL
返回值  : NULL
/********************************************/
void Uart_recv_interrupt()    /*byte completely received*/
{
int i;
//Copy character to reception FIFO.
i=RecvWritePtr+1;
if(i==FIFO_SIZE) i=0;
if(i!=RecvReadPtr)
{
RecvFIFO[RecvWritePtr]=UDR;
RecvWritePtr=i;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。