Busan IT/모터제어

AX-12+ Baud Rate 값 변경, Packet 전달 함수 만들기

newind2000 2015. 7. 13. 13:24

================================ Outline ====================================

AX-12+ Baud Rate 값 변경

Packet 전달 함수 만들기

----------------------------------------------------------------------------

/* 

모터가 300도와 0도 사이를 오가며 지속적으로 회전시키려면 모터 회전 후 딜레이 값을 0.865초를 주면 된다.

 

_delay_ms(865);


*/

 

AX-12+ Baud Rate 값 변경


Baud Rate를 변경해 주기 위해서는 instruction에 WRITE_DATA(#3), Length 값에 4, Write Address 값은 4이다.



바꿔주고자하는 Baud Rate는 9,600bps로 한다.

207은 Hex값으로 0x67이다.


AX-12+와 통신을 위해 AX-12+의 Baud Rate를 바꿔주고 난 후 ATmega2560의 Baud Rate도 변경해 주어야 한다.



그리하여 코드는 아래와 같다.



#include <avr/io.h>
#include <util/delay.h>

void USART0_init()
{
  /* BAUD Rate Set */
  UBRR0 = 0x67;

  /* USART Set */
  UCSR0A = 0b00000000;    //일반모드 
  UCSR0B = 0b00001000;    //인터럽트 사용 안함
  UCSR0C = 0b00000110;
}

void PacketTX_P1(unsigned char ID, unsigned char Length, unsigned char Instruction, unsigned char cmd, unsigned char para1)
{
    Data_Tx0(0xFF);  //ini
    Data_Tx0(0xFF);  //ini
    Data_Tx0(ID);  //ID
    Data_Tx0(Length);  //LENGTH = 2+ # of parameter
    Data_Tx0(Instruction);  //Instruction 
    Data_Tx0(cmd);  //command in control table
    Data_Tx0(para1);  //goal position(rear byte)
  
    Data_Tx0((unsigned char)(~(ID + Length + Instruction + cmd + para1)));
    _delay_ms(865);
}

int main(void)

{

  _delay_ms(1000);
  USART0_init();
  
  //Baud Rate 9600으로 변경 
  PacketTX_P1(0x0C, 0x040x030x040xCF);    
  _delay_ms(3000);
  UBRR0 = 0x67;
  _delay_ms(3000);

return 0;

}


 

Packet 전달 함수 만들기

 

주의할 점은 check sum의 값이다. parameter의 개수와 관련이 있음으로 Length의 값을 이용하여 함수를 만들어주면 쉽게 값을 도출할 수 있다.

parameter의 개수에 따라 함수를 달리해 주어야 한다.

void PacketTX_P1(unsigned char ID, unsigned char Length, unsigned char Instruction, unsigned char cmd, unsigned char para1)
{
    Data_Tx0(0xFF);  //ini
    Data_Tx0(0xFF);  //ini
    Data_Tx0(ID);  //ID
    Data_Tx0(Length);  //LENGTH = 2+ # of parameter
    Data_Tx0(Instruction);  //Instruction 
    Data_Tx0(cmd);  //command in control table
    Data_Tx0(para1);  //goal position(rear byte)
  
    Data_Tx0((unsigned char)(~(ID + Length + Instruction + cmd + para1)));
    _delay_ms(865);
}
void PacketTX_P2(unsigned char ID, unsigned char Length, unsigned char Instruction, unsigned char cmd, unsigned char para1, unsigned char para2)
{
    Data_Tx0(0xFF);  //ini
    Data_Tx0(0xFF);  //ini
    Data_Tx0(ID);  //ID
    Data_Tx0(Length);  //LENGTH = 2+ # of parameter
    Data_Tx0(Instruction);  //Instruction 
    Data_Tx0(cmd);  //command in control table
    Data_Tx0(para1);  //goal position(rear byte)
    Data_Tx0(para2);  //goal position(head byte)
  
    Data_Tx0((unsigned char)(~(ID + Length + Instruction + cmd + para1 + para2)));
    _delay_ms(865);
}






반응형