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

PWM problem

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



Joined: 12 May 2009
Posts: 1

View user's profile Send private message

PWM problem
PostPosted: Tue May 12, 2009 12:40 pm     Reply with quote

Hi!

I'm still a newbee to C and PIC programming, so..... I'm also not english...

OK, here is my problem:

I'm trying to program a PIC to move about 6 servos simultaneously without using the internal PWM. I don't know where I went wrong (it's probably just the lack of C knowledge but please help me, I'm losing my mind with that!!

For starters I'm just trying to move 2 servos and it just doesn't do the job write. Here is the code (I'll post the whole thing in case... so just ignore the first part, its for the keypad and it works great). Both servos work until the time of the 1. servo isn't smaller than the time of the 1. servo.
Here it is... PLEASE HELP ME
Code:

#include "E:\Documents and Settings\Vid\My Documents\PIC\16F876\Servoti + tipkovnica\servoti+tipkovnica.h"

#define LCD_DATA_PORT b
#define LCD_TYPE 2
#define LCD_TRIS_LOCATION trisb
//#include <lcd.c>

void main()
{
int1 gor, dol, levo, desno, naprej, nazaj, enter, izhod, meni;
unsigned long pwm;
unsigned long pb[2];
unsigned long s[2];
int8 pin[2];
int n,x,y,temp,q;

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_8);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_wdt(WDT_2304MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_adc_channel(pin_a0);

WHILE(1)
{
  output_high(pin_a1);                                       //KEYPAD
  if(input_state(pin_a4)==1){gor=1;}     else    gor=0;
  if(input_state(pin_a5)==1){dol=1;}     else    dol=0;
  if(input_state(pin_c7)==1){levo=1;}    else   levo=0;
  output_low(pin_a1);
  output_high(pin_a2);
  if(input_state(pin_a4)==1){izhod=1;}   else  izhod=0;
  if(input_state(pin_a5)==1){naprej=1;}  else naprej=0;
  if(input_state(pin_c7)==1){nazaj=1;}   else  nazaj=0;
  output_low(pin_a2);
  output_high(pin_a3);
  if(input_state(pin_a4)==1){meni=1;}    else   meni=0;
  if(input_state(pin_a5)==1){enter=1;}   else  enter=0;
  if(input_state(pin_c7)==1){desno=1;}   else  desno=0;
  output_low(pin_a3);

 if (pwm>2500){pwm=2500;}
 if (pwm<500){pwm=500;}
 if (s[0]>2201){s[0]=2200;}                       // s[0] = 1. servo position
 if (s[0]<799){s[0]=800;}                         // s[1] = 2. servo position
 
 if (levo==1) {s[0]=s[0]+20;}
 if (desno==1) {s[0]=s[0]-20;}

s[1]=read_adc()+1000;
n=2; 

pb[0]=s[0];
pb[1]=s[1];

  for( x=0; x<n; x++)                     // bubble sort
   {   
     for( y=0; y<n-1; y++)
      {
         if(pb[y]>pb[y+1])
         {
            temp = pb[y+1];
            pb[y+1] = pb[y];
            pb[y] = temp;
           
         }
         
      }             
   }
       
 
   for(q=0;q<n;q++)
   {
     
      if(s[q]==pb[0]){pin[q]=pin_b0;}
      if(s[q]==pb[1]){pin[q]=pin_b1;}
     
   }   
 // if(s[0]>s[1]){pin[1]=pin_b0;pin[0]=pin_b1;} else    pin[0]=pin_b0;pin[1]=pin_b1;
   
 // if(s[0]<s[1]){output_high(pin_b5);} else output_low(pin_b5);           
 
 // if(pin[1]==pin_b0) {output_high(pin_b4);}  output_low(pin_b4);   
  output_high(pin_b0);
  output_high(pin_b1);
  delay_us(pb[0]);
  output_low(pin[0]);
  delay_us(pb[1]-pb[0]);
  output_low(pin[1]);
  delay_us(20000-pb[1]);
}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 12, 2009 2:19 pm     Reply with quote

Quote:
set_adc_channel(pin_a0);

This is wrong. The ADC channel is a number such as 0, 1, or 2, etc.
It's not a CCS pin constant.

If you still have problems, then post this file:
Quote:
servoti+tipkovnica.h


Also post your compiler version.
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Tue May 12, 2009 2:23 pm     Reply with quote

I have posted a code for multiple servos on PIC18F4550, but it can be rewriten for other PIC-s ....


Here is the link.
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