|
Intro to Physical Computing Syllabus code, circuits, & construction
|
Digital Input and Output on the PIC |
|||
| Digital input and output on the PIC using PicBasic Pro is fairly simple. The I/O pins of the PIC work like the I/O pins on the BX-24, BS-2, and other 5-volt microcontrollers. When they are configured as inputs, they read as high (logic 1) when attached to +5V DC, and low (logic 0) when attached to ground. To wire basic switches, you can use the same wiring scheme as for other microcontrollers, as follows:
Wired like this, the switch will read as high when it's closed, and low when it's open. When the I/O pins are configured as outputs, they output +5V DC when set high (logic 1), and 0V DC when set low (logic 0). These pins can't supply a great amount of current, so if you want to drive motors or some other device, you should use a transistor circuit to do so. In order to use a pin as an input or output, you must first configure it as an input or output. The simplest way to do this in PBBro is to use the INPUT and OUTPUT commands, like so: input PORTB.0 makes the first pin on port B an input. Then, if we wanted to see if it was high or low, we would say if PORTB.0 = 1 then ' the switch is closed else ' the switch is open endif and that's it. Similarly, if we wanted to make the pin an output, we'd say output PORTB.0 and then to set it high or low, we could either use the HIGH and LOW commands, as follows: high PORTB.0 ' sets the pin high low PORTB.0 ' sets the pin low Or we could simply set it equal to 1, as follows: PORTB.0 = 1 ' sets the pin high PORTB.0 = 0 ' sets the pin low Note: to use this second method, you MUST declare the pin as an output first. The HIGH and LOW commands automatically declare the given pin as an output first, but setting the pin to 0 or 1 does not. |
||||
| Setting all the pins of a port at once
The I/O pins are controlled by the settings of a few addresses in memory. There are a number of different memory registers that control special functions on the PIC; they're called special function registers. The I/O pins are divided up into ports, each of which has up to 8 pins. They're labeled port A, port B, port C, etc. The number of ports depends on the number of pins on the type of PIC you're using. There are two special function registers in memory for each port, the tri-state register (TRIS) and the port register (PORT). Each register takes a byte of memory, and each pin of a port is assigned one bit of that byte. The TRIS register tells you whether the pins are inputs or output. A 1 in any bit means that pin is an input, and a 0 means it's an output. for example, if TRISC.2 = 1, then the pin addressed as PORTC.2 is an input. If TRISC.2 = 0, then PORTC.2 is an output. The PORT register tells you the actual states of the pins. For example, if pin PORTA.0 is an input (i.e. TRISA.0 = 1) then when a switch attached to it is closed, PORTA.0 will be equal to 1. You can see from this that the INPUT and OUTPUT statements are just setting the TRIS registers' bits. Since we can address memory a byte (or more) at a time, we can set several pins at once. For example, we could set all the pins of port C to be outputs with one command: TRISC = %00000000 (the % sign indicates a number formatted in base 2 in PBPro) From there, we could set all of the pins of PORTC high at once, like so: PORTC = %11111111 We don't have to use base 2; we can use any base we want, for example: PORTC = 170 would turn on every other pin. Base 2 is more convenient in this case, though. 170 in base 10 equals 10101010 in base 2, and if we saw the line above like this: PORTC = %10101010 We would know at a glance which pins are on and which are off. You can use any of these syntaxes to address the I/O pins in PicBasic Pro. If one confuses you, use another. Ultimately, whatever gets the pins working is the right way. |
You can read more about the special function registers on the register notes page. | |||