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 CCS Technical Support

Problem with 16F877A ADC after get value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Problem with 16F877A ADC after get value
PostPosted: Wed Mar 26, 2008 10:23 am     Reply with quote

Hi fellow forumers, i've got a weird problem here regarding my code. I'm running it on a PIC16F877A to control 4 servo motors with buttons n thru PC interfacing. Besides that, i have placed potentiometers to obtain feedback positions. Everything was working fine except for the ADC.

Here comes the problem. As i turn on the serial port, my servos are reacting to the instructions i send from my PC. But after i press the key to obtain the position readings, the microcontroller STOPS responding to my instructions from PC and sort of HUNG. But it is still reacting to manual controls by pushing the push buttons. Everytime this happens, i've to reset the circuit or turn it off and on back and then the same thing happens.

My compiler version is 4.017

Here's the code and sorry for the messiness =):

Code:

#include <16F877A.h>
#device ADC=8
#include <string.h>
#include <stdlib.h>
#fuses HS, NOLVP
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, PARITY=N)

#define XTAL_FREQUENCY  20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 40)

#define PWM_PIN1  PIN_C0
#define PWM_PIN2  PIN_C1
#define PWM_PIN3  PIN_C2
#define PWM_PIN4  PIN_C3

#define BTN_CEN1 PIN_B7           // base
#define BTN_R1 PIN_B6
#define BTN_L1 PIN_B5

#define BTN_CEN2 PIN_B4            //shoulder
#define BTN_R2 PIN_B3
#define BTN_L2 PIN_B2//

#define BTN_CEN3 PIN_D7            //elbow
#define BTN_R3 PIN_D6
#define BTN_L3 PIN_D5

#define BTN_CEN4 PIN_D4            //wrist
#define BTN_R4 PIN_D3
#define BTN_L4 PIN_D2

#define LOOPCNT 400

int i;
int16 widthC, widthL, widthR;
int16 temp0,temp1,actual0,actual1;
float width01, width02, width03, width04, widthL1, widthR1;
float step=1.0;
static int16 loop = LOOPCNT;
static int16 pulse1, pulse2, pulse3, pulse4;

char rec;
char string0[10];
char string1[10];

unsigned int fl_cen1, fl_right1, fl_left1, fl_pulse1;
unsigned int fl_cen2, fl_right2, fl_left2, fl_pulse2;
unsigned int fl_cen3, fl_right3, fl_left3, fl_pulse3;
unsigned int fl_cen4, fl_right4, fl_left4, fl_pulse4;
unsigned int fl_feed;

//---------------------------------------------------------------------------
void tick_interrupt(void);
void init_main();
void Initialize_RTC(void);
void move_servo();
void check_button();
void TIMER1_isr(void);
void pwm();
void main();
void adc_check();

//============================================================================

void init_main()
 {
   
   set_tris_c(0x00);                // servo port
   output_c(0x00);
   
   set_tris_b(0b1111111);            //button port set to all input
   output_b(0x00);
   
   fl_cen1 = FALSE;
   fl_right1 = FALSE;
   fl_left1 = FALSE; 
   fl_pulse1 = 0;
   
   fl_cen2 = FALSE;
   fl_right2 = FALSE;
   fl_left2 = FALSE; 
   fl_pulse2 = 0;
   
   fl_cen3 = FALSE;
   fl_right3 = FALSE;
   fl_left3 = FALSE; 
   fl_pulse3 = 0;
   
   fl_cen4 = FALSE;
   fl_right4 = FALSE;
   fl_left4 = FALSE; 
   fl_pulse4 = 0;
   
   fl_feed = FALSE;
   fl_graph = FALSE;
 }
 //***************************************************************************
 void Initialize_RTC(void)//////////*********************************************
{                 
  setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
                                              // exactly every 65536 clock cycles
                                              // (about 76 times per second) 

                                         
  enable_interrupts( INT_TIMER1 );            // Start RTC
}
//==================================================================================================

void btn1_check()
{
   if(fl_left1 == TRUE)
    {
       fl_left1 = FALSE;
       width01=width01-step;
       delay_ms(50);
       if(width01<widthL)
       {
          width01=width01+step;
          delay_ms(50);
       }
    }   
    if(fl_right1 == TRUE)
    {
       fl_right1 = FALSE;
       width01=width01+step;
       delay_ms(50);
       if(width01>widthR)
       {
         width01=width01-step;
         delay_ms(50);
       }
    }   
    if(fl_cen1 == TRUE)                 
    {
         if(width01<widthC)
         {
           for(i = width01; i < widthC; i++)
           {
             fl_cen1 = FALSE;
             width01=width01+1;
             delay_ms(50);
           }
         }
         else if(width01>widthC)
         {
          for(i = width01; i > widthC; i--)
          {
             fl_cen1 = FALSE;
             width01=width01-1;
             delay_ms(50);
           }
         }
         else if(width01 == widthC)
         {
           width01 = widthC;
           delay_ms(50);
         }
     }
 }
