So when timer1 reaches the tick number 12500, it is 20 ms! this is the reference value we will use in our software.
so for 20 ms, what is the number of ticks required? So 104.9 ms matches the final tick which is 65535 (because it has 16-bit resolution). Lowest timer1 frequency (as calculated before) = 9.53 Hz.
CCP will set its interrupt flag.Ĥ- inside interrupt service routine, reset CCP interrupt flag, load CCP registers with the "off-time" value, and set "0" at the output pin.Īlong all that, we must use a variable that holds the number of ticks that corresponds to 20 ms full period. All of this will happen inside the code.ġ- load timer1 with required values to generate the longest period it could (104.9 ms).Ģ- load the "on-time" value inside CCP module's register, put "1" at the output pin (RC2), and start timer1.ģ- timer1 will start counting until it reaches the value inside CCP registers (matches the on-time). We will use timer1 along with CCP module operated as "Compare, trigger special event". Q: Ok! connect it to CCP module in its PWM mode!!!Ī: No, CCP as PWM only uses timer2 module. So we conclude that timer1 can be used to generate 50 Hz signal because it can generate low frequencies up to 9.53 Hz and our 50 is just so much achievable! What about timer1? it has a maximum prescaler value of 8 and resolution of 16-bits = 65536 because it is a 16-bit timer. Thus we can not get the 50Hz signal that we wanted using this timer with CCP1 as PWM. So the lowest ever frequency timer2 can deal with (assuming 20MHz crystal oscillator) = 20000000 / (4 * 16 * 256) = 1220.7 Hz (approx 81.9 us period). It offers prescaler values up to 16 and this prescaler is a feature in timers that allows them to divide input frequency fed into them in order to achieve lower frequencies.
If it is less than 50Hz, then we can generate 50Hz or we will find another way.Įquation 2 is obvious, which is the period of that frequency.Įquation 3 is the number of "ticks" that the timer will tick until it reaches the time "t" that we want, according to the settings we specified like prescaler and crystal frequency.ĬCP1 module when operated as PWM uses timer2, and this timer2 is an 8-bit timer. We will use it to determine the lowest possible frequency that the timer can produce. N = (t * F_crystal) / (4 * prescaler)Įquation 1 calculates timer frequency. F_timer = F_crystal / (4 * prescaler * resolution) So how can we determine all necessary values to achieve our own required frequency? When it is configured as PWM, it uses Timer2 as a timing module in order to calculate its Ton and Toff. For the typical application, we always should configure it to be PWM (this is the straightforward way which we must use by default). PIC MCUs have something called CCP module which is Capture-Compare-PWM module, meaning, you can configure it to work as one of these 3. However, you can use the straightforward way (CCP as PWM) if you have a very slow oscillator like 32 KHz, because then timer2 can achieve 50 Hz directly without the need of what we will explain here. This tutorial is for those who has such very high oscillators which is the standard way of using microcontrollers. We will operate this PIC 20MHz oscillator.