0

选择“构建主项目”并尝试编译我的项目时,编译器不断显示以下错误消息:

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

我相信这与我的#define 代码行有关,但我不确定:

// 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();


        }
    }
}

也许我的“定义”命令的位置不正确?还是我忘记了某种代码行?我觉得我想太多了。

4

2 回答 2

0

在控制器标头中有​​一个定义,如:

#define _RB7 PORTBbits.RB7

所以你也可以使用类似的东西:

#define LCD_RS _RB7
于 2020-08-11T13:08:34.930 回答
0

#define是的,您的指令存在问题。它们没有按照应有的方式定义。
在这段代码中:

// 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 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

并将它们用作例如:

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

除了这些问题,我认为您的输入和输出配置寄存器TRISATRISB没有正确配置。

    // A-Type Registers

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

    // B-Type Registers

    TRISB = 0;
    // 0 - 15
    PORTB = 0;
于 2020-08-09T23:34:36.770 回答