1
\$\begingroup\$

I'm new in Embedded systems I write this code and tried to compile it in "AVR Studio" but I have one error and I can't correct it the error is :"expected declaration or statement at end of input" and refer to the last line in the code

please help me to find the location of this error

THE CODE :

/*
 * AVRGCC1.c
 *
 * Created: 2/8/2012 1:11:07 PM
 * Author: heha
 */
#define nibble 0 //determine nibble mode if 1 or 8-bit mode if 0
DDRD = 0XFF;
DDRB = 0X07;
#define portfor3pins PORTB
//define EN at PORTB pin no. 0
//define RS at PORTB pin no. 1
//define RW at PORTB pin no. 2
#define port PORTD
//LCD's DB pins deal with port D
//in 8-bit mode connect DB0 to p0 and DB1 to p1 and so on until DB7 to p7
//in nibble mode connect DB4 to p4 and DB5 to p5 and DB6 to p6 and DB7 to p7
#include <avr/io.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
void RS(unsigned char i);
void EN(unsigned char i);
void RW(unsigned char i);
void initialization();
void clearscreen();
void movecursortohome();
void setcursorstate(unsigned char isblinking, unsigned char ison);
void setcursorblink();
void setcursornoblink();
void setcursoron();
void setcursoroff();
void writecharatpos(unsigned char row, unsigned char col, char ch);
void setcursorpos(unsigned char row, unsigned char col);
void writestringatpos(unsigned char row, unsigned char col, const char data[]);

