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

CCP - Problem with capture mode
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 7:54 am     Reply with quote

Quote:
Total range over which you are trying to measure frequency.

Range : From 100 Hz to 6Khz


Quote:
Required accuracy over each range.

+/- 2Hz
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 8:58 am     Reply with quote

You've got a fairly narrow range to measure.

Simplest solution I can see:-

1) As it stands you're measuring the time for 1 period only.
2) At present you're looking to improve by a factor of ~25.
3) Simply change to measure either 64 or 100 periods.
4) Modify maths to suit.
5) You will be measuring to +/- ~0.5Hz at 6kHz
6) You will get better resolution at lower frequencies, as before.
7) Even measuring 100 periods, you'll update at once per second with 100Hz input.

Mike

EDIT Choosing 64 or 100 periods depends on which you're most comfortable with.
64 periods -> faster update.
100 periods -> better resolution.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 9:59 pm     Reply with quote

Thanks mike for your kind reply.
Quote:
Choosing 64 or 100 periods depends on which you're most comfortable with.
64 periods -> faster update.
100 periods -> better resolution.

now i prefer to measure 64 periods.

I have changed the math in the program. Please check that i'm doing it right.

I'm incrementing a variable 'f' in interrupt routine and in the while loop,

Code:
if(f<64)
{
local = local + current_ccp_delta;
}

if(f>64)
{
local = local/64;
frequency = (float32)((1000000) / (local));   
local = 0;
f = 0;

lcd_cmd(0x80);
printf(lcd_data,"%lu Hz      ", frequency);

delay_ms(100);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Thu Apr 18, 2013 12:37 am     Reply with quote

You are wasting one value. When f==64, you are not doing anything....
Code:
   
int8 old_f=0;
int16 temp_delta;

while(TRUE)
{
   while (old_f==f); //wait for f to change
   old_f=f;
   if(f<64)
   {
       do
       {
           temp_delta=current_ccp_delta;
       } while (current_ccp_delta!=temp_delta);
       local += temp_delta;
   }
   else
   {
       frequency = (float32)((64000000) / (local));   

       lcd_cmd(0x80);
       printf(lcd_data,"%lu Hz      ", frequency);
       delay_ms(100);
       local=f=old_f=0;
   }   
}


There are a couple of other things here. What delays the outside addition, till a new value is in f?. I've added a test that f has updated before the addition. 'else' is much safer and simpler. Avoids having to do two tests.

I've also double checked, that current_ccp_delta hasn't changed while it was being read. Generally when reading anything bigger than an int8, updated in an interrupt, you should always either disable interrupts when you read, or verify that the stored value matches as shown. Otherwise you will get spurious results if the value is changed in the interrupt aqt the same moment it is read.

Key question. How large is 'local'. needs to be an int32 remember.

I also don't clear 'f' till after the delay. Otherwise you risk getting one value from the start of the delay, and the rest afterwards.

General comment 'think'. If you want to divide a value by 64, and in the very next line are dividing another value by the result, realise that you can get the same result _without rounding errors_, by multiplying the second numerator value by 64, and do it faster as well....

Do indent your code. It makes working out what applies to which loop, or logic test, much easier.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Apr 18, 2013 10:13 am     Reply with quote

Quote:
I have changed the math in the program. Please check that i'm doing it right.
In my book there is not a "right" way.
Many wrong ones, some bad or poor ones, usually a number of near equivalnet good ones.

There's not much to add to Ttelmah's comments.

He's shown you some better ways to do things, and given some good advice.

Mike
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Thu May 16, 2013 4:58 am     Reply with quote

I have implemented it in my program and its working better. I appreciate you guys.

It measures the frequency and with the error of ±2 Hz error at 6Khz. But why you guys suggest 64 periods -> faster update.
100 periods -> better resolution. It is arbitrarily or pre calculated value. how can we calculate those values?

Also, i have another problem.

in the while loop,
read_frequency();
if(button_pressed)
write_int16_eeprom(FREQ_ADDRESS, frequency);

and reading the eeprom values, but it shows 0. Why is it? I have mentioned the FREQ_ADDRESS to 10.
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Thu May 16, 2013 8:52 am     Reply with quote

it's down to three things:

First, 'binary' multiples (2,4,8,16,32,64,128 etc.) are much quicker for the chip to work with. /64, can be done in a handful of instructions, while /10 or /100 take thousands of instructions. A huge difference. So it is always 'faster' (maths wise), to use binary multiples. However the multiple needs to be a 'constant' value for this to work.
Then the larger the sample multiple, the smaller the error, and the longer the time needed. Linear relation. So I'd probably say /128, will beat /100 on 'all grounds', while /64 will be faster but slightly lower resolution.
Finally there is a limit on the maths size for a given sample multiple/size. If you have samples that are 10bit, then that maximum number that can be combined using 16bit arithmetic, is 64 (6bits available, 2^6=64). Going beyond this implies 32bit arithmetic is needed.

You haven't shown us the 'write_int16_eeprom' function, so impossible to guess why it doesn't work.
Is the code based on the manual section:
'How do I write variables to EEPTOM that are not a byte'?.

Best Wishes
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Thu May 16, 2013 9:24 pm     Reply with quote

EEPROM code,

Code:

#define INT_EEPROM_ADDRESS  int8

void write_int16_eeprom(INT_EEPROM_ADDRESS address, int16 data);
int16 read_int16_eeprom(INT_EEPROM_ADDRESS address);

// Purpose:    Write a 16 bit number to internal eeprom
// Inputs:     1) An eeprom address
//             2) The 16 bit number to write to internal eeprom
// Outputs:    None
void write_int16_eeprom(INT_EEPROM_ADDRESS address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *((int8 *)(&data) + i));
   }
}


