Better understanding CCP module of PIC MCU, looking for real world examples - Page 1
Forum section: electronics / beginners

author: sairfan1
author_rank: Frequent Contributor
timestamp: March 21, 2018, 08:46:08 pm
content: Hi, I'm an electronics hobbyist, to learn CCP module i gone through data sheet by microship for 18f46k22 mcu i also read application notes for CCP modules by microchip,I wrote some program for PWM and was able to control brightness of LED, speed of DC motor and generate square wave on oscilloscope, but unfortunatly i did not find enough about Capture and Compare of CCP module.As I have been read about CCP I understand how to setup but for better understanding of Capture and Compare modules I want to know real examples like i did for PWM so that i can understand it better.I want to know what kind of advatages we can get from these modules in what kind of applicationsThanks.

author: kony
author_rank: Regular Contributor
timestamp: March 21, 2018, 09:18:06 pm
content: What is there not to understand? It takes binded timer value snapshot and saves it to its register at choosen event. Can invoke interrupt if you enable it.Measuring pulse period is real world example, timestamping some external event occurence another (with obvious implications for selected timer prescaler - if your event needs longer span than what the timer provides and/or periodic one, you have to add SW postscaler diven by the timer interrupt service routine).

author: sairfan1
author_rank: Frequent Contributor
timestamp: March 22, 2018, 02:47:51 pm
content: QuoteI need this kind of guidance, as i described in my question i know how to setup CCP but i need help regarding what i can develop from it, for exaple with pwm i was able to set brightness of LED and speed of DC motor.I want to know in what kind of applications I can use Capture and Compare. Thanks.

author: kony
author_rank: Regular Contributor
timestamp: March 22, 2018, 03:00:20 pm
content: Say you have a fans with tacho signal attached to your board. You want to know its speed. How will you do that with minimum impact on the rest of the firmware execution? For sake of example let's say gated timers are already used for something else, you have four fans and you have only CCP modules left. Another example would be recieving of standardised hobby model PWM signal and using the duty cycle as command variable for whatever purpose - how do you recover that information without inefficient SW polling?

author: hli
author_rank: Frequent Contributor
timestamp: March 22, 2018, 03:03:04 pm
content: Quote from: sairfan1 on March 22, 2018, 02:47:51 pmE.g. a frequency counter: the counter counts your input signal, and the capture event is created from your gate counter (e.g. a 1Hz signal).

author: kony
author_rank: Regular Contributor
timestamp: March 22, 2018, 03:07:14 pm
content: Frequency counter is exactly the case where you want to use gated timer module bundled together with second timer for a timebase. CCPs are fine for the less demanding usecases as I mentioned earlier.

author: sairfan1
author_rank: Frequent Contributor
timestamp: March 22, 2018, 06:46:06 pm
content: Thanks, these examples looks good.what i understand these are more related to Capture? or compare will be equally used in these examples,

author: kony
author_rank: Regular Contributor
timestamp: March 22, 2018, 07:24:19 pm
content: All regard capture mode so far.http://ww1.microchip.com/downloads/en/AppNotes/00594B.PDF

author: Buriedcode
author_rank: Super Contributor
timestamp: March 22, 2018, 09:58:02 pm
content: I'm still unsure exactly what you are asking, and I'm sure others have provided explanations in their own language.Capture - "captures" (copies) the value of a free running timer for a given event. This event is usually a rising or falling edge on an IO. This means you can detectwhenthe edge occurred because it stores a snapshot of the timer value, and gives your software time to read it, and perhaps use it to measure the period of an input (measuring speed, period, timed events). For repetitive signals (where the period doesn't change often) it has an input divider of 4 or 16. This means you can time (using the timer) how long it takes for the input to have a rising/falling edge 16 times - essentially averaging the result, but also increasing resolution.Compare - constantly "compares" the value of the timer to a fixed value you provide. Once the values match you can choose what happens - usually a pin is driven high, low, or toggles (changes state). I believe PIC's also have an event system, where the compare match drives a special event trigger - this resets the timer, and can also start and ADC reading. This is very handy for generating arbitrary frequencies, generating a "tick", triggering an ADC reading for precise ADC sampling periods. The fact the special even trigger resets the timer means you are essentially counting up to a specific value, then resetting, meaning you can quite accurately set this period. Essentially a programmable timer that, combined with the prescalers can generate timing signals accurate to 4/Fosc seconds. (for a 10MHz clock, thats a resolution of 400ns).Both the above can be done just with software and a timer, but you would have to poll the inputs, or poll the timer constantly, meaning the MCU can't do anything else. Like all peripherals, they are designed to off-load repetitive simple tasks to hardware, freeing up the CPU (or rather give it time to service interrupts without missing inputs). This is because MCU's are mostly single core, and can only do one thing at a time. Peripherals help it multitask by performing time-sensitive functions in separate hardware to be serviced later.

author: sairfan1
author_rank: Frequent Contributor
timestamp: March 23, 2018, 07:14:09 pm
content: Thanks for nicly describe, what kind of hobby projects you can suggest to build with CCP, i have already done some with PWM so you please advise some projects/or applications that can be done with the help CCP as @Kony and @hli describedthanks again.