Legacy Forum: Preserving Nearly 20 Years of Community History - A Time Capsule of Discussions, Memories, and Shared Experiences.

Mega168 -> AX-12 serial problem

Bioloid robot kit from Korean company Robotis; CM5 controller block, AX12 servos..
4 postsPage 1 of 1
4 postsPage 1 of 1

Mega168 -> AX-12 serial problem

Post by Miamicanes » Tue Jun 17, 2008 12:03 am

Post by Miamicanes
Tue Jun 17, 2008 12:03 am

A little while ago, I connected a Mega168 to an AX-S1 as follows:

* bus ground to AVR ground (cm5's ground)

* AVR TXD to bus data (cm5's data not connected to anything, AVR's TXD connected to AX-S1's data pin )

* bus +9.6v to 5v regulator (cm5's output to regulator)

and wrote a short program that basically initialized the USART to double-speed, 1mbps (UBRR0H:L = 0:0), no pairity, 1 stop bit, and sent the following datagram to the AX-S1:

0xFF 0xFF (prelude)
0x64 (recipient = address 100)
0x04 (length = 2 argument bytes + 2)
0x03 (write data)
0x28 (buzzer frequency)
0x10 (16 = buzzer index)
0xA3 ((0x64 + 0x04 + 0x03 + 0x28 + 0x10) && 0xff )

Nothing happened. About an hour earlier, I verified that writing 0x10 to register 0x28 on the device with address=0x64 DID produce a tone using robot terminal.

At this point, I'm trying to figure out where I went wrong, starting with the easy/obvious things...

a) Did I get the datagram right, including the checksum?

b) If the AVR is 5v, do I have to do anything special with the output of its TXD pin (not counting half-duplex switching, of course... right now I'm just trying to get an experiment involving a one-way broadcast that produces an observable result to work before moving on to worry about the half-duplex issues)
A little while ago, I connected a Mega168 to an AX-S1 as follows:

* bus ground to AVR ground (cm5's ground)

* AVR TXD to bus data (cm5's data not connected to anything, AVR's TXD connected to AX-S1's data pin )

* bus +9.6v to 5v regulator (cm5's output to regulator)

and wrote a short program that basically initialized the USART to double-speed, 1mbps (UBRR0H:L = 0:0), no pairity, 1 stop bit, and sent the following datagram to the AX-S1:

0xFF 0xFF (prelude)
0x64 (recipient = address 100)
0x04 (length = 2 argument bytes + 2)
0x03 (write data)
0x28 (buzzer frequency)
0x10 (16 = buzzer index)
0xA3 ((0x64 + 0x04 + 0x03 + 0x28 + 0x10) && 0xff )

Nothing happened. About an hour earlier, I verified that writing 0x10 to register 0x28 on the device with address=0x64 DID produce a tone using robot terminal.

At this point, I'm trying to figure out where I went wrong, starting with the easy/obvious things...

a) Did I get the datagram right, including the checksum?

b) If the AVR is 5v, do I have to do anything special with the output of its TXD pin (not counting half-duplex switching, of course... right now I'm just trying to get an experiment involving a one-way broadcast that produces an observable result to work before moving on to worry about the half-duplex issues)
Miamicanes
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 28
Joined: Thu Dec 28, 2006 1:00 am

Post by Miamicanes » Tue Jun 17, 2008 1:35 am

Post by Miamicanes
Tue Jun 17, 2008 1:35 am

OK, it's working now. I forgot the damn NOT operation after adding up the values and taking the lower 8 bits. For future reference if anyone's interested or has the same problem...

Code: Select all
.include "m168def.inc"

.def rTemp = r16
.def rValue = r17


rjmp RESET

.org 0x34
RESET:

   ldi rTemp, HIGH(RAMEND)
   out SPH, rTemp
   ldi rTemp, LOW(RAMEND)
   out SPL, rTemp

   sbi DDRB, 0
   sbi PORTB, 0

   ldi r20, 0x40
   pausing:
      ser xh
      ser xl
      delaying:
         sbiw xh:xl, 1
         brne delaying
      dec r20
      brne pausing
; initialize the serial port
   ; set baudrate to 1mbps (requires setting UCSR0A.U2X0 as well)
   clr rTemp
   sts 0xc5, rTemp ; 0xc5 = UBRR0H
   sts 0xc4, rTemp ; 0xc4 = UBRR0L

   ldi rTemp, 2 ; bit 1 is U2X0
   sts 0xc0, rTemp ; 0xc0 = UCSR0A

   ldi rTemp, 8 ; bit 3 is TXEN0
   sts 0xc1, rTemp ; 0xc1 = UCSR0B

   ldi rTemp, 6 ;(8-bit, no parity, 1 stopbit)
   sts 0xc2, rTemp ; 0xc2 = UCSR0C

; send datagram to make buzzer in AX-S1 beep once
; 0xff 0xff 0x64 0x04 0x03 0x28 0x10 (0x64 + 0x04 + 0x03 + 0x28 + 0x10)

   ser rValue
   rcall sendValue

   ser rValue
   rcall sendValue

   ldi rValue, 0xfe ; broadcast to everyone
   rcall sendValue
   
   ldi rValue, 0x04 ; 1 databyte to write + 3 = 4
   rcall sendValue

   ldi rValue, 0x03 ; WRITE_DATA
   rcall sendValue

   ldi rValue, 0x19 ; write to 0x19 (LED)
   rcall sendValue

   ldi rValue, 0x1 ; value to write to 0x19 (1 = on, 0 = off)
   rcall sendValue

   ldi rValue, 0xe0
   rcall sendValue

   clr rTemp
   sts 0xc1, rTemp ; clear TXEN0 in UCSR0B

LOOP:
   rjmp LOOP

sendValue:
   cbi PORTB, 0
   lds rTemp, 0xc0 ; 0xc0 is UCSR0A
   sbrs rTemp, 5 ; bit 5 is UDRE0
   rjmp sendValue
   sts 0xc6, rValue ; 0xc6 is UDR0
   sbi PORTB, 0
   ret


OK, it's working now. I forgot the damn NOT operation after adding up the values and taking the lower 8 bits. For future reference if anyone's interested or has the same problem...

Code: Select all
.include "m168def.inc"

.def rTemp = r16
.def rValue = r17


rjmp RESET

.org 0x34
RESET:

   ldi rTemp, HIGH(RAMEND)
   out SPH, rTemp
   ldi rTemp, LOW(RAMEND)
   out SPL, rTemp

   sbi DDRB, 0
   sbi PORTB, 0

   ldi r20, 0x40
   pausing:
      ser xh
      ser xl
      delaying:
         sbiw xh:xl, 1
         brne delaying
      dec r20
      brne pausing
; initialize the serial port
   ; set baudrate to 1mbps (requires setting UCSR0A.U2X0 as well)
   clr rTemp
   sts 0xc5, rTemp ; 0xc5 = UBRR0H
   sts 0xc4, rTemp ; 0xc4 = UBRR0L

   ldi rTemp, 2 ; bit 1 is U2X0
   sts 0xc0, rTemp ; 0xc0 = UCSR0A

   ldi rTemp, 8 ; bit 3 is TXEN0
   sts 0xc1, rTemp ; 0xc1 = UCSR0B

   ldi rTemp, 6 ;(8-bit, no parity, 1 stopbit)
   sts 0xc2, rTemp ; 0xc2 = UCSR0C

; send datagram to make buzzer in AX-S1 beep once
; 0xff 0xff 0x64 0x04 0x03 0x28 0x10 (0x64 + 0x04 + 0x03 + 0x28 + 0x10)

   ser rValue
   rcall sendValue

   ser rValue
   rcall sendValue

   ldi rValue, 0xfe ; broadcast to everyone
   rcall sendValue
   
   ldi rValue, 0x04 ; 1 databyte to write + 3 = 4
   rcall sendValue

   ldi rValue, 0x03 ; WRITE_DATA
   rcall sendValue

   ldi rValue, 0x19 ; write to 0x19 (LED)
   rcall sendValue

   ldi rValue, 0x1 ; value to write to 0x19 (1 = on, 0 = off)
   rcall sendValue

   ldi rValue, 0xe0
   rcall sendValue

   clr rTemp
   sts 0xc1, rTemp ; clear TXEN0 in UCSR0B

LOOP:
   rjmp LOOP

sendValue:
   cbi PORTB, 0
   lds rTemp, 0xc0 ; 0xc0 is UCSR0A
   sbrs rTemp, 5 ; bit 5 is UDRE0
   rjmp sendValue
   sts 0xc6, rValue ; 0xc6 is UDR0
   sbi PORTB, 0
   ret


Miamicanes
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 28
Joined: Thu Dec 28, 2006 1:00 am

Post by Miamicanes » Tue Jun 17, 2008 3:50 pm

Post by Miamicanes
Tue Jun 17, 2008 3:50 pm

OK, now that I've gotten outbound transmissions to work, I'm tackling the responses. Obviously, it's not working ;-)

One possible issue I'm looking at is clock drift. Is the AVR's built-in 8MHz oscillator reliable enough to handle 1mbps double-speed USART transmission? Or do I really need to use a crystal, and possibly jack up the speed to 16MHz so I won't have to rely on double-speed mode?
OK, now that I've gotten outbound transmissions to work, I'm tackling the responses. Obviously, it's not working ;-)

One possible issue I'm looking at is clock drift. Is the AVR's built-in 8MHz oscillator reliable enough to handle 1mbps double-speed USART transmission? Or do I really need to use a crystal, and possibly jack up the speed to 16MHz so I won't have to rely on double-speed mode?
Miamicanes
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 28
Joined: Thu Dec 28, 2006 1:00 am

Post by JonHylands » Tue Jun 17, 2008 9:07 pm

Post by JonHylands
Tue Jun 17, 2008 9:07 pm

You can talk to the bus using the internal oscillator at 8 MHz.

You don't say how you've got the ATmega168's Rx pin hooked up... Without that, we can't really help you...

- Jon
You can talk to the bus using the internal oscillator at 8 MHz.

You don't say how you've got the ATmega168's Rx pin hooked up... Without that, we can't really help you...

- Jon
JonHylands
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 512
Joined: Thu Nov 09, 2006 1:00 am
Location: Ontario, Canada


4 postsPage 1 of 1
4 postsPage 1 of 1