View previous topic :: View next topic |
Author |
Message |
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
Interrupt_Variable_pass |
Posted: Wed Sep 18, 2013 10:55 am |
|
|
Hi all
is there any way for passing variables to the Interrupt functions ?
I have to use public vars for this purpose , but this way my program has became so complicated . and also variables witch are changed just one time in the beginning of the program should stand on ram for always.
tnx _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 18, 2013 11:00 am |
|
|
No.
Any variable is going to 'stand on RAM', even one that was passed (you would need to store it in the interrupt function, and since these can occur anywhere all variables used in interrupt functions use separate RAM).
You could generate a global pointer, that points to configuration data, which is then copied to local variables in the interrupt function, but it won't save any RAM, and will involve more complexity in the interrupt function, which should be avoided.
It rather sounds as if you are doing more in interrupts than should really be done in these. |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Wed Sep 18, 2013 11:16 am |
|
|
Ttelmah wrote: | No.
Any variable is going to 'stand on RAM', even one that was passed (you would need to store it in the interrupt function, and since these can occur anywhere all variables used in interrupt functions use separate RAM).
You could generate a global pointer, that points to configuration data, which is then copied to local variables in the interrupt function, but it won't save any RAM, and will involve more complexity in the interrupt function, which should be avoided.
It rather sounds as if you are doing more in interrupts than should really be done in these. |
thanks
how fast!!!!!
thanks _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Sep 18, 2013 9:31 pm |
|
|
Quote: |
changed just one time
|
do you mean:
* calculated and stored in a var?
* read digitally, or from an ADC and stored in var?
* read of eeprom ?
* some form of invariant constant value , known at compile time?
*other
some combination of the above?
|
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Thu Sep 19, 2013 12:13 am |
|
|
I am Producing a software PWM that get's the freq and duty from operator and produce the desired pulse.
so I calculate about 8 variable ( all long ) and pass it to the ISR.
so they calculate one time and then they are just some consts.
I am writing it to use it as a library for all of my projects and deliver it to other for using it , but it's not good that a library contains a lot of public/global vars. _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Sep 19, 2013 9:33 am |
|
|
if there is no dependency on the value of these constant vars that MUST be computed in runtime-then by all means declare them CONSTS.
and when it comes to VARS operated on by the ISR or the main program,
if they are 8bits - you can just read them , any old time
if they are int16's or greater you can use this code to get an accurate read, with out disabling ints..
example code follows of how to do this
Code: |
//count is the var shared by your ISR and main or other functions
// getcount returns the safe value of count
//.....two reads verify that it has not changed mid reading
signed int16 getcount(void){
signed int16 local
while (local!=count){local=count);
return(local);}
|
|
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Fri Sep 20, 2013 2:08 am |
|
|
Quote: | if there is no dependency on the value of these constant vars that MUST be computed in runtime-then by all means declare them CONSTS. |
I don't think if there can be any tool for this job.
Quote: |
Code: |
signed int16 getcount(void){
signed int16 local
while (local!=count){local=count);
return(local);}
|
|
in your program: where does "count" variable come from? _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Sep 20, 2013 3:00 am |
|
|
He is just showing you how to read a 16bit variable that updates asynchronously to the read (in the example called 'count'), but can be anything you want. This is a way of reading a variable which is updated in an interrupt and needs to be read outside, without having to disable interrupts to do it.
The point is that you test to see if the local value matches the variable. If it doesn't, you read the variable, and repeat. If the variable updates during either the read, or the test, it won't match, so the read will repeat.
This type of double read and test, is what is triggered by the keyword 'volatile', in C's that support this. However CCS, though claiming in the manual that the keyword "Tells the compiler optimizer that this variable can be changed at any point during execution.", doesn't actually do anything about it....
It was one of the things I had hoped V5 would fix.
Best Wishes |
|
|
|