Author Topic: Why it showing type redeclared error?  (Read 803 times)

0 Members and 1 Guest are viewing this topic.

Offline radhika

  • Regular Contributor
  • *
  • Posts: 88
  • Country: in
Why it showing type redeclared error?
« on: October 18, 2018, 05:52:35 pm »
Hello,
Can anybody help me in simulating this code. It's showing tyoe redeclared error.
Please once simulate this code in your MPLAB X IDE.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <p18f4520.h>
#include <LCD.h>
#define ldata PORTD           //PORTD = LCD data pins
#define rs PORTEbits.RE0      //rs = PORTE.0
#define rw PORTEbits.RE1      //rw = PORTE.1
#define en PORTEbits.RE2      //en = PORTE.2
#define Up PORTAbits.RA0
#define Down  PORTAbits.RA1
#define Select PORTAbits.RA2
#define Clock PORTBbits.RB5
#define Data PORTBbits.RB6
// End specification for pins

/* function prototype */
void MSDelay(unsigned int);
void lcdinit(void);
void lcdWrite(unsigned char);
void lcdout(char c);
void lcdchr (char x);
void SendPotValue (unsigned short num);
void SelectArrow();

//POT values;
unsigned short aa, count1;
int t, i, Flag, Mask;
const char MESSAGE[] = "VOL:";


void main() {
   
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 1;
TRISAbits.TRISA2 = 1;

TRISBbits.TRISB6 = 0; //SCL 
TRISBbits.TRISB5 = 0; //SDA
   
CMCON = 0x07;   // Disable comparators
ADCON1 = 0x0F;  // Disable Analog functions

 lcdinit();            // Initialize LCD
 lcdcmd(0x00);         // CLEAR display
 lcdcmd(0x0C);         // Cursor off
 lcdout(1,1,MESSAGE);            // Write message1 in 1st row
 
 aa = 50;
 
 count1 = 2;
 SendPotValue(aa);
 do {
  if (!Up) {
   Debounce_Key();
   
     if (aa < 250) {
     aa = aa + 25;
     count1 = aa/25;
     }
     
 SendPotValue(aa);
  }

  if (!Down) {
   Debounce_Key();
     if (aa > 0) {
     aa = aa - 25;
     count1 = aa/25;
     }
   SendPotValue(aa);
  }

  if (!Select) {
   Debounce_Key();
   SelectArrow();
  }
 } while (1);
}

void SendPotValue(unsigned short num1)
{
unsigned int // Mask;

Mask = 0x80;
Data = 1;
Clock = 1;
MSDelay(5);
Clock = 0;

// Shift in 8-bit wiper position for POTA
 for (t=0; t<8; t++){
   Flag = num1 & Mask;
   if(Flag==0)
   Data = 0;
   else
   Data = 1;
   Clock = 1;
   MSDelay(1);
   Clock = 0;
   Mask = Mask >> 1;
}
  lcdcmd(0x00);
  lcdout(1,3,MESSAGE);            // Write message1 in 1st row
  SelectArrow();
}

 void SelectArrow()
 {
 for (i=0; i<count1; i++) {
  lcdchr(1, 6+i, 255);   //char row, char column, char out
 }
 }

 void Debounce_Key(){
 MSDelay(200);
}

void lcdcmd(unsigned char value)
{
    ldata = value;
    rs = 0;
    rw = 0;
    en = 1;
    MSDelay(1);
    en = 0;
}

void MSDelay(unsigned int itime)
{
    unsigned int i, j;
    for (i=0; i<itime; i++)
        for (j=0; j<255; j++);
}

void lcdinit()
{
    lcdcmd(0x38);
    lcdcmd(0x0C);
    lcdcmd(0x01);
    lcdcmd(80);
}

void lcdOut(char c)
{
 rs = 1;
 lcdWrite( c );
}

void lcdchar(char dat)
{
    ldata= dat;    /*Send data to LCD*/ 
    rs = 1;        /*Data Register is selected*/
    en=1;          /*High-to-Low pulse on Enable pin to latch data*/   
    NOP();
    en=0;
    MSDelay(1);
}



Thanks.
 

Offline taydin

  • Frequent Contributor
  • **
  • Posts: 490
  • Country: tr
