AnsweredHot!Difference between capture and compare modes with examples

Page: 12345.. > >> Showing page 1 of 13
Author
eagle1
Super Member
  • Total Posts : 309
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
2018/07/11 03:52:59 (permalink)
0

Difference between capture and compare modes with examples

Hello,
 
I want to prepare a demo for capture and compare modes; e.g. PIC18F4550.
 
I searched a lot trying to understand these two modes; for example:
https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-assembly/ccp-modules

 
This link has good explanation for the PWM, but there's no pictures of the actual timing and how the MCU deal with the capture and compare modes. But there are some good pictures for the PWM mode.
 
PWM is simple, but capture and compare are ambiguous to me.
 
Also, I would like to understand what applications use these two features.
post edited by eagle1 - 2018/07/20 21:39:45
#1
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/11 06:10:12 (permalink) ☄ Helpfulby eagle1 2018/07/11 06:21:34
+6 (6)
Have you read the PIC18F4550 datasheet?
"Capture" mode saves the current timer value into the CCP registers when a specific event occurs (usually a transition on the CCP pin).
"Compare" mode continually compares the value in the CCP register with the timer value, and triggers an interrupt or a transition on the CCP pin when the two values match.
 

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"
#2
eagle1
Super Member
  • Total Posts : 309
  • 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/11 09:01:47 (permalink)
0
qɥb
Have you read the PIC18F4550 datasheet?

Yes, and other websites, but also there are no much videos on YouTube. There's one video from Microchip but the explanation is on a block diagram which is not so much helpful. I think these two modes need signals diagrams and some real life examples. Because until now I don't know where to use these modes! What are their benefits.
 

"Capture" mode saves the current timer value into the CCP registers when a specific event occurs (usually a transition on the CCP pin).

OK, here are my assumptions about this mode:
1. The value is either 8 or 16-bits.
2. It's of course related to timing, so the timer starts to count in timer mode with one of the following timers:
----------------------------------------------
|   CCP/ECCP Mode Timer Resource        |
----------------------------------------------
|   Capture                Timer1 or Timer3  |
|   Compare              Timer1 or Timer3  |
|   PWM                    Timer2                 |
----------------------------------------------
until an event occurs. For example, capturing a time period; ms, s or minutes, by counting timer overflows. But in this case it won't save a value which maybe bigger than 16-bits value after overflowing at least couple times! So that's why I don't understand how capture works.
 

"Compare" mode continually compares the value in the CCP register with the timer value, and triggers an interrupt or a transition on the CCP pin when the two values match.



Also the same here I don't know what are the specific goals of capture compare modes!
 
What events I'm looking for here?
#3
jack@kksound
code tags!
  • Total Posts : 2842
  • 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/11 09:29:30 (permalink) ☄ Helpfulby eagle1 2018/07/11 17:03:30
+2 (2)
Try this link: http://microchipdeveloper.com/ and put ccp in the search box.... you should get 3 pages of links to lots of detail on how the capture/compare/pwm modules work and are configured.
#4
PStechPaul
Super Member
  • Total Posts : 2063
  • 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/11 10:30:02 (permalink) ☄ Helpfulby eagle1 2018/07/12 03:39:45
+3 (3)
I have never used capture or compare, so I also would like to know more and see specific examples. The links in the developer site are rather sparse. I found the following:
 
http://microcontrollerslab.com/ccp-module-pwm-pic-microcontroller/
 
http://www.circuitstoday.com/ccp-modules-in-pic-16f877
 
https://www.wikinote.org/Main/Savitribai-Phule-Pune-University/ENTC/MA-TE/Unit-5/CCP-Module/
 
http://www.electronicwings.com/pic/pic18f4550-timer-capture (Explains capture mode for reading frequency or duty cycle of an external signal)
 
http://tahmidmc.blogspot.com/2012/12/modalities-of-using-pic-16f877a-ccp_9.html (Explains compare mode as used to generate a PWM signal.)

 
#5
JorgeF
Super Member
  • Total Posts : 3299
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/11 10:50:14 (permalink) ☄ Helpfulby eagle1 2018/07/11 17:06:12
+2 (2)
Hi
 
In a nutshell.
 
Capture mode lets you measure time intervals of external events picked up by the CCP pin.
If the time gap of those events is too large, you can use the TIMER overflow interrupt to count rollovers and add it to the captured value.
 
Compare mode lets you generate regular time intervals in hardware, meaning, without software overhead or software induced skidding. Those regular intervals can be used to generate a signal to the outside world via the CCP pin, or used inside, either by triggering interrupts or by the "special event trigger" that can start actions in other peripherals.
 
