We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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
I will write now my code with volatile
many thanks