Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Need help urgent, Having a hard time with timers and compare modules

Status
Not open for further replies.

edwinvarghese

Junior Member level 1
Junior Member level 1
Joined
Feb 27, 2014
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
127
I'm trying to modify the below code for my project it is written in MIkro C by late Mr.Warren Schroeder. I'm having a hard time understanding what happening inside interrupt routines :sad::-|. Someone please help with the following code.
Thanks in advance......
Code:
/* 
 //'******************************************************************************* 
 //'  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 = 18800               ;  //  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 
         } 
     } 
 
 }

please help me with this, Thanks in advance ......
 

@ Ravi,
First of all thank you friend :). I understood the code:-D
I was new to timers and interrupts that was problem, Now i understood the working of Timers and Interrupts :)
anyway thank you for your helping mentality............
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top