Author Topic: PIC ports and C  (Read 1691 times)

0 Members and 1 Guest are viewing this topic.

Offline PerranOak

  • Frequent Contributor
  • **
  • Posts: 537
  • Country: gb
PIC ports and C
« on: October 25, 2019, 04:57:12 pm »
Using a 16F1827 I get that to address a port is, say:

PORTBbits.RB5=1

But, 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]
Some light can never be seen!
RJD
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2829
  • Country: se
  • Country: Broken Britain
Re: PIC ports and C
« Reply #1 on: October 25, 2019, 05:16:37 pm »
GPIO = 0b111111;
or
GPIO0 = 1;
to
GPIO5 = 1;
...probably.
« Last Edit: October 25, 2019, 05:18:09 pm by StillTrying »
.  That took much longer than I thought it would.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 9815
  • Country: fr
Re: PIC ports and C
« Reply #2 on: October 25, 2019, 06:13:37 pm »
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.
 

Offline PerranOak

  • Frequent Contributor
  • **
  • Posts: 537
  • Country: gb
Re: PIC ports and C
« Reply #3 on: October 27, 2019, 10:25:57 am »
Cheers both.

"Compiler's distribution" - what's that?
Some light can never be seen!
RJD
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2829
  • Country: se
  • Country: Broken Britain
Re: PIC ports and C
« Reply #4 on: October 27, 2019, 10:38:51 am »
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.
« Last Edit: October 27, 2019, 10:40:59 am by StillTrying »
.  That took much longer than I thought it would.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 11466
Re: PIC ports and C
« Reply #5 on: October 27, 2019, 01:33:09 pm »
Nope.  The *OLD* single port PICS are  GPIO (and GPIObits.GPn for 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).
« Last Edit: October 27, 2019, 03:40:14 pm by Ian.M »
 
The following users thanked this post: StillTrying

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 9815
  • Country: fr
Re: PIC ports and C
« Reply #6 on: October 27, 2019, 02:58:10 pm »
Cheers both.

"Compiler's distribution" - what's that?

The 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.
 

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: PIC ports and C
« Reply #7 on: October 28, 2019, 08:11:11 pm »
GPIO/TRISIO is wasted brain cells. I will define a PORTA/TRISA to be GPIO/TRISIO.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 11466
Re: PIC ports and C
« Reply #8 on: October 28, 2019, 08:22:52 pm »
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
 

Offline spudboy488

  • Regular Contributor
  • *
  • Posts: 136
Re: PIC ports and C
« Reply #9 on: October 29, 2019, 11:32:41 am »
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

That's what I do as well. The definitions are the net names (usually) from the schematic. It makes coding a lot more apparent.

Code: [Select]
// Hardware definitions

#define HB_LED LATBbits.LATB6 // Used for heartbeat
#define ADC1 LATDbits.LATD5
#define ADC2 LATDbits.LATD3
#define DIO1 LATBbits.LATB3
#define DIO2 LATAbits.LATA3
#define DIO3 LATCbits.LATC1
#define DDS_SEL LATAbits.LATA6
#define POT_OFFSET_CS         LATCbits.LATC0
#define POT_GAIN_CS         LATEbits.LATE0
#define POT_INC LATAbits.LATA5
#define POT_DIR LATAbits.LATA4
#define PWM1 LATCbits.LATC2
#define PWM2 LATDbits.LATD1
 

Offline PerranOak

  • Frequent Contributor
  • **
  • Posts: 537
  • Country: gb
Re: PIC ports and C
« Reply #10 on: October 29, 2019, 04:19:26 pm »
Cheers.

Using GPIObits.GP2 and TRISIO2 worked 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 Addresses
fsr equ 04h
fsr0 equ 04h
indf equ 00h
indf0 equ 00h
pc equ 02h
pcl equ 02h
pclath equ 0Ah
status equ 03h

... nothing about GPIO, etc. and nothing was there within MPLAB X in the source files or other directories.
Some light can never be seen!
RJD
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2829
  • Country: se
  • Country: Broken Britain
Re: PIC ports and C
« Reply #11 on: October 29, 2019, 04:28:11 pm »
Nothing in #define s ? :-//

I did go to put MPLAB X IDE v5.25 on this PC but when I say saw it was a 900 MB DL I didn't bother. :)
« Last Edit: December 07, 2019, 09:57:59 pm by StillTrying »
.  That took much longer than I thought it would.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 11466
Re: PIC ports and C
« Reply #12 on: October 29, 2019, 07:06:28 pm »
you looked in the wrong file.  try pic12f683.h
 
The following users thanked this post: SiliconWizard

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: PIC ports and C
« Reply #13 on: October 30, 2019, 09:12:32 am »
Quote
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
Yeah, 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.

Quote
#define HB_LED         LATBbits.LATB6            // Used for heartbeat
 #define ADC1         LATDbits.LATD5
In 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,4
Even in your defines, you don't have to write/think GPIO/TRISIO, anymore, once you have defined it to PORTA/TRISA.
« Last Edit: October 30, 2019, 09:41:43 am by KL27x »
 

Offline PerranOak

  • Frequent Contributor
  • **
  • Posts: 537
  • Country: gb
Re: PIC ports and C
« Reply #14 on: October 30, 2019, 10:32:28 am »
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 used GP2 instead of GPIObits.GP2 and that worked too! Brill.
Some light can never be seen!
RJD
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 11466
Re: PIC ports and C
« Reply #15 on: October 30, 2019, 11:29:59 am »
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 the SFRbits.bitname form . . . .
 

Offline PerranOak

  • Frequent Contributor
  • **
  • Posts: 537
  • Country: gb
Re: PIC ports and C
« Reply #16 on: October 30, 2019, 03:45:59 pm »
Oh right, cheers.
Some light can never be seen!
RJD
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 531
  • Country: gb
    • Electronic controls
Re: PIC ports and C
« Reply #17 on: December 05, 2019, 02:52:58 pm »
I never use GPIO to reference a port.
I use #define LED GPIO,0
Makes the code much more readable.


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf