I was wondering if someone could explain to me the initialization of servo1 and servo2, because I do not see how a delay is provided so that the servo moves in a certain position. I understand the interrupts and timers, how they are generated and preloaded, rescaled that is ok. I just really don't get how the servos get their pulse length (delay)...based on what????
int servo0 = 0xF63B; // Servo 0
int servo1 = 0xF077; // Servo 1
These two lines of code initialize the variables that hold the amount that the two servos should delay while the PWM signal is +5v (aka logic 1). The values put into them initiatially are just randomly chosen as they are just changed later on when the program operates.
The actual delay calculation....
If you look at the timer initialization it has a 1:2 prescalar. This means the timer counts twice as fast than normal.
Known values:
System Clock Frequency - 20MHz
Instruction Frequency - 20MHz/4 = 5 MHz
Max Timer Value (16 bit) - 0xFFFF
Set Timer Value - 0xF63B (servo 0) & 0xF077 (servo 1)
Prescalar - 2
This formula is actually in the data sheet. Take a look at it if you need a better understanding of what is going on. Otherwise it's pretty straight forward.
Thank you so much for answering! I am in such a mess right now with my final year project :( . I have a demonstration in two weeks and I am having trouble with my code .... Thank you so much for the explanation, I really appreciate it! I have one more question where I am totally lost, well two questions.
1. Where in the program do you bring PORT low?? If you have 5 servos and you want to bring the port high when timer 1 is set, and then bring it low at overflow, where exactly in the code do you do it?
2. I also get errors when I initialize the two timer1 registers with a different value in each switch statement :( . I am using PIC16F877A and I just initialize it like so:
TMR1H:TMR1L = some value; (0xf892 for example)
and then I do the same thing in the next switch statement and I get the same error, that I reinitialized the timer....
:?:
Thank you so much for your help! You are saving my grade :( I am at the point of desperation! :(
If you're using my code (or modifications) in your senior project be kind enough to mention it in your references in your project write up.
(1) The servo PWM's switch in this statement:
Code:
switch(count){
case 1: PORTA = 0x08; // First Stage
WriteTimer1( servo3 );
break;
case 2: PORTA = 0x04; // Left Gripper
WriteTimer1( servo1 );
break;
case 3: PORTA = 0x00;
PORTC = 0x02; // Swivel/Rotate
WriteTimer1( servo4 );
break;
case 4: PORTC = 0x00;
PORTA = 0x02; //Right Gripper
WriteTimer1( servo0 );
break;
case 5: PORTA = 0x00;
PORTC = 0x01; // Second Stage
WriteTimer1( servo2 );
break;
case 6: PORTC = 0x00; //Future Servo?
WriteTimer1( 0 );
break;
}
Case 1 turns PortA Pin 4 On & Resets the timer1.
Case 2 turns PortA Pin 4 Off, PortA Pin 3 on & Resets the timer1.
Case 3 turns PortA Pin 3 Off, PortC Pin 2 on & Resets the timer1.
Case 4 turns PoirtC Pin 2 Off, PortA Pin 1 on & Resets the timer1.
etc...
etc...
(2) It's a good idea not to initialize stuff within the switch statement. That will give compiler errors. Do all your initializations at the top of the program like I did. Modify them in the switch statement like I do. If this doesn't answer your second question feel free to copy and paste some code & the mplab error. Please not your entire program.
I am using the idea of using interrupts and switch statements that you did in your code, I will certainly put your code as a reference!
I am not using MPLAB and therefore I do not have functions such as writetimer1()....I can only give new values to registers....I am using MikroC...I will try again and tell you what happens.
I have a question...which might fix my problem...how do you I assign to a variable the first byte of a hex number in this form '0xf98E' ??? If I can do that I can fix the problem, cause I need to reassign the value of Timer1...
I don't have much time to look over your code but my initial thought is either that you can't define TMR1H:TMR1L = servoX; inside the switch. Or you have to define the TMR1H and TMR1L registers seperately. Try to do it without the switch statement, like with if statements and see if you get the same error.
It looks like the PIC16F877A has a two stage pipeline which means that the actual instruction frequency would be -->
20 MHz / 2
You can double check this by making a simple delay circuit to blink an led. Make the code so it should blink once a second then just see if it does. After you got that done then you're ready to move on.
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