Page 1 of 1
CMCON on a 12f629
Posted: Mon Jul 12, 2010 11:09 pm
by jimtr6
I would like to read an analog voltage and if a certain level is present go to a set of instructions (basically just turn an output high). Right now I'm doing it outside the chip with op amps and an opto isolator and turning it into a high to feed a port. The voltage I'm reading is an audio voltage amplified and rectified and sent to port A bit 0. It would be so much easier and use less hardware if I could just send the audio signal into a port, is this possible, the chip does have a comparator. Do I need to program in C or Assembly to do this, as in designating/configurating a port to be a receiver of something other than a digital high or low? thanks much for any help
Re: CMCON on a 12f629
Posted: Tue Jul 13, 2010 9:49 am
by Benj
Hello,
You could do this in a number of ways.
1) use an analogue pin and in your program simply use an ADC component to sample the pin. Then using a decision icon you can test to see if the return value is over a certain value. If it is then perform your task.
2) use the comparitor to see if the voltage on e 1 pin is over a test voltage. You will need some external hardware here eg a potential divider to provide the reference voltage. Then a couple of lines of C code and you can activate the comparitor peripheral which will set a flag when the test voltage is higher or lower then the reference depending on the configuration. Again you could use a decision to look at the flag.
3) Again using a comparitor you could set up a custom interrupt so that this fires every time there is an event.
Re: CMCON on a 12f629
Posted: Tue Jul 13, 2010 11:54 am
by medelec35
I rememer a thread dealing comparators:
http://www.matrixmultimedia.com/mmforum ... 40&p=14655
You could also look there.
Re: CMCON on a 12f629
Posted: Tue Jul 13, 2010 1:39 pm
by jimtr6
Thanks for the quick reply, this chip does not do any ADC but accepts an anologue in on bit 0. So it looks like the coding I have to do will be in configuring the port to read analogue. One thing (among many) I liked about Flowcode was it was not necessary to config the ports in TRISA, it seems this will be an exception, I do not know one lick of anything about C programming, I know a little about Assembly as the data sheets use that, anyway back to C, it looks to me it will only require a few lines to configure, also I could use a little help on setting up a reference voltage to compare for a decision, or would the input voltage be in the form of a variable so I could view what it is and set my thresholds accordingly. I know this is a limited little PIC but I like the size and the challenge. thanks again!
P.S. can you recommend a good easy to understand intro into C language
Re: CMCON on a 12f629
Posted: Tue Jul 13, 2010 4:16 pm
by Benj
Hello,
To turn a pin into analogue simply add an ADC component to your program and change the component connections so it is conected to the desired pin.
Using a component macro icon you can call the functions of the ADC to read a sample as a byte or as a 10-bit int.
if you read back the value as a byte then 0 will represent 0v and 255 will represent 5V on a 5V system.
Therefore you can have a decision icon that compares the value to a preset value.
eg
adcin > 128
would follow the yes branch if the adcin variable is over 128 or 2.5V otherwise the no branch would be followed.
If you want to learn C code then why not create your Flowcode program, compile to C and then use the Chip -> View C code menu to view the output C file.
Unfortunatley you cannot do the above with a 12F629 as it does not have the analogue capable inputs. You could instead use the 12F675 device or you can use the compartior hardware. Pages 37 - 39 of the datasheet should be useful for getting the comparitor up and running.
Let me know how you get on.