Another Timer Question

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Post Reply
henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Another Timer Question

Post by henker »

Hello Guys,

I have another timer question.
I’m using a 877a with the E-blocks and have made a simple flowcode program that I want to measure the time between pressing switch a1 (timer on) and a2 (timer off)
The first attempt was made with Timer 1:

Loop till A1 is 1
start timer (in C)
tmr1h = 0x00; // make Timer 1H value 0
tmr1l = 0x00; // Make Timer 1L value 0
t1con = 0x3B; // Start timer with 1:8 prescaler

Loop till A2 is 1
stop timer (in C)
t1con = 0xFE; //Stop Timer1 Counting
FCV_T1_COUNT_L = tmr1l; //Store LSB
FCV_T1_COUNT_H = tmr1h; //Store MSB

Then I’m trying to read out these values on the LCD. (At this stage I’m only interested in reading the L and H value).
Both these values show consistently 0 and 0. I presume that this is caused by the timer that overflows before I can even stop the timer and this is the reason it is showing 00
I have made the same program for timer two and indeed I actually get a reading. The problem here is that this timer doesn’t time higher then FF and then starts over again.
At this stage I have a few questions:
- Is my assumption on Timer 1 correct. (no reading due to overflow);
- If so, how can I overcome this?
- What is the best approach to make this program so I can measure between 2 and 30msec. (use T1 or T2)?

Thanks for all the help
Henk

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Another Timer Question

Post by Benj »

Hello Henk

I think it is unlikley that you are hitting the timer right on the overflow each time.

Try replacing this line of code
t1con = 0x3B; // Start timer with 1:8 prescaler

with this.
t1con = 0x39; // Start timer with 1:8 prescaler

The value 0x3B uses the external input T1CKI to increment the timer value whereas 0x39 should allow for the prescalar input from the internal clock.

You may also want to replace this line
t1con = 0xFE; //Stop Timer1 Counting

with this
t1con = 0x38; //Stop Timer1 Counting

to make sure that the timer value is consistant and not being effected by the T1CKI pin.

If this doesnt work then you could use the timer 2 option and increment a variable each time the timer overflows. Then use the timer value for the LSB and the variable count for the MSB to create a 16-bit count value. This would mean that you would have to enable the timer 2 interrupts or poll the timer2 count value regularly enough so that the additional count variable is incremented correctly.

henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Re: Another Timer Question

Post by henker »

Hi Ben,

That seems to be working; I'm reading both T1L and T1H seperate; Now I will be adding these together and some math on it!.

At least i have a reading; Thanks for your help

henk

henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Re: Another Timer Question

Post by henker »

Hi Ben,

As I mentioned, I have reading of both, T1L and T1H. I have now added them up and also that is showing an outcome. I have added a Interrupt on T1 overflow so I can even count more then just FFFF.
My question this time is with regards the Variable, INT A. It looks like that in flowcode the INT A variable only works from
-32768 to 32767. Is there any way to make the INT A 0 to 65535?

Thanks
Henk

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Another Timer Question

Post by Benj »

Hello Henk

Sorry but Flowcode only supports signed INT variables at the moment. You can however use some C code to get longer variables. YOu will not be able to use them directly in the Flowcode GUI but you can do larger number processing before passing the values back etc.

C code variable definitions.

INT var1; //signed int -32768 to 32767
unsigned INT var1; //Unsigned int 0 - 65535
long var1; //32-bit signed long
unsigned long var1; //Unsigned 32-bit long

Post Reply