View previous topic :: View next topic |
Author |
Message |
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
How to check timer flag without enabling interrupt? |
Posted: Wed Feb 07, 2024 10:26 am |
|
|
Hi,
I am writing a code whereby I am using timer1 without interrupt. i.e. I am loading a value in the Timer1 but not enabling interrupt.
How can I check whether my Timer1 flag in CCS compiler whether it has overflown or not? _________________ MVadukul |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Feb 07, 2024 10:30 am |
|
|
You can just read whatever register the flag is in, mask off the other bits (or 'bit test' ) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Wed Feb 07, 2024 10:38 am |
|
|
The timer flag sets even if the interrupt is not enabled.
Code: |
if (interrupt_active(INT_TIMER1))
{
clear_interrupt(INT_TIMER1); //clear the flag
//do what you want
}
|
|
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
|
Posted: Wed Feb 07, 2024 10:43 am |
|
|
I am writing as follows:-
setup_timer_1(T1_DISABLED);
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
set_timer1(DELAY_300USEC);
DELAY_300USEC = 0xff95
System Clock is 4MHz
Cycles required = 0.0003/0.00000025 = 1200
To make 1200 we can have division by 8 and 16 bit counter of 150 so we will load 65535-150 = 65385.
But for some reason when I am doing following after above it is not entering the loop below:-
if (interrupt_active(INT_TIMER1))
{
setup_timer_1(T1_DISABLED);
clear_interrupt(INT_TIMER1);
output_bit (CLK2, temp_flag_bit);
temp_flag_bit = ~temp_flag_bit;
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
set_timer1(DELAY_300USEC);
}
When I debug, it reaches the if loop but doesn't go in. i.e. my timer does not seem to be running. _________________ MVadukul |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Feb 07, 2024 10:55 am |
|
|
if I'm reading your code right,
the first thing you do is disable the timer....
so if don't enable the timer you'll never get the 'flag' set. |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
|
Posted: Wed Feb 07, 2024 10:59 am |
|
|
I have picked this code form the CCS help file.
When I check I understood as when I disable it, it should stop as you said.
However, when the next line of code executes, it should enable the timer. should it not?
setup_timer_1(T1_DISABLED);
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
set_timer1(DELAY_300USEC); _________________ MVadukul |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Feb 07, 2024 11:50 am |
|
|
I would say no.
There's a specific option to disable the timer
setup_timer_1(T1_DISABLED);
this command 'stops' the timer from operating
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
this command says to connect Timer one internally to a clock source AND divide the rate by a factor of 8.
It does not specifically say 'enable' the timer
You should post which PIC though, as options for timers have changed over the decades. Some timers can have 'gates' to allow when the clock signal to actually be counted.
here's an example that I've cut/pasted the following from the PIC16F877C datasheet, Timer1 chapter...
TMR1ON: Timer1 On bit
1 = Enables Timer1
0 = Stops Timer1
.....
so in this old PIC, you CAN 'start/stop' the action of timer1 by set/clr the TMR1ON bit.
You can think of it this way.Your car has a standard transmission
You can say 'setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8); ' runs the engine and puts transmission into 1st gear...
Nothing happens...you don't go anywhere
Now 'setup_timer_1(T1_enabled)' engages the clutch (connects engine to transmission ) and you drive down the road. |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
|
Posted: Thu Feb 08, 2024 2:51 am |
|
|
Thanks for your help.
The issues are in the device's .h files whereby the constants they defined do not tie up with the datasheet's register. T1_INTERNAL should turn on the Timer and place it under the internal source as well. The value it has is 0x07. i.e. 0000 0111. But when I looked at the datasheet to select internal source, it should be 0000 0001 and the LSB is actually to turn ON the timer.
Once I define my own value as below, it started working how it should do.
#define TIMER1ON 0x81
setup_timer_1 (T1_DIV_BY_8|TIMER1ON);
set_timer1(DELAY_300USEC);
The issue is in 18F65J11.h file.
Many thanks for your comments. _________________ MVadukul |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Feb 08, 2024 7:00 am |
|
|
happy you got it working !!
email CCS about it, they can update that header in new compiler version. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Thu Feb 08, 2024 8:45 am |
|
|
Yes. It actually wants to be 0x81 (the compiler assumes 16bit reads and
writes). |
|
|
leach67
Joined: 13 Feb 2024 Posts: 3
|
Re: How to check timer flag without enabling interrupt? |
Posted: Fri Feb 16, 2024 5:46 am |
|
|
mayur.k.vadukul wrote: | Hi,
I am writing a code whereby I am using timer1 without interrupt. i.e. I am loading a value in the Timer1 but not enabling interrupt.
How can I check whether my Timer1 flag in CCS compiler whether it has overflown or not? |
hello
check if timer1 has overflowed by monitoring the corresponding flag bit in timer 1 control register.
thanks!!! |
|
|
|