//--------------------------------------------------------------------
void btn2_check()
{
   if(fl_left2 == TRUE)
    {
       fl_left2 = FALSE;
       width02=width02-step;
       delay_ms(50);
       if(width02<widthL1)
       {
          width02=width02+step;
          delay_ms(50);
       }
    }   
    if(fl_right2 == TRUE)
    {
       fl_right2 = FALSE;
       width02=width02+step;
       delay_ms(50);
       if(width02>widthR1)
       {
         width02=width02-step;
         delay_ms(50);
       }
    }   
    if(fl_cen2 == TRUE)                 
    {
         if(width02<widthC)
         {
           for(i = width02; i < widthC; i++)
           {
             fl_cen2 = FALSE;
             width02=width02+1;
             delay_ms(50);
           }
         }
         else if(width02>widthC)
         {
          for(i = width02; i > widthC; i--)
          {
             fl_cen2 = FALSE;
             width02=width02-1;
             delay_ms(50);
           }
         }
         else if(width02 == widthC)
         {
           width02 = widthC;
           delay_ms(50);
         }
     }
 }
 //-------------------------------------------------------
 void btn3_check()
{
   if(fl_left3 == TRUE)
    {
       fl_left3 = FALSE;
       width03=width03-step;
       delay_ms(50);
       if(width03<widthL)
       {
          width03=width03+step;
          delay_ms(50);
       }
    }   
    if(fl_right3 == TRUE)
    {
       fl_right3 = FALSE;
       width03=width03+step;
       delay_ms(50);
       if(width03>widthR)
       {
         width03=width03-step;
         delay_ms(50);
       }
    }   
    if(fl_cen3 == TRUE)                 
    {
         if(width03<widthC)
         {
           for(i = width03; i < widthC; i++)
           {
             fl_cen3 = FALSE;
             width03=width03+1;
             delay_ms(50);
           }
         }
         else if(width03>widthC)
         {
          for(i = width03; i > widthC; i--)
          {
             fl_cen3 = FALSE;
             width03=width03-1;
             delay_ms(50);
           }
         }
         else if(width03 == widthC)
         {
           width03 = widthC;
           delay_ms(50);
         }
     }
 }
 //-----------------------------------------------------------
 void btn4_check()
{
   if(fl_left4 == TRUE)
    {
       fl_left4 = FALSE;
       width04=width04-step;
       delay_ms(50);
       if(width04<widthL)
       {
          width04=width04+step;
          delay_ms(50);
       }
    }   
    if(fl_right4 == TRUE)
    {
       fl_right4 = FALSE;
       width04=width04+step;
       delay_ms(50);
       if(width04>widthR)
       {
         width04=width04-step;
         delay_ms(50);
       }
    }   
    if(fl_cen4 == TRUE)                 
    {
         if(width04<widthC)
         {
           for(i = width04; i < widthC; i++)
           {
             fl_cen4 = FALSE;
             width04=width04+1;
             delay_ms(50);
           }
         }
         else if(width04>widthC)
         {
          for(i = width04; i > widthC; i--)
          {
             fl_cen4 = FALSE;
             width04=width04-1;
             delay_ms(50);
           }
         }
         else if(width04 == widthC)
         {
           width04 = widthC;
           delay_ms(50);
         }
     } 
 }
 
//========================================================
 void adc_check()
 {
  if(fl_feed==TRUE)
   {
   fl_feed = FALSE;
   set_adc_channel( 0 );
   delay_us(10);
   temp0=read_adc() ;
   actual0=((float)temp0/255)*360;
   itoa(actual0,10,string0);
   puts(string0);

   set_adc_channel( 1 );
   delay_us(10);
   temp1=read_adc() ;
   actual1=((float)temp1/255)*360;
   itoa(actual1,10,string1);
   puts(string1);
   }
 } 
 
