Flags in MCUinit.c

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Flags in MCUinit.c

1,907 Views
Giovanni66
Contributor I

Hello,

I have some problem with flags and variables (I use Codewarrior 5.9.0 with device MC9S08QG8). I created a project using Device Initialization and, enabling timer interrupts, in MCUinit.c I find the following lines :

 

/*

** ===================================================================

**     Interrupt handler : isrVtpmovf

**

**     Description :

**         User interrupt service routine.

**     Parameters  : None

**     Returns     : Nothing

** ===================================================================

*/

__interrupt void isrVtpmovf(void)

{

  /* Write your interrupt code here ... */

}

/* end of isrVtpmovf */

 

 

In main.c I defined some flags like this:

 

typedef union {

  byte Byte;

  struct {

    byte b0       :1;                                       /*  */

    byte b1       :1;                                       /*  */

    byte b2       :1;                                       /*  */

    byte b3       :1;                                       /*  */

    byte b4       :1;                                       /*  */

    byte b5       :1;                                       /*  */

    byte b6       :1;                                       /*  */

    byte b7       :1;                                       /*  */

  } Bits;

} flagstr;

extern volatile flagstr _flag;

#define flags                        _flag.Byte

#define f0                             _flag.Bits.b0

#define f1                             _flag.Bits.b1

#define f2                             _flag.Bits.b2

#define f3                             _flag.Bits.b3

#define f4                             _flag.Bits.b4

#define f5                             _flag.Bits.b5

#define f6                             _flag.Bits.b6

#define f7                             _flag.Bits.b7

 

 

When I use the byte “flags” or the single bits “f0”, “f1” … in main.c I have no problem, but I can’t use “flags” or “f0”, “f1” … in the interrupt handler isrVtpmovf. I tried using extern, volatile,  I always have error.

Can you help me? Thanks in advance!

Giovanni

Labels (1)
Tags (1)
0 Kudos
7 Replies

613 Views
CrasyCat
Specialist III

Hello

 

Where did you define the variable flags?

Where did you place the external declaration?

 

If you want to be able to use flags and f1 in MCUinit.c, you need to have the define and the external declaration for flags in a .h file which is included in main.c and MCUinit.c

 

CrasyCat

0 Kudos

613 Views
Giovanni66
Contributor I

Thank you very much for your reply, anyway I still have some problem. Sorry, I’m a new user.

 

In main.c I defined:

 

unsigned char var;

 

typedef union {

  byte Byte;

  struct {

    byte b0       :1;                                       /*  */

    etc…

  } Bits;

} flagstr;

extern volatile flagstr _flag;

#define flags                        _flag.Byte

#define f0                             _flag.Bits.b0

etc…

 

___________________________________________

 

In MCUinit.h:

 

/* User declarations and definitions */

/*   Code, declarations and definitions here will be preserved during code generation */

 

extern unsigned char var;

extern flags;

extern f0;

etc…

 

/* End of user declarations and definitions */

_____________________________________________

 

For example I insert some instructions into the interrupt handler in MCUinit.c:

 

__interrupt void isrVtpmovf(void)

{

  /* Write your interrupt code here ... */

var = 7;

flags = 5;

f0 = 1;  

}

/* end of isrVtpmovf */

 

In this case var works properly: the previous value of  var (inserted in main.c) is overwritten. As regard flags and f0 instead, two new variables are created by default and values 5 and 1 are inserted into those new variables. So I can’t work with the same variables I have in main.c. Where is my mistake?

 

Giovanni

0 Kudos

613 Views
kef
Specialist I

Giovanni,

 

I think you should get and study some book about C language, ASAP.

 

This should be moved to header file:

 

typedef union {

  byte Byte;

  struct {

    byte b0       :1;                                       /*  */

    etc…

  } Bits;

} flagstr;

extern volatile flagstr _flag;

#define flags                        _flag.Byte

#define f0                             _flag.Bits.b0

 

 

In both main.c and mcuinit.c you should include your header file

 

Additionally in one of c files, main.c probably you should define _flag. It should be the same line like in header file, but without extern:

 

volatile flagstr _flag;

0 Kudos

613 Views
Giovanni66
Contributor I

I can't include a header file like that in main.c and also in MCUinit.c

LINK ERROR: variables duplicated

0 Kudos

613 Views
Giovanni66
Contributor I

WOW

At last I made it out!!!

 

I include in both main.c and MCUinit.c only this:

 

typedef union {

  byte Byte;

  struct {

    byte b0       :1;                                       /*  */

    byte b1       :1;                                       /*  */

    byte b2       :1;                                       /*  */

    byte b3       :1;                                       /*  */

    byte b4       :1;                                       /*  */

    byte b5       :1;                                       /*  */

    byte b6       :1;                                       /*  */

    byte b7       :1;                                       /*  */

  } Bits;

} flagstr;

 

 

In main.c I define all the bytes of this type that I need:

 

volatile flagstr flag0;

volatile flagstr flag1;

etc.

 

To use the same bytes in the interrupt handler I write in MCUinit.c:

 

extern volatile flagstr flag0;

extern volatile flagstr flag1;

etc.

 

and so it works well with bytes

 

e.g.      flag0.Byte = 6;

 

and flags

 

e.g.      flag1.Bits.b3 = 1;

 

0 Kudos

613 Views
JO_O_BR
Contributor I

Hello people,

 

I'm trying to migrate from QT2 (HC908) to QD2 (HCS08), but I'm having one trouble.

I think that my problem is like Giovanni's problem: I can't make the files communicate with each other. I'm trying to migrate using the Device Initialization and the person who helped me to write my programs used to make a main.h on his programs, and I think that the problem is in main.h.

 

Then I ask you.... Is there any document that is written what I can and can't write in a header file (main.h)????? 

 

I'm sending the "main.h" as an attachment.

 

 

Tank's for your attention!!!!!!

0 Kudos

613 Views
rocco
Senior Contributor II

Hi Jo,


JO�O - BR wrote: 
Is there any document that is written what I can and can't write in a header file (main.h)????? 

 

A good book on C should explain it all.

 

But here is a tip on your current "main.h":

A header file should contain NO definitions, only declarations. A test of this is that a header file should be able to be called in every .c file without errors. If you put definitions in a .h file, you will end up with multiple-definition errors if it is included in more than one source file. Multiple declarations, on the other hand, are allowed.

0 Kudos