View previous topic :: View next topic |
Author |
Message |
Delfy_Coltech
Joined: 25 Nov 2009 Posts: 27 Location: Vietnam
|
[Help] Setup Timer for measurement purpose |
Posted: Mon Apr 04, 2011 2:53 am |
|
|
Hi all,
I want to use TIMER1 of dSPIC30 to measure DC Current.
I found the way to initialize timer1 as this:
Code: |
setup_timer1(TMR_INTERNAL |TMR_DIV_BY_8,4096);
|
Follow this code,
+ Timer1 using internal clock (~ 8MHz with dSPIC30)
+ Prescaler =8
+ PRx = 4096
But I don't know how to calculate the overflow time in this case?
Can anybody help me?
Thanks! _________________ -------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
------------------------------------------------- |
|
|
Delfy_Coltech
Joined: 25 Nov 2009 Posts: 27 Location: Vietnam
|
|
Posted: Fri Apr 08, 2011 8:05 pm |
|
|
Follow me,
timer1_over = (8*4096)/(8*10^6) = 4096*10^(-6) s = 0.4096 ms _________________ -------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
------------------------------------------------- |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 09, 2011 2:41 pm |
|
|
The reason you don't get an answer is because many of us don't have the
PCD compiler. We can't easily help you. We could read the PIC data
sheet, but that would be work.
My suggestion is to make a small test program with a Timer1 interrupt
routine. Inside the Timer1 isr, put in a line of code to toggle an output
pin. Run the program and measure the frequency on the pin with your
oscilloscope. The interrupt frequency will be 2x the frequency that you
measure with the scope. For example, if you measure 20 KHz, the
interrupt frequency will be 40 KHz. Example of isr:
Code: |
#int_timer1
void timer1_isr(void)
{
output_toggle(PIN_B0);
} |
|
|
|
Delfy_Coltech
Joined: 25 Nov 2009 Posts: 27 Location: Vietnam
|
A nice way ^^ |
Posted: Mon Apr 11, 2011 10:06 pm |
|
|
Thanks Mr.PCM,
I have tried your method to determine overflow time of TIMER1 (dSPIC)
It's so good with nice result on my oscillator.
This is my code to test
Code: |
#include<30F6010A.h>
#FUSES HS2_PLL4, NOWDT, NOPROTECT
// Xtal = 20MHz
#INT_TIMER1
void Timer1_isr(void)
{
output_toggle(PIN_E4);
}
void main(void)
{
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_8,4096);
enable_interrupts(INT_TIMER1);
while(1)
{
}
}
|
In my board, I used crystal = 20MHz.
And the result on my Oscillator is:
f = 152.5581Hz --> timer1_over = 1/ (2*f) = 1/(2*152.5581) = ~3.277 ms
But, I don't understand why f must be multiplied by 2? Can U make it clearly!
Thanks, _________________ -------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
------------------------------------------------- |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 11, 2011 10:31 pm |
|
|
Draw the waveform on a piece of paper, and mark on it where you think
the interrupts are occurring. How often do the interrupts occur ? Do
they occur every cycle of the waveform, or every half cycle ? In other
words, do they occur on all edges, or only on the rising edges ? |
|
|
Delfy_Coltech
Joined: 25 Nov 2009 Posts: 27 Location: Vietnam
|
To be continue... |
Posted: Wed Apr 13, 2011 5:00 am |
|
|
Good afternoon,
'Cos we toggle pin E4, so the waveform is in the square shape.
Minimum voltage =0 and maximum one = 5V.
So, we can't determine where the interrupt timer1 occur?
I have read the datasheet of this PIC. But I can't find any related information. Hix
But I think U r true. real f = f*2. _________________ -------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
------------------------------------------------- |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 13, 2011 2:36 pm |
|
|
Quote: | we can't determine where the interrupt timer1 occur? |
Yes we can.
The interrupt occurs just before each edge (rising or falling) of the
waveform. It has to. You toggle the edge of the signal inside the
interrupt routine. |
|
|
Delfy_Coltech
Joined: 25 Nov 2009 Posts: 27 Location: Vietnam
|
Way back into my title...^^ |
Posted: Fri Apr 15, 2011 7:18 pm |
|
|
Hi Mr PCM Programmer,
Quote: | Yes we can.
The interrupt occurs just before each edge (rising or falling) of the
waveform. It has to. You toggle the edge of the signal inside the
interrupt routine. |
Yes, that's right, I'm so silly^^
Now, way back into my title. I have known the "overflow" time of TIMER1.
For example, t_over=3.2 ms. So in one second, the interrupt occur 1000/3.2 = ~ 312 times.
--> Base on this characteristic of TIMER1 interrupt. I want to measure DC Voltage using it.
When interrupt occur, I read the ADC. So in one second, I can get maximum 312 samples. Averagement the samples, I can receive the most exaclly result.
Is my method right?
Plz, discuss continously.
Thanks, ( I'm so sorry, if my English makes you confusing ^^) _________________ -------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
------------------------------------------------- |
|
|
|