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 CCS Technical Support

Can't access bit field bits
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 5:16 pm     Reply with quote

Cmatic wrote:
I am using extern config in the other module.
However, in my other module (where I declare:extern config),.

The pic is 16f1825 and there is no multiple compilations.


The CCS manual says 'extern' is only used with mcu's.
Quote:
Type Qualifiers

extern - External variable used with multiple compilation units. No storage is allocated. Is used to make otherwise out of scope data accessible. There must be a non-extern definition at the global level in some compilation unit.

Since you are not using mcu's you should not be using extern.
CMatic



Joined: 11 Jan 2012
Posts: 69

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 8:21 pm     Reply with quote

PCM Programmer thanks for the clarification on extern. Can you suggest why then config.Factory = 1 works in the main program but I am unable to do the same in other modules? Any thoughts?
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 11:09 pm     Reply with quote

It depends on how you are defining modules. If you are not doing multiple compilation units, how are you dividing your code into modules (There are various methods, but you have us shooting in the dark)?

Is there a way you can put together a small example? Not asking for your 7 module code, but something seprate with just a couple empty functions per module (just need two modules to show the problem) and a very sparse main and your bit field using it the way you want to use it. This would be a small representative code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 21, 2016 1:20 am     Reply with quote

It works for me. I tested the programs below with compiler vs. 5.053
in MPLAB vs. 8.92 simulator. I set the Factory bit to 0 and 1, by each
method (structure access or #bit), and here are the results:
Quote:

Main Module:
00
01
00
01

Module2:
00
01
00
01

Here is the main module, using your structure. The #bit statements were
fixed by removing the semi-colons at the end of each line. Semi-colons
are not used with #bit or #byte.
Code:

#include <16F1825.h>
#fuses INTRC_IO, NOWDT,PLL_SW
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

struct { 
    int8 Units:1;        //bit0 - Units LSB
    int8 Sound:1;        //bit 1- sound
    int8 Alarm:1;        //bit 2- Alarm
    int8 Bcklgt:1;       //bit 3- Bcklgt
    int8 Autopwroff:1;   //bit 4- Autopwroff
    int8 BatLow:1;       //bit 5- BatLow
    int8 Reserved:1;     //bit 6- Reserved
    int8 Factory:1;      //bit 7- Factory MSB
 } config;   

#locate config = 0x2000 //define where this variable will be located

#bit Units = config.0
#bit Sound = config.1
#bit Alarm = config.2
#bit Bcklgt = config.3
#bit Autopwroff = config.4
#bit BatLow = config.5
#bit Reserved = config.6
#bit Factory = config.7

#include "module2.c"

//=========================================
void main()
{
printf("Main Module: \r");

config.Factory = 0;
printf("%x \r", config.Factory);

config.Factory = 1;
printf("%x \r", config.Factory);

Factory = 0;
printf("%x \r", Factory);

Factory = 1;
printf("%x \r", Factory);

function_in_module2();

while(TRUE);
}

Here is module2.c:
Code:

void function_in_module2(void)
{
printf("\rModule2:\r");

config.Factory = 0;
printf("%x \r", config.Factory);

config.Factory = 1;
printf("%x \r", config.Factory);

Factory = 0;
printf("%x \r", Factory);

Factory = 1;
printf("%x \r", Factory);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Jan 21, 2016 5:55 am     Reply with quote

One obvious screaming thing is the address being used.

He is locating the variable to 0x2000. Yet the chip does not have RAM at this location....

Now it is interesting if you #locate to 0x1000, you will get an error saying that this is outside the RAM range. However 2000 is accepted, though it doesn't exist. It wraps back, and puts the variable at 0x20. If you look at the RAM table for the chips, they only list pages to 0xFFF, but with lots of the addresses existing, and wrapping to low addresses. It looks as if the compiler is confused by this and accepts 0x2000 though it shouldn't.

Why is #locate being used?.

Then we have the obvious thing of the wrong declaration of config. Using extern.
Extern _requires_ that the data reference is going to be satisfied by a global definition in an external source. Not wanted or needed here. My guess is that his config reference in the primary module, is actually as a local variable, not a global. This then doesn't get propagated to the other module, so config does not exist. If he hadn't got the 'extern' reference, this would give an error.

In fact you can generate his error, if you do this. Generate config as a _local_ variable in the main, then add an extern declaration of this to the second module. Duh...
CMatic



Joined: 11 Jan 2012
Posts: 69

View user's profile Send private message

PostPosted: Thu Jan 21, 2016 11:59 am     Reply with quote

PCM Programmer thanks so much for testing this in your program. I am amazed by the results you posted, I will post my results very soon.

Thelma thanks for checking the #locate 0x2000, I will try and simulate this as well and post back. I appreciate everyone's replies, thanks.
CMatic



Joined: 11 Jan 2012
Posts: 69

View user's profile Send private message

PostPosted: Fri Jan 22, 2016 7:19 am     Reply with quote

PCM Programmer and Ttelmah, the program is working as PCM Programmer showed above and I can retrieve config.Factory perfectly.
Again, you guys are amazing. Thank you
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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