• AVR Freaks

AnsweredDifference between capture and compare modes with examples

Page: << < ..111213 > Showing page 12 of 13
Author
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 08:08:31 (permalink)
0
1and0
Hint: My Post #186.

 Oh wow I didn't notice:
for (i=31;i>=0;i--)

With the quick reading, I missed that the for loop is in decreasing way! That could have saved me and other members a lot of time and arguments!
Sorry for not noticing that and wasting your time.

Hint: My Post #188 regarding if/else.  Your code tests TMR1 ranges of 400-2000, 400-700, and 1000-2000, but _not_ 700-1000. The other issue might be %lx does not display leading zeros.

Yes those conditions for false readings, that if TMR1 reading is out of range then code returns false value.
 
the %lx issue, I just removed the "l" and it surprisingly giving me the 16-bits valuable code! I'm still don't understand how that is happening, that the complement values are removed and I get only the valuable "address/command" bits!
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 08:53:59 (permalink)
+2 (2)
eagle1
Oh wow I didn't notice:
for (i=31;i>=0;i--)

With the quick reading, I missed that the for loop is in decreasing way! That could have saved me and other members a lot of time and arguments!

You also did not notice the 1UL. :(
 

Sorry for not noticing that and wasting your time.

I'm starting to lose interests in this thread.
 

Yes those conditions for false readings, that if TMR1 reading is out of range then code returns false value.

Like I said, look at your code again, what happen when TMR1 falls between 700-1000?
 

the %lx issue, I just removed the "l" and it surprisingly giving me the 16-bits valuable code! I'm still don't understand how that is happening, that the complement values are removed and I get only the valuable "address/command" bits!

Why is it a surprise? "%x" is 16-bit hex and "%lx" is 32-bit hex. Google "format specifier in C" and read up on it. To display leading zeros too, try "%08lx" or "%.8lx".
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 09:07:25 (permalink)
0
Take a look at this snippet of yours:
 while ((PORTBbits.RB2) && (TMR1 < 2000));
if ((TMR1 > 2000) || (TMR1 < 400)) // return if TMR1 < 400 or TMR1 > 2000
    return 4; 
if((TMR1 < 700) && (TMR1 > 400)) // so check > 400 is redundant here
    ir_code_INT &= ~(1<<i);
else if((TMR1 < 2000) && (TMR1 > 1000)) // and check < 2000 is redundant too
    ir_code_INT |= (1<<i);

... and what happen when TMR1 is 700-1000?
 
Edit: Notice TMR1 is changing all the time.
 
 
post edited by 1and0 - 2018/07/29 09:38:41
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 13:06:07 (permalink)
0
1and0
You also did not notice the 1UL. :(

 
 
 

Like I said, look at your code again, what happen when TMR1 falls between 700-1000?

 

Why is it a surprise? "%x" is 16-bit hex and "%lx" is 32-bit hex. Google "format specifier in C" and read up on it. To display leading zeros too, try "%08lx" or "%.8lx".

Yes, I know "format specifier in C" I also know how to specify the digits by a "." then the number of digits.
What I guess it's surprising, that the message format of the 32-bit is:
 

 
 
According to the diagram I think the MSB is on left and LSB on right, like all data format. That's why it worked with the decrementing method.
And with "%x" I get like the address/command out of the inverse values!
 
              ______                  ________
 address/address - command/command
  byte4     byte3       byte2        byte1
 
And I get byte4 and byte2! If I'm calling 16-bit value then I should get byte2 and byte1 but instead I get byte4 and byte2. Hmmm maybe because the inverse values are the opposite of the real values, I don't know I think this reason is related.
 
=====================================================================
1and0
Take a look at this snippet of yours:
 while ((PORTBbits.RB2) && (TMR1 < 2000));
if ((TMR1 > 2000) || (TMR1 < 400)) // return if TMR1 < 400 or TMR1 > 2000
    return 4; 
if((TMR1 < 700) && (TMR1 > 400)) // so check > 400 is redundant here
    ir_code_INT &= ~(1<<i);
else if((TMR1 < 2000) && (TMR1 > 1000)) // and check < 2000 is redundant too
    ir_code_INT |= (1<<i); 

... and what happen when TMR1 is 700-1000?
 
Edit: Notice TMR1 is changing all the time.

In which location of the code?
 
This is my latest version of the function:
 
uint32_t PIN_POLLING_VS1838b(void)
{
    uint8_t i;
    TMR1=0;
    while ((!PORTDbits.RD2) && (TMR1 < 10000)); // test 9ms low 9182
 if ((TMR1 > 10000) || (TMR1 < 9000))
  return 1;
 TMR1 = 0;
    while ((PORTDbits.RD2) && (TMR1 < 5000)); // test 4.5ms high ~4480 
 if ((TMR1 > 4600) || (TMR1 < 4300))
  return 2; 
    TMR1 = 0;
 for(i=0;i<32;i++){ // 2.25ms & 1.25ms test
 TMR1 = 0;
    while ((!PORTDbits.RD2) && (TMR1 < 800));
    if ((TMR1 > 800) || (TMR1 < 300))
  return 3; 
    TMR1 = 0;
    while ((PORTDbits.RD2) && (TMR1 < 2000));
    if ((TMR1 > 2000) || (TMR1 < 400))
        return 4;
    if((TMR1 < 700) && (TMR1 > 400))
        ir_code_INT &= ~(1<<(31 -i));
    else if((TMR1 < 2000) && (TMR1 > 1000))
        ir_code_INT |= (1<<(31 -i)); 
    }
    return ir_code_INT;
}

qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 13:24:21 (permalink) ☄ Helpfulby eagle1 2018/07/29 13:25:59
+2 (2)
eagle1
1and0
...
Edit: Notice TMR1 is changing all the time.

In which location of the code?

Everywhere!
Note that every time you access TMR1, it fetches a fresh copy of the timer value from the peripheral.
Thus, if you do:
if ((TMR1 > 4600) || (TMR1 < 4300))

The second "TMR1" value can be different to the first "TMR1" value, because the timer is still counting as your code is running. This can cause very subtle bugs if you are not aware of it.
It would be much safer to read it once into a temporary variable, and do your tests on that temporary variable.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 13:33:01 (permalink)
0
qɥb
Everywhere!
Note that every time you access TMR1, it fetches a fresh copy of the timer value from the peripheral.
Thus, if you do:
if ((TMR1 > 4600) || (TMR1 < 4300))

The second "TMR1" value can be different to the first "TMR1" value, because the timer is still counting as your code is running. This can cause very subtle bugs if you are not aware of it.
It would be much safer to read it once into a temporary variable, and do your tests on that temporary variable.



Absolutely! I actually thought of that, Hmmm that's why when I read the raw periods for the 32-bits. They came different and fluctuated sometime the bit is 560, 610, 605, 557 ... etc.
 
Also that explains the big periods of the test if; like, 4600 - 4300, 10000 - 9000 and 800 - 300.
PStechPaul
Super Member
  • Total Posts : 2375
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 14:31:48 (permalink) ☄ Helpfulby eagle1 2018/07/29 22:05:57
+1 (1)
It would help if you used a macro for the mark (pulse) and space (idle) conditions. That makes it easier to deal with the inverted logic. Also, it seems you are resetting the timer after the "mark" pulse, so the timing will be the length of the "space", which is 562 uSec for a zero, and 1.688 mSec for a one. Perhaps you are already doing that, but the specification is actually distance (time) between rising edges of the pulses.
#define NEC_Code_Space PORTDbits.RD2
#define NEC_Code_Mark !NEC_Code_Space

I also agree that you should read the TMR1 value into a local variable so you won't get as much variation. Of course, the CCP capture would be even more precise, but perhaps more difficult to implement at this point.
uint16_t TMR1_val;
  TMR1=0;
  while ( NEC_Code_Mark && (TMR1 < 10000) ); // test 9ms Mark 9182
  TMR1_val = TMR1;
  if ((TMR1_val > 10000) || (TMR1_val < 9000))
    return 1;
  TMR1 = 0;
  while ( NEC_Code_Space && (TMR1 < 5000) ); // test 4.5ms high ~4480 
  TMR1_val = TMR1;
  if ( (TMR1_val > 4600) || (TMR1_val < 4300) )

    return 2; 
  TMR1 = 0;
...

post edited by PStechPaul - 2018/07/29 14:39:09

 
PStechPaul
Super Member
  • Total Posts : 2375
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/29 19:14:30 (permalink) ☄ Helpfulby eagle1 2018/07/29 22:06:15
+1 (1)
I just thought of another improvement for constructing the 32 bit word:
for( i=0; i<32; i++ ){ // 2.25ms & 1.125ms test
  TMR1 = 0; // start timing
  while ( NEC_Code_Mark && (TMR1 < 10000) ); // wait for end of mark pulse or timeout

  if( TMR1 >= 10000 ) // timeout
    return 3; 
  while ( NEC_Code_Space && (TMR1 < 10000) ); // wait for end of space or timeout

  TMR1_val = TMR1;
  if( TMR1_val >= 10000 ) // timeout
    return 3; // Error code
  else if ( (TMR1_val > 1000) || (TMR1_val < 1500) ); // Check for 1.125 mSec nominal

    // Zero detected
  else if ( (TMR1_val >= 1500) || (TMR1_val < 2500) ) // Check for 2.250 mSec nominal
    // One detected
    ir_code_INT |= 1; // Set LSB
  else
    return 4; // Error code
  if( i < 31 )

    ir_code_INT <<=1; // Shift into next MSB and prepare for next bit

  else
    return ir_code_INT;
  }


 
DougRice
Super Member
  • Total Posts : 533
  • Reward points : 0
  • Joined: 2008/10/08 23:44:59
  • Location: 0
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 00:50:49 (permalink) ☄ Helpfulby eagle1 2018/07/30 02:00:15
+1 (1)
Thanks for all the comments. I wanted to learn how to use the CCP compare modes. It is not obvious from the data sheet how to use it.
There are good application notes on microchips web site.
 
Software adds jitter to the timings as it loops. The CCP module allows this jitter to be minimised.
 
When using TMR1 your set it to count, and when it gets to 0xFFFF it wraps round to 0.
 
It is a bad idea to use software to adjust TMR1, as this adds jitter.
 
I learnt that you have to keep CCPR1 ahead of the value in TMR1 by adding the number of ticks to the next match to CCPR1, using:
 
CCPR1 += nextTimeOut;
or
CCRP1 = CCPR1 + nextTimeOut;
 
When TMR1==CCPR1 it generates a pulse which SETS or CLEARS the pin depending on the mode, so you have to toggle the mode.
 
The diagram shows a latch.
 
You can turn off the CCP and turn it back on to reset the output, but there will be jitter added to the pulse width.
 
If you do not want to do 16 bit sums use CCRP1H. If you only add to CCPR1L, it is unlikely to be ahead of the value in TMR1, so you have to wait for TMR1 to wrap around and count up to the value in CCPR1.
 
Printing out decimal numbers takes a lot longer than printing out HEX numbers like 0xFFFF.
 
I read an article about electric motors. You have a rotating bit and a static bit. Sometimes it helps to take out the "rotation" from the maths to simplify things!
 
Here we have TMR1 that is "rotating" and CCPR1 needs to be kept ahead of it, but adding the next delay to it.
 
many thanks everybody.
 
jack@kksound
code tags!
  • Total Posts : 3203
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 07:45:34 (permalink)
0

 
 
According to the diagram I think the MSB is on left and LSB on right, like all data format.

Your statement does NOT follow from the diagram shown as the diagram plainly states that the LSB is "on the left" of the diagram (LSB transmitted first). Or maybe are you looking at everything in a mirror?wink: wink
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:46:11 (permalink)
+1 (1)
jack@kksound
Your statement does NOT follow from the diagram shown as the diagram plainly states that the LSB is "on the left" of the diagram (LSB transmitted first). Or maybe are you looking at everything in a mirror?wink: 

Neither OP's code nor Paul's code follows the LSB first.
 
Edit: Also, I'd use READTIMER1() instead TMR1.
post edited by 1and0 - 2018/07/30 08:50:02
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:48:56 (permalink)
0
1and0
jack@kksound
Your statement does NOT follow from the diagram shown as the diagram plainly states that the LSB is "on the left" of the diagram (LSB transmitted first). Or maybe are you looking at everything in a mirror?wink: 

Neither OP's code nor Paul's code follows the LSB first.


But how? You provided to me the decrementing for loop! Do you mean that there's another more formal way to do the decoding?
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:51:26 (permalink)
+1 (1)
eagle1
But how? You provided to me the decrementing for loop! Do you mean that there's another more formal way to do the decoding?

The first data bit read is the LS bit of the 32-bit word. You can use whatever loop you like, but I'd do it in an efficient way. There is always more than one way to skin a cat.
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:54:22 (permalink)
0
How? Because in the function I did for decoding, the only way worked is by starting from the MSB. You also the first one who provided the decrementing for loop. Do you mean that applying the decrementing for loop is one way and there's a better way?
 
If so I would like to know it :)
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:55:56 (permalink)
+1 (1)
1and0
There is always more than one way to skin a cat.

Wait a minute! .. this part is so funny LOL
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 08:58:33 (permalink)
+1 (1)
eagle1
How? Because in the function I did for decoding, the only way worked is by starting from the MSB.

If you have to ask how to shift or place bits inside a variable with the least significant bit first, then you will need a lot more helps than I can provide here. Why MSb is the ONLY way? Why can't it be LSb first?
 

You also the first one who provided the decrementing for loop. Do you mean that applying the decrementing for loop is one way and there's a better way?

I suggested that decrementing ONLY as a replacement for your code, which involved a subtraction, to avoid the additional subtraction operation.  It was not based on the LS bit first.
 

If so I would like to know it :)

