PIC ports and C - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 25, 2019, 04:57:12 pm
content: Using a 16F1827 I get that to address a port is, say:PORTBbits.RB5=1But, when I want to do the same for a 12F683 (which has no PORTA or PORTB) how do I know how to address it?I've looked in the 12F683 datasheet (all assembly examples) and the compiler user guide to no avail.Cheers.[using MPLAB X IDE v5.25 and XC8 v2.10]

author: StillTrying
author_rank: Super Contributor
timestamp: October 25, 2019, 05:16:37 pm
content: GPIO = 0b111111;orGPIO0 = 1;toGPIO5 = 1;...probably.

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 25, 2019, 06:13:37 pm
content: Why not look at the header file corresponding to your PIC model in the compiler's distribution?You'll see all definitions you need in there.

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 27, 2019, 10:25:57 am
content: Cheers both."Compiler's distribution" - what's that?

author: StillTrying
author_rank: Super Contributor
timestamp: October 27, 2019, 10:38:51 am
content: Start a new blank 12F683 project, and look in the source and include files MPLAB adds to it?The single port ICs are always GPIO AFAIK.

author: Ian.M
author_rank: Super Contributor
timestamp: October 27, 2019, 01:33:09 pm
content: Nope. The *OLD* single port PICS are GPIO (and GPIObits.GPnfor individual bit access), but newer ones datasheets typically call their only port PORTA. The SFR address doesn't change, only the name attached to it.e.g. PIC10F2xx has GPIO and PIC10F32x has PORTA (and LATA, as those oddballs are enhanced midrange I/O grafted onto a standard midrange core, presumably to save silicon area to permit the tiny SOT23-6 package).

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 27, 2019, 02:58:10 pm
content: Quote from: PerranOak on October 27, 2019, 10:25:57 amThe directory where all the compiler's files are installed? Then look in all the "include" subdirectories and find the header files for all the supported PIC models.

author: KL27x
author_rank: Super Contributor
timestamp: October 28, 2019, 08:11:11 pm
content: GPIO/TRISIO is wasted brain cells. I will define a PORTA/TRISA to be GPIO/TRISIO.

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2019, 08:22:52 pm
content: That's a royal PITA because of the port bit definitions. Instead, its better to #define pins named by function and localize the brain damage by avoiding all use of PORTA vs GPIO and TRISA vs TRISIO except in your hardware.h

author: spudboy488
author_rank: Regular Contributor
timestamp: October 29, 2019, 11:32:41 am
content: Quote from: Ian.M on October 28, 2019, 08:22:52 pmThat's what I do as well. The definitions are the net names (usually) from the schematic. It makes coding a lot more apparent.Code:// Hardware definitions#define HB_LEDLATBbits.LATB6// Used for heartbeat#define ADC1LATDbits.LATD5#define ADC2LATDbits.LATD3#define DIO1LATBbits.LATB3#define DIO2LATAbits.LATA3#define DIO3LATCbits.LATC1#define DDS_SELLATAbits.LATA6#define POT_OFFSET_CSLATCbits.LATC0#define POT_GAIN_CSLATEbits.LATE0#define POT_INCLATAbits.LATA5#define POT_DIRLATAbits.LATA4#define PWM1LATCbits.LATC2#define PWM2LATDbits.LATD1

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 29, 2019, 04:19:26 pm
content: Cheers.UsingGPIObits.GP2andTRISIO2worked a treat.I still can see how you can tell this from any data provided. I looked in the XC8 included file for the 12F683 and all it had (apart from disclaimer bumph) was:; Code-generator required, PIC12F683 Definitions;; SFR Addressesfsr equ 04hfsr0 equ 04hindf equ 00hindf0 equ 00hpc equ 02hpcl equ 02hpclath equ 0Ahstatus equ 03h... nothing about GPIO, etc. and nothing was there within MPLAB X in the source files or other directories.

author: StillTrying
author_rank: Super Contributor
timestamp: October 29, 2019, 04:28:11 pm
content: Nothing in#defines ?I did go to putMPLAB X IDE v5.25on this PC but when Isaysaw it was a 900 MB DL I didn't bother.

author: Ian.M
author_rank: Super Contributor
timestamp: October 29, 2019, 07:06:28 pm
content: you looked in the wrong file. try pic12f683.h

author: KL27x
author_rank: Super Contributor
timestamp: October 30, 2019, 09:12:32 am
content: QuoteYeah, Ian. I do that, too. But sometimes ports and registers are important to the software for byte instructions/manipulations to save instruction cycles and/or where state changes must happen at the same time, rather than serially.You can do those things through this additional layer, as well, but then you need more defines (and more cumbersome code, which is the cost for pin flexibility).PORTA vs GPIO at least keeps things the same across devices.QuoteIn order to do more involved/minute/direct stuffs, you might end up needing more defines ala:#define HB_LED.port PORTA#define HB_LED.tris TRISA#define HB_LED.lat LATA#define HB_LED.pin 4#define HB_LED PORTA,4Even in your defines, you don't have to write/think GPIO/TRISIO, anymore, once you have defined it to PORTA/TRISA.

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 30, 2019, 10:32:28 am
content: Cheers StillTrying - I don't blame you: it's a big, messy install. Not sure where "#define s" is to be found.Thank you Ian.M I just looked at the first file with 12F683 in it.From info in the file you suggested (though I don't understand it all) I usedGP2instead ofGPIObits.GP2and that worked too! Brill.

author: Ian.M
author_rank: Super Contributor
timestamp: October 30, 2019, 11:29:59 am
content: Although the short port pin names and SFR bit names work on old PICs, and are currently still there for compatibility with old code, Microchip are committed to removing them and they aren't defined on new PICs. Get used to using theSFRbits.bitnameform . . . .

author: PerranOak
author_rank: Frequent Contributor
timestamp: October 30, 2019, 03:45:59 pm
content: Oh right, cheers.

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: December 05, 2019, 02:52:58 pm
content: I never use GPIO to reference a port.I use #define LED GPIO,0Makes the code much more readable.