CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to access SD21 Servo controller from CCS Compiler?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RAMBO29



Joined: 14 Jul 2005
Posts: 0

View user's profile Send private message

How to access SD21 Servo controller from CCS Compiler?
PostPosted: Mon Jul 18, 2005 12:07 am     Reply with quote

I am using P18F452 MCU and SD21 servo controller with MPLAB 7.2 and
CCS compiler. But I am unable to get the servos working.
This is the code I am using.
Code:

#include <18F452.h>
#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3,address=0xc2)

#define  SD21 0xC2
#define  servo1  0
#define  Servo2  3
#define  Servo3  6         
#define  Servo4  9                                 
#define  Servo5  12
#define  Servo6  15         
#define  Servo7  18
#define  Servo8  21
#define  Servo9  24
#define  Servo10 27
#define  Servo11 30
#define  Servo12 33
#define  Servo13 36
#define  Servo14 39
#define  Servo15 42
#define  Servo16 45
#define  Servo17 48
#define  Servo18 51


void main ()
{
unsigned char tmp[3];
   

i2c_start();
tmp[0]=0;
tmp[1]=0x34;
tmp[2]=0x08;
i2c_start();   //I2C Start
i2c_write(SD21);
i2c_write(servo1);   //Memory location
i2c_write(tmp);   //Data
i2c_stop();   //I2C Stop


i2c_start();
tmp[0]=0;
tmp[1]=0x7C;
tmp[2]=0x05;
i2c_start();   //I2C Start
i2c_write(SD21);   //Chip Address
i2c_write(servo2);   //Memory location
i2c_write(tmp);   //Data
i2c_stop();   //I2C Stop

  }

Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 18, 2005 1:34 pm     Reply with quote

Look at this page, which has sample Visual Basic code for the SD21:
http://www.robot-electronics.co.uk/files/EH2_SD21_BX24.txt

Scroll down near the bottom of that page, until you get to
the Sub I2cByteWrite function. Anything that talks
to the SD21 uses that routine. So that's the main routine
that you want to translate. Once you've done that, all the
rest of the code is easy, because it mostly calls that routine.

Here's a sample program where I've translated the I2cByteWrite
and BackRightRev functions to CCS. I've also translated some
constants and a variable. If you compare this code to their code,
you see how to translate the rest of it.

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)     
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
//====================================

#define SD21   0xC2      // SD21 I2C Address

#define RFPbck 119
#define LMPbck 111
#define RRPbck 117


int8 Speed;

//===================================
void I2cByteWrite(int8 I2cAddr, int8 I2cReg, int8 I2cData)
{
i2c_start();
i2c_write(I2cAddr);         // send device address
i2c_write(I2cReg);          // send register address
i2c_write(I2cData);         // send the data
i2c_stop();
}

//---------------------------------
Void BackRightRev(void)
{
I2cByteWrite(SD21, RFPbck, Speed);
I2cByteWrite(SD21, LMPbck, Speed);
I2cByteWrite(SD21, RRPbck, Speed);
}

//=====================================
void main()
{
Speed = 10;

BackRightRev(); 

while(1);  // Prevent PIC from going to sleep
}
Guest








PostPosted: Wed Dec 24, 2008 3:12 am     Reply with quote

Hello,

Did you solved your trouble?
If you did it, could you tell how to do in the forum?
I'm a complete noob with the same problem that you.

Thanks!
Guest








PostPosted: Tue Feb 03, 2009 3:50 pm     Reply with quote

Can anyone expain how the SD21 gets its instructions. Im not familar with I2C and I cant seem to find a simple program to domonstrate this.
The main problem is the datasheet for the SD21 is clear on how to write to the registers. Here is my code so far.

Code:
#include <18F4520.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT, WDT128, INTRC_IO, NOPROTECT, BROWNOUT, BORV20, NOPUT, NOCPD, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, IESO, FCMEN, PBADEN, NOWRTC, NOWRTB, NOEBTR, NOEBTRB, NOCPB, NOLPT1OSC, MCLR, XINST
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   #use delay(clock=40000000)

   
   i2c_start();     //start tansmission
   i2c_write(0xC2); //write to board address
   i2c_write(??);   //  Im not sure on rest
   i2c_write(??);
   i2c_write(??);
   i2c_stop();

}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Feb 03, 2009 4:24 pm     Reply with quote

Quote:
#FUSES NOWDT, WDT128, INTRC_IO, NOPROTECT, BROWNOUT, BORV20, NOPUT, NOCPD, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, IESO, FCMEN, PBADEN, NOWRTC, NOWRTB, NOEBTR, NOEBTRB, NOCPB, NOLPT1OSC, MCLR, XINST
#use delay(clock=20000000)
20MHz is a wrong setting. INTRC_IO selects the internal oscillator with a clock frequency in the range 31kHz to 8MHz. Check the datasheet for the possible frequencies.

The XINST fuse enables the eXtended INSTruction set. This feature is not supported by the CCS compiler and is known to create problems. Change to NOXINST instead.

You have selected NODEBUG but also device ICD=TRUE, these two instructions are the opposite of each other. I'm not sure what the compiler will make of this, but it might be different from what you want it to be. Solve the conflict.

Code:
   #use delay(clock=40000000)
You really like changing the clock frequency, don't you? If 20MHz is too fast, then for sure 40MHz isn't going to work either. Only put one '#use delay' line at the start of your program (unless you know exactly what you are doing and have good reasons for a second clock setup).

Right now it looks like you have no clue as to what you are doing. My suggestion to you would be to first create a simple program blinking a led at a 1 second interval. This is the most simple program but important because it proves your hardware is working correctly. And from there start expanding the program in small increments.

Anonymous wrote:
Can anyone expain how the SD21 gets its instructions. Im not familar with I2C and I cant seem to find a simple program to domonstrate this.
You have found this thread with some great pointers provided by PCM Programmer, what more do you need???
I never used the SD21 servo, but looking at the provided example programs there is all the info I would need.
Guest








PostPosted: Thu Feb 05, 2009 8:28 am     Reply with quote

Yes you're correct. I need to stop relying on the wizard to set things correctly for me. But my actual question is how to communicate the I2c data to servo controller. The data sheet doesn't really tell you how.
Code:

#include <18F4520.h>
#include "math.h"

#device ICD=TRUE
#device adc=8
#FUSES  NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, INTRC_IO
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

//=====================================
void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

i2c_start();
i2c_write(SD21);         // send device address
i2c_write(0x00);          // send register address
i2c_write(0x00);         // send the data
i2c_write(0x01);          // send register address
i2c_write(0x4c);         // send the data
i2c_write(0x02);          // send register address
i2c_write(0x4);         // send the data
i2c_stop();

}

Above is the main code for my test program. I have the servo that I'm testing moving but it doesn't seem be correct. when I send 1100 to lowbyte/highbyte the servo should move left not right. Then when I go to the opposite 1800 it still moves to the right. I don't know if the way for sending the data is correct.
Guest








PostPosted: Thu Feb 05, 2009 8:31 am     Reply with quote

Sry accidently deleted #define SD21 0xC2 in my code posting. But it is in the program.
Guest








PostPosted: Sat Feb 07, 2009 1:11 pm     Reply with quote

After a little playing around I fiqured out the answer to my question thanks for your help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group