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

problem with a variable

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



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

problem with a variable
PostPosted: Sat Jul 23, 2011 1:50 pm     Reply with quote

Hi
I've maked a program that uses an Interrupt subroutine(AD_isr).
I defined a variable:
Code:

static int8 counter;

I want to increase this variable in my AD_isr when it occurs. But it doesn't work. I mean everytime ISR takes place this variable is started from zero and increases to one (1).
Now, how I solve my problem ? Please help me.

Best regards
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jul 23, 2011 2:52 pm     Reply with quote

You'll have to show us your program, PIC type, and compiler version.
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sat Jul 23, 2011 2:59 pm     Reply with quote

Hi
this is a part of my code:
Code:

#include <16f877a.h>
#FUSES XT,NOWDT,PUT,BROWNOUT,NOLVP,HS
#device ADC=10
#use delay (clock=16000000)
#include <math.h>
#include <H:\PIC\My Projects\Hava_Faza\source code\flex_lcd.c>

float sample[4];
float offset,amp,sample_temp,cos_wt;
int1 start_flg=0,shift_flag=0;
static unsigned int8 samp;

#int_AD
void  AD_isr(void)
{

if(!start_flg)
  {
   sample[samp]=read_adc(ADC_READ_ONLY);
   samp++;
   if(samp==4)
     start_flg=1;
  }
else
  {
   sample_temp=read_adc(ADC_READ_ONLY);
   shift_flag=1;
  }
}

and the version of compiler is : 4.105
best regards
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Jul 23, 2011 11:23 pm     Reply with quote

It's getting late and my eyes are tired, but I don't see anywhere in the ISR that you even reference "counter" - are you actually meaning "samp" ?

Something else that may be getting you (I got nailed with this several years ago with an earlier version of this compiler). Unless you declare the variable volatile the compiler may optimize it not seeing that something else is changing it. (in my case, the ISR was supposed to check a bit on a port, but apparently the compiler decided nothing else was changing it and skipped looking at the port each time :-) )

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Ttelmah



Joined: 11 Mar 2010
Posts: 19336

View user's profile Send private message

PostPosted: Sun Jul 24, 2011 2:34 am     Reply with quote

Separately, I have to ask what is triggering INT_AD?.
INT_AD, is _only_ used for two things really:
1) Combined with sleep, to allow the processor to sleep while the conversion takes place, in which case, and interrupt handler is not needed, and costs time.
2) Using the CCP to trigger an AD conversion for 'synchronous' sampling.

Otherwise, it generally takes _longer_ to get into and out of the ADC interrupt, than to simply do the conversion 'inline' in the code.....

You have both an XT oscillator fuse, and an HS oscillator fuse - should be only one....

So, what is triggering INT_AD?. If you are using a simulator, be aware of the limitations of these.

Best Wishes
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Mon Jul 25, 2011 12:45 pm     Reply with quote

Hi
Dear Ttelmah thanks for help.
I want to get an analog value every 400us. I want to read converted exactly after convertion because I need 4 real samples to estimate a harmonic of input signal by a formula. So I need a counter in AD_isr to count samples when they take place.
Anyway thank you very much. you are so kind.

best regards
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jul 25, 2011 1:43 pm     Reply with quote

Another way is to just have the ISR read and store 4 readings...

pseudo code

...........................
enter isr.....

sample1=read_adc.
sample2=read_adc..
sample3=read_adc...
sample4=read_adc....

exit isr

as long as the variables are global, 'main' can read them.
it's also probably the fastest way to get 4 equally timed readings
just another way to look at the problem.
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