|
|
View previous topic :: View next topic |
Author |
Message |
asimraufawan
Joined: 04 Jul 2008 Posts: 12 Location: Karachi Pakistan
|
counter increment & decrement |
Posted: Mon Sep 07, 2009 6:37 am |
|
|
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
|
|
Posted: Mon Sep 07, 2009 2:30 pm |
|
|
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
|
|
Posted: Tue Sep 08, 2009 7:10 am |
|
|
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
|
|
Posted: Fri Sep 11, 2009 6:17 am |
|
|
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
|
|
Posted: Fri Sep 11, 2009 7:06 am |
|
|
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 |
|
|
|
|
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
|