Dynamixel dx-series – Robotis Dynamixel DX-117 User Manual
Page 33
32
DYNAMIXEL
DX-Series
/*
About Register and value of bits, vide Mega128 Data Sheet.
*/
void PortInitialize(void)
{
DDRA = DDRB = DDRC = DDRD = DDRE = DDRF = 0; //Set all port to
input direction first.
PORTB = PORTC = PORTD = PORTE = PORTF = PORTG = 0x00; //PortData
initialize to 0
cbi(SFIOR,2); //All Port Pull Up ready
//Set 5 LED port and RS485Direction port to output
DDRB |= (BIT_LED_E1);
DDRE
|=
(BIT_RS485_DIRECTION|BIT_LED_M0|BIT_LED_M1|BIT_LED_M2|BIT_LED_E0);
//TurnOff LED
LED_M0_OFF; LED_M1_OFF;LED_M2_OFF;LED_E0_OFF;LED_E1_OFF;
}
/*
TxPacket() send data to RS485.
TxPacket() needs 3 parameter; ID of Dynamixel, Instruction byte,
Length of parameters.
TxPacket() return length of Return packet from Dynamixel.
*/
byte TxPacket(byte bID, byte bInstruction, byte bParameterLength)
{
byte bCount,bCheckSum,bPacketLength;
gbpTxBuffer[0] = 0xff;
gbpTxBuffer[1] = 0xff;
gbpTxBuffer[2] = bID;
gbpTxBuffer[3]
=
bParameterLength+2;
//Length(Paramter,Instruction,Checksum)
gbpTxBuffer[4] = bInstruction;
for(bCount = 0; bCount < bParameterLength; bCount++)
{
gbpTxBuffer[bCount+5] = gbpParameter[bCount];
}
bCheckSum = 0;
bPacketLength = bParameterLength+4+2;
for(bCount = 2; bCount < bPacketLength-1; bCount++) //except
0xff,checksum
{
bCheckSum += gbpTxBuffer[bCount];
}
gbpTxBuffer[bCount] = ~bCheckSum; //Writing Checksum with Bit
Inversion
RS485_TXD;
for(bCount = 0; bCount < bPacketLength; bCount++)
{
sbi(UCSR0A,6);//SET_TXD0_FINISH;
TxD80(gbpTxBuffer[bCount]);
}
while(!CHECK_TXD0_FINISH); //Wait until TXD Shift register empty
RS485_RXD;
return(bPacketLength);
}
#define RX_WAIT_TIMEOUT 0xf000
#define RX_TIMEOUT_COUNT2 3000L
#define RX_TIMEOUT_COUNT1 (RX_TIMEOUT_COUNT2*10L)
/*
RxPacket() read data from buffer.
RxPacket() need a Parameter; Total length of Return Packet.
RxPacket() return Length of Return Packet.
*/
byte RxPacket(byte bRxPacketLength)
{
unsigned long ulCounter;
byte bCount, bLength, bChecksum;
byte bTimeout;
bTimeout = 0;
for(bCount = 0; bCount < bRxPacketLength; bCount++)
{
ulCounter = 0;
while(gbRxBufferReadPointer == gbRxBufferWritePointer)
{
if(ulCounter++ > RX_TIMEOUT_COUNT1)
{
bTimeout = 1;
break;
}
}
if(bTimeout) break;
gbpRxBuffer[bCount]
=
gbpRxInterruptBuffer[gbRxBufferReadPointer++];
}
bLength = bCount;
bChecksum = 0;
if(gbpTxBuffer[2] != BROADCASTING_ID)
{
if(bTimeout && bRxPacketLength != 255)
{
TxDString("\r\n [Error:RxD Timeout]");
CLEAR_BUFFER;
}
if(bLength > 3) //checking is available.
{
if(gbpRxBuffer[0] != 0xff || gbpRxBuffer[1] != 0xff )
{
TxDString("\r\n [Error:Wrong Header]");
CLEAR_BUFFER;
return 0;
}
if(gbpRxBuffer[2] != gbpTxBuffer[2] )
{
TxDString("\r\n [Error:TxID != RxID]");
CLEAR_BUFFER;
return 0;
}
if(gbpRxBuffer[3] != bLength-4)
{
TxDString("\r\n [Error:Wrong Length]");
CLEAR_BUFFER;
return 0;
}
for(bCount = 2; bCount < bLength; bCount++) bChecksum +=
gbpRxBuffer[bCount];
if(bChecksum != 0xff)
{
TxDString("\r\n [Error:Wrong CheckSum]");
CLEAR_BUFFER;
return 0;
}
}
}
return bLength;
}
/*
PrintBuffer() print data in Hex code.
PrintBuffer() needs two parameter; name of Pointer(gbpTxBuffer,
gbpRxBuffer)
*/
void PrintBuffer(byte *bpPrintBuffer, byte bLength)
{
byte bCount;
for(bCount = 0; bCount < bLength; bCount++)
{
TxD8Hex(bpPrintBuffer[bCount]);
TxD8(' ');
}
TxDString("(LEN:");TxD8Hex(bLength);TxD8(')');
}
/*