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

counter increment & decrement

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



Joined: 04 Jul 2008
Posts: 12
Location: Karachi Pakistan

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

counter increment & decrement
PostPosted: Mon Sep 07, 2009 6:37 am     Reply with quote

Hi

I want to know how I will generate a counter of 32 count which will increment or decrement a count by comparing the value from ADC of PIC16F877A.
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 07, 2009 2:30 pm     Reply with quote

Read the ADC inside a while() loop. Compare the ADC result value
to your limits, and increment or decrement a variable based on the
result of the test. This program shows how to setup the ADC.
(It has some PWM code which can be removed).
http://www.ccsinfo.com/forum/viewtopic.php?t=40007&start=1
asimraufawan



Joined: 04 Jul 2008
Posts: 12
Location: Karachi Pakistan

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Sep 08, 2009 7:10 am     Reply with quote

I have made the following code its working fine.
I have a range of 2 to 4 volt for adc, but the problem I'm facing is
that when count goes 11111 like approx at 2 volt, then at 1.9 it again starts from 00000.
Code:

void main()
{
int a[32]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25,26,27,28,29,30,31};

int count;
float b,c,d;

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

   // TODO: USER CODE!!

While(TRUE)
 {
  output_high(PIN_C0);

  set_adc_channel(0);
  delay_ms(5);
  b=read_adc();

  c=((b*5)/1023);

  if(c<d)
   {
    count++;
    output_D(a[count]);
    delay_ms(1000);
    d=c;
   }
  else if(c>d)
   {
    count--;
    output_D(a[count]);
    d=c;
   }

  if(count>=31)
   {
    count=0;
   }

 }

}

How will I define my limit?
asimraufawan



Joined: 04 Jul 2008
Posts: 12
Location: Karachi Pakistan

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Fri Sep 11, 2009 6:17 am     Reply with quote

Hi

What will be the best way to adopt to give range to counter using the above code from 4 to 2 volt.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Sep 11, 2009 7:06 am     Reply with quote

Hi,
Your problem is this

Code:

if(count>=31)
   {
    count=0;
   }


To do what you want (if I have got it right) is you need to just stop the count when it reaches 31 or 0. When the ADC starts increasing or dropping again you start the count.

To do this with your code either requires a signed int or just a slight change to the main routine when you increment or decrement the count.

Code:

  if (count > 0)
    count--;


and

Code:

  if (count < 31)
    count++;


You may want to stop (fix) the d value!
Code:

  if (count > 0)
  {
     count--;
     d = c;
  }


and

Code:

  if (count < 31)
  {
    count++;
    d = c;
  }


The reason you would want to do this is because as the value climbs above the threashold the count does not continue upwards, but as soon as the AD value starts dropping the count will start to decrement.
I assume you don't want the count to start decrementing until the AD drops below the threashold.

If you don't quite understand this I am sure someone else would be able to explain it better than me at the moment ;)

I could do with some paper and a pen Smile
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