I2C interface to SD21 Servo Controller

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Bryan
Registered User
Registered User
Posts: 18
Joined: Tue Feb 19, 2008 6:50 am
Location: Nelson, B.C. Canada

I2C interface to SD21 Servo Controller

Post by Bryan » Sun Apr 22, 2012 10:59 pm

Hi, I've been trying to interface a 21 channel servo controller to a Pic 18f458 for several days and could really use a hand.
Communications is I2C
For each servo there are only two parameters to deal with, Speed and Position
To control servo1 I need to write the speed into register 0 (speed is a byte-range0-255)
Then I need to write position which is a word (range 1900-2300) into registers 2 & 3, low byte first followed by hi byte.
I haven't used I2C before so I started out reading everything I could find about it. Seemed to me that using the I2C Library would make it quite simple to do this. Next I started with David's I2C EEPROM example and adapted it (I thought) ...to talk to the Servo controller - after many attempts so far no success.
Here's a link to the short "Manual" for the Devantech SD21 Servo Controller if that helps
ttp://www.robotstorehk.com/sd21tech.pdf

I sure appreciate any time you spend helping me out with this
Thank you
Bryan

Here's my last attempt at coding this:

Code: Select all

Device = 18F458
Clock = 10

// import libraries...
Include "I2C.bas"
'include "usart.bas"

// target SD21 Servo Controller device...
Const Servo_Controller = $C2

// local variables...
Dim
   Address As byte,  'only need to access registers 0-65
   Speed As Byte,    ' 0 is Full speed, range0-255
   Position As Word, '800 to 2200 (translates to uSec of pulse width, 1500=centered

   
Public Sub WritePosition(pAddress As Byte,pPosition As Word) 'SF doesn' like this !!
       WriteByte(pAddress,pPosition.byte0)                                  'thought it might work
       WriteByte(pAddress+1,pPosition.byte1)
       End Sub 
   
// program start...
Address = 0       'point address to first register
Speed=0           'full speed for now
Position=1500     'center position for initial test

I2C.Initialize

I2C.Start                          'begin I2C comm
I2C.WriteByte(Servo_Controller)    'wake up the controller
WriteByte(Address)                 'write speed to first reg
WriteByte(Speed)
Address=Address+1                  'point at next register
WritePosition                 'call Sub above to get position data written
                                   'Problem here as I don't know how to convert
                                   'Position word into LSB and MSB and then get
                                   'them into the next two regs (2 & 3) LSB first.
I2C.Stop

DelayMS(10) 'allows Servo Controller to write data

DelayMS(1000) 'hold position for 1 second then move

end

Bryan
Registered User
Registered User
Posts: 18
Joined: Tue Feb 19, 2008 6:50 am
Location: Nelson, B.C. Canada

Post by Bryan » Mon Apr 23, 2012 3:27 am

Hello again, persistance pays off - I have it working.
Here's the code to write the speed and position to the controller.

Code: Select all

Speed=10          '0=full speed, 1 = slowest
Position=900     '1500 = center position for initial test

I2C.Start                          'begin I2C comm
I2C.WriteByte(Servo_Controller)    'wake up the controller
WriteByte(Address)                 'write speed to first reg
WriteByte(Speed)
Address=Address+1                  'point at next register
WriteByte(Position.byte0)                   
WriteByte(Position.byte1)
I2C.Stop
The following two lines were the answer to how to write a word (Position)
into the two Position registers for the first servo.
WriteByte(Position.byte0) 'write LSB first
WriteByte(Position.byte1) 'next write MSB

I know someone would have stepped up and given me a hand - its a great forum, but you know it sure does feel good when you finally figure it out for yourself.

Bryan

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Post by Jon Chandler » Mon Apr 23, 2012 7:18 pm

Good show!

I2C is great when it works...and a pain in the butt to get working. There are so many places for mistakes to creep in. Always read the data sheet carefully because crucial pieces can end up in odd registers...like the "enable chip" bit that ends up in some odd register.
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Post Reply