ckeil8051

I am getting a weird error while programing in C for microcontroller 8051


I am trying to make a mini model of radar using 8051 microcontroller, servo motor, ultrasonic sensor, and 16*2 LCD.

I give my code as follows:

#include <reg51.h>  // include header file for 8051 microcontroller

// Define pins for ultrasonic sensor
sbit trig = P1^0;
sbit echo = P1^1;

// Define pins for LCD display
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
sbit servo_pin = P3^0;

// Global variables
unsigned int distance;  // to store distance measured by ultrasonic sensor
unsigned char angle;  // to store angle of detected object
unsigned char buffer[10];  // buffer to store distance and angle data for display on LCD

// Function prototypes
void delay(unsigned int time);
void lcd_cmd(unsigned char command);
void lcd_data(unsigned char data);
void lcd_init();
void lcd_string(unsigned char *str);
void servo_rotate(unsigned char degrees);
void ultrasonic_sensor(void);

void main()
{
    lcd_init();  // initialize LCD display
    servo_rotate(0);  // set servo motor to starting position
    while (1)
    {
        ultrasonic_sensor();  // measure distance and angle of detected object
        sprintf(buffer, "Distance: %dcm", distance);
        lcd_cmd(0x80);  // set cursor to first line on LCD
        lcd_string(buffer);  // display distance data on first line of LCD
        sprintf(buffer, "Angle: %d", angle);
        lcd_cmd(0xC0);  // set cursor to second line on LCD
        lcd_string(buffer);  // display angle data on second line of LCD
        delay(1000);  // wait for 1 second before measuring again
    }
}

// Function to generate delay in microseconds
void delay(unsigned int time)
{
    unsigned int i, j;
    for (i = 0; i < time; i++)
    {
        for (j = 0; j < 12; j++)
            ;
    }
}

// Function to send commands to LCD display
void lcd_cmd(unsigned char command)
{
    P0 = command;
    rs = 0;  // set RS pin low for command mode
    rw = 0;  // set RW pin low for write mode
    en = 1;  // set EN pin high to enable LCD
    delay(1);  // wait for some time
    en = 0;  // set EN pin low to disable LCD
}

// Function to send data to LCD display
void lcd_data(unsigned char data)
{
    P0 = data;
    rs = 1;  // set RS pin high for data mode
    rw = 0;  // set RW pin low for write mode
    en = 1;  // set EN pin high to enable LCD
    delay(1);  // wait for some time
    en = 0;  // set EN pin low to disable LCD
}

// Function to initialize LCD display
void lcd_init()
{
    lcd_cmd(0x38);  // 2 lines, 5x7 matrix
    lcd_cmd(0x0E);  // display on, cursor blinking
    lcd_cmd(0x01);  // clear display
    lcd_cmd(0x80);  // set cursor to first line on LCD
}

// Function to display string on LCD display
void lcd_string(unsigned char *str)
{
    while (*str)
    {
        lcd_data(*str++);
    }
}

// Function to rotate servo motor to specified degrees
void servo_rotate(unsigned char degrees)
{
    unsigned char i;
    for (i = 0; i < degrees; i++)
    {
        servo_pin = 1;
        delay(10);  // wait for 10 milliseconds
        servo_pin = 0;
        delay(10);  // wait for 10 milliseconds
    }
}

// Function to measure distance and angle of detected object using ultrasonic sensor and servo motor
void ultrasonic_sensor()
{
    unsigned int i;
    unsigned int duration;
    unsigned int angle_list[150];  // angle list for servo motor
    unsigned int distance_list[150];  // list to store distance measured at each angle
    for (i = 1; i <= 150; i++)
    {
        angle_list[i - 1] = i;
    }
    for (i = 0; i < 150; i++)
    {
        servo_rotate(angle_list[i]);  // rotate servo motor to current angle
        delay(100);  // wait for some time before taking measurement
        trig = 1;  // set trig pin high to trigger ultrasonic sensor
        delay(10);  // wait for 10 microseconds
        trig = 0;  // set trig pin low to stop triggering
        while (!echo)  // wait for echo pin to go high
            ;
        TMR1 = 0;  // reset Timer1
        while (echo)  // measure duration of echo pulse
        {
            TMR1++;
            delay(1);  // wait for 1 microsecond
        }
        duration = TMR1;  // store duration of echo pulse
        distance_list[i] = duration * 0.017;  // calculate distance in centimeters
    }
    // Find minimum distance and corresponding angle
    unsigned int min_distance = distance_list[0];
    unsigned int min_distance_index = 0;
    for (i = 1; i < 150; i++)
    {
        if (distance_list[i] < min_distance)
        {
            min_distance = distance_list[i];
            min_distance_index = i;
        }
    }
    distance = min_distance;  // set global variable for distance measurement
    angle = angle_list[min_distance_index];  // set global variable for angle of detected object
}

While building this program in Keil, I am getting the error as:
radar.c(21): error C141: syntax error near ')', expected '<id>'.

I cross-checked the code for missing semicolon, but there is no problem with it.

I just want to know how to solve this error.


Solution

  • I strongly assume that it is this line:
    void lcd_data(unsigned char data);

    You cannot use data as a user symbol, because it is a reserved keyword as an extension of Keil C51.

    See this page of the documentation for a complete list.

    Rename it to something else, and the error is gone.