// Purpose:    Read a 16 bit number from internal eeprom
// Inputs:     An eeprom address
// Outputs:    The 16 bit number read from internal eeprom
int16 read_int16_eeprom(INT_EEPROM_ADDRESS address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *((int8 *)(&data) + i) = read_eeprom(address + i);
   }

   return(data);
}
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Thu May 16, 2013 11:19 pm     Reply with quote

the problem looks very strange,

The below program works good.
Code:
while(1)
{
read_frequency();      // call read_frequency function

      if(input(PIN1) == 0)
      {
      if(input(PIN2) == 0) 
      {
      if(input(BUTTON) == 0)      // if pushbutton is pressed
      {
      read_frequency();           // read the frequency value from the function
      LED = 0;
      
      write_int16_eeprom(FREQ_ADDRESS, frequency);     // write frequency value to eeprom address
      delay_ms(1000);
      FREQ_VALUE   = read_int16_eeprom(FREQ_ADDRESS);   // reads the eeprom value and stored in a variable
         lcd_cmd(0x80);
             printf(lcd_data,"eeprom %5lu Hz  ", FREQ_VALUE);
             delay_ms(1000);
      }
      }
      }
}


The same program is written like this,
Code:


while(1)
{
read_frequency();
routine();
}

void routine()
{
if(input(PIN1) == 0)
      {
      if(input(PIN2) == 0) 
      {
      if(input(BUTTON) == 0)      // if pushbutton is pressed
      {
      read_frequency();           // read the frequency value
      LED = 0;
      
      write_int16_eeprom(FREQ_ADDRESS, frequency);     // write frequency value to eeprom address
      delay_ms(1000);
      FREQ_VALUE   = read_int16_eeprom(FREQ_ADDRESS);   // reads the eeprom value and stored in a variable
         lcd_cmd(0x80);
             printf(lcd_data,"eeprom %5lu Hz  ", FREQ_VALUE);
             delay_ms(1000);
      }
      }
      }
}


Both the program are same. But in the 2nd program, it does not read the eeprom value and it shows only 0. why it is not reading the frequency value from the routine function?
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Tue May 21, 2013 10:31 pm     Reply with quote

Code:
while(1)
{
  disable_interrupts(GLOBAL);               //   disables Global interrupt
  current_ccp_value = isr_ccp_value;         //    copy the value in isr_ccp_value to current_ccp_value
  enable_interrupts(GLOBAL);            //   enable interrupts

while(old_f == f);
old_f = f;
 if(f < 64)
   {
       do
       {
         temp_data=current_ccp_value;
       } while (current_ccp_value!=temp_data);
       local += temp_data;
   }
   else
   {
       frequency = (int32)((64000000 * 3.93822) / (local));
        local=f=old_f=0;
   }   

if(button == 0)
{
checking some conditions with frequency value
reading data from eeprom,
writing data to eeprom
}
}


when i comment the if condition, frequency reads exactly. But when i uncomment the if condition in the above program, frequency reads different value. Please help. I'm stucked.

