This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

CC1_CC8IC interrupt

Hello
can someone tell me why my interrupt does not respond, with the debugger, I always get z = 0

double z;

main {
.
.
.
.
.
.

while(1)
 {
CC1_M2      =  0x0001;//  001=>capture on posirtive Flanke
CC1_T01CON  =  0x0047;
CC1_CC8IC   =  0x00C0; // Interrupt enable und
z = Periode ;
 }//end of while
 }// end of main


 void Drehzahl_messung(void) interrupt(0x18)
  {

Periode = 100;
  }


or what I have since forgotten
Thanks

Parents
  • Hello
    Firstly, I would like to thank all the people who tried to help me
    The problem was simply the PSW register in the

    PSW_IEN = 1;
    


    and also with the priority of the interrupt

    CC1_CC8IC   =  0x0044;
    


    and that has works
    I just wanted to test whether my Prograam to interrupt and the changes worked. I will now in the interrupt a speed measurement program

    Thanks Chris Wunderlich
    Thanks to all

Reply
  • Hello
    Firstly, I would like to thank all the people who tried to help me
    The problem was simply the PSW register in the

    PSW_IEN = 1;
    


    and also with the priority of the interrupt

    CC1_CC8IC   =  0x0044;
    


    and that has works
    I just wanted to test whether my Prograam to interrupt and the changes worked. I will now in the interrupt a speed measurement program

    Thanks Chris Wunderlich
    Thanks to all