How you use these features its up to you, I guess you know when they can be usefull when you face the need.
 
First time I saw these features beeing used was:
Capture mode: used to detect/decode a DTMF dialing signal.
Compare mode: Used to generate the PWM signal to control R/C servos.
 
 
HIH
post edited by JorgeF - 2018/07/11 10:53:31

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
#6
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/11 13:09:34 (permalink)
+3 (3)
eagle1

"Capture" mode saves the current timer value into the CCP registers when a specific event occurs (usually a transition on the CCP pin).

OK, here are my assumptions about this mode:
1. The value is either 8 or 16-bits.

No, it's always 16 bit, which is the size of the TIMER1/3 counters and of the CCPRxL/H registers.
 

2. It's of course related to timing, so the timer starts to count in timer mode with one of the following timers:
----------------------------------------------
|   CCP/ECCP Mode Timer Resource        |
----------------------------------------------
|   Capture                Timer1 or Timer3  |
|   Compare              Timer1 or Timer3  |
|   PWM                    Timer2                 |
----------------------------------------------
until an event occurs. For example, capturing a time period; ms, s or minutes, by counting timer overflows. But in this case it won't save a value which maybe bigger than 16-bits value after overflowing at least couple times! So that's why I don't understand how capture works.

You're coming at this from the wrong direction. It is the count value that is stored when the transition occurs.
This is most often used to get accurate timing of rapidly occurring events.
Yes, as others have mentioned, you need to count timer overflows (via a timer interrupt) as well if the events are spaced apart more than the time it takes for the selected timer to roll over.
 
I think the block diagram "FIGURE 15-1: CAPTURE MODE OPERATION BLOCK DIAGRAM" in the datasheet illustrates the operation of this mode fairly well.
 


"Compare" mode continually compares the value in the CCP register with the timer value, and triggers an interrupt or a transition on the CCP pin when the two values match.




Also the same here I don't know what are the specific goals of capture compare modes!
What events I'm looking for here?

As I already said, it lets you generate an interrupt after a precise length of time.
It's up to you to find an application where you need to do that.

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"
#7
eagle1
Super Member
  • Total Posts : 309
  • 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/11 17:14:20 (permalink)
0
I'm sorry I'm stuck in another problem away of capture/compare skills.
 
I can't get the proper result of timer 0 in PIC18F4550, I'm just running the timer in 8-bits mode and waiting it to overflow, and when TMR0IF is set, then I set a pin; e.g. LATDbits.LATD2=1;
 
I'm working in MPLAB simulator mode and monitoring the SFR registers and program memory.
 
When the timer 0 overflows, the program memory falls into NOP area, I know the reason could be so easy but I tried to solve it but I don't know I searched websites trying to figure out the problem.
 
Here's' my code:
 
I'm sorry I'm stuck in another problem away of capture/compare skills.
 
I can't get the proper result of timer 0 in PIC18F4550, I'm just running the timer in 8-bits mode and waiting it to overflow, and when TMR0IF is set, then I set a pin; e.g. LATDbits.LATD2=1;
 
I'm working in MPLAB simulator mode and monitoring the SFR registers and program memory.
 
When the timer 0 overflows, the program memory falls into NOP area, I know the reason could be so easy but I tried to solve it but I don't know I searched websites trying to figure out the problem.
 
Here's' my code:
 
// PIC18F4550 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_XT // Oscillator Selection bits (Internal oscillator, XT used by USB (INTXT))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>

#define _XTAL_FREQ 4000000
unsigned char counter = 0;
void main(void)
 {
    ADCON0=0x00;ADCON1=0x0f;CMCON=0x07;
    T0CON=0xC0;INTCON=0xE0;
    TRISD=0x00;TRISA=0xFF;TRISB=0x80;
    
   while (1)
   {
      if (PORTAbits.RA0==1)
      {
        LATDbits.LATD0=1;
        __delay_ms(10);
        LATDbits.LATD0=0;
      }

      if (INTCONbits.TMR0IF==1)
      {
        INTCONbits.TMR0IF=0;
        counter++;
        if (counter==2)
        {
            LATDbits.LATD2=1;
        }
      }
   }
 }

post edited by eagle1 - 2018/07/11 17:16:26
#8
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/11 17:33:37 (permalink) ☄ Helpfulby eagle1 2018/07/11 17:43:37
+2 (2)
Change
INTCON=0xE0;
to
INTCON=0;
 
