Undeclared (First use in function) error / XC16

993 Views Asked by At

When selecting the "Build Main Project" and attempting to compile my project, the compiler keeps bringing up these error messages:

Source.c:64:8: error: 'RB6' undeclared (first use in this function)

I believe it has something to do with my #define lines of code, but I'm not certain:

// Include Commands

#include "xc.h"
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
#include "pragma.h"
#include <stdbool.h>

// Define Commands

#define LCD_RS RB7
#define LCD_D0 RB15
#define LCD_D1 RB14
#define LCD_D2 RB13

#define ULTRAECHO RA4 
#define ULTRATRIG RB5 

#define PIR RB6
#define PIRII RB7

#define MOTOR RB4
#define MOTORII RB12

#define XTAL_FREQ 8000000


void setup() {
    // A-Type Registers

    TRISA = 0;
    // 0 - 5
    PORTA = 0;

    // B-Type Registers

    TRISB = 0;
    // 0 - 15
    PORTB = 0;


    LCD_Cmd(LCD_CLEAR);

    __delay_ms(50);

    LCD_Begin();
}

void loop() {
    int value;

    value = 0;

    __delay_ms(300);

    PORTBbits.RB6 = 1;
    PORTBbits.RB7 = 1;

    if (PIR == 1 || PIRII == 1) {            // Checks both PIR Sensors
        PORTBbits.RB8 = 1;                  // Activates LCD Screen 
        PORTBbits.RB15 = 1;
        PORTBbits.RB14 = 1;
        PORTBbits.RB13 = 1;

        PORTAbits.RA4 = 1;                  // Activates ULTRASONIC
        PORTBbits.RB5 = 1;

        LCD_Cmd(LCD_CLEAR);
        (value = value + 1);
    }
    else {
        PORTBbits.RB8 = 0;              // No Life Detected
        PORTBbits.RB15 = 0;
        PORTBbits.RB14 = 0;
        PORTBbits.RB13 = 0;
        (value = value);
    }

    {
        LCD_Goto(1, 1);
        LCD_Print(value);
        goto __delay_ms(300);
    }

    {

        if (PORTAbits.RA4 = 1 || PORTBbits.RB5 = 1) {

            __bit wait_sensor();            // Activates Timer   

            uint16_t i = 0;
            TMR1 = 0;                // Resets Timer
            TMR1 = 1;
            while (!ULTRAECHO && (i < 1000)) {
                i = (TMR1 << 8) | TMR1;

                if (i >= 1000)
                    return 0;

                else {
                    return 1;
                }
            }
        }

        __bit get_distance(uint16_t *ticks);

        {
            uint16_t *ticks = 0;
            TMR1 = 0;

            while (ULTRAECHO && ('uint16_t *ticks' < 23200))
                uint16_t *ticks = (TMR1 << 8) | TMR1;               // Read Timer Value

            TMR1 = 0;

            if (uint16_t *ticks >= 23200)
                return 1;

            else {
                return 0;

            }
        }

        {
            OSCCON = 0x70;              // Sets Timer and Oscillator Value
            T1CON = 0x10;
            TMR1 = 0;

            __delay_ms(500);

            ULTRATRIG = 0;              // Toggle Pin to LOW
            __delay_us(2);
            ULTRATRIG = 1;              // Generate 10us Pulse
            __delay_us(10);
            ULTRATRIG = 0;


            if (ULTRAECHO = 1)              // Read Pulse from ULTRAECHO
                uint16_t distance;

            if (get_distance()) {            // Distance greater than 400cm
                LCD_Goto(2, 1);
                LCD_Print("  No Proximity  ");

                MOTORII = 0;
                MOTOR = 0;

            }
            else {
                uint16_t distance = uint16_t distance / 58;       // Calculates Distance
                LCD_Goto(2, 1);
                LCD_Print(distance, " cm ");

                MOTORII = 1;
                MOTOR = 1;

            }


            uint16_t distance = uint16_t distance / 58;
            if (uint16_t distance <= 20)
                MOTORII = 0;
            MOTOR = 0;


            __delay_ms(500);

            goto void setup();


        }
    }
}

Perhaps the placement of my "define" commands are incorrect? Or is there some sort of line of code I'm forgetting? I feel like I'm overthinking this.

2

There are 2 best solutions below

0
Sourabh Choure On

Yes, there are issues with your #define directives. They are not defined as they ought to be.
In this block of code:

// Define Commands
#define LCD_RS RB7
#define LCD_D0 RB15
#define LCD_D1 RB14
#define LCD_D2 RB13

#define ULTRAECHO RA4 
#define ULTRATRIG RB5 

#define PIR RB6
#define PIRII RB7

#define MOTOR RB4
#define MOTORII RB12

Define those macros as:

#define LCD_RS PORTBbits.RB7  //Check if it is RB7 or RB8, as somwhere it is RB8
#define LCD_D0 PORTBbits.RB15
#define LCD_D1 PORTBbits.RB14
#define LCD_D2 PORTBbits.RB13

#define ULTRAECHO PORTAbits.RA4 
#define ULTRATRIG PORTBbits.RB5 

#define PIR PORTBbits.RB6
#define PIRII PORTBbits.RB7

#define MOTOR PORTBbits.RB4
#define MOTORII PORTBbits.RB12

And use them for example as:

...
    PIR = 1;
    PIRII = 1;

    if (PIR == 1 || PIRII == 1) {            // Checks both PIR Sensors
        LCD_RS = 1;                          // Activates LCD Screen 
        LCD_D0 = 1;
        LCD_D1 = 1;
        LCD_D2 = 1;
...

Apart from those issues, I think your Input and Output configuring registers TRISA and TRISB are not properly configured.

    // A-Type Registers

    TRISA = 0;
    // 0 - 5
    PORTA = 0;

    // B-Type Registers

    TRISB = 0;
    // 0 - 15
    PORTB = 0;
0
Mike On

In the controller header there is a define like:

#define _RB7 PORTBbits.RB7

so you could also use something like:

#define LCD_RS _RB7