Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Is it to use Timer2 and PWM both at same time, 16F877A

Status
Not open for further replies.

wolf12

Advanced Member level 4
Joined
Mar 31, 2011
Messages
109
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
2,102
Is it ok to use Timer2 and PWM both at same time, 16F877A

Datasheet says PWM uses Timer2. If I program to erase Timer2 and clear overflow bits while microcontroller is running, will it affect PWM?
 

Yes, the Timer2 value is used to compare with the CCPR1H and PR2 values during PWM generation.

So if you are using the PWM feature of the PIC, the Timer2 value must remain valid.

BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating
Yes, the Timer2 value is used to compare with the CCPR1H and PR2 values during PWM generation.

So if you are using the PWM feature of the PIC, the Timer2 value must remain valid.

BigDog

I was trying to make motor encorder with rpm measurment. For that I thought to put the encoder sensor to timer0 and wait till 255 pulses, and then by using timer2 value to get RPM. Now that timer2 is not suitable since I'm using pwm too, I thought to use Timer1. But im stuck because its a 16bit counter. Im using mikroC.
I get RPM by
rpm = 255/pulsesPerRound/(65535*tmr1Overflows+TMR1)/20000000*60;
Theres no TMR1 identifier, which means I have to access 2 bytes of TMR1 separately right?
Can you tell me an alternative please?
 

So you are attempting to drive the motor with PWM and calculate its current RPM at the same time on the PIC16F877A, correct?

What is the maximum PWM frequency and duty cycle? Also what is the maximum RPM of the motor?

BigDog
 

So you are attempting to drive the motor with PWM and calculate its current RPM at the same time on the PIC16F877A, correct?

What is the maximum PWM frequency and duty cycle? Also what is the maximum RPM of the motor?

BigDog

Correct. Maximum PWM is 5KHz, Maximum duty cycle is 100%, I dont know the maximum RPM of the motor :-|
 

What is your system clock or crystal frequency?

There are two CAPTURE/COMPARE/PWM MODULES on the PIC16F877A.

I would recommend manually generating the PWM using one CCP module and then use the other CCP module in capture mode to calculate RPM.

BigDog
 

What is your system clock or crystal frequency?

There are two CAPTURE/COMPARE/PWM MODULES on the PIC16F877A.

I would recommend manually generating the PWM using one CCP module and then use the other CCP module in capture mode to calculate RPM.

BigDog
Crystal clock is 20MHz.
 

By using both CCP modules to off load the PWM and Capture task from the PIC you have more than enough instruction cycles to control the motor.

BigDog
 

What is your system clock or crystal frequency?

There are two CAPTURE/COMPARE/PWM MODULES on the PIC16F877A.

I would recommend manually generating the PWM using one CCP module and then use the other CCP module in capture mode to calculate RPM.

BigDog
Crystal clock is 20MHz.

---------- Post added at 11:03 ---------- Previous post was at 10:01 ----------

By using both CCP modules to off load the PWM and Capture task from the PIC you have more than enough instruction cycles to control the motor.

BigDog

Can you give me example code? I can't find a library for ccp in mikroC.
 

The CCP modules can be setup to Capture, Compare and PWM (PWM).

I know MikroC has a PWM library, however I not sure if they have a Capture or Compare library.

You may have to write your own routines.

BigDog
 

The CCP modules can be setup to Capture, Compare and PWM (PWM).

I know MikroC has a PWM library, however I not sure if they have a Capture or Compare library.

You may have to write your own routines.

BigDog
Ok but how can I use this 16bit value? Lets say I programmed to capture every 16th rising edge. Elapsed time is stored in two seperate bytes. How can use that time? I mean I cant ignore the higher bits or lower bits right? How can get them to a single value?
 

Ok but how can I use this 16bit value? Lets say I programmed to capture every 16th rising edge. Elapsed time is stored in two seperate bytes. How can use that time? I mean I cant ignore the higher bits or lower bits right? How can get them to a single value?

Here is a project which uses a timer to count pulses from a IR sensor, basically a tachometer, and generates PWM to drive a motor:

PIC18 Pulse Width Modulation (PWM) DC Motor Speed Controller with the RPM Counter Project

It is not exactly the same design as your project, however many of the principles are the same.

Also I posted numerous examples in the following discussion thread:

some questions motor speed monitoring

Romel's project code for the tachometer technique of using Capture Mode is also posted in the above thread.

The above links should get you started in the right direction.

BigDog
 

From your first link,

volatile unsigned int rpm_value;
rpm_timer=TMR0L; // Get the first 8-bit TIMER0 Counter
rpm_timer+=(TMR0H << 8); // Get the last 8-bit TIMER0 Counter

What they have done is shifting 8 bits to left and adding it to rpm timer,
MikroC help says "<< bitwise shift left; moves the bits to the left, discards the far left bit and assigns 0 to the right most bit. "

I'm confused. Doesn't he loose the higher 8 bits?

---------- Post added at 20:21 ---------- Previous post was at 19:05 ----------

From your first link,

volatile unsigned int rpm_value;
rpm_timer=TMR0L; // Get the first 8-bit TIMER0 Counter
rpm_timer+=(TMR0H << 8); // Get the last 8-bit TIMER0 Counter

What they have done is shifting 8 bits to left and adding it to rpm timer,
MikroC help says "<< bitwise shift left; moves the bits to the left, discards the far left bit and assigns 0 to the right most bit. "

I'm confused. Doesn't he loose the higher 8 bits?

Ok i get it now, if you assign the higher bits to a 16bit varible no bites will be lost when shifting. It took alot of time to understand about this. :sad:
Thanks Anyway :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top