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

Controlling A Potentiometer

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



Joined: 10 Feb 2019
Posts: 26

View user's profile Send private message

Controlling A Potentiometer
PostPosted: Sun Feb 10, 2019 1:14 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 10, 2019 2:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 4:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 5:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 8:29 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 10, 2019 9:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 11:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 1:08 pm     Reply with quote

Thank you for the additional help. I am using a pickit 2 programmer.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 10, 2019 1:19 pm     Reply with quote

In that case, I am a fan of the FT232 breakout boards from Sparkfun
to do the serial to USB translation.

https://learn.sparkfun.com/tutorials/sparkfun-usb-to-serial-uart-boards-hookup-guide

If you have never done serial printouts before, there are many tutorials
online in addition to the one I just posted. You may need to adapt it to your
own PIC and the CCS compiler though.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 1:43 pm     Reply with quote

https://www.banggood.com/3_3V-5V-USB-to-TTL-Converter-CH340G-UART-Serial-Adapter-Module-STC-p-1232728.html?rmmds=search&cur_warehouse=CN
Here's another TTL<>USB module, only $2 CDN, I've used older versions for years with zero problems. If your power needs are low, the PC USB port can power your project. You also have 3 or 5 volt VDD. I remove the POWER LED as it's way too bright for me.

Jay
Rez



Joined: 10 Feb 2019
Posts: 26

View user's profile Send private message

PostPosted: Sun Feb 10, 2019 9:00 pm     Reply with quote

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);
}
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