View Full Version : Pic I/o
tomskk
Oct 06, 2005, 03:38 AM
Can any one tell me what is the difference between digtal I/O on PIC MCU and analog I/O?
This may be a stupid question for all you experts, but i am a beginner.
Malc C
Oct 06, 2005, 04:51 AM
Digital means its looking for a logic one or zero, ie if there is full supply voltage on the pin or not. Analogue is where the PIC is expecting any voltage between zero and supply.
tomskk
Oct 06, 2005, 04:57 AM
Thanks.
LukeZ
Oct 06, 2005, 11:47 AM
Also, technically speaking, the PIC has digital I/O, but only Analog I (input). It is possible to output an analog voltage with a PIC, but it involves using PWM (which is digital out), and filtering it with external hardware.
In digital In, the PIC will read the value at the pin as either 1 (5 volts or whatever) or 0 (Ground). You could use this to have the PIC read the position of a switch, for example.
In digital Out, the PIC will set the voltage at the pin to either 1 or 0. You could use this to turn an LED on or off.
With Analog In, the PIC will read a voltage between some range, often between Ground and 5 volts or whatever your supply is, and it converts that analog voltage into a number between 1 and 1024 (if you have a 10 bit Analog-to-Digital converter on your PIC - this is most common). This involves a different setup in software on your PIC than just setting a pin to digital in or out, which can be done with about one command. Reading an analog voltage is useful for measuring things - the position of a trim resistor, for example, or else the output of some kind of sensor, like a thermistor.
Luke
tomskk
Oct 06, 2005, 12:03 PM
What is the CMCON value and what does it do. There are several settings of CMCON value. COMCON = 7 for example?
LukeZ
Oct 06, 2005, 01:13 PM
On some PICs the Port A pins (could also be a different port, but I think A is the usual one) can be used in comparator mode. I won't get into it because I haven't used it. CMCON = 7 I believe turns off the comparator mode for the entire Port A and allows the pins on that port to be used for regular digital I/O. Check out this page: http://www.rentron.com/PIC16F628.htm.
By the way, what PIC are you using? That could also make a difference. Whichever one it is, you'll want to download the PDF datasheet for that chip from the Microchip website. It's a massive document, and sometimes hard to get into at first, but it has everything you need to know in it, including what the CMCON register is all about.
Luke
tomskk
Oct 06, 2005, 02:15 PM
I am trying to use a 12F629 or 12F675. How do set the ports to digital?
LukeZ
Oct 06, 2005, 03:40 PM
tomskk,
First of all, here is the datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/41190c.pdf) for those two chips in PDF format. I'd save a copy to my harddrive. This thing will be invaluable for you.
Here is an example from this document, at the beginning of chapter 3:
bcf STATUS,RP0 ;Bank 0
clrf GPIO ;Init GPIO
movlw 07h ;Set GP<2:0> to
movwf CMCON ;digital IO (07h = 00000111)
bsf STATUS,RP0 ;Bank 1
clrf ANSEL ;Digital I/O
movlw 0Ch ;Set GP<3:2> as inputs
movwf TRISIO ;and set GP<5:4,1:0> as outputs (0Ch = 00001100)
So ignore my earlier comments about the comparator, that was for a different chip. The chip you're using only has 6 pins available for digital I/O, they call this the GPIO port.
To initialize the GPIO port, you clear all 6 bits of the GPIO register.
To set some pins to digital I/O (as opposed to analog inputs), you set the appropriate bits in the CMCON register to 1. If you wanted them to be analog, you would set them to 0. In the example above, it is setting pins 5-3 as analog inputs, and 2-0 as digitial I/O (they count zero (0) as a number, so instead of 1 to 6 it's 0 to 5). This is done by writing the value 00000111 (07h in hex) to the CMCON register. You'll notice it's an 8 bit value, even though the CMCON register only applies to 6 pins. So it just reads the right-most 6 bits and ignores the other two.
Pins: n/a n/a 5 4 3 2 1 0
CMCON: 0 0 0 0 0 1 1 1 (00000111 = 07 in hex)
Result: i i A A A D D D
i = ignore
A = Analog input
D = Digital I/O
Above is what I mean. The two left-most bits are ignored because they don't correspond to any pins. The next three zero's tells the chip to set pins 5,4, and 3 to analog inputs. The final three 1's tells the chip to set pins 2,1 and 0 to digital I/O.
But you're not quite done yet: setting a pin to digital I/O isn't enough, you have to specify whether it's going to be I or O (input or output). This is done through the TRISIO register. Writing a 1 means input, a 0 means output. In the example above they wrote 0Ch (00001100) to this register:
Pins: n/a n/a 5 4 3 2 1 0
TRISIO: 0 0 0 0 1 1 0 0 (00001100 = 0C in hex)
Result: i i Out Out In In Out Out
i = ignore
In = input
Out = output
Remember from above that pins 2,1 and 0 are digital I/O. Now we just set pin 2 to input, and 1 and 0 to output.
Earlier we set pins 5,4 and 3 to analog. Now we just set 5 and 4 to analog input, and 3 to analog output. This latter is kind of silly, but that's just the example they gave. If you read the text you'll see that port 3 is a special case, and no matter what you write to it, it will always be an input. So they didn't actually set it to output, even though they "tried."
Anyways you should read the whole chapter 3 of that document, and I'd recommend reading the whole document period. There are also some good tutorials on the web and elsewhere, if you do a search for them. There's even a few on these boards. If I have time later I'll try to find some links for you.
Luke
tomskk
Oct 06, 2005, 04:17 PM
Thanks Luke, you have just revieled me the secret of PIC's I/O in a manner that I, a beginner will understand. If I could I would buy a beer this minute. Thanks again.
LukeZ
Oct 06, 2005, 06:32 PM
tomskk,
No problem. You can have a beer for me... just don't try to program and drink at the same time, as it does't work too well. :p
Here's some links I bookmarked that may help you:
0x09 Guy's tutorial: http://www.0x09.com/media/physcomp/
PIC "Elmer" tutorials: http://amqrp.org/elmer160/lessons/
Mikroelectronika online lessons: http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm
PIC Assembly for the Complete Beginner: http://www.covingtoninnovations.com/noppp/picassem2004.pdf
When I was first starting I found this short PDF to be extremely useful! It's very basic but it explains some things that a lot of other places assume you already know.
PIC Tutorial by CatsEyes and Ironsides: http://www.rc-float-flying.rchomepage.com/RCWeb/PIC/PIC.htm
This one was set up by some fellow eZoner's, and at least part of it even deals with the exact same chips as you have.
Cybot's PIC tutorial: http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm
Starting with PIC Microcontrollers: http://www.voti.nl/swp/
This guy talks about what kinds of things to think about when picking a chip for the first time, and has some other useful advice.
Finally,here is a eZone thread with a bunch of other good links. Basically a thread of PIC resources for the beginner: http://www.rcgroups.com/forums/showthread.php?t=359220
Luke
vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.