Re: Why it showing type redeclared error?
« Reply #1 on: October 18, 2018, 05:58:18 pm »
It's probably a mismatch between how the functions are declared and how they are defined.

In order to make life easier, always try to place function definitions at the top of the source code, so that functions below don't need function "declarations". If you use both function declaration and definition, then you are putting on yourself the responsibility to keep them exactly in sync. But if you just define a function and let functions below just "see" it, then you don't need declarations.

Always try to avoid duplication!
Real programmers use machine code!

My hobby projects http://mekatronik.org/forum
 
The following users thanked this post: radhika

Offline helius

  • Super Contributor
  • ***
  • Posts: 2866
  • Country: us
Re: Why it showing type redeclared error?
« Reply #2 on: October 18, 2018, 06:15:28 pm »
Quote from: radhika
Code: [Select]
void lcdchr (char x);
You probably meant lcdchar to match the definition.
You want to be consistent with forward declarations, and use (void) for declaring zero arguments. The reason is that the Pre-ANSI, K&R C style uses no argument prototypes in function declarations, and having just an empty parameter list is ambiguous with that style.
 
The following users thanked this post: radhika

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 2560
  • Country: it
Re: Why it showing type redeclared error?
« Reply #3 on: October 18, 2018, 10:16:39 pm »
don't include p18f4520.h
xc.h takes care of that.
ALWAYS state which mcu, compiler, compiler version, ide and ide version. The less we have to guess the better.
Copy and paste the actual error output from the compiler.

if that's the only file of course you're going to have the redeclared error somewhere. You need to write all the local functions prototypes before the main or the compiler doesn't know how to make the call
 
The following users thanked this post: radhika

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 2009
  • Country: us
Re: Why it showing type redeclared error?
« Reply #4 on: October 19, 2018, 05:19:29 am »
There are several problem with this code.

You don't have a prototype for lcdcmd.

If you don't have a prototype the compiler will essentially automatically generate a declaration with default argument and return types.  Then it gets to the definition which doesn't match, and that causes a type mismatch.

You also declare lcdout(char c) but call it as lcdout(1,1,MESSAGE) and define it as lcdOut(char c).

lcdchr / lcdchar ambiguity.

In lcdinit(), the call to lcdcmd(80); is probably supposed to be lcdcmd(0x80).

 
The following users thanked this post: radhika

Offline radhika

  • Regular Contributor
  • *
  • Posts: 88
  • Country: in
Re: Why it showing type redeclared error?
« Reply #5 on: October 19, 2018, 07:17:03 am »
I have done some changes according to all the suggestions:
 Please check:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <LCD.h>
#define ldata PORTD           //PORTD = LCD data pins
#define rs PORTEbits.RE0      //rs = PORTE.0
#define rw PORTEbits.RE1      //rw = PORTE.1
#define en PORTEbits.RE2      //en = PORTE.2
#define Up PORTAbits.RA0
#define Down  PORTAbits.RA1
#define Select PORTAbits.RA2
#define Clock PORTBbits.RB5
#define Data PORTBbits.RB6
// End specification for pins

/* function prototype */
void MSDelay(unsigned int);
void lcdinit(void);
void lcdWrite(unsigned char);
void lcdout(char row, char column, char text);
void lcdchar (char x);
void SendPotValue (unsigned short num);
void SelectArrow();

//POT values;
unsigned short aa, count1, i;
int t, Flag, Mask;
const char MESSAGE[] = "VOL:";

void SendPotValue(unsigned short num1)
{
unsigned int // Mask;

Mask = 0x80;
Data = 1;
Clock = 1;
MSDelay(5);
Clock = 0;

// Shift in 8-bit wiper position for POTA
 for (t=0; t<8; t++){
   Flag = num1 & Mask;
   if(Flag==0)
   Data = 0;
   else
   Data = 1;
   Clock = 1;
   MSDelay(1);
   Clock = 0;
   Mask = Mask >> 1;
}
  lcdcmd(0x01);
  lcdout(1,3,MESSAGE);            // Write message1 in 1st row
  SelectArrow();
}

 void SelectArrow()
 {
 for (i=0; i<count1; i++) {
  lcdchar(1, 6+i, 255);   //char row, char column, char out
 }
 }

 void Debounce_Key(){
 MSDelay(200);
}