You do NOT want interrupts enabled if you are just polling the bit.
What you did is tell the PIC to jump to the interrupt service vector when the timer rolls over. As you do not have an interrupt service routine there, it just crashes.
 

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"
#9
eagle1
Super Member
  • Total Posts : 309
  • 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/11 18:02:32 (permalink)
0
qɥb
Change
INTCON=0xE0;
to
INTCON=0;
 
You do NOT want interrupts enabled if you are just polling the bit.
What you did is tell the PIC to jump to the interrupt service vector when the timer rolls over. As you do not have an interrupt service routine there, it just crashes.
 


I think this answer to me is one of the best answers I got so far for my whole experience in embedded systems about interrupts, so if I'm only looking for the flag, then there's no need for ISR. Another question, is ISR only for interrupts not for only flags? 
#10
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/11 18:07:19 (permalink) ☄ Helpfulby eagle1 2018/07/12 03:36:25
+2 (2)
eagle1
 is ISR only for interrupts not for only flags? 

That's a strange question, but I guess the answer is Yes.
An ISR is an "Interrupt Service Routine".
It is a special function which is called if you have interrupts enabled, and an interrupt occurs.
 

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"
#11
eagle1
Super Member
  • Total Posts : 309
  • 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/12 03:39:21 (permalink)
0
qɥb
eagle1
 is ISR only for interrupts not for only flags? 

That's a strange question, but I guess the answer is Yes.
An ISR is an "Interrupt Service Routine".
It is a special function which is called if you have interrupts enabled, and an interrupt occurs.

Sorry didn't phrase the question more clearly, I mean what exactly triggers the ISR, is it the flags or the exact interrupt? For example, like for the timer case, if the timer interrupt is disabled. Can I in this case call the ISR with only the flag while the interrupt is disabled?
#12
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/12 03:47:50 (permalink) ☄ Helpfulby eagle1 2018/07/12 15:12:15
+2 (2)
Sorry, you are speaking nonsense.
You don't call the ISR. You should not try to call the ISR.
The hardware jumps to it if all the appropriate flags are set.
 

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"
#13
eagle1
Super Member
  • Total Posts : 309
  • 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/12 15:10:06 (permalink)
0
qɥb
Sorry, you are speaking nonsense.
You don't call the ISR. You should not try to call the ISR.
The hardware jumps to it if all the appropriate flags are set.
 


I know that! I know the program does this job. The ISR parameters are defined within the microcontroller's header file. And the values of each interrupt are connected to the hardware interrupt. So if any of the defined interrupt values is called, the microcontroller jumps to the a defined code to deal with the ISR.
If no code for ISR, the program crashes as did with me :) I want to tell that I understood all this until now.
I've dealt with ISR for UART library I was developing, so I know something about ISRs.
 
But my previous question was about the things that call the ISR, is it only the interrupts and not the flags or the ISR can be called with only flags with interrupts disabled? But I understood that the interrupt is the one which issues the ISR, flags doesn't do that. I actually asked the question before getting the full idea about the story of the process of handling interrupts so it wasn't a very smart question :)
 
 
 
Going back to the capture mode, I've read in the following link that capture is for measuring the PWM signal!
 
That's interesting and provides a lot of sense. So PWM generates pulse width signal and capture mode measures PWM signals. So my understanding before is that capture measures time periods, then I thought capture counts clocks, then I just read this link and it's much more better in understanding the capture mode.
 
https://letanphuc.net/2015/06/stm32f0-timer-tutorial-and-counter-tutorial/

In the 3rd part of the tutorial.
 
OK, I know now that capture is for measuring PWM signals, but can I use it to measure IR remote control signals? Because they don't come in one pule, it comes in bursts of pulses; like in following picture, one complete remote control button sends a complete command format like this one.
 
It's divided to my understanding to 6 parts. First two parts: 9ms high then 4ms low is the NEC protocol.
After that comes the address which is 8-bits, and rest is the same process. But my question here is that can I measure the address burst with capture mode? I think each bit is like 1.25us. 
 
 
Now, still the compare mode :)
#14
qɥb
Monolothic Member
  • Total Posts : 3329
  • 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/12 15:22:04 (permalink) ☄ Helpfulby eagle1 2018/07/12 15:52:46
+2 (2)
eagle1
...
Going back to the capture mode, I've read in the following link that capture is for measuring the PWM signal!

Note: You are reading a page about a totally different family of processors.
The author mentions one possible application of capture mode, which is to measure a PWM signal from somewhere else.
 

 OK, I know now that capture is for measuring PWM signals, but can I use it to measure IR remote control signals?

