Difference between revisions of "Digital inputs & outputs"

From Mech
Jump to navigationJump to search
 
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:


==Which pins are accessible on the 4520 prototyping board?==

[[Image:pinsbroughtout2.jpg|right]]

Most pins on the PIC are brought out to easily accessible lines on the 4520 board. Feel free to connect to the few others as well.
Most pins on the PIC are brought out to easily accessible lines on the 4520 board. Feel free to connect to the few others as well.


Line 4: Line 10:


Most pins have several possible different input or output purposes, as evidenced by several names. Your software determines which purpose is in effect.
Most pins have several possible different input or output purposes, as evidenced by several names. Your software determines which purpose is in effect.


[[Image:pinsbroughtout.gif]]


===Digital I/O pins (five "ports")===

[[Image:4520pindiagramdigitalports.jpg|right]]


With the exception of the positive voltage supply and ground pins, all pins on the PIC18F4520 can be used as digital I/O, however a few other pins (shown in gray below) are commonly utilized for communication instead of for digital I/O.
With the exception of the positive voltage supply and ground pins, all pins on the PIC18F4520 can be used as digital I/O, however a few other pins (shown in gray below) are commonly utilized for communication instead of for digital I/O.



[[Image:4520pindiagramdigitalports.jpg‎]]


==Demo circuit for digital inputs and outputs==
The 4520 board has 32 1K resistors-to-ground built in, and spaces for 32 LEDs to be inserted, as shown in the circuit below You can see that the resistors are isolated from and have no effect on the PIC unless the LED is inserted.



[[Image:Digitalio.gif]]
[[Image:Digitalio.gif|right]]
The 4520 board has 32 1K resistors-to-ground built in, and spaces for 32 LEDs to be inserted, as shown in the circuit below You can see that the resistors are isolated from and have no effect on the PIC unless the LED is inserted.
Above is a demo circuit for digital input and digital output. Two LEDs have been inserted, and a pull-up switch and 4.7K pull-down resistor added.
At right is a demo circuit for digital input and digital output. Two LEDs have been inserted, and a pull-up switch and 4.7K pull-down resistor added.


Pin C1 is intended as a digital input, and will be designated so in software. When pushbutton switch S1 is depressed, it brings input C1 high, and the left LED illuminates. Otherwise input C1 is held low by the 4.7K resistor to ground. (The LED and its resistor cannot be counted upon to hold input C1 low.)
Pin C1 is intended as a digital input, and will be designated so in software. When pushbutton switch S1 is depressed, it brings input C1 high, and the left LED illuminates. Otherwise input C1 is held low by the 4.7K resistor to ground. (The LED and its resistor cannot be counted upon to hold input C1 low.)


Pin D7 is intended as a digital output. When the PIC drives it high, the right LED illuminates, and otherwise not.
Pin D7 is intended as a digital output. When the PIC drives it high, the right LED illuminates, and otherwise not.




==Sample code==

[http://peshkin.mech.northwestern.edu/pic/code Link to all sample code here.]


The code digitalio.c is a complete C program that will compile and run.
The code digitalio.c is a complete C program that will compile and run.
Line 33: Line 53:


switch bounce - most mechanical switches "bounce" which means they open and close several times when activated, over a period of a few mS. If this is a problem for your software, there are a lot of ways to debounce switches electronically. Better, write your software so it doesn't get confused by multiple open-close cycles.
switch bounce - most mechanical switches "bounce" which means they open and close several times when activated, over a period of a few mS. If this is a problem for your software, there are a lot of ways to debounce switches electronically. Better, write your software so it doesn't get confused by multiple open-close cycles.

==See also==
*[[PIC18F4520: Digital Inputs]]
*[[PIC18F4520: Digital Outputs]]

Latest revision as of 21:40, 12 March 2009


Which pins are accessible on the 4520 prototyping board?

Pinsbroughtout2.jpg

Most pins on the PIC are brought out to easily accessible lines on the 4520 board. Feel free to connect to the few others as well.

Do not drive any pin outside the range 0 to 5 volts, which can damage the PIC.

Most pins have several possible different input or output purposes, as evidenced by several names. Your software determines which purpose is in effect.



Digital I/O pins (five "ports")

4520pindiagramdigitalports.jpg

With the exception of the positive voltage supply and ground pins, all pins on the PIC18F4520 can be used as digital I/O, however a few other pins (shown in gray below) are commonly utilized for communication instead of for digital I/O.



Demo circuit for digital inputs and outputs

Digitalio.gif

The 4520 board has 32 1K resistors-to-ground built in, and spaces for 32 LEDs to be inserted, as shown in the circuit below You can see that the resistors are isolated from and have no effect on the PIC unless the LED is inserted.

At right is a demo circuit for digital input and digital output. Two LEDs have been inserted, and a pull-up switch and 4.7K pull-down resistor added.

Pin C1 is intended as a digital input, and will be designated so in software. When pushbutton switch S1 is depressed, it brings input C1 high, and the left LED illuminates. Otherwise input C1 is held low by the 4.7K resistor to ground. (The LED and its resistor cannot be counted upon to hold input C1 low.)

Pin D7 is intended as a digital output. When the PIC drives it high, the right LED illuminates, and otherwise not.



Sample code

Link to all sample code here.

The code digitalio.c is a complete C program that will compile and run.

You can read or write one bit at a time, or a whole 8-bit port as a byte. The demo code illustrates the functions input_c() which reads the whole C port as a byte (and returns an int8 value) and output_d(value) which writes to the whole D port as a byte (and takes an int8 argument).

You can also read a single pin using input(PIN_C4) which returns 1 or 0. PIN_C4 and its siblings are defined in the 18f4520.h "include" file.

For output to a single pin, you can use output_high(PIN_D7) and output_low(PIN_D7) which drive pin D7 high or low, or you can set it to a value in a variable with output_bit(PIN_D7, value) where value is 1 or 0.

All of the above built-in functions include instructions to set the targeted port or pin to digital input mode or digital output mode. That takes a few extra microseconds every time the function is called. It is possible to turn this part of the function code off, if you take responsibility to set the direction of the port or pin yourself.

switch bounce - most mechanical switches "bounce" which means they open and close several times when activated, over a period of a few mS. If this is a problem for your software, there are a lot of ways to debounce switches electronically. Better, write your software so it doesn't get confused by multiple open-close cycles.

See also