void lcdcmd(unsigned char value)
{
    ldata = value;
    rs = 0;
    rw = 0;
    en = 1;
    MSDelay(1);
    en = 0;
}

void MSDelay(unsigned int itime)
{
    unsigned int i, j;
    for (i=0; i<itime; i++)
        for (j=0; j<255; j++);
}

void lcdinit()
{
    lcdcmd(0x38);
    lcdcmd(0x0C);
    lcdcmd(0x01);
    lcdcmd(0x80);
}

void lcdout(char rows, char columns, char texts)
{
 rs = 1;
 lcdWrite( texts );
}

void lcdchar(char dat)
{
    ldata= dat;    /*Send data to LCD*/ 
    rs = 1;        /*Data Register is selected*/
    en=1;          /*High-to-Low pulse on Enable pin to latch data*/   
    NOP();
    en=0;
    MSDelay(1);
}





void main() {
   
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 1;
TRISAbits.TRISA2 = 1;

TRISBbits.TRISB6 = 0; //SCL 
TRISBbits.TRISB5 = 0; //SDA
   
CMCON = 0x07;   // Disable comparators
ADCON1 = 0x0F;  // Disable Analog functions

 lcdinit();               // Initialize LCD
 lcdout(1,1,MESSAGE);     // Write message1 in 1st row
 aa = 50;
 count1 = 2;
 SendPotValue(aa);
 do {
  if (!Up) {
   Debounce_Key();
   
     if (aa < 250) {
     aa = aa + 25;
     count1 = aa/25;
     }
     
 SendPotValue(aa);
  }

  if (!Down) {
   Debounce_Key();
     if (aa > 0) {
     aa = aa - 25;
     count1 = aa/25;
     }
   SendPotValue(aa);
  }

  if (!Select) {
   Debounce_Key();
   SelectArrow();
  }
 } while (1);
}


But I am still getting an error, i.e :

Speaker_Check.c:34: warning: (346) declaration of "Mask" hides outer declaration     ------- Mask = 0x80;
Speaker_Check.c:52: warning: (361) function declared implicit int                             ------- lcdcmd(0x01);
Speaker_Check.c:53: warning: (358) illegal conversion of pointer to integer               ------- lcdout(1,3,MESSAGE);
Speaker_Check.c:60: error: (186) too many function arguments                               ------- lcdchar(1, 6+i, 255);
Speaker_Check.c:69: error: (984) type redeclared                                                   ------- {
Speaker_Check.c:69: error: (1098) conflicting declarations for variable "lcdcmd" (Speaker_Check.c:68)  --------- {
(908) exit status = 1
nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/Speaker_Check.p1' failed
make[2]: Leaving directory 'C:/Users/Soni/MPLABXProjects/Speaker_Check.X'
nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/Soni/MPLABXProjects/Speaker_Check.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/Speaker_Check.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 2560
  • Country: it
Re: Why it showing type redeclared error?
« Reply #6 on: October 19, 2018, 07:26:35 am »
have you tried reading the messages? start with the first error.
too many arguments at line 60. you called lcdchar with three arguments. lcdchar has only one argument. maybe you meant to use lcdout?
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1368
  • Country: us
Re: Why it showing type redeclared error?
« Reply #7 on: October 19, 2018, 07:32:27 am »
Put an explicit declaration for lcdcmd above.
Fix the explicit declaration for lcdchar. You are calling it with three parameters when you declared it took only one.
lcdout must take “texts” as a (const) char * (a string or a char pointer) not as a char.

You probably also need to add an additional MSDelay in the bit-banging loop, assuming you want a square wave clock signal. As the code is now, the clock pin will spend most of its time high and be periodically (and quickly) strobed low. Maybe that’s right, but it’s a little unusual.

I’d also seek to write an MSDelay not based on arbitrary looping, but based on an actual timer on the chip. As it is now, an optimizing compiler is allowed to make that function arbitrarily fast.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 2560
  • Country: it
Re: Why it showing type redeclared error?
« Reply #8 on: October 19, 2018, 07:52:53 am »
there are builtin functions for delays in ms and us. Unfortunately, they require you read the compiler manual to understand how to use them
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf