View previous topic :: View next topic |
Author |
Message |
drawbars
Joined: 07 Mar 2006 Posts: 8 Location: Ashby, MA
|
Global structures |
Posted: Tue Mar 21, 2006 10:47 am |
|
|
Hi All,
I'm trying to access several variables of different sizes, both individually, and eventually as a data string to send to the host.
struct mydata { int a; int16 b; int c; int d; int status; };
mydata foo;
foo.a = 25;
In fact, I would also I also like to be able to access bits in some bytes by name:
#bit foo.status.0 = my_special_bit;
In order to guarantee the location and order of these variables (it matters) in a PIC with its Harvard architecture, I was given the advice to use a STRUCT. This seemed like a good idea; however, it seems that a structure can only be used inside a function in PICC. I would prefer to place the structure outside of the functions, in the global variables. Is there a way to do this?
Regards,
-BW |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 21, 2006 10:52 am |
|
|
Structures can be declared outside the functions fine. What makes you think they can't be?.
Best Wishes |
|
|
drawbars
Joined: 07 Mar 2006 Posts: 8 Location: Ashby, MA
|
|
Posted: Tue Mar 21, 2006 10:59 am |
|
|
Maybe they can be, but they cannot be initialized, etc.
The following code fails.
typedef struct {
BYTE statcnt;
BYTE mtype;
BYTE fnum;
WORD fdata;
} FIELDSTAT;
FIELDSTAT foo;
foo.statcnt = 10;
The last line fails with an "Expecting a (" error, unless placed inside a function. Is my only alternative to do the initialzing inside main(). The variable isn't directly used there; it's used by othermodule.c.
-BW |
|
|
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Tue Mar 21, 2006 11:40 am |
|
|
Version? _________________ -Pete |
|
|
drawbars
Joined: 07 Mar 2006 Posts: 8 Location: Ashby, MA
|
|
Posted: Tue Mar 21, 2006 12:06 pm |
|
|
Latest (v3.245) ... |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Mar 21, 2006 12:11 pm |
|
|
Code: | #include <16F877a.h>
#device *=16
#use delay(clock=4000000)
#fuses hs,nolvp,nowdt
#use rs232(baud=19200,xmit=PIN_E0,INVERT,stream=DEBUG)
#case
#zero_ram
struct FIELDSTAT{
BYTE statcnt;
BYTE mtype;
BYTE fnum;
BYTE fdata;
};
struct FIELDSTAT foo;
//=== main ===//
main() {
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
set_tris_b(0xFF);//all inputs
printf("Start\r\n");
foo.statcnt = 10;
//n_pos.one=10;
while (TRUE) {
}
}
|
|
|
|
drawbars
Joined: 07 Mar 2006 Posts: 8 Location: Ashby, MA
|
|
Posted: Tue Mar 21, 2006 12:17 pm |
|
|
Hi,
Thanks for the reply. The answer was about what I suspected. I didn't really want to init the variables inside main(), because they are never used there, but the only other alternative seems to be to create a dummy function that does nothing but init the variables. I was hoping that there might be some clever way I didn't see.
Regards,
-BW |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 21, 2006 12:25 pm |
|
|
Quote: |
The answer was about what I suspected. I didn't really want to init the variables inside main(). |
The following program initializes the structure outside of main().
It displays this:
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
typedef int16 WORD;
typedef struct {
BYTE statcnt;
BYTE mtype;
BYTE fnum;
WORD fdata;
} FIELDSTAT;
FIELDSTAT foo = {10, 0x55, 0xAA, 0x1234};
//============================
void main()
{
printf("%x %x %x %lx \n\r", foo.statcnt, foo.mtype, foo.fnum, foo.fdata);
while(1);
} |
|
|
|
drawbars
Joined: 07 Mar 2006 Posts: 8 Location: Ashby, MA
|
|
Posted: Tue Mar 21, 2006 12:29 pm |
|
|
Yeah, that'll work ...
I keep being stubborn and thinking like an Assembly programmer! |
|
|
|