You really must try to avoid coding GPIO.0 or PortA.0 (or similar) in your programs... I'll tell you why... you have fifteen instances in a thousand lines of code where you refer to a particular pin... Firstly - just get ONE of those instances wrong and your code is screwed... but you'll never know it until that particular piece of code is executed. Secondly - you're laying out your PCB and you discover it's easier to place your components and lay your tracks if you swapped pins PortA.5 with PortB.6. You now spend half a day trying to find all the references to those pins in your program and worry about swapping them over.Thus I kinda do things the "long Hand" way...GPIO.0, Porta.0, or whatever.
There is a better way...
At the start of your program, right after you've done all your Config Fuse Defines, you have a section where each and every pin on your PIC is defined (aliased), and you ONLY use those aliases in your program... example - a snip out of a program that uses a PIC16F876...
Now, if I have to amend my program, and turn my Red LED on, rather than try to remember what pin my Red LED is connected to, I simply say...Code:' ' Hardware Defines ' ================ ' ' LCD Display ' ----------- Define LCD_DREG PORTC ' Port for LCD Data Define LCD_DBIT 4 ' Use upper 4 bits of Port Define LCD_RSREG PORTC ' Port for RegisterSelect (RS) bit Define LCD_RSBIT 3 ' Port Pin for RS bit Define LCD_EREG PORTC ' Port for Enable (E) bit Define LCD_EBIT 0 ' Port Pin for E bit Define LCB_BITS 4 ' Using 4-bit bus Define LCD_LINES 2 ' Using 2 line Display Define LCD_COMMANDUS 2000 ' Command Delay (uS) Define LCD_DATAUS 50 ' Data Delay (uS) LCDBackLight var PORTC.1 ' LCD Backlight PWM Pin LCDContrast var PORTC.2 ' LCD Contrast PWM Pin ' ' Sensor Inputs ' ------------- PressIN var PortA.0 ' Pressure Transducer Input ' ' Button (Internet) Inputs ' ------------------------ ButtonDown var PortB.0 ' SW2/Button DN ButtonUp var PortB.1 ' SW1/Button UP ButtonSet var PortB.2 ' SW3/Button SET/RST ButtonReserve var PortB.7 ' SW4/Exclusive CPU Access Button ' ' System Outputs ' -------------- GreenLED var PortA.1 ' Green LED RedLED var PortA.2 ' Red LED Beeper var PortA.3 ' Piezo Sounder ' ' Communications I2C Bus ' ---------------------- SCL var PortA.4 ' I2CLock SDA var PortA.5 ' I2CData ' ' Communications Inter-Processor (IP) Bus ' --------------------------------------- IPAEnable var PortB.3 ' IPENABLEA IPBEnable var PortB.6 ' IPENABLEB IPClock var PortB.4 ' IPCLOCK IPData var PortB.5 ' IPDATA
High RedLED
and if I'm laying out my PCB and need to shuffle the pins about for convenience, there's just one section I need to look in to make changes globally in my program.
And better still, if I need to port my program to a different PIC, again, just one section reassigns all the pins.
Get in the habit of doing it this way - it will save you countless HOURS in the long term.




Bookmarks