CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Global structures

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
drawbars



Joined: 07 Mar 2006
Posts: 8
Location: Ashby, MA

View user's profile Send private message Send e-mail Visit poster's website

Global structures
PostPosted: Tue Mar 21, 2006 10:47 am     Reply with quote

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. Sad 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







PostPosted: Tue Mar 21, 2006 10:52 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Mar 21, 2006 10:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 21, 2006 11:40 am     Reply with quote

Version?
_________________
-Pete
drawbars



Joined: 07 Mar 2006
Posts: 8
Location: Ashby, MA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Mar 21, 2006 12:06 pm     Reply with quote

Latest (v3.245) ...
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 21, 2006 12:11 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Mar 21, 2006 12:17 pm     Reply with quote

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. Sad

Regards,

-BW
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 21, 2006 12:25 pm     Reply with quote

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:
Quote:
0a 55 aa 1234


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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Mar 21, 2006 12:29 pm     Reply with quote

Yeah, that'll work ...

I keep being stubborn and thinking like an Assembly programmer! Laughing
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group