The software for this tutorial was used in the wooden menace and the sharpie dotifier projects. The portion of the software below shows the interrupt routine which decides if a pulse should occur or not.
Timer0 is used to refresh the system every 20 miliseconds or at 50 Hz. Timer1 is used to cycle through and create a pulse for each servo that we are using. This system only has 2 servos so there are only two unique states that the system can switch between to send servo PWM pulses.
PWM Control - Interrupt Routine
------------« Begin Code »------------
void InterruptHandlerHigh() // Declaration of InterruptHandler { //Run When Timer overflows from FFFF->0000 if(INTCONbits.TMR0IF) //If TMR0 interrupt flag set { WriteTimer0( 0x3CAF ); //Reset Timer0 for 20mS Delay WriteTimer1( 0xFC77 ); //Reset Timer1 for 1mS Delay count = 0; //Reset Timer1 Interrupt Counter INTCONbits.TMR0IF = 0; //Clear TMR0 Flag } if(PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1) { //If Timer1 interrupts, do your thing... count++; //Increment Timer1 Interrupt Counter switch(count){ //Choose which servo to modify: case 1: PORTD = 0x01; WriteTimer1( servo0 ); //Right Eyebrow break; case 2: PORTD = 0x02; WriteTimer1( servo1 ); //Left Eyebrow break; case 3: PORTD = 0x00; WriteTimer1( 0 ); break; } PIR1bits.TMR1IF = 0; //Clear Timer1 flag } INTCONbits.GIE = 1; //Re-enable all interrupts }
------------« End Code »------------
Everytime timer0 interrupts with the 20mS delay, the system refreshes. Everytime timer1 interrupts a servo pulse is cleared and a new one triggered. This type of system can control up to 10 servos in its current form. An additional timer or modiifed code could be added to control even more servos.
Download the software above and get it onto your PIC, so that you can test out the system! Let's continue forward and take a look at how the system works when running and what to expect from the low cost approach to animatronics.