Whats wrong with this code!

Status
Not open for further replies.

edwinvarghese

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
Code:
//CONTROLLER: PIC16F877A @ 16MHz Compiler MikroC
//original code : Warren Schroeder  June 5, 2008
#define Servo0  PORTB.f0
#define Servo1  PORTB.f1
#define Servo2  PORTB.f2
#define Servo3  PortB.f3
unsigned int register volatile
unsigned int o=7,q=7,m=7,n=1               ;
unsigned short AllOn                       ;
unsigned short frame80                     ;
unsigned short t0                          ;
unsigned short t1                          ;
unsigned short t2                          ;
unsigned short ServoPos[4]                 ;
unsigned short ServoWrk[4]                 ;
char ch                                    ;

void interrupt() {
    if (AllOn) {                              //  If AllOn flag=true then
           PORTB = 15                     ;  //  All PORTB servos ON
           AllOn =0                        ;  //  reset AllOn flag
           frame80 = 0                     ;  //  80us frame counter
           CCPR1 = 940                     ;  //  940us = 0 position
          for (t2=0;t2<4;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

           if (++frame80 == 16)               //  finished 15 80us position periods?
           {
               CCPR1 = 17900               ;  //  load remaining time for 20ms interrupt
               AllOn = 1                   ;  //  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        = 32         ;  //  Timer1 Prescaler = 4 = 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 servoup1 ()
{
   o++;
   ServoPos[1] =  o   ;
   if(o==15)
   o=o-2;
   }
   void servodown1 ()
{
   o--;
   ServoPos[1] = o    ;
   if(o==0)
   o=o+2;
   }

void main() {

    ADCON1 = 6                ;
    CMCON  = 7                ;
    PORTB   = 0               ;

    TRISB   = 0               ;
    AllOn   = 1               ;   //  all servos ON flag


    UART1_Init(19200)         ;
    CCP1_Setup()              ;

         for (t0=0;t0<4;t0++)
         {
             ServoWrk[t0] = 7        ;  //  pre-load servo array with neutral position
          }


    while(1)
    {
              if (UART1_Data_Ready())  // If data is received,
              {
               ch = UART1_Read();     // read the received data,
              }

              switch (ch)
              {
              case 49: servoup1();
              break;
              case 50:servodown1();
              break;
              /*default: break;*/
              }
        }
    }
The code was meant to rotate servo1 by step by step but instead of servo1, servo0 rotates! Even on changing to servo2 or servo3 only servo0 rotates on below function
]
Code:
void servoup1 ()
{
   o++;
   ServoPos[1] =  o   ;
   if(o==15)
   o=o-2;
   }
Proteus:
 

Attachments

  • Untitled.jpg
    242.6 KB · Views: 103

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…