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
 
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: Sun May 26, 2013 10:41 pm     Reply with quote

Sorry mike. Now you can compile the program and help me where i made the fault in the program.
Code:
#include "18f2520.h"
#fuses H4, NOWDT               
#use delay(clock=16000000)         // 4Mhz crystal with PLL enabled. 4Mhz * 4 => 16Mhz

#define     output_pin    PIN_A4
#define     LED          PIN_C4

#define FLAG_ADDRESS       10
#define FLAG_STATUS_ADDRESS 20

int8 old_f=0, f;
int16 temp_data;
int32 local;
int8 HIGH;

int8 rising;
int8 flag1, flag2;

int16 isr_ccp_value;
int16 current_ccp_value;
int32 frequency;
int flag = 0;

#int_ccp1
void ccp1_isr(void)
{
   int16 current_ccp;
   static int16 old_ccp = 0;
   
   current_ccp = CCP_1;   // Read the 16-bit hardware CCP1 register
   
   isr_ccp_value = current_ccp - old_ccp;       
                                
   old_ccp = current_ccp;        // Save the current ccp value for the next pass.
   f++;
}

void main()
{
set_tris_a(0x00);
set_tris_c(0x00);

set_timer1(0);                               //   Timer value to 0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);    //   Timer1 internal & 1:1 Prescaler
setup_ccp1(CCP_CAPTURE_RE);                  //   Looks for Rising edge

clear_interrupt(INT_CCP1);                  //   disables the CCP1 interrupt
enable_interrupts(INT_CCP1);                //   enables the CCP1 Interupt
enable_interrupts(GLOBAL);                  //   Enables global interrupt

HIGH = 1;

while(1)
{ 
  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
      {
       disable_interrupts(GLOBAL);
        
      frequency = (int16)((640 * 393822) / (local));
   
   if((frequency < 2000) && (flag == 0))     
   {
      output_high(LED);
      output_high(output_pin);                           
      flag = 1;                               
      write_eeprom(FLAG_ADDRESS, flag);               
      rising = 1;                             
      write_eeprom(FLAG_STATUS_ADDRESS, rising);         
   }
   
   flag1 = read_eeprom(FLAG_ADDRESS);               // read the value in eeprom address - flag
   flag2 = read_eeprom(FLAG_STATUS_ADDRESS);         // read the value in eeprom address - rising
   
   if((frequency < 2000) && (flag1 == 0))     
   {
      output_high(LED);
      output_high(output_pin);                         
      flag = 1;                                 
      write_eeprom(FLAG_ADDRESS, flag);             
      rising = 1;                               
      write_eeprom(FLAG_STATUS_ADDRESS,rising);         
   }
   
   if((frequency > 2000) && (frequency < 3000) && (flag2 == 1) && (flag1 == 1))   
   {
      output_high(LED);
      output_high(output_pin);                         
      flag = 1;                                 
      write_eeprom(FLAG_ADDRESS,flag);               
      rising = 1;                               
      write_eeprom(FLAG_STATUS_ADDRESS,rising);         
   }
   
   if((frequency > 3000) && (flag1 == 1))       
   {
      output_low(LED);
      output_low(output_pin);                         
      flag = 0;                                 
      write_eeprom(FLAG_ADDRESS, flag);               
      rising = 0;                               
      write_eeprom(FLAG_STATUS_ADDRESS,rising);         
   }
   
   if((frequency >3000) && (flag == 0))     
   {
      output_low(LED);
      output_low(output_pin);                         
      flag = 0;                               
      write_eeprom(FLAG_ADDRESS, flag);             
      rising = 0;                               
      write_eeprom(FLAG_STATUS_ADDRESS,rising);       
   }
   
   if((frequency > 2000) && (frequency < 3000) && (flag2 == 0) && (flag1 == 0)) 
   {
      output_low(LED);
      output_low(output_pin);
      flag = 0;                               
      write_eeprom(FLAG_ADDRESS,flag);               
      rising = 0;                               
      write_eeprom(FLAG_STATUS_ADDRESS,rising);       
   } 
   local=f=old_f=0;
   }
}
}


frequency reads wrongly.. program compiles successfully. Please check it and help me to correct the problem. sorry for my ignorance ..
avatarengineer



Joined: 13 May 2013
Posts: 51
Location: Arizona

View user's profile Send private message Visit poster's website

some possible fixes for you...
PostPosted: Mon May 27, 2013 9:25 pm     Reply with quote

One problem may be the interrupt needs to be suspended while
communicating with the LCD.
I would enable/disable the isr as needed.