//=========================================================================================
void main()
{
   init_main();
   widthL = 15;  //0.7 ms duty cycle
   widthC = 30;  //1.5 ms duty cycle
   widthR = 46;  //2.3 ms duty cycle
   
   widthL1 = 20;  //shoulder config
   widthR1 = 30;
   
   width01 = widthC;
   width02 = widthC;
   width03 = widthC;
   width04 = widthC;
   
   Initialize_RTC();
     
   setup_adc_ports( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );
   setup_counters(RTCC_INTERNAL | RTCC_8_BIT, RTCC_DIV_1); // (20MHz / (256*4*1)) = 20KHz
   enable_interrupts(INT_RTCC);                            // equals 0.05ms per interrupt
   enable_interrupts(GLOBAL);                              // 0.05ms*400 = 20ms
   
   while(1)
   {
     btn1_check();
     btn2_check();
     btn3_check();
     btn4_check();
     adc_check();
   }
}
//====================================================================================
#INT_RTCC
void tick_interrupt(void)
{
  if(--loop == 0 )
  {
    loop = LOOPCNT;
    pulse1 = width01;
    pulse2 = width02;
    pulse3 = width03;
    pulse4 = width04;
  }
  if(pulse1>0)
  {
    output_high(PWM_PIN1);
    pulse1=pulse1-1;
  }
  if(pulse1<=0)
  {
    output_low(PWM_PIN1);
  }
 
 
  if(pulse2>0)
  {
    output_high(PWM_PIN2);
    pulse2=pulse2-1;
  }
  if(pulse2<=0)
  {
    output_low(PWM_PIN2);
  }
 
 
  if(pulse3>0)
  {
    output_high(PWM_PIN3);
    pulse3=pulse3-1;
  }
  if(pulse3<=0)
  {
    output_low(PWM_PIN3);
  }
 
 
  if(pulse4>0)
  {
    output_high(PWM_PIN4);
    pulse4=pulse4-1;
  }
  if(pulse4<=0)
  {
    output_low(PWM_PIN4);
  }
}


#INT_TIMER1  //////*************************************************************
void TIMER1_isr(void)
{
  set_timer1(65535);
 
  if(kbhit())
  {
   rec=getc(); 
  }
 
  switch(rec)
  {
   case 'h':                  //base centre
     fl_cen1 = TRUE;
     fl_cen2 = TRUE;
     fl_cen3 = TRUE;
     fl_cen4 = TRUE;
     rec = NULL;
     break;
   case 'q':
     fl_right1 = TRUE;
     rec = NULL;
     break;
   case 'a':
     fl_left1 = TRUE;
     rec = NULL;
     break;

   case 'w':                  //shoulder
     fl_right2 = TRUE;
     rec = NULL;
     break;
   case 's':
     fl_left2 = TRUE;
     rec = NULL;
     break;

   case 'e':                  //elbow
     fl_right3 = TRUE;
     rec = NULL;
     break;
   case 'd':
     fl_left3 = TRUE;
     rec = NULL;
     break;

   case 'r':                     //gripper
     fl_right4 = TRUE;
     rec = NULL;
     break;
   case 'f':
     fl_left4 = TRUE;
     rec = NULL;
     break;
     
   case 't':         // feedback button
      fl_feed = TRUE;
      rec = NULL;
      break;
     
   default:
     break;
  }

  if(input(BTN_CEN1) ==0)
   {
     fl_cen1 = TRUE;
   }   
  if(input(BTN_R1) ==0)
   {
     fl_right1 = TRUE;
   }   
  if(input(BTN_L1) ==0)
   {
     fl_left1 = TRUE;
   }
     
  if(input(BTN_CEN2) ==0)
   {
     fl_cen2 = TRUE;
   }   
  if(input(BTN_R2) ==0)
   {
     fl_right2 = TRUE;
   }   
  if(input(BTN_L2) ==0)
   {
     fl_left2 = TRUE;
   }
     
  if(input(BTN_CEN3) ==0)
   {
     fl_cen3 = TRUE;
   }   
  if(input(BTN_R3) ==0)
   {
     fl_right3 = TRUE;
   }   
  if(input(BTN_L3) ==0)
   {
     fl_left3 = TRUE;
   }
     
  if(input(BTN_CEN4) ==0)
   {
     fl_cen4 = TRUE;
   }   
  if(input(BTN_R4) ==0)
   {
     fl_right4 = TRUE;
   }   
  if(input(BTN_L4) ==0)
   {
     fl_left4 = TRUE;
   }     

//******************************************************************************



Really appreciate it if anyone can find out the prob =D
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 26, 2008 10:46 am     Reply with quote

Quote:
#INT_TIMER1
void TIMER1_isr(void)
{
set_timer1(65535);

What is this doing ? You are loading Timer1 with 0xFFFF.
Timer1 is an up-counter. From the PIC data sheet:
Quote:
6.0 TIMER1 MODULE
The Timer1 module is a 16-bit timer/counter consisting
of two 8-bit registers (TMR1H and TMR1L) which are
readable and writable. The TMR1 register pair
(TMR1H:TMR1L) increments from 0000h to FFFFh
and rolls over to 0000h.
soong2020



Joined: 13 Feb 2008
Posts: 25

View user's profile Send private message

PostPosted: Wed Mar 26, 2008 11:23 am     Reply with quote

Sorry for forgetting to log in when posting this. Oh i was thinking the timer was supposed to countdown. Now I've removed the set_timer instruction and yet the problem persisted. I suspect is that the code is stucked somewhere whenever i press the key read the ADC values.

Can anyone please help me on this?

Thank you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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