You are using very awkward language.
Capture is NOT just for "measuring PWM signals". That is just one possible application.
It is a general purpose facility for detecting precisely WHEN a transition on a signal happens.
Yes, using it to decode an IR signal is perfectly feasible.
 

But my question here is that can I measure the address burst with capture mode? I think each bit is like 1.25us. 

You can use the capture mode to detect precisely WHEN each transition occurs, and then by subtraction you can calculate the width of each pulse.
The peripheral simply detects WHEN the edge happens, everything else has to be done by code written by you.

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"
#15
JorgeF
Super Member
  • Total Posts : 3299
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/12 15:46:11 (permalink) ☄ Helpfulby eagle1 2018/07/12 15:54:23
+2 (2)
Hi
eagle1
But my previous question was about the things that call the ISR, is it only the interrupts and not the flags or the ISR can be called with only flags with interrupts disabled? But I understood that the interrupt is the one which issues the ISR, flags doesn't do that. I actually asked the question before getting the full idea about the story of the process of handling interrupts so it wasn't a very smart question :)

Take a look at figure 9-1 @ page 100 of the datasheet (PIC18F4550).
Its quite extense because the PIC18F have 2 prioritie level for interrupts, so everything is duplicated.
 
The "xxxIF" flags signal an event but to force the execution of the ISR (hardware call) this signal must reach the core of the micro (right of the picture).
In order for that to happen the event signal must be able to travel accross all the logical gates in its path.
To keep it simple look only at the top right section and ignore the priorities stuff (xxxIP).
Look at the TMR0IF flag.
When activated , logic "1", it must pass the first "AND" gate, meaning TMR0IE must be logic '1' (enabled) to allow the TMR0IF "1" to reach the "OR" gate that follows. A logic "1" allways passes an "OR" gate. The same for the next "OR" gate in the path.
Then comes the last "AND" gate. For the signal to pass this gate, the other input must be "1", so if GIE is "1" the signal from the TMR0IF finally reaches the micro and triggers the ISR.
If either TMR0IE or GIE are "0" (disabled) the signal is blocked from reaching the micro.
The remining of the diagram follows this logic for all the available "IF" sources and is duplicated due to the 2 priority levels. If you are not using priorities (IPEN = 0), just the upper half of the diagram aplies.
 
You can also see that the interrupt sources are divided into 2 groups.
The core interrupts (TMR0, INTx, ...)) and the peripherics (USB, ADC, MSSP, EUSART,...)  interrupts.
For the peripherics interrupts tehre is an aditional barrier, the PEIE/GIEL bit.
 
In a nutshell.
The "IF" flags are allways activated by the events they signal.
Then you choose if you want your code to use poling, or interrupt to handle those events.
If you want to use poling, leave the "IE" bits at "0", if you want to use an ISR to process the events, enabled the "IE" flag, the "GIE" flag and thE "PEIE" if its an event from a peripheral.
 
The complexity of it might look a bit unjustified at first sight, but this organization allow you to create any mix of poling and ISR handling for each interrupt source in use.
For example, in teh case of the EUSART, its quite common to use interrupt driven processing for RX and poling for t.
 
hih
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
#16
JorgeF
Super Member
  • Total Posts : 3299
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/12 15:57:14 (permalink) ☄ Helpfulby eagle1 2018/07/12 16:02:12
+3 (3)
Hi
eagle1
OK, I know now that capture is for measuring PWM signals, but can I use it to measure IR remote control signals? Because they don't come in one pule, it comes in bursts of pulses; like in following picture, one complete remote control button sends a complete command format like this one.

Like qyb said. Yes.
 
I think you are mixing the "screw driver" and the "screw".
The "screw driver" is a tool that drives "screws".
It doesn't matter if  the screw belongs to a car engine, a door hinge or a chair. You can use the same "screw driver".
 
PWM its not only for driving electrical motors, driving R/C servos, or generate waveforms. It can be used for all of that and for a lot more of other applications.
The same with "Capture" or "Compare".
 
HIH
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
#17
eagle1
Super Member
  • Total Posts : 309
  • 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/12 16:22:17 (permalink)
0
qɥb
Note: You are reading a page about a totally different family of processors.
The author mentions one possible application of capture mode, which is to measure a PWM signal from somewhere else.

Yes I know it's a STM32 microcontroller, but what was interesting to me was the idea of a clear application of capture mode, you know when I read that section it was clear right away that in CCP module I can generate PWM signals with PWM mode and measure a received PWM signal with capture mode.
 
What's more interesting is that you're saying that this is only one possible application of capture mode where I think I realized the only application of capture mode :)
 
It was difficult enough to understand the main idea of this mode, but where I can learn the other applications of this mode?
 

