Jump to content
Sign in to follow this  
Danny Higgins

Problem With Global Variables

Recommended Posts

I have declared a global variable which I want to use to indicate the results of tests -

 

char error_flag;

 

I have also defined a bit in the flag for a particular test -

 

#define BCD_fail error_flag.0

 

In main() and the function that does the test I have put -

 

extern char error_flag;

 

I get the error - "Trying to add bank switching for var that has no memory allocated."

 

If I change the external declaration to -

 

extern char error_flag.0

 

I get the message "missing semi-colon"

 

It works if I change the #define to -

 

#define BCD_fail error_flag

 

but I only want to use 1 bit for each test.

 

Is there any way I can do this?

 

Thanks,

 

Danny

Share this post


Link to post
Share on other sites
I have declared a global variable which I want to use to indicate the results of tests -

 

char error_flag;

 

I have also defined a bit in the flag for a particular test -

 

#define BCD_fail error_flag.0

 

In main() and the function that does the test I have put -

 

extern char error_flag;

 

I get the error - "Trying to add bank switching for var that has no memory allocated."

 

If I change the external declaration to -

 

extern char error_flag.0

 

I get the message "missing semi-colon"

 

It works if I change the #define to -

 

#define BCD_fail error_flag

 

but I only want to use 1 bit for each test.

 

Is there any way I can do this?

 

Thanks,

 

Danny

 

I have never used extern in my programming so far. But i think you should have No problem defining the bits of a global character variable.

 

Just remove "extern char error_flag;" in your functions.

 

Declare the error_flag as global by writing them outside the functions.

char error_flag;

#define BCD_fail error_flag.0

 

 

Pls let me know if your program is working properly & if the flag bits work as expected.

 

Regards,

Raghunathan.

Edited by ra68gi

Share this post


Link to post
Share on other sites

Thanks Raghunathan.

 

I was blindly following a book on C that said that global variables should be declared as extern in the functions that use them. As you said, it works OK with the extern removed.

 

Danny

Share this post


Link to post
Share on other sites

Danny,

I have declared a global variable which I want to use to indicate the results of tests -

 

char error_flag;

 

I have also defined a bit in the flag for a particular test -

 

#define BCD_fail error_flag.0

 

In main() and the function that does the test I have put -

 

extern char error_flag;

 

I get the error - "Trying to add bank switching for var that has no memory allocated."

Please provide the code that produced this error (its not an error you should ever get to see, so I would like to look into it).

 

Regards

Dave

Share this post


Link to post
Share on other sites
Danny,
I have declared a global variable which I want to use to indicate the results of tests -

 

char error_flag;

 

I have also defined a bit in the flag for a particular test -

 

#define BCD_fail error_flag.0

 

In main() and the function that does the test I have put -

 

extern char error_flag;

 

I get the error - "Trying to add bank switching for var that has no memory allocated."

Please provide the code that produced this error (its not an error you should ever get to see, so I would like to look into it).

Also what compiler version are you using?

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave.

 

Don't laugh too loudly! This is my first attempt at BoostC and you can tell that I have an assembler background.

 

I am using Version 6.6. This version of the code compiles with the "Trying to add bank switching for var that has no memory allocated" error. If I remove the "extern" lines (as suggested) the problem goes away.

 

 

 

 

// Test2 02/04/2007

 

// Test program to read BCD switches on PORTD

// with switch debounce and validation

 

#include <system.h>

#include <stdio.h>

#include <string.h>

 

#pragma CLOCK_FREQ 19660000

 

#pragma DATA _CONFIG1H, _OSC_ECIO6_1H & _FCMEN_OFF_1H & _IESO_OFF_1H

#pragma DATA _CONFIG2L, _PWRT_OFF_2L & _BOREN_OFF_2L

#pragma DATA _CONFIG2H, _WDT_OFF_2H

#pragma DATA _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H

#pragma DATA _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _DEBUG_ON_4L

#pragma DATA _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L

#pragma DATA _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H

#pragma DATA _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L

#pragma DATA _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H

#pragma DATA _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L

#pragma DATA _CONFIG7H, _EBTRB_OFF_7H

 

#define BCD_port portd // BCD switches are connected to portd

#define BCD_select_10K portb.2 // 10 kHz BCD select line is on RB2

#define BCD_select_1K portb.3 // 1 kHz BCD select line is on RB3

#define BCD_select_100H portb.4 // 100 Hz BCD select line is on RB4

#define BCD_select_10H portb.5 // 10 Hz BCD select line is on RB5

#define BCD_select portb

#define BCD_debounce 10 // BCD switch bounce delay time (ms)

#define BCD_fail error_flag.0 // 1 = fail BCD switch read test

 

void read_BCD(void); // read BCD switches

void read_RX(void); // read RX frequencies

void set_TX(void); // set TX frequency

char BCD_check(char check); // checks to see if BCD switch readings are stable and valid

char swap_nibble(char swap); // swap top and bottom nibbles of char

void initialise_PIC(void); // initialise PIC

 

char RX1_QRG[9]; // array for RX1 frequency in Hz

char RX2_QRG[9]; // array for RX2 frequency in Hz

char TX_QRG[9]; // array for TX frequency in Hz

int temp; // global temporary variable

char BCD10K, BCD1K, BCD100H, BCD10H; // global BCD switch readings

char error_flag; // global error flag

// bit 0 = BCD failure

 

main ()

 

{

extern char error_flag;

 

initialise_PIC();

read_RX();

read_BCD();

set_TX();

 

while (1)

{

}

}

 

