|
|
View previous topic :: View next topic |
Author |
Message |
Lockhead
Joined: 16 Dec 2008 Posts: 1 Location: Malaysia
|
PIC Timing with servo |
Posted: Wed Jul 25, 2012 11:19 pm |
|
|
Hi,
I have written a basic program controlling five rc servo and basically the program works for pic16F877a. But when I am changing PIC, some PIC results in no movement at the servo (I'm trying PIC16F1826).
I thought that, by minor modification on the header & fuses relate to the timing then the program will work exactly the same regarding on frequency oscillator. When I trace the program, function "delay_ms(1)" only gives me 250ns instead of 1 miliseconds. (simulation by proteus).
I've tried LP,XT,HS,EC FUSES but none seems working with freq 20Mhz and 32Mhz. I've also tried the same program structure with MPLAB but yield the same result. Nevertheless, I'm planning to build the hardware soon to verify whether it is just bug in my proteus simulation or is it something wrong with my program flow.
1) Is it just a bug in simulation that the program will work on the hardware?
2) Based on this experience, I'm not sure on how reliable is the delay_ms() and delay_us() function when cmpared to other PIC in the same 8-bit family?
3) Moreover, does internal and external oscillator provide significant degree of accuracy?
4) Currently my program are based on delay_ms() and delay_us() function. Should I used cycle count instead of timing count?
5) From what I have read, for controlling simultaneous multiple servo I have to use timer interrupt right? Advice me on the program structure
... Sorry for the long post
Code: |
#include <16f877a.h>
#FUSES hs,noprotect,nowdt,nolvp,NOBROWNOUT,PUT
#use delay(clock=20000000)
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9
#define SERVO1 pin_b0
#define SERVO2 pin_b1
#define SERVO3 pin_b2
#define SERVO4 pin_b3
#define SERVO5 pin_b4
void main(){
set_tris_b(0x00);
while(1)
{
output_high(SERVO1); //90 degree
delay_ms(1);
delay_us(500);
output_low(SERVO1);
output_high(SERVO2); //20 degree
delay_ms(1);
delay_us(111);
output_low(SERVO2);
output_high(SERVO3); //150 degree
delay_ms(1);
delay_us(833);
output_low(SERVO3);
output_high(SERVO4); //70 degree
delay_ms(1);
delay_us(389);
output_low(SERVO4);
output_high(SERVO5); //180 degree
delay_ms(2);
output_low(SERVO5);
}
}
|
This is the header I used for PIC16F1826
Code: |
#include <16f1826.h>
#FUSES ec,noprotect,nowdt,nolvp,NOBROWNOUT,PUT
#use delay(clock=20000000)
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Jul 26, 2012 5:23 am |
|
|
from my experience..
1) NEVER ever trust Proteus. It's a piece of 'junkware'. I could expand on that but really, just delete from your PC.
2)The delay_functions are very good and have worked fine for me during the past 2 decades. I've never needed nanosecond timing, so maybe others can reply with actual numbers.
3)The internal oscillator is not 'stable' compared to an external crystal or external oscillator. While 'fine' for simple, non time critical projects, you'll get better results using a crystal and 2 caps, best results from an external oscillator in a 'can'.
4)delay_ms(xxx) and delay_us(yyy) MUST be completed before the PIC will do anything else so on all but the simplest projects it's best to use a 'timer based' ISR to control the program.
5)Yes, better to use ISR for your servo controlling. There are several ways to do it. Though with RC servos you don't have any feedback, making the program much easier to write.
hth
jay |
|
|
|
|
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
|