Improve your Frequency measurement resolution;
Timer1 is 16 bit and your count value is only around 800.
Max is 65K+, so you're wasting the resolution that's possible.

Very Happy simple change;

setup_ccp1(CCP_CAPTURE_RE);
to
setup_ccp1(CCP_CAPTURE_DIV_16);

to increase the count value (and resolution) by 16X (800>>>12800)

Freq= 16x4MHz/count; (use 32bit math)

This is all you need...
#int_ccp1
void CCP1_isr() {
Fcount = CCP_1; // 16bit value
set_timer1(0);
}



in main()...

Freq=64000000/Fcount;


To increase resolution further, count the interrupts
and not reset the timer until the count is exceeded
or use a 32bit bucket to sum CCP_1 values.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Mon May 27, 2013 9:46 pm     Reply with quote

hi avatarEngineer,
Thanks for your valuable reply.
I'll consider your post and keep working on it.

But can you please analyse the problem in my code. After f = 64, i have disabled the interrupts. so that it won't go into the interrupt routine. I have calculated the frequency value and the frequency value is compared with IF statements. Then setting f =0 and enable the interrupts. I think the logic is right?
What do you suggest?

Thank you. I'm modifying the code as suggested by you.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Mon May 27, 2013 10:11 pm     Reply with quote

Updated,
Hi avatarengineer,
I have modified the code as suggested by you.
I have measured the frequency at lower frequencies (say about 1000 HZ)and the observed value is 1001 Hz (it is good, in my case). But while i increase the frequency to 5000 Hz, there was an error of 20Hz difference with the actual frequency.

Also i have to update you, the frequency measurement was quite so good with my program which i have posted. I have problem in measuring frequency during writing and reading the eeprom values? During reading and writing data from eeprom, i have disabled the global interrupt. But still it creates the problem. I'm totally screwed. No clue in finding the problem.

Please in need of help.
Mike Walne



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

View user's profile Send private message

PostPosted: Tue May 28, 2013 9:38 am     Reply with quote

hemnath wrote:
I have measured the frequency at lower frequencies (say about 1000 HZ)and the observed value is 1001 Hz (it is good, in my case). But while i increase the frequency to 5000 Hz, there was an error of 20Hz difference with the actual frequency.

You're trying to run before you can walk.
I don't consider you've mastered the frequency measuring process yet.
I don't have your PIC. So I used a version of your code on a '252 (early cousin of your part).
I used the CCP2 in PWM mode as a signal source, and removed all your bells and whistles.
I got frequency measurements which were in error by several % most of the time and were not steady.
My 'scope showed the signal source to be rock steady.

As a test I simply recorded a series of CCP_1 values. They were totally consistent with the known generator frequency.
So the problem is not with the capture process. It's your data processing.
I also suspect that you're not actually capturing 64 samples as you believe.
If you were capturing 64 samples you'ld simply divide 256,000,000 by your variable 'local' to calculate frequency.
Try a much smaller number of samples to see what happens.

I don't intend trying to decipher you code.

Mike

In its simplest form you capture two adjacent values of CCP_1 to measure a single period.
So for 'N' periods you capture two values of CCP_1 which are 'N' periods apart.
You can ignore what's going on whilst doing other processes!!!!!!
Mike Walne



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

View user's profile Send private message

Re: some possible fixes for you...
PostPosted: Tue May 28, 2013 1:23 pm     Reply with quote

avatarengineer wrote:

Timer1 is 16 bit and your count value is only around 800.
Max is 65K+, so you're wasting the resolution that's possible.

Very Happy simple change;

setup_ccp1(CCP_CAPTURE_RE);
to
setup_ccp1(CCP_CAPTURE_DIV_16);

to increase the count value (and resolution) by 16X (800>>>12800)

Careful. Goal posts have moved.

Started off with 4MHz clock & 1MHz instructions.
Now it's 16MHz clock & 4MHz instructions.
So count for 1 cycle is ~40,000 @ 100Hz.
(Unless of course measurement range has changed as well!)

Mike
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Tue May 28, 2013 9:44 pm     Reply with quote

Hi mike,
I was waiting for your reply.

In the mean while, i have tried the method as suggested by avatarengineer. And the results were posted yesterday.

I have posted the code as suggested by you. You told that i haven't posted the code which compiles. Please look into that thread which were before the avatarengineer thread. The code compiles successfully. so that it would be easy for you to find where the problem occurs in that code.

