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

FT90R servo test program

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
PrinceNai



Joined: 31 Oct 2016
Posts: 384
Location: Montenegro

View user's profile Send private message

FT90R servo test program
PostPosted: Sun Dec 11, 2022 10:22 am     Reply with quote

Hi, below is a very simple test program for 9g FT90R 360 deg servo. No libraries, nothing. It uses Timer0 and CCP1 interrupts to generate pulses. It is done for 32MHz clock, for any other some modifications must be done. 8ms time window between two pulses.


Code:

#include <18F46K22.h>
#device ADC=10

#FUSES NOWDT                                                   //No Watch Dog Timer
#FUSES NOPUT

#device ICD=TRUE
#use delay(internal=32000000)

#byte CCPR1H=getenv("SFR:CCPR1H")
#byte CCPR1L=getenv("SFR:CCPR1L")

#DEFINE SERVO_PIN PIN_A6

int16 DesiredPulseWidth = 900;                                   // desired pulse width in us
int8 Right = TRUE;
int8 ChangeSpeed  = 0;

// ************************************************************
// ************************************************************
#INT_TIMER0
void  TIMER0_isr(void)
{
   clear_interrupt(INT_CCP1);                                  // because of possible different speeds between TMR0 and TMR1, Timer1 might have already set CCP1IF, so prevent jumping to the CCP1 interrupt
   
// 1 in DesiredPulseWidth means a pulse of 1us (with 32MHz clock)
// Meant to be used with real numbers from 900 - 2000     
   set_timer1((-8 * DesiredPulseWidth));                       // preload Timer1 to trigger CCP1 interrupt in DesiredPulseWidth microseconds
   enable_interrupts(INT_CCP1);                                // enable interrupt on CCP1 match                                                     
   output_high(SERVO_PIN);                                     // SERVO_PIN high, CCP1 interrupt will set it low
   
   ChangeSpeed  = 1;                                           // remind main it has work to do
}

// ------------------------------------------------------------
#INT_CCP1
void  CCP1_isr(void)
{
   clear_interrupt(INT_CCP1);
   disable_interrupts(INT_CCP1);                               // disable interrupt until TMR0 interrupt enables it in the next time frame
   output_low(SERVO_PIN);
}
// ************************************************************
// ************************************************************

void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);                    //at 32MHz 8,1 ms overflow, also time frame for servo cycle
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);                     //8,191875 ms overflow. 0,125us resolution
   setup_ccp1(CCP_COMPARE_INT);

   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   
   CCPR1H = 0;                                                 // preset CCP1 registers
   CCPR1L = 0;
   output_low(SERVO_PIN);

   while(TRUE)
   {
   
      if(ChangeSpeed){
         ChangeSpeed = 0;                                      // reset flag

// SOME TEST, SPIN THE MOTOR LEFT AND RIGHT AT DIFFERENT SPEEDS
         if(Right){
            DesiredPulseWidth++;
            if(DesiredPulseWidth == 2000){
               Right = FALSE;
            }   
         }
         else{
            DesiredPulseWidth--;
            if(DesiredPulseWidth == 900){
               Right = TRUE;
            }     
         }           
     
      }     // if ChangeSpeed

   }        // while TRUE

}           //main
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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