CodeWarrior V5.1 for 9S08QG8 beginner questions

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

CodeWarrior V5.1 for 9S08QG8 beginner questions

跳至解决方案
3,868 次查看
stevec
Contributor III
I pressed the wrong key so the message got put on without any text.
I've just started using CodeWarrior for C code developement. I am after some fairly fundamental info about where to declare variables and how to make them available to other modules. I have used a Keil C compiler for 8051s before now and there were tons of documentation and examples published that helped me get a head start. My simple trial code I have tried so far has declared variables in the 'home'  .c module and declared them as external in the 'away' .c module. There must be a more elegant way of doing this. I guess what I need is some kind of overall project template. I guess if I look long enough I'll find what I need but some pointers (sorry about the pun) would be handy.

I've already had some very useful help from you guys regarding the processor. I expect there'll be quite a lot more in the coming weeks.

Steve
标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,509 次查看
CrasyCat
Specialist III
Do you have a #define _main in main.c prior to including the header file?
 
CrasyCat

在原帖中查看解决方案

0 项奖励
回复
6 回复数
1,509 次查看
CrasyCat
Specialist III
Hello
 
I would recommend to use usual ANSI C modular programming rules here.
 
I would define the variables in a C source file and declare them (with extern keyword) in a header file (.h).
They you just need to include the header file in each C source file, which wants to use the variable.
 
CrasyCat
0 项奖励
回复
1,509 次查看
stevec
Contributor III
But wouldn't I have to have a header file for the variables used in the 'home' module which didn't declare them as extern.

I seem to recall using a header file with something like

#ifdef _MAIN            //if header is included in module 'main'
   byte variable1;      //variable used in main

   void main_function1(void); //prototype for function in 'main'

#else                           //this header file included in module other than 'main' so define externals
   extern byte variable1; // variable not defined in this module so external

   extern void main_function1(void);

#endif

and used a header file like this which declared variables and functions for each module.

Can  I do something similar with CodeWarrior?

Steve
0 项奖励
回复
1,509 次查看
CrasyCat
Specialist III
Hello
 
Yes this is working in CodeWarrior too.
 
CrasyCat
0 项奖励
回复
1,509 次查看
stevec
Contributor III
Ok...I've given that a try.
I have used Device Initialisation to set up PB7 as o/p and to interrupt on 1ms timer. Interrupt code just says if timer1_count > 5000 then toggle PB7 and reset count.

I am initialising a variable  unsigned int timer1_count in source main.c and incrementing it in the timer interrupt routine in MCUinit.c. I have included the header file main.h in both main.c and MCUinit.c, which is:

#ifdef _main
   unsigned int timer1_count;

#else
   extern unsigned int timer1_count;

#endif

I get a warning L1823: External object timer1_count in......MCUinit.c.o   created by default.

Have I not used the correct name in the #ifdef line (have tried _main_,  __main also)


0 项奖励
回复
1,510 次查看
CrasyCat
Specialist III
Do you have a #define _main in main.c prior to including the header file?
 
CrasyCat
0 项奖励
回复
1,509 次查看
stevec
Contributor III
Ooops...thats exactly it. Many thanks.

Steve
0 项奖励
回复