As already said, the data is coming in LS bit first, so store it as such.
post edited by 1and0 - 2018/07/30 09:10:59
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 09:15:18 (permalink)
0
1and0
If you have to ask how to shift or place bits inside a variable with the least significant bit first, then you will help a lot more helps than I can provide here. Why MSb is the ONLY way? Why can't it be LSb first?

 Oh, I actually didn't have a clear idea about NEC framing ... I still didn't read a file or a website explaining how NEC works unless a website explaining the timing of the mark and the space. But not an explanation of which bit is first LSB or MSB.
 

I suggested that decrementing ONLY as a replacement for your code, which involved a subtraction. It was not based on the LS bit first.

But did you have an idea about the NEC fram that it's in a way that the first bit is the LSB? Or you just figuring out a more efficient way for my code regardless of whether it's MSB or LSB.
 

 
As already said, the data is coming in LS bit first, so store it as such.

But how would I know that it's a LSB first? By the code demonstration, I believe that it's the MSB first because the incrementing for loop didn't work. So I either have only two solutions:
1. Apply a decrementing for loop:
 
 for(i=31;i>=0;i--){ // 2.25ms & 1.25ms test
 TMR1 = 0;
    while ((!PORTDbits.RD2) && (TMR1 < 800));
    if ((TMR1 > 800) || (TMR1 < 300))
  return 3;
    TMR1 = 0;
    while ((PORTDbits.RD2) && (TMR1 < 2000));
    if ((TMR1 > 2000) || (TMR1 < 400))
        return 4;
    if((TMR1 < 700) && (TMR1 > 400))
        ir_code_INT &= ~(1<<i);
    else if((TMR1 < 2000) && (TMR1 > 1000))
        ir_code_INT |= (1<<i);
    }

 
