1

I was trying to program a PIC 16f877A Micro-controller to rotate a servomotor, 0 to 180 degrees, but every time i try to build the program i get an error "member reference base type 'volatile unsigned char' is not a structure or union".I am using MPLAB with xc8 compiler. I couldn't find the issue.

#include<xc.h>

void Rotation0()
{
    int i;
    for(i=0;i<50;i++)
    {
        PORTB.F0 = 1;
        __delay_us(800); 
        PORTB.F0 = 0;
        __delay_us(19200);
    }
}

void Rotation90() 
{
    int i;
    for(i=0;i<50;i++)
    {
        PORTB.F0 = 1;
        __delay_us(1500); 
        PORTB.F0 = 0;
        __delay_us(18500);
    }
}

void Rotation180()
{
    int i;
    for(i=0;i<50;i++)
    {
        PORTB.F0 = 1;
        __delay_us(2200); 
        PORTB.F0 = 0x00;
        __delay_us(17800);
    }
}

void main()
{
    TRISB = 0; 
    PORTB = 0x00;
    do
    {
        Rotation0(); 
        __delay_ms(2000);
        Rotation90(); 
        __delay_ms(2000);
        Rotation180(); 
    }while(1);
}
  • I'm guessing that `PORTB` is not a structure. Take a look in `xc.h` and find the definition of `PORTB`. – user3386109 Aug 31 '21 at 19:57
  • You may want to consider a delay at the end of your main do loop. When the loop wraps around, the Rotation0() call will immediately follow the Rotation 180 call. – EBlake Sep 01 '21 at 05:13

1 Answers1

5

As @user3386109 pointed out in the comments, PORTB isn't a structure.

You can use PORTBbits.RB0 = 1; or just RB0 = 1; as it's also defined. I prefer the first one because it's more verbose.

The same naming convention is used for all registers which have special named bits. Register and bit names match with the ones in the datasheet. Sometimes the same bit name may be used in multiple registers. In this case, you need the verbose method.

PORTB refers the 8-bit register itself and can be used for direct 8-bit assignments, like PORTB = 0x00;

Tagli
  • 2,412
  • 2
  • 11
  • 14