step 1: First i'll measure the period for 64 rising edges
step 2: With that period, i'm calculating the frequency and disabled the interrupt to stop incrementing the f value.
step 3: Frequency is compared with values in the IF statements
step 4: writing and reading EEPROM
Again starts from step 1. and continues ..
This is what i did in the program. So, IF statements will wait till writing data to eeprom.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 28, 2013 11:45 pm     Reply with quote

I had a quick look at the program but gave up as it is too difficult to follow.

A few suggestions though:
- Indentation is terrible making your code hard to read.
- Try using more descriptive names than flag, flag1, flag2 and f.
- Add a little bit of comments inside the program to indicate what you are doing. Comments could be as few as the steps you have described in the post directly above this. Hey, if you describe your program here to us, why don't write the same comments inside your program? Then everyone studying your program gets the information automatically.
- If all flags are booleans, then declaring them as boolean or int1 makes it easier to understand, prevents errors and the compiler can do better optimizations.
- Writing to eeprom is slow, about 4 -5 ms per byte. Also, writing to EEPROM is limited to a maximum number of writes. After 100k to 1M writes the EEPROM will be dead. Perhaps not a problem but always be aware.

- A common mistake, but bug none-the-less, your program doesn't work for the values of 2000 and 3000. See if you can find the bug yourself.
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Wed May 29, 2013 12:10 am     Reply with quote

hi ckielstra,
The program is very simple. Depending on the frequency, LED output changes and Writing/reading the data 0 or 1 to the eeprom.

most simplest,
Code:
#include "18f2520.h"
#fuses H4, NOWDT               
#use delay(clock=16000000)         // 4Mhz crystal with PLL enabled. 4Mhz * 4 => 16Mhz

#define     output_pin    PIN_A4
#define     LED          PIN_C4

#define FLAG_ADDRESS       10

int8 old_f, f = 0;
int16 temp_data;
int32 local;
int8 read_rising_flag;

int16 isr_ccp_value;
int16 current_ccp_value;
int32 frequency;
int1 rising_flag;

#int_ccp1
void ccp1_isr(void)
{
   int16 current_ccp;
   static int16 old_ccp = 0;
       
   current_ccp = CCP_1;   // Read the 16-bit hardware CCP1 register
   f++;
   isr_ccp_value = current_ccp - old_ccp;                                       
   old_ccp = current_ccp;        // Save the current ccp value for the next pass.
}

void main()
{
set_tris_a(0x00);
set_tris_c(0x00);

set_timer1(0);                               //   Timer value to 0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);    //   Timer1 internal & 1:1 Prescaler
setup_ccp1(CCP_CAPTURE_RE);                  //   Looks for Rising edge

clear_interrupt(INT_CCP1);                  //   disables the CCP1 interrupt
enable_interrupts(INT_CCP1);                //   enables the CCP1 Interupt
enable_interrupts(GLOBAL);                  //   Enables global interrupt

while(1)
{   
enable_interrupts(GLOBAL);                  //   enable interrupts
current_ccp_value = isr_ccp_value;           //   copy the value in isr_ccp_value to current_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;         // for 64 rising edges, local value gets added
      }
   else
      {
       disable_interrupts(GLOBAL);         // disabled the interrupt       
        frequency = (int16)((640 * 393822) / (local));   

         if((frequency < 2000))        // if frequency is lesser than 2000
         {
            output_high(LED);
            output_high(output_pin);                           
            rising_flag = 1;                                               
         }
      
         read_rising_flag = read_eeprom(FLAG_ADDRESS);                // read the rising_flag value in eeprom address
         
         if((frequency > 3000))      // if frequency is greater than 3000
         {
            output_low(LED);
            output_low(output_pin);                         
            rising_flag = 0;                                                     
         }
      
         if((frequency > 2000) && (frequency < 3000))      // if frequency between 2000 and 3000
         {
            if(read_rising_flag == 1)      // and if read_rising_flag is equal to 1
            {
               output_high(LED);
               output_high(output_pin);                         
               rising_flag = 1;
            }
            else if (read_rising_flag ==0)      // else if read_rising_flag is equal to 0
            {
               output_low(LED);
               output_low(output_pin);
               rising_flag = 0;
              }                                                 
         }
       write_eeprom(FLAG_ADDRESS,rising_flag);    // write the flag data in flag_address   
      local = f = old_f = 0;         // reset all value to zero
      }
}
}
Mike Walne



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

View user's profile Send private message

PostPosted: Wed May 29, 2013 2:17 am     Reply with quote

I stand by EVERYTHING I said in my previous post.

I refused to decipher your code for the same reasons asmboy gives.
You should take a close look at this section of code.
Code:

 if(f < 64)
      {
          do
          {
             temp_data=current_ccp_value;
          } while (current_ccp_value!=temp_data);
          local += temp_data;         // for 64 rising edges, local value gets added
      }
   else