2. Do the subtraction:
 for(i=0;i<32;i++){ // 2.25ms & 1.25ms test
 TMR1 = 0;
    while ((!PORTDbits.RD2) && (TMR1 < 800));
    if ((TMR1 > 800) || (TMR1 < 300))
  return 3;
    TMR1 = 0;
    while ((PORTDbits.RD2) && (TMR1 < 2000));
    if ((TMR1 > 2000) || (TMR1 < 400))
        return 4;
    if((TMR1 < 700) && (TMR1 > 400))
        ir_code_INT &= ~(1<<(31 -i));
    else if((TMR1 < 2000) && (TMR1 > 1000))
        ir_code_INT |= (1<<(31 -i));
    }

jack@kksound
code tags!
  • Total Posts : 3203
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 09:18:18 (permalink)
+2 (2)
But how would I know that it's a LSB first?

From the diagram YOU provided in your posts. That is how it is labeled.
1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 09:26:59 (permalink)
+2 (2)
eagle1
 Oh, I actually didn't have a clear idea about NEC framing ... I still didn't read a file or a website explaining how NEC works unless a website explaining the timing of the mark and the space. But not an explanation of which bit is first LSB or MSB.
 
As Jack said, it is from the diagram YOU posted here.  Perhaps this one will be more explicit:
 
 
 

But did you have an idea about the NEC fram that it's in a way that the first bit is the LSB? Or you just figuring out a more efficient way for my code regardless of whether it's MSB or LSB.

Like I said before, it was suggested as a better code than yours. It was NOT suggested basing on MSb or LSb first.
 

But how would I know that it's a LSB first?

See above diagram.

Attached Image(s)

1and0
Access is Denied
  • Total Posts : 9607
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/30 09:30:26 (permalink)
+1 (1)
eagle1
So I either have only two solutions:
 

Both your solutions do MS bit first. :(
Page: << < ..111213 > Showing page 12 of 13
Jump to:
© 2019 APG vNext Commercial Version 4.5