FREE Servo RC Controller code [ALMOST WORKING]

General discussion on mikroC.
Author
Message
spookyrufus
Posts: 71
Joined: 10 Jun 2006 13:45

#46 Post by spookyrufus » 17 Jun 2008 20:35

This vanishes my need of an oscilloscope (for now).My heart beats at 8Mhz now :D
Great. Thanks xor, I think this entire community shuld be graceful to you.

fweinrebe
Posts: 28
Joined: 29 May 2008 13:22
Location: Namibia
Contact:

#47 Post by fweinrebe » 17 Jun 2008 23:15

Using the USART Terminal, how does the data string looks like to test this code? Let's assume I want to position al 12 servos in the centre position.

Or is it tested in some other way?
Fritz

Proud owner of EasyPic5, PICPLC16 V6, PIC32 MMB, PIC40 and registered mikroC PIC and PIC32 compiler.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#48 Post by xor » 17 Jun 2008 23:28

Read the notes at the top of the program. Send whatever bytes you wish. Each USART byte has the Servo ID (upper 4 bits) and its Position (lower 4 bits). Super simple. From Position0 each additional rotary position is 80us each.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

martinv
Posts: 2
Joined: 19 Nov 2010 07:13

Re: FREE Servo RC Controller code [ALMOST WORKING]

#49 Post by martinv » 20 Nov 2010 08:45

Sorry to bring back old thread. I'm trying to get this code by Xor a few posts back to compile but I get these errors :

Code: Select all

0 357 main function is not defined main function is not defined
0 102 Finished (with errors): 20 Nov 2010, 01:32:40 12servo_2error.mcppi
I'm very new to programming and micros and trying to learn. So far i've mostly just figured out how to load and make slight modifications to code.

This is the code:

Code: Select all

/*
//'*******************************************************************************
//'  Program to Control 12- 50Hz Servos @ 16 Positions Each With USART Updates
//'*******************************************************************************
//'           Code compiled with mikroC 8.2 and tested on 16F887 @ 8MHz
//'           By Warren Schroeder  June 5, 2008
//'*******************************************************************************
//'*******************************************************************************
//'
//'           Each USART RX Byte:
//'              Servo ID        = Upper 4 bits  (0 to 11)   12 Servos
//'              Servo Position  = Lower 4 bits  (0 to 15)   16 Positions
//'
//'   HITECH-HS422 50Hz Servo: 940us to 2140us for min and max servo rotation
//'                            16 positions (940us= Position#0 + (15 x 80us))
//'                            Position#7 = 1500us Center
//'
//'       Timer1 Setup for 1us ticks  (Prescaler=2)
//'
//'       CCP1 is set up for Special Event Compare Mode
//'       On Compare-Match between TIMER1 and CCPR1L:CCPR1H registers
//'       CCP1IF is Set (=1) and TIMER1 is RESET (=0)
//'
//'
//'*******************************************************************************
//'*******************************************************************************
*/

#define Servo0  PORTB.f0
#define Servo1  PORTB.f1
#define Servo2  PORTB.f2
#define Servo3  PortB.f3
#define Servo4  PORTB.f4
#define Servo5  PORTB.f5
#define Servo6  PORTB.f6
#define Servo7  PORTB.f7
#define Servo8  PORTD.f0
#define Servo9  PORTD.f1
#define Servo10 PORTD.f2
#define Servo11 PORTD.f3

unsigned int register volatile
   CCPR1          absolute 0x0015               ;
unsigned short AllOn                       ;
unsigned short frame80                     ;
unsigned short t0                          ;
unsigned short t1                          ;
unsigned short t2                          ;
unsigned short ServoPos[12]                ;  //  12 Servo Position Array from RX
unsigned short ServoWrk[12]                ;  //  12 Servo Position Work Array


