|
|
View previous topic :: View next topic |
Author |
Message |
Lost_whit_servo Guest
|
servo pwm problem, what´s wrong, where´s the errors? |
Posted: Sun Apr 20, 2008 12:22 pm |
|
|
I want to drive one or more rc servo but...I can´t find where´s the error(s).
Can some one help me?
thanks in advance
Code: |
#include <16F628.h>
#use delay(clock=20000000) // Fosc
#fuses HS,NOWDT,NOPUT
#use fast_io(B)
#bit RB1 = 0x06.1
#bit RB2 =0x06.2
static int16 k=0;
#INT_TIMER0
void tmr0_int()
{
RB2 = !RB2; //int is working?
k++;
set_timer0(206); // 10useg
}
void main(void)
{
int i;
set_tris_b(0);
output_low(PIN_B1);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
set_timer0(206); // 10useg int
while(1)
{
for(i=90;i<=150;i=i+30) //900useg, 1200useg, 1500useg
{
if(k<=i)
{
bit_set(*RB1,1);
}
while(k<=2000) //itxaron 200mseg bete arte
{
bit_clear(*RB1,0);
k=0;
}
}
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Apr 20, 2008 1:41 pm |
|
|
I'm not really sure what you want your code to do but there are several errors: Code: | while(k<=2000) //itxaron 200mseg bete arte
{
bit_clear(*RB1,0);
k=0;
} | 1) This loop will never exit. Even with the timer interrupt increasing the value of K it's value will soon be reset to 0 and never reach 2000.
2) This code for changing the value of an I/O-port is completely wrong: *RB1 means 'take the contents of the variable pointed to by RB1', with RB1 being a 1-bit variable this will be either the contents of address 0 or 1. Completely not what you intend to do.
Possible methods to clear an I/O-port are: Code: | Method 1:
#byte PORT_B = 0x06
#bit RB1 = PORT_B.1
RB1 = 0;
Method 2:
bit_clear(PORT_B, 1);
Methode 3:
output_low(PIN_B1); | I like method_3 because you don't have to manually specify the port's memory address which makes it very easy to change the program to another processor.
Code: | void tmr0_int()
{
RB2 = !RB2; //int is working?
k++;
set_timer0(206); // 10useg
} | An interrupt routine that has to execute every 10us is extremely fast and will consume all your processor's execution time.
To understand this you have to know that an interrupt has a lot of overhead for saving all registers on entering the interrupt and restoring the registers on exit. Depending on the number of registers this takes about 45 instructions on a PIC16 and 80 on a PIC18.
Your timer interrupt routine takes about 10 instructions, a total of: 45 + 10 = 55 instructions.
The processor is running at 20MHz, or 5 instructions per us. Meaning it takes 11us to execute 1 interrupt and leaving no time to execute your main program.
Servo programs have been discussed many times before on this forum, I suggest you use the search function to find those discussions. Here is one link: http://www.ccsinfo.com/forum/viewtopic.php?t=30139 |
|
|
|
|
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
|