Search code examples
cpicmplabservoxc8

member reference base type 'volatile unsigned char' is not a structure or union


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);
}

Solution

  • 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;