It is a general purpose facility for detecting precisely WHEN a transition on a signal happens.
Yes, using it to decode an IR signal is perfectly feasible.

 
OK, "detecting precisely WHEN a transition on a signal happens" now I'm trying to understand this sentence as I can, so you mean by "transition" is when a signal goes from HIGH to LOW then LOW to HIGH, and vise versa for one complete pulse?
 
But detecting a transition is also the main application for "pin change interrupts" which do the same, I think you mean more things. Like, you also mentioned "detecting precisely WHEN", how when?
 

You can use the capture mode to detect precisely WHEN each transition occurs, and then by subtraction you can calculate the width of each pulse.
The peripheral simply detects WHEN the edge happens, everything else has to be done by code written by you.

 
Does it mean that "WHEN" is the period of the pulse?



#18
eagle1
Super Member
  • Total Posts : 309
  • 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/12 16:38:28 (permalink)
0
JorgeF
Hi
Take a look at figure 9-1 @ page 100 of the datasheet (PIC18F4550).
Its quite extense because the PIC18F have 2 prioritie level for interrupts, so everything is duplicated.
 
The "xxxIF" flags signal an event but to force the execution of the ISR (hardware call) this signal must reach the core of the micro (right of the picture).
In order for that to happen the event signal must be able to travel accross all the logical gates in its path.
To keep it simple look only at the top right section and ignore the priorities stuff (xxxIP).
Look at the TMR0IF flag.
When activated , logic "1", it must pass the first "AND" gate, meaning TMR0IE must be logic '1' (enabled) to allow the TMR0IF "1" to reach the "OR" gate that follows. A logic "1" allways passes an "OR" gate. The same for the next "OR" gate in the path.
Then comes the last "AND" gate. For the signal to pass this gate, the other input must be "1", so if GIE is "1" the signal from the TMR0IF finally reaches the micro and triggers the ISR.
If either TMR0IE or GIE are "0" (disabled) the signal is blocked from reaching the micro.
The remining of the diagram follows this logic for all the available "IF" sources and is duplicated due to the 2 priority levels. If you are not using priorities (IPEN = 0), just the upper half of the diagram aplies.
 
You can also see that the interrupt sources are divided into 2 groups.
The core interrupts (TMR0, INTx, ...)) and the peripherics (USB, ADC, MSSP, EUSART,...)  interrupts.
For the peripherics interrupts tehre is an aditional barrier, the PEIE/GIEL bit.
 
In a nutshell.
The "IF" flags are allways activated by the events they signal.
Then you choose if you want your code to use poling, or interrupt to handle those events.
If you want to use poling, leave the "IE" bits at "0", if you want to use an ISR to process the events, enabled the "IE" flag, the "GIE" flag and thE "PEIE" if its an event from a peripheral.
 
The complexity of it might look a bit unjustified at first sight, but this organization allow you to create any mix of poling and ISR handling for each interrupt source in use.
For example, in teh case of the EUSART, its quite common to use interrupt driven processing for RX and poling for t.
 
hih

Thank you so much for this explanation, now I understand that diagram much more better, and in this thread I learned so much about the core of the microcontroller.
 
Interrupts are much clear to me now. So I know:
1. With flags only, I can use polling in the software.
2. Two types of interrupts; core and peripheral interrupts.
3. Enabling GIE must be with the specific interrupt.
4. When multiple interrupts occur, the microcontroller process the first one. But I think the second one could be lost because the CPU is busy processing the first one. Or the CPU may pend the second interrupt until it finish processing the first one.
#19
JorgeF
Super Member
  • Total Posts : 3299
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/12 16:47:08 (permalink) ☄ Helpfulby eagle1 2018/07/12 18:14:24
+2 (2)
Hi
eagle1
But detecting a transition is also the main application for "pin change interrupts" which do the same, I think you mean more things. Like, you also mentioned "detecting precisely WHEN", how when?

The capture mode detects the transitions and at the same time gives you a precise indication of the time interval between 2 transitions. Or, in other words, measures the duration of a pulse or the inetrval between pulses.
IOC detects changes (transitions) on a pin but gives no measurement of the time gap between changes.
Common INT pins also detect transitions, but again gave no information of the time between transitions.
 
The precision, as mentioned by qyb is in the measurement of the time between 2 transitions, as it is done by hardware it doesn't suffer from errors or skiddinbg due to program execution times and also saves you from complex timming routines in your code.
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
#20
Page: 12345.. > >> Showing page 1 of 13
Jump to:
© 2018 APG vNext Commercial Version 4.5