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 support@ccsinfo.com

Serial Servo controller problem

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



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

Serial Servo controller problem
PostPosted: Tue Nov 04, 2003 12:34 am     Reply with quote

I have been trying to get a serial servo controller to work for a couple of days, but I get very strange results.

CCS 3.155
16F628 @10MHz

With the program listing as it is, the program runs for a while but the output to the servo just disappears after a couple of seconds. I also tried to put the servo code in the ISR but thenthe program does not see the serial comms after a couple of seconds. I did look at the ASM listing and it seems that kbhit() may not be switched into the correct page.

In essence I need to do the following:

1. Run an output at a 20ms frame.
2. At the start of each 20ms frame, the servo output will be high for between 1ms and 2ms.
3. Recieve input from the PC in three bytes:
1. 255 Sync
2. Servo No
3. Servo Pos where 0=1ms 127=1.5ms and 255=2ms.
4. Reposition the appropriate servo in relation to the Servo Position.
5. I do not want to bit bang the timing because I may need to have up to 8 servos.

Any help will be appreciated:

SerialServo.h
----------------
#use delay(clock=10000000)
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,xmit=PIN_B2,rcv=PIN_B1)


SerialServo.c
----------------
#include <SerialServo.h>

#define ServoPin PIN_B4

int ServoPos;
boolean IntSet;

#int_TIMER1
TIMER1_isr()
{
set_timer1(15536);
IntSet = TRUE;
}

void SetServo()
{
int i = 0;

if (IntSet)
{
output_high(ServoPin);

delay_ms(1);

for (i=0;i<=ServoPos;++i)
delay_us(4);

output_low(ServoPin);
IntSet = FALSE;
}
}

void main() {
byte Sync = 0; // 0x55
byte ServoNo = 0; // 1 - 255

output_low(ServoPin);

set_timer1(15536);
setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(global);

ServoPos = 127;

IntSet = FALSE;

while (TRUE)
{
SetServo();

if (kbhit())
{
Sync = getc();
ServoNo = getc();
ServoPos = getc(); // 0-255
}
// printf("D");
}

// printf("C");
}
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

Some more info
PostPosted: Tue Nov 04, 2003 2:53 am     Reply with quote

I have run the prg for 15 minutes without sending and serial commands to the device. The device is still running fine.

The problem must be related to the combination of serial input and interrupt.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Nov 04, 2003 7:05 am     Reply with quote

I didn't look at the code that closely, but you should add the ERRORS parameter to your #use rs232 statement. I know this can cause you to stop receiving but I am not sure if it would cause the getc() to hangup forever.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 04, 2003 1:13 pm     Reply with quote

Can you post a link to the manufacturer's web site for these servos ?
I can see better ways to do it, but I would like to read the data sheet
first.
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

I found the answer - Strange but true
PostPosted: Tue Nov 04, 2003 7:18 pm     Reply with quote

When I switched from using Timer1 to Timer0 the problem went away. I changed the program a little so that the 20ms loop does not hang up if the getc() gets stuck. Yes I did add ERRORS and RESTART_WDT to #use rs232 but it made no difference on Timer1

So here goes with a working program - ready to be modified for more servos.

Serialservo.h
---------------
#include <16F628.h>
#use delay(clock=10000000)

#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_B2,rcv=PIN_B1,ERRORS,RESTART_WDT)

SerialServo.c
----------------
#include <SerialServo.h>

#define PinServo0 PIN_B4

int PosServo0;
int i = 0;

#int_TIMER0
TIMER0_isr()
{
output_high(PinServo0);

delay_ms(1);

for (i=0;i<=PosServo0;i++)
delay_us(3);

output_low(PinServo0);
}


void main()
{
byte Sync = 0; // 0xFF
byte ServoNo = 0; // 1 - 255

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);

PosServo0 = 127; // default servo pos

output_low(PinServo0);

while (TRUE)
{
if (kbhit())
{
Sync = getc();

if (Sync==0xFF)
{
ServoNo = getc();

if (ServoNo==0)
PosServo0 = getc(); // 0-255
}
}
}
}
johnh



Joined: 03 Jan 2004
Posts: 19
Location: UK - Brighton

View user's profile Send private message

Servo control
PostPosted: Sat Jan 03, 2004 9:23 am     Reply with quote

Hello,

I am a newbie to all this C programming and PIC stuff. As it happens i am doing a project that controls a servo (Hi-Tec MG81) through a serial port. I am going to use VB6 as the control.

My question is to Hillcraft - can you/somebody put comments in the program below so that i can follow it through and understand what is going on.

I would appreciate any help on this that ANYONE can give me.

Also PCM programmer, you mentioned that you could see better ways to do this, what do you mean? I'll see if i can dig out a data sheet

Also (on a roll now!) I was thinking of using a 16F84 but would a 16F628 be better??

Cheers

John

Quote:
Serialservo.h
---------------
#include <16F628.h>
#use delay(clock=10000000)

#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,NOCPD
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_B2,rcv=PIN_B1,ERRORS,RESTART_WDT)

SerialServo.c
----------------
#include <SerialServo.h>

#define PinServo0 PIN_B4

int PosServo0;
int i = 0;

#int_TIMER0
TIMER0_isr()
{
output_high(PinServo0);

delay_ms(1);

for (i=0;i<=PosServo0;i++)
delay_us(3);

output_low(PinServo0);
}


void main()
{
byte Sync = 0; // 0xFF
byte ServoNo = 0; // 1 - 255

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);

PosServo0 = 127; // default servo pos

output_low(PinServo0);

while (TRUE)
{
if (kbhit())
{
Sync = getc();

if (Sync==0xFF)
{
ServoNo = getc();

if (ServoNo==0)
PosServo0 = getc(); // 0-255
}
}
}
}

_________________
There are 10 kinds of people who understand binary, those who do and those who don't
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

Servo Control
PostPosted: Sat Jan 03, 2004 10:44 am     Reply with quote

Johnh,
I love your tagline:

"There are 10 kinds of people who understand binary, those who do and those who don't"

It took about 5 seconds for it to slap me in the face....! Laughing

Dave
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

Servo Controller
PostPosted: Sun Jan 04, 2004 2:28 am     Reply with quote

Hi John,

I will document the program for you - It is 10am here in Cape Town, South Africa. We are off to the beach so I'll do it early this evening.

If you mail me on adrianh@hillcraft.co.za then I will send you the entire project.

Regards
Adrian
johnh



Joined: 03 Jan 2004
Posts: 19
Location: UK - Brighton

View user's profile Send private message

PostPosted: Sun Jan 04, 2004 6:33 am     Reply with quote

Cheers Adrian, I have emailed you.

"It took about 5 seconds for it to slap me in the face....! "

Dave surely you mean 101 seconds !! Very Happy

Regards

John
_________________
There are 10 kinds of people who understand binary, those who do and those who don't
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