Children
  • "The problem was simply the PSW register in the"

    This represents a third problem.

    You still have to think about the other two. Use volatile and protect the read of the variable from being interrupted.

  • Hello altogether

    Hello westermark

    I have now a second problem:
    I have used volatile but I still get no good results
    I must measure the period between two positive transition flanks ..
    sometimes I have good result but does not remain constant, over and over period = 0,
    what could be the reason?
    I have to change my code or the funktion with which I will measure the time (periode)

    double  periode;
    
    volatile int Timer_alt;
    volatile int Timer_neu;
    volatile int Timer_Diff;
    
    void main (void)           {
    
    .
    .
    .
    .
    .
    
     while(1)
     {
     CC1_M2      =  0x0001;            //Bit3  allocated to Timer T0/T7  ,Bit 210  001=>capture on posirtive Flanke
     CC1_T01CON  =  0x0047;        //Bit6 0=>Timer is enabled ,Bit3  1=>Timer Mode,Bit210  111=>Timer Resolution 25,6 µs
     CC1_CC8IC   =  0x0044;        // Interrupt enable und Interrupt Flag enable
     PSW_IEN = 1;                              // globally enable interrupts
    
     periode = Timer_Diff*0.0000256;        // Periode
    
     }//end of while
     }// end of main
    
    /* __|----|___|----|___|----|____       */  //010101
    
    
     void Drehzahl_messung(void) interrupt 24
      {
        Timer_alt = CC1_CC8 ; // Timer contents captured
    
       do{
         }while (P2 & 0x0100); //wait of the Low signal
    
             Timer_neu = CC1_CC8 ;// Timer contents captured
         Timer_Diff = Timer_neu - Timer_alt;
      }
    


    Thanks for your help

  • What is generating your pulses?

    Have you verified that you don't have a bouncing signal, in which case the pulse may have ended before you enter your ISR. This will result in a zero length being sampled.

    Timer_alt and Timer_neu don't need volatile if they are only used inside the ISR. The volatile keyword is only needed when a variable may be asynchronously updated by other code or the hardware.

    The ISR may asynchronously update a variable with relation to the main loop. But the ISR does not see any asynchronous update (unless you support nested interrupts).

    If the input signal produces bouncing, then you should learn your ISR to not assign to the global Timer_Diff variable until after you have figured out if you sampled a bounce or a real signal, i.e. that the pulse had a certain minimum length.

    Another thing: Is there a reason for the use of a double? You can often use fixed-point arithmetic for handling this kind of data. But it depends a bit on what further evaluations you need to do with the value.

  • I must measure the period between two positive transition flanks ..

    Now you are measuring the high period.

    You wait for the trailing pulse edge in the interrupt - very bad!

    No need to declare Timer_alt and Timer_neu volatile, but Timer_Diff must be volatile.

    Reading Timer_Diff in the main loop needs to be atomic.

    Timer_alt and Timer_neu must be unsigned, else the pulse width calculation fails. Timer_Diff should also be unsigned. Timer_neu is actually not needed, as it is the captured timer value in CC1_CC8.

    Capture mode initialization should be outside the loop.

    Try this:

    static unsigned int Timer_alt;
    static volatile unsigned int Timer_Diff;
    
    void main (void)
    {
       float  periode;
       unsigned int diff;
    .
    .
    .
    .
    .
      CC1_M2 = 0x0001;        //Bit3  allocated to Timer  T0/T7  ,Bit 210  001=>capture on posirtive Flanke
      CC1_T01CON = 0x0047;    //Bit6 0=>Timer is enabled ,Bit3  1=>Timer Mode,Bit210  111=>Timer Resolution 25,6 µs
      CC1_CC8IC = 0x0044;     // Interrupt enable und Interrupt Flag enable
    
      PSW_IEN = 1;            // globally enable interrupts
    
      while(1)
      {
         _atomic( 0);
         diff = Timer_Diff;
         _endatomic();
    
         periode = (float)diff*0.0000256f;  // Periode
      }//end of while
    }// end of main
    
    /* __|----|___|----|___|----|____       */  //010101
    
    
    void Drehzahl_messung(void) interrupt 24
    {
      Timer_Diff = CC1_CC8 - Timer_alt;
      Timer_alt = CC1_CC8;
    }
    

    The Timer_Diff value is not correct until after the second interrupt.

  • Thanks for your Help

    It works now
    I have changed little the program , and it worked,now ; I can read the period.
    with the _atomic and _endatomic the compiler has always ERROR. perhaps i have to file a Haeder.
    but I hope that I get no problems when I write my programme

    static unsigned int Timer_alt;
    static volatile unsigned int Timer_Diff;
    static volatile double  periode;
    
    void main (void)
    .
    .
    .
    .
    .
     CC1_M2      =  0x0001;            //Bit3  allocated to Timer T0/T7  ,Bit 210  001=>capture on posirtive Flanke
     CC1_T01CON  =  0x0047;        //Bit6 0=>Timer is enabled ,Bit3  1=>Timer Mode,Bit210  111=>Timer Resolution 25,6 µs
     PSW_IEN = 1;                              // globally enable interrupts
     CC1_CC8IC   =  0x0044;        // Interrupt enable und Interrupt Flag enable
    
    while(1)
     {
     periode =Timer_Diff*0.0000256; // Periode
    
     }//end of while
     }// end of main
    
    
    
    
      void Drehzahl_messung(void) interrupt 24
    {
    
      Timer_Diff = CC1_CC8 - Timer_alt;
      Timer_alt = CC1_CC8;
    }
    
    
    


    Thank you

  • with the _atomic and _endatomic the compiler has always ERROR

    that is because you forgot to

    #include <intrins.h>
    

    Please write your program according to the guidelines of Sauli Porttila.

  • oh yes, I have forget to include <intrins.h>
    I just have to try again .. but I have the variable as declared.

      unsigned int Timer_alt;
      unsigned int Timer_Diff;
      double  periode;
    


    and it works
    I must write my program as Portila ?
    I will understand why when the works anyway?
    and apologised if I ask so many!

    Thanks for the explanation

  • It works because reading the 16-bit value of Timer_Diff is atomic by the processor design. If you had a 32-bit value which you modify in the interrupt and read in the main loop, that would most certainly not work as it does now.

  • You do not understand why, because you have not spent the time reading our posts, or reading the C and/or C++ language standard or any good C/C++ programming book.

    The word volatile IS REQUIRED. With volatile specified, the main loop is required to always pick up the current value of the variable. Without the volatile keyword, the compiler MAY (but are not required to) pick up a changed variable.

    Right now, it is just a question of optimization and register allocations that controls if your program "works" or not.

    Are you satisfied with a program that "looks like it works", or do you want a program "that works"?

    Now, please do add the volatile keyword for any variable that is written to by an interrupt hander and that is read from outside an interrupt handler. If you have nested interrupts, then variables read in interrupt handlers may also require the 'volatile' keyword.

    An important difference for you to learn:
    - A program that is correct will produce correct results.
    - A program that produces correct results need not be correct. You might just have failed to find the combination where it will produce incorrect results.

  • First, thank you for this honest response
    I have already read c programming. but sometimes I do not really understand the meaning of things.
    but I am now convinced. with what you have written me. the two sentences were convincingly

    
    An important difference for you to learn:
    - A program that is correct will produce correct results.
    - A program that produces correct results need not be correct. You might just have failed to find the combination where it will produce incorrect results.
    
    

    I will write now my code with volatile

    many thanks