Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Header file error in MPLAB

Status
Not open for further replies.

kushal nandanwar

Full Member level 3
Joined
Jun 9, 2013
Messages
177
Helped
6
Reputation
12
Reaction score
6
Trophy points
18
Activity points
1,258
I am trying to add my own header file in MPLAB but it's showing some error.

Code:
PORT_direction.h:9:Error: syntax error

Header file code

Code:
// TRISE=0;
 //  TRISC=0;				
  // LATC=0;

//  Port B as output port

 TRISDbits.TRISD0=1;
 TRISDbits.TRISD1=1;
 TRISDbits.TRISD2=0;
 TRISDbits.TRISD3=0;
 TRISDbits.TRISD4=0;
 TRISDbits.TRISD5=0;
 TRISDbits.TRISD6=0;
 TRISDbits.TRISD7=0;

 TRISCbits.TRISC2=1;
 TRISCbits.TRISC1=1;

 TRISCbits.TRISC0=0;           // LED
 TRISBbits.TRISB1=0; 
 TRISBbits.TRISB5=0;
 

I might be wrong but I think the syntax for defining pins in XC8 would be like:
TRISDbits.RD0 = 0;
I am assuming you have #defined the pins beforehand.
I do not have the compiler with me at the moment to check the syntax.

Brian.
 

I might be wrong but I think the syntax for defining pins in XC8 would be like:
TRISDbits.RD0 = 0;
I am assuming you have #defined the pins beforehand.
I do not have the compiler with me at the moment to check the syntax.

Brian.

What do you mean by #defined the pins beforehand.

- - - Updated - - -

Code:
//======================== chip config ======================//
                 		    
#pragma config FOSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF        
#pragma config PWRT = OFF        
#pragma config BOR = OFF        
#pragma config VREGEN = OFF 
#pragma config WDT = OFF    
#pragma config WDTPS = 32768     
#pragma config CCP2MX = OFF     
#pragma config PBADEN = OFF      
#pragma config LPT1OSC = OFF         
#pragma config MCLRE = ON            
#pragma config STVREN = ON       
#pragma config LVP = OFF     
#pragma config ICPRT = OFF       
#pragma config XINST = OFF       
#pragma config DEBUG = OFF
#pragma config WRTD = OFF

#pragma code main = 0x50

//============================================= header file =============================================//
#include "PORT_direction.h"
#include <p18f4550.h>
#include <delays.h> 
//#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwm.h>
//#include <timers.h>
 

Header file code

Code:
// TRISE=0;
 //  TRISC=0;				
  // LATC=0;

//  Port B as output port

 TRISDbits.TRISD0=1;
 TRISDbits.TRISD1=1;
 TRISDbits.TRISD2=0;
 TRISDbits.TRISD3=0;
 TRISDbits.TRISD4=0;
 TRISDbits.TRISD5=0;
 TRISDbits.TRISD6=0;
 TRISDbits.TRISD7=0;

 TRISCbits.TRISC2=1;
 TRISCbits.TRISC1=1;

 TRISCbits.TRISC0=0;           // LED
 TRISBbits.TRISB1=0; 
 TRISBbits.TRISB5=0;

You can't have executable code placed outside of function bodies.

One solution could be:
Code:
void init_pins(void){
// TRISE=0;
 //  TRISC=0;				
  // LATC=0;

//  Port B as output port

 TRISDbits.TRISD0=1;
 TRISDbits.TRISD1=1;
 TRISDbits.TRISD2=0;
 TRISDbits.TRISD3=0;
 TRISDbits.TRISD4=0;
 TRISDbits.TRISD5=0;
 TRISDbits.TRISD6=0;
 TRISDbits.TRISD7=0;

 TRISCbits.TRISC2=1;
 TRISCbits.TRISC1=1;

 TRISCbits.TRISC0=0;           // LED
 TRISBbits.TRISB1=0; 
 TRISBbits.TRISB5=0; 
}

and call init_pins() in your main()

- - - Updated - - -

I might be wrong but I think the syntax for defining pins in XC8 would be like:
TRISDbits.RD0 = 0;...
Nop. Syntax on #1 TRIS pin naming is correct.
 

This method work only when I define .C file but not for .h

- - - Updated - - -

My new problem ,

If I define one variable in one.c file, how can I access this variable in second.c file

I am using C18 compiler.

what does this two static and extern do in embedded C?
 

This method work only when I define .C file but not for .h
of course you need to do this:
Code:
//this works for your code
#include <p18f4550.h>
#include "PORT_direction.h"
in stead of this:
Code:
//this is not working for you
#include "PORT_direction.h"
#include <p18f4550.h>
or include pic header inside your header file
(because you cannot use a name before you define it!)



My new problem ,

If I define one variable in one.c file, how can I access this variable in second.c file

I am using C18 compiler.
use the keyword extern in the second .c file (or an .h file)
 

of course you need to do this:
Code:
//this works for your code
#include <p18f4550.h>
#include "PORT_direction.h"
in stead of this:
Code:
//this is not working for you
#include "PORT_direction.h"
#include <p18f4550.h>
or include pic header inside your header file
(because you cannot use a name before you define it!)

Both are not working, I think I have to link header file to my main file.

- - - Updated - - -

Error using variable

This feature is not implemented in compiler. There may be a definition of external symbol 'SENSE1' found instead of its declaration
 
Last edited by a moderator:

static tells the C Compiler that the variable will have lifespan throughout, until the program ends.

extern tells the C Compiler that the variable or function is defined in another source file.
 

I am getting this error

Error [1521] This feature is not implemented in compiler. There may be a definition of external symbol 'SENSE1' found instead of its declaration

- - - Updated - - -

static tells the C Compiler that the variable will have lifespan throughout, until the program ends.

extern tells the C Compiler that the variable or function is defined in another source file.

Can I call a function present in main file from other files?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top