View previous topic :: View next topic |
Author |
Message |
danielz85
Joined: 22 Sep 2012 Posts: 37
|
several issues while calculating pulse width |
Posted: Tue Oct 23, 2012 9:40 am |
|
|
Hi,
I'm trying to measure the width of a pure sine wave.
I've configured COMP2-OUT to be TIMER1's GATE, such that I'm basically calculating the number of ticks of TIMER1 in a single sine-cycle using an interrupt routing of COMP2.
I'm buffering 30 of those calculations and then printing them out.
Code: | #include <16F785.h>
#fuses INTRC_IO, PUT, NOWDT,NOPROTECT, NOBROWNOUT, NOIESO, NOFCMEN, NOMCLR
#use delay(clock = 8M)
#use rs232(baud=4800,xmit=pin_A0,rcv=pin_A1)
#define arr_size 40
unsigned int i=0;
unsigned int k=0;
unsigned int16 t1[30]= {} ;
#INT_COMP2
void intcomp2_isr()
{
t1[i]=get_timer1();
i++;
set_timer1(0);
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_comparator(CP2_C1_C0 | CP2_T1_GATE | CP2_T1_SYNC); //Timer1 synced with rising edge of CMP2
SETUP_TIMER_1(T1_DIV_BY_1 | T1_INTERNAL | T1_GATE); //f=8/4=2MHz
delay_ms(5000);
printf("\n\r reset!");
set_timer1(0);
enable_interrupts(GLOBAL);
enable_interrupts(INT_COMP2);
while (TRUE)
{
if (i % 30 == 0)
{
disable_interrupts(INT_COMP2);
for (k=0 ; k<30; k++)
{
printf("%ld \n\r", t1[k]);
}
set_timer1(0);
enable_interrupts(INT_COMP2);
}
}
}
|
The issues I'm witnessing are as follows:
1. according to the calculation for a 8MHZ-INT-OSC (no presaclar), and a 10KHz sine, TIMER1 should count (8*10^6)/(4*10000)=200ticks
however, in the first 2-3 calculations I get "random" numbers, and in the following 28-27 calculations I get 186 or 185:
Quote: |
reset!42 157 186 186 185 186 186 186 185 186 186 185 186 186 185 186 186 185 186 186 185 186 186 186 185 186 186 185 186 186 reset!0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
Should I always ignore the first 2-3 cycles? Why is there a CONSTANT ~13ticks difference between the expected value and the one I get?
2. As can be noticed in the quote above (2nd reset), the program resets itself (goes to main) because of
Code: | enable_interrupts(INT_COMP2); |
Is that normal behavior?
Does the order of enabling/disabling interrupts matter?
Thank you very much for your kind help! |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Oct 23, 2012 10:32 am |
|
|
Several questions
1) What happens when i>30 ?
2) What are the actual frequencies of your internal clock and the sine wave?
3) How is the sine wave coupled to the PIC?
4) What is the sine wave amplitude?
5) How long does each line of the interrupt routine take?
6) Where are you on the sine wave when you first enable the interrupt?
Mike |
|
|
danielz85
Joined: 22 Sep 2012 Posts: 37
|
|
Posted: Tue Oct 23, 2012 11:05 am |
|
|
Hi!
I've changed t1:
from:
Code: | unsigned int16 t1[30]= {}
.
.
.
printf("%ld \n\r", t1[k])
|
to
Code: | unsigned int8 t1[60]= {}
.
.
.
printf("%u \n\r", t1[k])
|
and apparently the resetting issue got fixed... I have no idea how that's possible though....
however, the result remains the same (increased to 190):
Quote: | reset!41 148 190 189 190 190 189 190 190 190 189 190 189 190 190 189 190 190 190 189 190 190 189 190 190 189 190 190 189 190 190 189 190 190 190 189 190 190 189 190 190 189 190 190 189 190 190 190 189 190 190 189 190 190 189 190 190 190 189 190 1? |
2. the internal clock frequency is 8MHz and the sine is 10KHZ
3. it is coupled via a 3.55mm audio jack from the pc.
4. the amplitude is around 1v
5. How can I tell? Do you mean I should count the instruction number inside the ISR?
6. There's no way to tell, it's something I can't and not supposed to know.
Thanks again, |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Oct 23, 2012 12:45 pm |
|
|
The purpose of my questions was to make YOU think about what you are doing.
1) You never reset i. So where are you writing to after the first reset?
2) What is the tolerance on each of your frequencies?
3) Is it DC or AC coupled?
4) Where abouts on the sine wave are you gating?
5) What is the effect of the time taken to execute instructions in the ISR?
6) Your gating can't be synchronised on the first period but MAY subsequently.
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Oct 23, 2012 4:18 pm |
|
|
this is one of those instances where it REALLY matters what
you have for a CIRCUIT!!!
why not post your schematic?
more help may start to arrive if you do.
w/o that info - i can't begin to guess the extent of your problem.
BTW: what limits the MAX value of array index 'i'?
|
|
|
danielz85
Joined: 22 Sep 2012 Posts: 37
|
|
Posted: Wed Oct 24, 2012 1:21 am |
|
|
Hi,
Mike,
Quote: | What is the effect of the time taken to execute instructions in the ISR
|
Please explain specifically how to take this into consideration. Should I count the instruction number within the ISR routine?
Quote: | What is the tolerance on each of your frequencies?
|
Do you mean how precise they are?
asmboy,
There's really nothing special in the circuit,
Input of 1v pure sine wave is coming from the PC (wave generated using MATLAB).
I'm sampling it using the comparator, and using the comparator's output as input to TIMER1's GATE. producing a Square Wave. Then I measure it's width.
The only thing that limits it is my memory!
I need to leave some of it free for other calculations.
Thanks guys |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Oct 24, 2012 2:04 am |
|
|
These are all the same question asked in different ways.
Quote: | 1) What happens when i>30 ?
1) You never reset i. So where are you writing to after the first reset?
BTW: what limits the MAX value of array index 'i'? |
Quote: | Quote:
What is the tolerance on each of your frequencies?
Do you mean how precise they are? | Yes. You're using the INTERNAL oscillator. The data sheet tells you its tolerance. Look it up. I don't use MATLAB so you're on your own. Then you can work out what to expect for the error in your measurements (including, of course, +/- one count for the digital sampling error).
Quote: | 3) How is the sine wave coupled to the PIC?
why not post your schematic?
more help may start to arrive if you do. | We want to SEE exactly what you're doing.
Quote: | 5) What is the effect of the time taken to execute instructions in the ISR? | You've changed the size of i from int16 to int8. The results you're getting are different. Does that tell you anything? You're reading timer0, taking time to increment i and then reset timer0. Each of the processes is introducing timing errors. Try swapping the order of the increment i and reset timer0. See what effect that has on your readings.
Quote: | 6) Where are you on the sine wave when you first enable the interrupt? | Another reason for wanting a SCHEMATIC. How do you KNOW where you are on the waveform when the comparator triggers? Are you triggering near a peak, trough, midpoint, where? Is it important?
Mike |
|
|
danielz85
Joined: 22 Sep 2012 Posts: 37
|
|
Posted: Wed Oct 24, 2012 10:43 am |
|
|
Quote: | We want to SEE exactly what you're doing. |
like I said, nothing special:
a 3.55mm standard audio cable from PC into LowPinCountDemo board, into into
C1 C0 of COMPARATOR2 (Right audio channel into C1, ground into C2).
1V pure sine wave is generated using MATLAB and is input into the PIC.
That's as detailed as I can get.
I've noticed two new things:
I've changed:
Code: | setup_comparator(CP2_C1_C0 | CP2_T1_GATE | CP2_T1_SYNC);
SETUP_TIMER_1(T1_DIV_BY_1 | T1_INTERNAL | T1_GATE); |
to:
Code: | setup_comparator(CP2_C1_C0 );
SETUP_TIMER_1(T1_DIV_BY_1 | T1_INTERNAL);
|
and witnessed no change at all!!
does it mean that the COMPARATOR output was never SYNCED TO TIMER1's gate? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Oct 24, 2012 11:23 am |
|
|
OH - the "hardware" is being fed with the MATLAB "sine wave " generator?
looks like my job is all done here .
so this just a simulation right?
if it WAS real - your comparator hookup is guaranteed to be flaky based on your lack of understanding of HARDWARE circuit concepts.
( switching with GROUND as a reference and no DC restoration/ pedestal voltage is NOT a way to get symmetric,drift free , zero crossings detected)
i'll check back when you have actual circuitry, and
that all important clue as to how to do this for REAL
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Oct 24, 2012 2:18 pm |
|
|
Before we go any futher.
Please tell me you're using REAL hardware and not PROTEUS.
Mike |
|
|
danielz85
Joined: 22 Sep 2012 Posts: 37
|
ok |
Posted: Wed Oct 24, 2012 2:43 pm |
|
|
Thanks for your help.
My REAL circuit is quite simple.
I am having a sinus wave, which comes from a source, the source is not an issue (assume its a computer and you can hear a clear tone).
The + pin of the sin is going to the + of the comparator.
The - pin of the sin is going to the - of the comparator.
It then should output a square wave from the comparator output.
To be honest I CAN measure frequency.
Question:
Do i have to put an outside reference voltage of about 0.5v at the comparator negative pin ? Then reconnecting the sinus - pin to pic ground?
Will it eliminate noise ?
Thanks a lot ! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Oct 24, 2012 3:03 pm |
|
|
ok - thats a bit better
1- you are applying a sine wave that is ground referenced of what amplitude to the comparator ?
2- Are you looking at the signal on the INPUT of the comparator with an O'scope?? - when looking at the negative half cycle - how nice a sine wave is it REALLY ??
3- so you have read - and DO understand table 19-7 and NOTE #1 of the datasheet right?
4- You also understand the CMOS protection clamping for ALL pins of the PIC ??
5- so in producing a Square Wave from the comparator , may i assume you have carefully measured the DUTY cycle and the jitter of the positive part of the comp output signal ??
6- lastly - why did you not consider the capture / compare feature
- that this chip offers ??
If i am guessing right - this MIGHT lead you to a better understanding of where a key problem is for you. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Wed Oct 24, 2012 3:19 pm |
|
|
You could run a test of a SQUARE wave as the input source and see what happens.
Without posting your schematic it is very difficult to 'see' what is happening. There's lots of specs (source (output of signal generator) impedance, terminations, actual voltage range) that will influence the overall design and performance of the circuit, heck even wiring and cellphones can affect things!
Also there's no indication WHEN you're sampling the analog. Usually a zero cross detector circuit is used, maybe with a programmable offset to tune it.
Start off with a known square wave for test purposes. When it's working 100% then and only then try your sine wave input.
hth
jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Oct 24, 2012 3:58 pm |
|
|
Quote: | The + pin of the sin is going to the + of the comparator.
The - pin of the sin is going to the - of the comparator.
| This isn't what you told us previously, that's why we've kept asking for a diagram/schematic.
OK. So you've got a real circuit.
Quote: | To be honest I CAN measure frequency. | I'm asking you again. HOW have you measured the frequency of your clock and that of the sinus?
Take up temtronic's suggestion and start with a square-wave. That way you won't have to answer a lot of my other awkward questions (the ones you've been either evading or avoiding).
When you've done that we can help you sort out the software issues.
Mike |
|
|
|