Dutycyle ....accuracy

Status
Not open for further replies.

Asterix101

New Member
hello there, i'm wiriting a code in assembly language using PIC16F877A, my input is a sequence of pulses with variable dutycycle from an acceleromter to 1 pin , but i'm having an issue calculating an exact dutycycle for the input, (i.e starting the measurement from the rising edge)...thx
 
We do not know enough about your code to give help specific to your case.
 
I just have a squence of square pulses, having a certain period and frequency, which will be inputted to a pin of the Microcontroller..... I just want to know how can I calculate the dutycycle for this input, other that this example:

while RC0=0
toff=toff+1
wend
while RC0=1
ton=ton+1
wend

dutycycle=ton/(ton+toff)

The thing is that I want to calculate the dutycycle, but starting from the rising edge of the pulse sequence over a period...
 
Last edited:
You're going to use the same principle, but using Timer1.
Should be something like this:

1. Wait for low-to-high transition.
2. Start timer1, being set for the period you're expecting.
3. Wait for high-to-low transition.
4. Stop timer and store value.
5. Timer value divided by the [known] period is the duty cycle.

-Mike
 
You need to add an additional wait at the start of your code,

while RC0=1 //wait for previous pulse to end
wend
while RC0=0
toff=toff+1
wend
while RC0=1
ton=ton+1
wend

It will mean that it takes two cycles to read the value but it should be more accurate.

For better accuracy use one of the hardware timers or the PWM module in capture mode.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…