Consider:-

1) What is f's value the first time the code executes?
2) What is f's value the last time is executes?
3) How many times has the code operated between (1) and (2)?
4) If you can't work it out, try a much smaller number of loops.

With the code you've just posted:-

5) Where does the magic number 393822 come from?
6) How do you KNOW what your measured frequency is?

I have taken the trouble to run a version of your earlier code on real hardware.
I don't see why my '252 should be dramatically different to your '2520.
Then there's the glaring error asmboy's highlighted.
(I couldn't cope with looking at that code section.)

Mike

EDIT I've had a further look at the coding this morning.

I still firmly believe, you're NOT at the stage where you are measuring frequency accurately, (even without all the bells and whistles).

a) The fact that your getting frequency dependant errors SHOULD tell you something.
b) You're handling of the initial & final conditions and local are all suspect.
c) Like I said before I don't propose deciphering or fixing your code.

I debugged the my version of your code by:-

i) Recording a series of "current_ccp" etc values in an arrays.
ii) Then printf the values to examine at leisure.
Mike Walne



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

View user's profile Send private message

PostPosted: Thu May 30, 2013 3:50 pm     Reply with quote

I've removed a lot of your code.

This works on my PICDEM 2 PLUS board.
Link CCP_2 output to CCP_1 input.
Code:
#include "18f252.h"
#fuses HS, NOWDT, NOLVP               
#use delay(clock=16000000)         // 16Mhz xtal
#use rs232(baud=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)

#define MAX_SAMPLE    100                // Sets No of period samples

unsigned int8 sample = 0;                // Current sample
unsigned int32 total_period;             // Total period count in machine cycles
unsigned int16 new_ccp_value;            //
unsigned int16 old_ccp_value;            //
unsigned int16 new_period_value;         // latest period value = difference between ccp values
unsigned int32 frequency;

#int_ccp1
void ccp1_isr(void)
{
   new_ccp_value = CCP_1;
   if ((sample > 0)&&(sample<MAX_SAMPLE+1))              // Ignores sample zero!
   {
      new_period_value = new_ccp_value - old_ccp_value;  //
      total_period+=new_period_value;                    //
   }
   old_ccp_value = new_ccp_value;                        // ready for next sample
   if (sample < (MAX_SAMPLE+1))                          // stops overflow when done
   {
      sample++;
      output_toggle(PIN_B0);                             // activity indicator with LED
   }
}
     
void main()
{
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);    //   Timer1 internal & 1:1 Prescaler
   setup_ccp1(CCP_CAPTURE_RE);                  //   Looks for Rising edge

   enable_interrupts(INT_CCP1);                 //   Enables the CCP1 Interupt
   enable_interrupts(GLOBAL);                   //   Enables global interrupt

   setup_ccp2(CCP_PWM);                         //   Uses PWM_2 as signal source
   setup_timer_2(T2_DIV_BY_16,249,1);           //   Defines source frequency
   set_pwm2_duty(10);                           //   With narrow pulses

   printf("Begin \r\n");                        //   Checks comms are working

   while(1)
   {
      total_period = 0;                        // Ensure variables are reset
      sample = 0;                              //   

      while (sample<(MAX_SAMPLE+1)){}                // waits here 'til done sampling

      frequency = (int32)((4000000*MAX_SAMPLE)/(total_period));
      printf(" Frequency = %ld Hz \r\n", frequency );
      delay_ms(500);                          // do anything else here, twiddle thumbs
   }
}
Used int8 value for sample variable.
Works with any No. of samples from 1 to 254.
Seems to be OK with 10kHz source & 16MHz clock.
Beyond that, you'd have to check for having enough time for the #int_ccp routine.
i.e. get in, run and get out.
Values shown create 1kHz source.

Mike
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Fri May 31, 2013 2:27 am     Reply with quote

Great work mike. It's working good.
I appreciate you guys. SmileSmile Thank you very much Smile.. I'm learning a lot from you guys.

I tried your program and added a line,
write_eeprom(100,5);
below to delay_ms(500);

just simply writing a value of 5 in eeprom address 100.

But i read in another thread, too many writes to eeprom will destroy the PIC.. Is it?

Thank you once again Smile
Mike Walne



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

View user's profile Send private message

PostPosted: Fri May 31, 2013 3:10 am     Reply with quote

Quote:
But i read in another thread, too many writes to eeprom will destroy the PIC.. Is it?

It's in all the data sheets!
You shouldn't have to rely on posted threads.

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
Page 3 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