|
|
View previous topic :: View next topic |
Author |
Message |
Username
Joined: 01 Nov 2011 Posts: 17
|
Have a few simple questions. |
Posted: Wed Jan 25, 2012 11:31 pm |
|
|
Hey all,
A few months ago I started to use this forum and had no idea what I was doing, but now I know whats going on. But I have a few questions that I couldn't seem to answer myself from searching, so I ways wondering if you guys could help:
How can you make a variable global in the sense that other function will see it as the same and not just their own local version? I have tried extern, which I know is for multiple source files. I've put the declaration in the header file, and also tried it there as static. So how can I solve this problem. Here is the code that I'm trying to solve this on:
Header file link isn't posted but it does exist in my source code, just to clarify.
Code: |
#int_TIMER0
TIMER0_isr(sb_status)
{
if(input(PIN_A1))
sb_status=1;
else
{ sb_status=0;}
return (sb_status);
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_INTRC);
while(1)
{
if (sb_status==1);{
output_high(PIN_A0);
}
}
}
|
I also have some more questions:
What is the IESO fuse used for? I ask because the wizard always includes it.
If I want to use the internal oscilator and still use the clkout pin as a general I/O pin, I select internal oscilator with no clkout in the wizard, right?
Thanks in advance. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Jan 26, 2012 2:34 am |
|
|
A variable that is declared outside any function, is always global.
So:
Code: |
int8 fred=10; //declare variable fred
void function1(void) {
fred=20; //writes to the global variable 'fred'
}
void function2(void) {
fred=fred*2; //doubles the global variable 'fred'
}
int8 function3(void) {
int8 fred=15;
fred=fred*2; //doubles the _local_ variable 'fred'
return(fred); //returns the local value to the calling program
}
|
Note in particular the way the local definition overrides the global one.
Hence it is quite dangerous to have local variables, and global's that may use the same name....
Solution, is to use your own 'standard' for naming, so (for example), always use something like 'global' in the name to avoid this.
int8 ifred_global;
for the global definition, both gives a hint that the value is 'integer' (i for integer...), and that it is global. Makes it much less likely that future code will tread on the wrong variable....
The wizard should not be trusted. It tends quite often to put unnecessary things into the code, and sometimes to get things wrong.
If you look in your compiler directory, there is a file 'fuses.txt', which gives a description of what every fuse does (very useful). Also the second key document, is the chip data sheet.
IESO, supports clock switching on wake up. The chips don't start running, till the clock is stable. For some oscillators in particular (crystal's and ceramic resonators), this can take quite a long time. With IESO, the chip will start using an internal oscillator (which starts much faster), and automatically switch to the main oscillator once this is running. Makes boot up faster.
Best Wishes |
|
|
|
|
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
|