Sie sind auf Seite 1von 4

I.

PIC 32 Digital IO a. References i. PIC32 Reference Guide IO PORTS ii. PIC32 Data Sheet iii. C32 Peripheral Library b. Pin Controls i. TRIS Tristate Register sets the data direction of the pin: Output 0, Input - 1 ii. LAT Output latch sets the pin output state 1. SET sets the pin output high 2. CLR sets the pin output low 3. INV inverts the pin outputs iii. PORT Reads pin (for either direction) c. IO Diagram

Figure 1. PIC32MX5XX/6XX/7XX Family Data Sheet Pin Diagram

Page

Figure 2. IO Pin internal operations

II.

IO Pin software instructions a. Register based instructions - TRIS, LAT, SET, CLR, INV, and PORT b. Peripheral C32 instructions --- PORT.(IOPORT_x, value) c. Peripheral C32 macro instructions mPORTX(value)

Page

/* Examples of IO control Richard Wall University of Idaho January 27, 2012 Note: See text pg. 17 when using RA0, RA1, RA4, or RA5, use the statement when initializing. DPPCONbits.JTAGEN = 0; */

#include <plib.h> #include "config_bits.h" int main(void) { int pins;

// Includes all C32 libraries // Specific to the ECE 341 project boards

// These 3 instructions are equivalent for setting a pin as an output


AD1PCFGCLR = BIT_10; // These two instructions must be used together to set for digital IO
1

TRISBCLR = BIT_10; PORTSetPinsDigitalOut(IOPORT_B, BIT_10); mPORTBSetPinsDigitalOut(BIT_10); // These next 4 instructions are equivalent for setting an output pin high
AD1PCFGSET = BIT_10; // These two instructions must be used together to set for digital IO

LATBSET = BIT_10; PORTSetBits(IOPORT_B, BIT_10); mPORTBSetBits(BIT_10); PORTBSET = BIT_10; /* These next 4 instructions are equivalent for setting output pins low */ LATBCLR = BIT_10; PORTClearBits(IOPORT_B,BIT_10); mPORTBClearBits(BIT_10); PORTBCLR = BIT_10; // These next 3 instructions are equivalent for Inverting the state of the pin output LATBINV = BIT_10; PORTToggleBits(IOPORT_B, BIT_10); mPORTBToggleBits(BIT_10); // Writing IO Ports high and low - simultaneously sets all 16 bits LATB = 0x0400; PORTWrite(IOPORT_B, 0x0400); mPORTBWrite(0x400); PORTB = 0x0400;
1

PIC32 Family Reference Guide, Section 12.3.1 specifically states that the default to analog inputs on power up reset. Hence the requirement to use the AD1PCFGSET instruction in conjunction with the TRISx, TRISxSET, TRISxINV, and TRISxCLR instructions.

Page

// These 3 instructions are equivalent for setting a pin as an input


AD1PCFGSET = BIT_10; // These two instructions must be used together to set for digital IO

TRISBSET = BIT_10; PORTSetPinsDigitalIn(IOPORT_B, BIT_10); mPORTBSetPinsDigitalIn(BIT_10); // Reading LATCH registers pins = LATB; pins = mPORTBReadLatch(); // Reading IO Pins pins = PORTB; // returns all PORT 10 bits pins = PORTReadBits(IOPORT_B, BIT_10); // returns PORT B & BIT_10 pins = mPORTBRead(); // All embedded programs must have an infinite loop to prevent the execution of the return 0; while(1); return 0; }

Page

Das könnte Ihnen auch gefallen