Using ReadStatusRegister to know which interruption is triggered

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

     Hi, I'm developing a project that uses a Timer, and I want to set an interrupt when I receive an input on the capture input of the timer, or when the counter value of the timer is equal to zero (TC interrupt).

   

    The problem is that if I check both interruptions ("On TC" and "On Capture [1-4]"), in the main.c I don't know which interrupt is triggered.

   

    In the API said that ReadStatusRegister returns a uint8 with the value of the register, but I don't know which value use to compare it to know which kind of interruption has been triggered.

   

 

   

## CODE ##

   

CY_ISR(theres_an_interruption){

   

    if(Timer_ReadStatusRegister() == value_when_theres_a_TC_interruption){

   

        // TC interruption

   

    } else {

   

        //  Capture interruption

   

    }

   

}

0 Likes
1 Solution
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

More like this -

   

 

   

if(Timer_ReadStatusRegister() & 00000001)                 // If that bit is true virtue of using mask off upper 7 bits

   

 

   

Regards, Dana.

View solution in original post

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You do not state quite clear which timer component you use.

   

For the TCPWM you will have to use the GetInterruptSource() function. For the Timer (v 2.70) open the datasheet, go to the Timer_ReadStatusRegister() and click on the "Status Register" link which will bring you to the bit definitions for TC and Capture. Keep in mind that the bits are cleared on read, namely Timer_STATUS_TC and Timer_STATUS_TC (#defined in your Timer.h)

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thank you Bob, I'm using the Timer (v 2.70), and you are right, I should check for the Timer_STATUS_TC and the Timer_STATUS_CAPTURE.

   

My doubt is what value should i use to compare the Timer_ReadStatusRegister(). I mean, if the bit "Timer_STATUS_TC" it's located at position 0 in the status register, and I want to check if the interruption is caused by the TC interruption, I must use something like this?

   

if(Timer_ReadStatusRegister() == 00000001){

   

  // TC interruption here

   

}

   

 

   

Or maybe as Timer_ReadStatusRegister() returns a uint8, I just may compare the return value to:

   
        
  • When TC bit is set in the Status Register it looks like this 00000001, so I must check if Timer_ReadStatusRegister() == 1
  •     
  • When Capture bit is set in the Status Register it looks like this 00000010, so I must check if Timer_ReadStatusRegister() == 2
  •    
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

More like this -

   

 

   

if(Timer_ReadStatusRegister() & 00000001)                 // If that bit is true virtue of using mask off upper 7 bits

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

C-language was designed to write that request as short as even possible, long before optimizing compilers were invented.

   

A logical "false" is defined in C as zero (0, NULL), logical "true" is then the boolean opposite wihich is != zero.

   

To question your above flag you never should make a comparison to be equal to the bit you want to test because there might another bit set, too. Better is -as Dana suggested- to perform a logical AND and compare the result to non zero. With the above in mind you may write

   

if(Timer_ReadStatusRegister() & Timer_STATUS_TC)

   

for the case that both of your flags may be active you should remember thar the flags get reset by a ReadStatusRegister() and you will have to save the value for a second quest.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thank you Bob and Dana, tomorrow when I come back to my college lab I will compile it and tell you if it works.

0 Likes