# Timer 2 PIC16F690

Thread Starter

#### Cosmyn99

Joined Jan 8, 2022
4
Hello,

I am sorry to bother you, but after some days staring at this problem I wanted to ask for help.
I have some homework, most of wich I have already done, but the I got to the timer part and I have no ideea how to solve this.

So I want to ask for some experts help.
Task: Generate a PWM signal, with 8Hz frequency and duty cycle of 20%, connected on output led(pin DS3/RC2), with the help of timer2 register.

I have no ideea where to begin with this.
Thank you for all of you time.

#### JohnInTX

Joined Jun 26, 2012
4,654
Have you read chapter 11 in the databook? It describes in detail the PWM and how to generate an output using TIMER 2 as the time base.

Thread Starter

#### Cosmyn99

Joined Jan 8, 2022
4
Have you read chapter 11 in the databook? It describes in detail the PWM and how to generate an output using TIMER 2 as the time base.
You mean the databook for PIC16F690?

Thread Starter

#### Cosmyn99

Joined Jan 8, 2022
4
You mean the databook for PIC16F690?
The thing is I kinda understand how timer 2 works on paper, but I have no idea how to make it work in code. I am a beginner in C programming.

#### Ian Rogers

Joined Dec 12, 2012
988
Yes! He means chapter 11.3 of the datasheet...

8 hz isn't very fast do you mean 8Khz.. Timer two only has 1:1 , 1:4 and 1;16 prescalers.

All you need to do is set the timer pre-scale set the PR2 register and the timer post scaler and switch it on.

Thread Starter

#### Cosmyn99

Joined Jan 8, 2022
4
Yes! He means chapter 11.3 of the datasheet...

8 hz isn't very fast do you mean 8Khz.. Timer two only has 1:1 , 1:4 and 1;16 prescalers.

All you need to do is set the timer pre-scale set the PR2 register and the timer post scaler and switch it on.
I read that chapter of the datasheet, but I dont get how to do the setup. The frequency is 8Hz

#### sagor

Joined Mar 10, 2019
671
What clock frequency is the PIC running at? No one can calculate the PWM values without knowing the clock.
For standard 4Mhz or higher, there is no "standard" PWM solution for 8Hz. If running at a low clock like 32Khz, you can get 8Hz with:

Code:
/*
* PWM registers configuration
* Fosc = 32000 Hz
* Fpwm = 8.00 Hz (Requested : 8 Hz)
* Duty Cycle = 20 %
* Resolution is 10 bits
* Prescaler is 4
* Ensure that your PWM pin is configured as digital output
* see more details on http://www.micro-examples.com/
* this source code is provided 'as is',
* use it at your own risks
*/
PR2 = 0b11111001 ;
T2CON = 0b00000101 ;
CCPR1L = 0b00110001 ;
CCP1CON = 0b00111100 ;
See online PWM calculators:
https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html

#### sagor

Joined Mar 10, 2019
671
Other possible solution is to set interrupt with Timer2 at a higher rate, like 40Hz, and count the timer intervals and turn on the output 20% of the time, then wait until 125ms is up and restart the cycle count. Still, interrupt rate is dependent on clock rate...

#### Ian Rogers

Joined Dec 12, 2012
988
I would do the same...
estimate a resolution say 8 bit...
Set a duty between 0 ~ 255 . F
Set a pin low.
Fire the timer every 488uS and count each interrupt to 255 comparing against the duty
when its equal set the pin high...

Repeat..