View previous topic :: View next topic |
Author |
Message |
Rez
Joined: 10 Feb 2019 Posts: 26
|
Controlling A Potentiometer |
Posted: Sun Feb 10, 2019 1:14 am |
|
|
I have a pic12f506 and I am trying to get it to control different time intervals
but I cant get it to work, the first time output works but when I turn the pot to the next value it skips the 2nd one and goes to the 3rd one.
Code: | #include <16F506.h>
#define GP5 PIN_B5
#use delay(clock=4000000)
#fuses NOMCLR,NOPROTECT,NOWDT,INTRC
void main()
{
int result;
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports (AN0_AN2);
set_adc_channel (0);
setup_comparator(0);
while(TRUE)
{
result = read_adc();
if(result ++ > 254)
{
output_high(GP5);
delay_ms(50);
output_low(GP5);
delay_ms(50);
}
if(result ++ > 245)
{
output_high(GP5);
delay_ms(500);
output_low(GP5);
delay_ms(500);
}
if(result ++ > 235)
{
output_high(GP5);
delay_ms(2000);
output_low(GP5);
delay_ms(2000);
}
}
}
|
|
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Sun Feb 10, 2019 2:06 am |
|
|
This should make it good
Code: | if(result ++ > 254)
{
output_high(GP5);
delay_ms(50);
output_low(GP5);
delay_ms(50);
}
else
if(result ++ > 245)
{
output_high(GP5);
delay_ms(500);
output_low(GP5);
delay_ms(500);
}
else
if(result ++ > 235)
{
output_high(GP5);
delay_ms(2000);
output_low(GP5);
delay_ms(2000);
}
|
I do not understand why you do the if (result ++ > x). Simply if (result > x) should suffice |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Sun Feb 10, 2019 4:47 am |
|
|
Jerson wrote: | This should make it good
Code: | if(result ++ > 254)
{
output_high(GP5);
delay_ms(50);
output_low(GP5);
delay_ms(50);
}
else
if(result ++ > 245)
{
output_high(GP5);
delay_ms(500);
output_low(GP5);
delay_ms(500);
}
else
if(result ++ > 235)
{
output_high(GP5);
delay_ms(2000);
output_low(GP5);
delay_ms(2000);
}
|
I do not understand why you do the if (result ++ > x). Simply if (result > x) should suffice |
Thank you for the reply.
I have tried the else if statement but unfortunately that still does not fix it. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Sun Feb 10, 2019 5:42 am |
|
|
I did get it to work finally! All I had to do was swap the + and - on the pot and increase the value number between the different times. Thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sun Feb 10, 2019 8:29 am |
|
|
It is worth understanding that this:
if(result ++ > 254)
Only has one possible pot value that will return 'true'. A pot value of
254. If the pot is any higher (255), the increment will overflow the
arithmetic and give 0.
The point about using the 'else', is that if you do all the tests in turn as
you show, if the value is 246, it'll do the second output, and then do the
third.... |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Sun Feb 10, 2019 9:08 am |
|
|
One thing I like to do is to set a uart serial output to print the potentiometer
value. That makes it much easier to "calibrate", letting you see the max/min
values, your range, the direction of the pot, etc.
I noticed that your chip doesn't have a uart module. I have not worked with
PIC12 before, but if you have a spare pin, CCS provides a macro to setup a
software uart. That is sufficient to output your potentiometer value. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Sun Feb 10, 2019 11:03 am |
|
|
dluu13 wrote: | One thing I like to do is to set a uart serial output to print the potentiometer
value. That makes it much easier to "calibrate", letting you see the max/min
values, your range, the direction of the pot, etc.
I noticed that your chip doesn't have a uart module. I have not worked with
PIC12 before, but if you have a spare pin, CCS provides a macro to setup a
software uart. That is sufficient to output your potentiometer value. |
It's worth noting that if you are using CCS's programmer (ICD-U64 or ICD-U80), that you can also setup a software uart through the programmer itself.
Code: |
#use rs232(ICD,DISABLE_INTS,stream=SOME_STREAM_NAME)
|
I have the IDE version, so I don't know if the command line compiler has this option. You do need the SIOW program from CCS for it to work as the programmer does some magic with that to setup the software UART through the programmer. It can be a little glitchy (SIOW interprets raw data in interesting ways), but for quick printing, it can be useful. |
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Sun Feb 10, 2019 1:08 pm |
|
|
Thank you for the additional help. I am using a pickit 2 programmer. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
|
Rez
Joined: 10 Feb 2019 Posts: 26
|
|
Posted: Sun Feb 10, 2019 9:00 pm |
|
|
I came up with this loop to allow better range of time delay. Its much nicer than trying to type in a bunch of ranges, better on memory as well.
Code: |
int result;
result = read_adc();
for(int i = 0; i < result; i++)
{
output_high(GP5);
delay_ms(120); // 0 to 30 sec range
output_low(GP5);
}
|
|
|
|
|