I've started to split my code into .H modules, plus a .C file containing:
#includes to above .H files as "filename.h"
#define macros
variables
function prototypes
functions used directly
main{}
I have also added the .H files to the project.
Now I get a load of Undeclared identifier... in the .H files.
What am I doing wrong?
Undeclared identifiers in .H files after program tidy-up?
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
C file include in your project if not you get Undeclared identifier..
Project->Add File To Project
Header file include using #include "name.h"
Project->Add File To Project
Code: Select all
#include "HW_Init.h"
//---------------------------------------------------------------------------
unsigned int var1;
//---------------------------------------------------------------------------
void Init_HW() {
T0CON.TMR0ON= 1; // Enable Timer0
T0CON.T08BIT= 0; // 0 = Timer0 is configured as a 16-bit timer/counter
T0CON.T0CS= 0; // 0 = Internal instruction cycle clock (CLKO)
T0CON.PSA= 0; // 0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
T0CON.T0PS0= 0; // Prerscaler
T0CON.T0PS1= 1; // Prerscaler
T0CON.T0PS2= 0; // Prerscaler
PORTA= 0; // Clear PORTA
TRISA= 0; // Designate PORTA pins as output
PORTB= 0; // Clear PORTB
TRISB= 0x00; // Designate PORTB pins as output
PORTC= 0; // Clear PORTC
TRISC= 0x83; // Designate PORTC pins
PORTD= 0; // Clear PORTD
TRISD= 0; // Designate PORTD pins as input
PORTG= 0; // Clear PORTG
TRISG= 0xf4; // Designate PORTG pins
INTCON.GIE= 1; // Global interrupt enabel
INTCON.PEIE= 1; // Peripheral interrupt enable
INTCON.TMR0IE= 1; // Enable Timer0 interrupt
}
//--------------------------------------
Code: Select all
// HW_Init.h
#ifndef __HW_Init_H__ // use this to prevent same header to be included more than once
#define __HW_Init_H__
//---------------------------------------------------------------------------
extern void Init_HW(); // this function is accessible where #include "name.h is used
//---------------------------------------------------------------------------
extern unsigned int var1; // this variable is accessible where #include "name.h is used
//---------------------------------------------------------------------------
#endif
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
I think I have done this OK, but then I am a novice!PeterA wrote:No
Look in the project manager an verify that you have the c file there.
Check the syntax in your header files and that you included the header in your c file
I don't really understand the differences between .C and .H files. Perhaps I should be splitting my code into a number of .C files - is this acceptable?
Yes try to get specific function code in each one like,
Lets say that we do a program that will listen to a keyboard and send keystrokes using a uart.
If you do this program in C it's usually easy to put all code regarding a uart in one file and all code for the keyboard in another and then you connect the to together in main.c.
The MyUart.c will contain some public functions "void SendString(char *byte,int len)" and "void InitUart()" there might also be private functions like "void sendbyte(char byte)" and a public variable "short TxEmpty".
It's always nice to make it easy to change some parameters like baud rate so lets make it a definition in the header.
the header file for this will look like this
And the c file will contain all the code for the uart
then you do the same for the keyboard, one H file containing the public functions and defs and a C file containing the code.
In main you connect the functions together and make your program.
If you manage to divide functionality like this it's easy to reuse the code next time.
Hope it helps
Lets say that we do a program that will listen to a keyboard and send keystrokes using a uart.
If you do this program in C it's usually easy to put all code regarding a uart in one file and all code for the keyboard in another and then you connect the to together in main.c.
The MyUart.c will contain some public functions "void SendString(char *byte,int len)" and "void InitUart()" there might also be private functions like "void sendbyte(char byte)" and a public variable "short TxEmpty".
It's always nice to make it easy to change some parameters like baud rate so lets make it a definition in the header.
the header file for this will look like this
Code: Select all
#ifndef c_uart.h
#define c_uart.h
#define BAUD_RATE 9600
extern short TxEmpty;
extern void SendString(char *byte,int len);
extern void InitUart();
#endif
And the c file will contain all the code for the uart
Code: Select all
#include "MyUart.h"
short TxEmpty;
void sendbyte(char byte)
{
UartSend(byte);
}
void SendString(char *byte,int len)
{
unsined int i;
TxEmpty= 0;
for( i=0; i<=len; i++)
sendbyte(byte[i]);
TxEmpty= 1;
}
void InitUart()
{
InitUart( BAUD_RATE);
}
In main you connect the functions together and make your program.
If you manage to divide functionality like this it's easy to reuse the code next time.
Hope it helps
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
Right, I've done my best to sort this out, now I need an expert! For example, in one of the .C files I have a function clsLCD(). This is defined and declared as a prototype in the associated .C file, plus declared as a prototype in my definitions.h file. However, when I build the project I get Undeclared identiifier 'clsLCD' in expression relating to another .C file that calls the function.aleksandar.nikolic wrote:Yes, exactly!I think I understand now: the .H files are only for "public functions and defs", and the modules go in .C files?
Could someone have a look at these files and explain how to get rid of these errors.
TIA
Nigel
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
Pretty please?nigelmercier wrote:
Could someone have a look at these files and explain how to get rid of these errors.
TIA
Nigel
-
- Posts: 316
- Joined: 23 Oct 2008 09:36
All sorted now, files removed.nigelmercier wrote:
Could someone have a look at these files and explain how to get rid of these errors.