CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

[Help] Setup Timer for measurement purpose

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Delfy_Coltech



Joined: 25 Nov 2009
Posts: 27
Location: Vietnam

View user's profile Send private message Visit poster's website

[Help] Setup Timer for measurement purpose
PostPosted: Mon Apr 04, 2011 2:53 am     Reply with quote

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! Wink
_________________
-------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
-------------------------------------------------
Delfy_Coltech



Joined: 25 Nov 2009
Posts: 27
Location: Vietnam

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 08, 2011 8:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 09, 2011 2:41 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

A nice way ^^
PostPosted: Mon Apr 11, 2011 10:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 11, 2011 10:31 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

To be continue...
PostPosted: Wed Apr 13, 2011 5:00 am     Reply with quote

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? Smile
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. Wink
_________________
-------------------------------------------------
Mechatronics Department, Coltech, VNUH
Hanoi, Vietnam.
-------------------------------------------------
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 13, 2011 2:36 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

Way back into my title...^^
PostPosted: Fri Apr 15, 2011 7:18 pm     Reply with quote

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.
-------------------------------------------------
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group