void interrupt() {
    if (AllOn) {                              //  If AllOn flag=true then
           PORTB = 255                     ;  //  All PORTB servos ON
           PORTD = 15                      ;  //  All PORTD servos ON
           AllOn = ~AllOn                  ;  //  reset AllOn flag
           frame80 = 0                     ;  //  80us frame counter
           CCPR1 = 940                     ;  //  940us = 0 position
           for (t2=0;t2<12;t2++) {            //  load work array from USART RX array
              ServoWrk[t2] = ServoPos[t2  ];  //
           }
    }                                         //
    else {                                    //
           CCPR1 = 80                      ;  //  80us frame delay (x 15 total = 1200us)
           FSR = (unsigned short)&ServoWrk ;  //  servo pointer.. point to first servo pos
           if (INDF == 0) Servo0  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo1  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo2  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo3  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo4  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo5  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo6  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo7  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo8  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo9  = 0      ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo10  = 0     ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter
           FSR++                           ;  //  point to next servo
           if (INDF == 0) Servo11  = 0     ;  //  turn servo OFF on 0
           INDF--                          ;  //  decrement position counter

           if (++frame80 == 16) {             //  finished 15 80us position periods?
               CCPR1 = 17900               ;  //  load remaining time for 20ms interrupt
               AllOn = ~AllOn              ;  //  turn ON all servos at next 20ms interrupt
           }
    }
    PIR1.CCP1IF = 0                        ;  //  clear CCP1 interrupt flag
}


void CCP1_Setup() {
    CCP1CON      = 11         ;  //  CCP1 Compare MODE with special event trigger; resets Timer1 on match
    CCPR1        = 65000      ;  //  preload for 65ms delay before servo startup
    T1CON        = 16         ;  //  Timer1 Prescaler = 2 = 1us ticks
    PIE1.CCP1IE  = 1          ;  //  Enable CCP1 interrupt
    PIR1.CCP1IF  = 0          ;  //  Clear CCP1 Interrupt Flag
    INTCON       = 192        ;  //  Global & Peripheral interrupts enabled
    T1CON.TMR1ON = 1          ;  //  Start Timer1...
}



void main() {

    ANSEL   = 0               ;   //  disable adc's
    ANSELH  = 0               ;   //  disable adc's
    CM1CON0 = 0               ;   //  disable comparators
    CM2CON0 = 0               ;   //  disable comparators
    PORTB   = 0               ;
    PORTD   = 0               ;
    TRISB   = 0               ;
    TRISD   = 0               ;
    AllOn   = 255             ;   //  all servos ON flag
    

    USART_INIT(19200)                 ;
    CCP1_Setup()                      ;
    
    
         for (t0=0;t0<12;t0++) {         //  pre-load servo array with some values
             ServoPos[t0] = t0        ;  //
          }
          
          
    while(1) {
       if (USART_DATA_READY) {           // test for RX byte
          t0 = USART_READ()            ; // save new RX byte
          t1 = t0 >> 4                 ; // extract servo ID from RX byte
          ServoPos[t1] =  t0 & 15      ; // save Position Value per ID
        }
    }

}

martinv
Posts: 2
Joined: 19 Nov 2010 07:13

Re: FREE Servo RC Controller code [ALMOST WORKING]

#50 Post by martinv » 20 Nov 2010 08:53

OK, I realized the code was not loaded as a "Source File". (I just started with Mikroc, am more used to MPLAB)

Now that it is though I get more errors:

Code: Select all

43 393 'CCPR1' Identifier redefined 'CCPR1' Identifier redefined
120 324 Undeclared identifier 'USART_DATA_READY' in expression Undeclared identifier 'USART_DATA_READY' in expression
137 324 Undeclared identifier 'USART_INIT' in expression Undeclared identifier 'USART_INIT' in expression
147 324 Undeclared identifier 'USART_DATA_READY' in expression Undeclared identifier 'USART_DATA_READY' in expression
148 324 Undeclared identifier 'USART_READ' in expression Undeclared identifier 'USART_READ' in expression
0 102 Finished (with errors): 20 Nov 2010, 01:48:53 12servo_2error.mcppi

Post Reply

Return to “mikroC General”