void initialise_PIC(void)

 

// set I/O port directions and initialise

 

{

 

// set port I/O

 

trisa = 0B00000000;

trisb = 0B00000011;

trisc = 0B00011001;

trisd = 0B11110000;

trise = 0B00000000;

 

// initialise ports

 

porta = 0;

portb = 0;

portc = 0;

portd = 0;

porte = 0;

 

return ;

 

}

 

void read_BCD(void)

 

// read BCD switches, check for bounce and valid BCD data

// store values in global BCD10K, BCD1K, BCD100H and BCD10H

// sets BCD_fail = 1 if any digit fails tests

 

{

extern char error_flag;

 

char temp; // define internal variables

 

BCD_select &= 0B11000011; // clear all BCD switch selects

BCD_select_10K = 1; // select only BCD10K switch

BCD10K = BCD_port; // read BCD10K switch

BCD10K = BCD10K << 4; // place in top nibble

BCD_select_10K = 0; // clear BCD10K switch select

BCD_select_1K = 1; // select BCD1k switch

BCD1K = BCD_port; // repeat for all 4 BCD switches

BCD1K = BCD1K << 4;

BCD_select_1K = 0;

BCD_select_100H = 1;

BCD100H = BCD_port;

BCD100H = BCD100H << 4;

BCD_select_100H = 0;

BCD_select_10H = 1;

BCD10H = BCD_port;

BCD10H = BCD10H << 4;

BCD_select_10H = 0;

 

delay_ms(BCD_debounce); // BCD switch debounce

 

BCD_select_10K = 1; // select only BCD10K switch

temp = BCD_port; // read BCD10K switch

temp &= 0B00001111; // mask bottom 4 bits

BCD10K |= temp; // BCD10K top and bottom nibbles are equal if no bounce

BCD_select_10K = 0; // clear BCD10K switch select

BCD_select_1K = 1; // select BCD1k switch

temp = BCD_port; // repeat for all 4 BCD switches

temp &= 0B00001111;

BCD1K |= temp;

BCD_select_1K = 0;

BCD_select_100H = 1;

temp = BCD_port;

temp &= 0B00001111;

BCD100H |= temp;

BCD_select_100H = 0;

BCD_select_10H = 1;

temp = BCD_port;

temp &= 0B00001111;

BCD10H |= temp;

BCD_select_10H = 0;

 

BCD_fail = 1; // set BCD fail flag

BCD10K = 0xAB; // TEST CODE ONLY !!!

BCD10K = BCD_check(BCD10K); // check for bounce on BCD 10kHz switch

BCD1K = BCD_check(BCD1K); // check for bounce on BCD 1kHz switch

BCD100H = BCD_check(BCD100H); // check for bounce on BCD 100Hz switch

BCD10H = BCD_check(BCD10H); // check for bounce on BCD 10Hz switch

if(BCD10K + BCD1K + BCD100H + BCD10H < 0x25) // check to see if all BCD tests passed

{

BCD_fail = 0;

}

return;

}

 

char swap_nibble(char swap)

 

// swaps upper and lower nibbles in a char and returns swapped char

 

{

char temp = swap;

temp >>= 4;

swap <<= 4;

 

return (swap | temp);

}

 

char BCD_check(char check)

 

// checks that the upper and lower nibbles are the same (no bounce)

// checks that the BCD digit is 0 > 9

// returns BCD value if OK

// returns 0x25 if failure because max value of 4 good BCD digits is 36 = 0x24, so if

// the sum of the 4 BCD digits is > 0x24 then at least one test failed

 

{

if(check != swap_nibble(check))

{

return 0x25; // fails test

}

check &= 0x0F; // delete upper nibble

if(check > 0x09) // check for out of range

{

return 0x25; // fails test

}

return check; // passes both tests

}

 

void read_RX(void)

 

// read frequencies of RX1 and RX2

 

{

strcpy(RX1_QRG, "12345678"); // TEST CODE ONLY !!!

strcpy(RX2_QRG, "23456789");

 

return;

}

 

void set_TX(void)

 

// set TX frequency

 

{

return;

}

Share this post


Link to post
Share on other sites

extern is normally (mostly) used in header-source files, not in your main

(unless you have some non-standard habits).

 

A rough example would be:

Header:

extern unsigned char BLUE[];

 

Source File for Header:

unsigned char BLUE[42] = {'\n'};

 

This just exports a global variable array that is specific to your header-source

that you may want/need access to when using it. An example might be some

type of sensor that returns a large block of data, so you use a generic holder

and then pull out the parts you need for your application.

 

It also avoids the nasty cyclic includes that some people use when they forget

or do not use externs but need/want to pass out a global from a private library.

Share this post


Link to post
Share on other sites

Hi empt.

 

I am working my way through "C Programming in Easy Steps" and I quote:

 

"An external global variable must be defined exactly once at the start of the program. It must also be declared at the start of each function that wants to use it. Each declaration should begin with the extern keyword which denotes that it is for a global variable rather than for a regular local variable. These declarations should not be used to initialise the global variable."

 

It then goes on to give an example:

 

int num;

 

int main();

 

{

extern int num; // decalre external variable

num = 5; // initialise external variable

printf("Global variable value is %d", num);

 

return 0;

}

 

I was looking for guidance on global variables but I think I must have got confused between global and external variables, although the example only added to the confusion.

 

I think I've got it sorted now.

 

Thanks,

 

Danny

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...