void RS(unsigned char i) {
    if (i == 1)
        portfor3pins |= 0X02;
    else
        portfor3pins &= 0XFD;
}
void EN(unsigned char i) {
    if (i == 1)
        portfor3pins |= 0X01;
    else
        portfor3pins &= 0XFE;
}
void RW(unsigned char i) {
    if (i == 1)
        portfor3pins |= 0X04;
    else
        portfor3pins &= 0XFB;
}
void initialization() {
    _delay_ms(3); //3
    EN(1);
    if (nibble) {
        port = 0X30;
        _delay_ms(1);
        port = 0x00;
        _delay_ms(50);
        port = 0x30;
        _delay_ms(1);
        port = 0x00;
        _delay_ms(10);
        port = 0x30;
        _delay_ms(1);
        port = 0x00;
        _delay_ms(1);
        port = 0x20;
        _delay_ms(1);
        port = 0x00;
        _delay_ms(50);
        RS(0);
        RW(0);
        port = 0X20; //DL=0
        _delay_ms(1);
        port = 0X80; //N=1,F=0
        _delay_ms(1);
        port = 0X00;
        _delay_ms(1);
        port = 0X80; //D=C=B=0
        _delay_ms(1);
        port = 0X00;
        _delay_ms(1);
        port = 0XF0;
        _delay_ms(1);
        port = 0X00;
        _delay_ms(1);
        port = 0X60; //S=0,I/D=1
        _delay_ms(1);
        port = 0X00;
        _delay_ms(1);
        port = 0X10;
        _delay_ms(50);
    } else {
        port = 0x30;
        _delay_ms(50);
        port = 0x30;
        _delay_ms(10);
        port = 0x30;
        _delay_ms(1);
        port = 0x30;
        _delay_ms(50);
        RS(0);
        RW(0);
        port = 0X38; //DL=1,N=1,F=0
        _delay_ms(1);
        port = 0X08;//D=C=B=0
        _delay_ms(1);
        port = 0X0F;
        _delay_ms(1);
        port = 0X06;//S=0,I/D=1
        _delay_ms(1);
        port = 0X01;
        _delay_ms(50);
    }
}
void clearscreen() {
    EN(1);
    RS(0);
    RW(0);
    if (nibble) {
        port = 0X00;
        _delay_ms(1);
        port = 0X10;
    } else
        port = 0X01;
    _delay_ms(1);
}
void movecursortohome() {
    EN(1);
    RS(0);
    RW(0);
    if (nibble) {
        port = 0X00;
        _delay_ms(1);
        port = 0X30;
    } else
        port = 0X03;
    _delay_ms(2);
}
void setcursorstate(unsigned char isblinking, unsigned char ison) {
    RS(0);
    RW(0);
    EN(1);
    if ((isblinking == 0) && (ison == 0)) {
        if (nibble) {
            port = 0x00;
            _delay_ms(1);
            port = 0xC0;
        } else
            port = 0x0C;
    } else if ((isblinking == 1) && (ison == 0)) {
        if (nibble) {
            port = 0X00;
            _delay_ms(1);
            port = 0XD0;
        } else
            port = 0X0D;
    } else if ((isblinking == 0) && (ison == 1)) {
        if (nibble) {
            port = 0X00;
            _delay_ms(1);
            port = 0XE0;
        } else
            port = 0X0E;
    } else {
        if (nibble) {
            port = 0X00;
            _delay_ms(1);
            port = 0XF0;
        } else
            port = 0X0F;
    }
    _delay_ms(1);
}
void setcursorblink() {
    RS(0);
    RW(0);
    EN(1);
    port &= 0x0f;
    _delay_ms(1);
    if (!nibble)
        port |= 0x09;
    else if (nibble)
        port |= 0x90;
    _delay_ms(1);
}
void setcursornoblink() {
    RS(0);
    RW(0);
    EN(1);
    if (!nibble) {
        port &= 0X0E;
        _delay_ms(1);
        port |= 0X08;
    } else if (nibble) {
        port = 0x00;
        _delay_ms(1);
        port |= 0x80;
    }
    _delay_ms(1);
}
void setcursoron() {
    RS(0);
    RW(0);
    EN(1);
    port &= 0x0f;
    _delay_ms(1);
    if (!nibble)
        port |= 0X0A;
    else if (nibble)
        port |= 0XA0;
    _delay_ms(1);
}
void setcursoroff() {
    RS(0);
    RW(0);
    EN(1);
    if (!nibble) {
        port &= 0X0D;
        _delay_ms(1);
        port |= 0X08;
    } else if (nibble) {
        port = 0X00;
        _delay_ms(1);
        port |= 0X80;
    }
    _delay_ms(1);
}
void writecharatpos(unsigned char row, unsigned char col, char ch)
{
    char pos, i, y, z;
    RS(0);
    RW(0);
    EN(1);
    if (row == 0) {
        pos = 0X80 + col; //in fact I don't use this variable later
        movecursortohome();
        for (i = 0; i < col; i++) {
            if (!nibble) {
                port &= 0XFE;
                _delay_ms(1);
                port |= 0X06;
                _delay_ms(1);
                port = 0X14;
            } else if (nibble) {
                port = 0X00;
                _delay_ms(1);
                port = 0X60;
                _delay_ms(1);
                port = 0X10;
                _delay_ms(1);
                port = 0X40;
            }
        }
    } else if (row == 1) {
        pos = 0XC0 + col; //in fact I don't use this variable later
        if (nibble) {
            port = 0XC0;
            _delay_ms(1);
            port = 0X00;
        } else if (!nibble)
            port = 0XC0;
        for (i = 0; i < col; i++) {
            if (!nibble) {
                port &= 0XFE;
                _delay_ms(1);
                port |= 0X06;
                _delay_ms(1);
                port = 0X14;
            }

            else if (nibble) {
                port = 0X00;
                _delay_ms(1);
                port = 0X60;
                _delay_ms(1);
                port = 0X10;
                _delay_ms(1);
                port = 0X40;
            }
        }
        _delay_ms(1);
        RS(1);
        if (!nibble)
            port = ch;
        else {
            z = ch;
            y = ch;
            for (i = 0; i <3;i++)
                z << 1;
            port = y;
            _delay_ms(1);
            port = z;
        }
        _delay_ms(1);
    }
    void setcursorpos(unsigned char row, unsigned char col) {
        char i;
        RS(0);
        RW(0);
        EN(1);
        if (row == 0)
            movecursortohome();
        else if (row == 1) {
            if (nibble) {
                port = 0XC0;
                _delay_ms(1);
                port = 0X00;
            } else
                port = 0XC0;
        }
        _delay_ms(1);
        for (i = 0; i < col; i++) {
            if (!nibble)
                port = 0X14;
            else {
                port = 0X10;
                _delay_ms(1);
                port = 0x40;
            }
        }
    }
    void writestringatpos(unsigned char row, unsigned char col,
            const char data[]) {
        char length, i, z, y;
        if (row == 0)
            movecursortohome();
        else if (row == 1) {
            if (nibble) {
                port = 0XC0;
                _delay_ms(1);
                port = 0X00;
            } else
                port = 0XC0;
        }
        _delay_ms(1);
        for (i = 0; i < col; i++) {
            if (!nibble)
                port = 0X14;
            else {
                port = 0X10;
                _delay_ms(1);
                port = 0X40;
            }
        }
        _delay_ms(1);
        length = strlen(data);
        RS(1);
        for (i = 1; i <= length; i++) {
            if (!nibble) {
                port = data[i];
                _delay_ms(1);
                port &= 0XFE;
                _delay_ms(1);
                port |= 0X06;
                _delay_ms(1);
                port = 0X14;
            } else {
                z = data[i];
                y = data[i];
                for (i = 0; i <3;i++)
                    z << 1;
                port = y;
                _delay_ms(1);
                port = z;
                _delay_ms(1);
                port = 0x00;
                _delay_ms(1);
                port = 0x60;
                _delay_ms(1);
                port = 0x10;
                _delay_ms(1);
                port = 0x40;
            }
        }
        _delay_ms(1);
    }

    int main(void) {
        initialization();

        /*while(1)
         {

         }*/
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ When posting code please use the code button marked as { } to correctly format code. When you paste the code, select it and click the button. \$\endgroup\$ – AndrejaKo Feb 14 '12 at 2:43
  • \$\begingroup\$ Also check your original source code for occurrence of character . In some keyboard layouts it may show up if you try to type <3 and that could upset the compiler. Also check if the parentheses are correct and braces. This mostly looks to me like a simple error caused by a typo somewhere. \$\endgroup\$ – AndrejaKo Feb 14 '12 at 2:48
  • 1
    \$\begingroup\$ Posting a solid wall of unindented and uncommented code is downright impolite. \$\endgroup\$ – posipiet Feb 14 '12 at 3:10
  • 1
    \$\begingroup\$ It's very hard to see whats going on when the code is set out with no indentation. I have altered the first few blocks to give an example of a way to make it more readable. Also it's a good idea to actually include the full error message as given from the compiler (i.e. cut/paste) \$\endgroup\$ – Oli Glaser Feb 14 '12 at 3:18
  • \$\begingroup\$ Did you check the arduino LCD library for reference? \$\endgroup\$ – posipiet Feb 14 '12 at 3:37
4
\$\begingroup\$

The curly braces in writecharatpos don't add up.

To find errors of that kind, paste your original code into a tool with source formatting help, and autoformat your code.

The error will become clear at first sight once formatted: The main function is not on column one, meaning the some earlier function is not correctly closed. Also, the first search result on the error message says "curly braces dont add up".

Formating your code is considered good practice. It helps you, and others who you ask about your code. I format code while typing, and regularly autoformat it too.

You might also consider a naming policy like CamelCasing or so, because WriteCharAtPos is so much better to read.

Commenting code, especially port-fiddling, is also considered good practice.

Some comment on what the delays are good for in initialization, for instance, would be nice. Also comment on what is being intented to be written to the port. You'll thank yourself in 2 years, when you take a look at the library because you notice you have to change it one day.

Comments like _delay_ms(3); //3 are useless. Comment why, not what is done.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.