After the frequency value is generated, using some if statements . I'm switching LED ON and OFF.

Will this program won't work when we read the eeprom value?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed May 22, 2013 2:47 pm     Reply with quote

I'm assuming you mean the section of code
Code:
if(button == 0)
{
checking some conditions with frequency value
reading data from eeprom,
writing data to eeprom
}
is causing you a problem
(You have two if conditions)

Have you considered what's happening whilst your doing the reading/ writing eeprom routines?
Bear in mind where you've located this line
Code:
        local=f=old_f=0;


Mike

PS The code you've posted is neither complete nor compilable, so makes it difficult for us to help you.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed May 22, 2013 9:58 pm     Reply with quote

I'm measuring the frequency and depending on the frequency, I'm switching the output ON and OFF. that's what i did in the above program.
The way of representing the IF statements is wrong???
There is long list of if statements and that's the reason i didn't post the complete program.

Below is the sample program,
Code:

while(1)
{
  disable_interrupts(GLOBAL);               //   disables Global interrupt
  current_ccp_value = isr_ccp_value;         //    copy the value in isr_ccp_value to current_ccp_value
  enable_interrupts(GLOBAL);            //   enable interrupts

while(old_f == f);
old_f = f;
 if(f < 64)
   {
       do
       {
         temp_data=current_ccp_value;
       } while (current_ccp_value!=temp_data);
       local += temp_data;
   }
   else
   {
       frequency = (int32)((64000000 * 3.93822) / (local));
        local=f=old_f=0;  // i've added here?? is it right?
   }    // we'll get the frequency from this loop

if(button == 0)
{
if(frequency < 2000) // the frequency value obtained above is used for comparing here.
{
output = 1;
flag1 = 1;
write_eeprom(FLAG_ADDRESS, flag);
mode = 1;
write_eeprom(FLAG_STATUS_ADDRESS, mode);   
}

flag1 = read_eeprom(FLAG_ADDRESS);
flag2 = read_eeprom(FLAG_STATUS_ADDRESS);

if(frequency < 2000) && (flag1 == 0)
{
doing something like before
writing and reading data to and from eeprom
}

if(frequency > 2000) && (flag 2 == 1)
{
doing something like before
writing and reading data to and from eeprom
}
.
.
.
many if statements, like before
}
}

When i comment the read_eeprom and write_eeprom lines, frequency reads correctly.
Syntax of representing the IF statements are wrong???
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu May 23, 2013 10:33 am     Reply with quote

I'm asking you to THINK for yourself and examine your logic!

What's happening in the background whilst you're doing eeprom read/write etc.?

Try reducing your code significantly so that:-

1) You still get the error.
2) You can see for yourself what's going wrong.

Mike

PS You can probably replace the eeprom read/write with a simple delay_ms(xx) to show up where your error is.
Good indenting would help make your code more readable.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Fri May 24, 2013 10:04 pm     Reply with quote

Thanks mike for your reply. I'm still thinking and examining the logic.

Pl check that i'm going in the right way. I have written the logic in different way.

For f < 64, i'm capturing the CCP1 values and adding it in local variable. And when f >= 64, i have disabled the interrupt and finding the frequency value and with that frequency value, i'm checking with some conditions and reading and writing eeprom values. During this conditional statements, CCP1 is disabled. and finally i have reset the f value to 0.

Code:
while(1)
{
  enable_interrupts(GLOBAL);
  current_ccp_value = isr_ccp_value;

  while(old_f == f);
  old_f = f;
   if(f < 64)
   {
       do
       {
         temp_data=current_ccp_value;
       } while (current_ccp_value!=temp_data);
       local += temp_data;
   }
   else
   {
       disable_interrupts(GLOBAL);
       frequency = (int32)((64000000 * 3.93822) / (local));

       if(button == 0)
       {
       checking some conditions, reading and writing eeprom data
       }
        local=f=old_f=0;
   }
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat May 25, 2013 5:35 am     Reply with quote

You're not making it easy to help you.

1) The code you post is neither complete nor compilable so we can't copy and paste to test. You may be doing something, you're not showing, which screws things up.
2) Like I said before there is no one right way.
3) You should not be having to ask if the code is right. Either the code generates the expected results or it doesn't. You don't tell us either way.
4) What I can see looks better than it did before. The glaring logic error has been removed.

Mike
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, 3  Next
Page 2 of 3

 
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