Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Test and modify of PORT bits

2007-07-19
2013-03-12
  • franciscojar
    franciscojar
    2007-07-19

    Hi all!

    My first post was solved fast, hope this time too.

    Some time ago, i programed a PIC12F675 and can test and modify the port using GP0, GP1 ... with no problem, but now I need to program a PIC16F874A and test and modify only RA0 (PORTA.0) but appears syntax error:

    portatest.c

    #define __PIC16F874A
    #include "pic/pic16f874a.h"

    void main(void)
    {
    if (PORTA.0 == 1)
      PORTA.0 = 0;
    else
      PORTA.0 = 1;
    }

    sdcc --debug -c -mpic14 -p16f874a --lib-path /usr/share/sdcc/lib/pic portatest.c

    portatest.c:7: syntax error: token -> '.0' ; column 11

    Then change PORTA.0 to RA0 but appears:

    sdcc --debug -c -mpic14 -p16f874a --lib-path /usr/share/sdcc/lib/pic portatest.c
    portatest.c:7: error 20: Undefined identifier 'RA0'
    portatest.c:8: error 20: Undefined identifier 'RA0'
    portatest.c:10: error 20: Undefined identifier 'RA0'

    Then change RA0 to PORTA.RA0 but appears:

    sdcc --debug -c -mpic14 -p16f874a --lib-path /usr/share/sdcc/lib/pic portatest.c
    portatest.c:7: error 25: Structure/Union expected left of '.->'
    portatest.c:8: error 25: Structure/Union expected left of '.->'
    portatest.c:10: error 25: Structure/Union expected left of '.->'

    I know how to do it in assembler:

    begin:
    btfss PORTA,0
    goto set
    bcf PORTA,0
    goto begin
    set:
    bsf PORTA,0
    goto begin

    Do you know how I can test and modify individual port bits on SDCC for PICs?

    Thanks in advance.

    franciscojar.

     
    • Hi!

      Use:

      if(PORTAbits.RA1)

      or

      LATAbits.LATA1 = 1;
      LATAbits.LATA2 = 0;

      Thoses structs are in picXXXXXX.h file in the compiler include folder.

      Tercio.

       
    • franciscojar
      franciscojar
      2007-07-23

      Hello Mr Tercio.

      I'm use PORTAbits.RA0

      code.c file
      ------------------
      #define __PIC16F874
      #include "pic/pic16f874.h"

      void main(void)
      {
      if (PORTAbits.RA0)
        PORTAbits.RA0 = 0;
      else
        PORTAbits.RA0 = 1;
      }
      -----------------

      but appears:

      sdcc --debug -c -mpic14 -p16f874 --lib-path /usr/share/sdcc/lib/pic portatest.c
      portatest.c:7: error 20: Undefined identifier 'PORTAbits'
      portatest.c:7: error 25: Structure/Union expected left of '.->'
      portatest.c:8: error 20: Undefined identifier 'PORTAbits'
      portatest.c:8: error 25: Structure/Union expected left of '.->'
      portatest.c:10: error 20: Undefined identifier 'PORTAbits'
      portatest.c:10: error 25: Structure/Union expected left of '.->'
      -:0: error 103: code not generated for 'main' due to previous errors
      make: *** [portatest.o] Error 1

      And if I use

      code.c
      ---------------
      #define __PIC16F874
      #include "pic/pic16f874.h"

      void main(void)
      {
      if (LATAbits.LATA0)
        LATAbits.LATA0 = 0;
      else
        LATAbits.LATA0 = 1;
      }
      -----------------

      Appears:

      sdcc --debug -c -mpic14 -p16f874 --lib-path /usr/share/sdcc/lib/pic portatest.c
      portatest.c:7: error 20: Undefined identifier 'LATAbits'
      portatest.c:7: error 25: Structure/Union expected left of '.->'
      portatest.c:8: error 20: Undefined identifier 'LATAbits'
      portatest.c:8: error 25: Structure/Union expected left of '.->'
      portatest.c:10: error 20: Undefined identifier 'LATAbits'
      portatest.c:10: error 25: Structure/Union expected left of '.->'
      -:0: error 103: code not generated for 'main' due to previous errors
      make: *** [portatest.o] Error 1

      By the way, I watch the pic16F874.h and no exist a struct for PORTX

      I watch the pic12F675.h and exist struct for GPIO.

      My version of sdcc is:
      SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.6.0 #4309 (Apr 27 2007) (UNIX)

      I hope somebody can help me.

      Best Regards.

      franciscojar.

       
    • franciscojar
      franciscojar
      2007-07-31

      Hello again:

      I try to use the -> notation:

      code.c
      --------------

      #define __PIC16F874A
      #include "pic/pic16f874a.h"

      void main(void)
      {
      if (PORTA->RA0)
        PORTA->RA0 = 0;
      else
        PORTA->RA0 = 1;
      }

      ---------------------

      but appears:

      sdcc --debug -c -mpic14 -p16f874a --lib-path /usr/share/sdcc/lib/pic portatest.c
      portatest.c:7: error 27: Pointer required
      portatest.c:7: error 27: Pointer required
      portatest.c:8: error 27: Pointer required
      portatest.c:8: error 27: Pointer required
      portatest.c:10: error 27: Pointer required
      portatest.c:10: error 27: Pointer required

      I watch the pic12f675.h file and found the port GPIO declaration:

      // ----- GPIO bits --------------------
      typedef union {
        struct {
          unsigned char GP0:1;
          unsigned char GP1:1;
          unsigned char GP2:1;
          unsigned char GP3:1;
          unsigned char GP4:1;
          unsigned char GP5:1;
          unsigned char :1;
          unsigned char :1;
        };
        struct {
          unsigned char GPIO0:1;
          unsigned char GPIO1:1;
          unsigned char GPIO2:1;
          unsigned char GPIO3:1;
          unsigned char GPIO4:1;
          unsigned char GPIO5:1;
          unsigned char :1;
          unsigned char :1;
        };
      } __GPIO_bits_t;
      extern volatile __GPIO_bits_t __at(GPIO_ADDR) GPIO_bits;

      #define GP0                  GPIO_bits.GP0
      #define GPIO0                GPIO_bits.GPIO0
      #define GP1                  GPIO_bits.GP1
      #define GPIO1                GPIO_bits.GPIO1
      #define GP2                  GPIO_bits.GP2
      #define GPIO2                GPIO_bits.GPIO2
      #define GP3                  GPIO_bits.GP3
      #define GPIO3                GPIO_bits.GPIO3
      #define GP4                  GPIO_bits.GP4
      #define GPIO4                GPIO_bits.GPIO4
      #define GP5                  GPIO_bits.GP5
      #define GPIO5                GPIO_bits.GPIO5

      then I modify this to PORTA in pic16f874a.h:

      // ----- PORTA bits --------------------
      typedef union {
        struct {
          unsigned char RA0:1;
          unsigned char RA1:1;
          unsigned char RA2:1;
          unsigned char RA3:1;
          unsigned char RA4:1;
          unsigned char RA5:1;
          unsigned char RA6:1;
          unsigned char RA7:1;
        };
      } __PORTA_bits_t;
      extern volatile __PORTA_bits_t __at(PORTA_ADDR) PORTA_bits;

      #define RA0                  PORTA_bits.RA0
      #define RA1                  PORTA_bits.RA1
      #define RA2                  PORTA_bits.RA2
      #define RA3                  PORTA_bits.RA3
      #define RA4                  PORTA_bits.RA4
      #define RA5                  PORTA_bits.RA5
      #define RA6                  PORTA_bits.RA6
      #define RA7                  PORTA_bits.RA7

      but all the previous errors still remained!!!!!!!!!!

      Anybody has a style to read/modify individual bits of PORTX????

      Thanks in advance.

      franciscojar.

       
    • franciscojar
      franciscojar
      2007-08-10

      Hello again,

      I'm in the same problem...

      I change the pic16f874.h PORTA to:

      // ----- PORTA bits --------------------
      typedef union {
        struct {
          unsigned char RA0:1;
          unsigned char RA1:1;
          unsigned char RA2:1;
          unsigned char RA3:1;
          unsigned char RA4:1;
          unsigned char RA5:1;
          unsigned char RA6:1;
          unsigned char RA7:1;
        };
      } __PORTA_bits_t;
      volatile __PORTA_bits_t __at(PORTA_ADDR) PORTA;

      #define RA0                  PORTA.RA0
      #define RA1                  PORTA.RA1
      #define RA2                  PORTA.RA2
      #define RA3                  PORTA.RA3
      #define RA4                  PORTA.RA4
      #define RA5                  PORTA.RA5
      #define RA6                  PORTA.RA6
      #define RA7                  PORTA.RA7

      but errors continue.

      Anybody knows how change individual PORTX bits, without use Mask??

      Thanks in advance.

      franciscojar.

       
    • franciscojar
      franciscojar
      2007-09-14

      Hello again,

      Today I looked into picxxfxxx.lib files with an hex viewer and found the following:

      Into file 12f675.lib exist _GPIO and _GPIO_bits so I suppose when _GPIO_bits are declared in pic12f975.h as :

      // ----- GPIO bits --------------------
      typedef union {
      struct {
      unsigned char GP0:1;
      unsigned char GP1:1;
      unsigned char GP2:1;
      unsigned char GP3:1;
      unsigned char GP4:1;
      unsigned char GP5:1;
      unsigned char :1;
      unsigned char :1;
      };
      struct {
      unsigned char GPIO0:1;
      unsigned char GPIO1:1;
      unsigned char GPIO2:1;
      unsigned char GPIO3:1;
      unsigned char GPIO4:1;
      unsigned char GPIO5:1;
      unsigned char :1;
      unsigned char :1;
      };
      } __GPIO_bits_t;
      extern volatile __GPIO_bits_t __at(GPIO_ADDR) GPIO_bits;

      #define GP0 GPIO_bits.GP0
      #define GPIO0 GPIO_bits.GPIO0
      #define GP1 GPIO_bits.GP1
      #define GPIO1 GPIO_bits.GPIO1
      #define GP2 GPIO_bits.GP2
      #define GPIO2 GPIO_bits.GPIO2
      #define GP3 GPIO_bits.GP3
      #define GPIO3 GPIO_bits.GPIO3
      #define GP4 GPIO_bits.GP4
      #define GPIO4 GPIO_bits.GPIO4
      #define GP5 GPIO_bits.GP5
      #define GPIO5 GPIO_bits.GPIO5

      SDCC can manage individual bits as GPx or GPIOx

      But... in the file pic16f874.lib only exist _PORTA, so when I try to add a structure and definitions like in pic12f675, sdcc can not found "_PORTA_bits" in the pic16f874.lib and refuse manage individual bits.

      Somebody know how I will modify the library to add _PORTx_bits function to be able to manage individual bits???

      Thanks in advance.

      franciscojar