Skip to main content
deleted 72 characters in body
Source Link
Unimportant
  • 5.1k
  • 1
  • 22
  • 30

There are some low level problems with your code that have to be handled first. These problems have the potential to break your code even if the logic would otherwise seem correct.

  • Variables that are shared between the interrupt service routine (ISR) and the main program code should be declared volatile. In your case that is all three of them:

     volatile int compt = 0;
     volatile int seconds = 10;
     volatile int enable = 0;
    

Volatile tells the compiler that a variable can change in between different accesses even if the code does not appear to modify it. This prevents certain reads and writes being optimized away because the optimizer does not understand there's an ISR working with those same variables asynchronously.

  • The C standard requires a int to be at least 16-bit. Your PIC, however, is a 8-bit device. This means manipulating a int will be a non-atomic operation. Non-atomic operations require multiple CPU instructions to complete. A interrupt could be triggered in the middle of such a operation which would cause either the ISR to see a (corrupt) intermediate value of the variable or the ISR modifying the variable while the main code was in the middle of doing an operation on it, possibly corrupting it.

A possible solution to this is to use single byte variables instead, which can be manipulated in a single instructionmutex or turn off interrupts during any relevant non-atomic operation.

There are some low level problems with your code that have to be handled first. These problems have the potential to break your code even if the logic would otherwise seem correct.

  • Variables that are shared between the interrupt service routine (ISR) and the main program code should be declared volatile. In your case that is all three of them:

     volatile int compt = 0;
     volatile int seconds = 10;
     volatile int enable = 0;
    

Volatile tells the compiler that a variable can change in between different accesses even if the code does not appear to modify it. This prevents certain reads and writes being optimized away because the optimizer does not understand there's an ISR working with those same variables asynchronously.

  • The C standard requires a int to be at least 16-bit. Your PIC, however, is a 8-bit device. This means manipulating a int will be a non-atomic operation. Non-atomic operations require multiple CPU instructions to complete. A interrupt could be triggered in the middle of such a operation which would cause either the ISR to see a (corrupt) intermediate value of the variable or the ISR modifying the variable while the main code was in the middle of doing an operation on it, possibly corrupting it.

A possible solution to this is to use single byte variables instead, which can be manipulated in a single instruction or turn off interrupts during any relevant non-atomic operation.

There are some low level problems with your code that have to be handled first. These problems have the potential to break your code even if the logic would otherwise seem correct.

  • Variables that are shared between the interrupt service routine (ISR) and the main program code should be declared volatile. In your case that is all three of them:

     volatile int compt = 0;
     volatile int seconds = 10;
     volatile int enable = 0;
    

Volatile tells the compiler that a variable can change in between different accesses even if the code does not appear to modify it. This prevents certain reads and writes being optimized away because the optimizer does not understand there's an ISR working with those same variables asynchronously.

  • The C standard requires a int to be at least 16-bit. Your PIC, however, is a 8-bit device. This means manipulating a int will be a non-atomic operation. Non-atomic operations require multiple CPU instructions to complete. A interrupt could be triggered in the middle of such a operation which would cause either the ISR to see a (corrupt) intermediate value of the variable or the ISR modifying the variable while the main code was in the middle of doing an operation on it, possibly corrupting it.

A possible solution to this is to use a mutex or turn off interrupts during any relevant non-atomic operation.

Source Link
Unimportant
  • 5.1k
  • 1
  • 22
  • 30

There are some low level problems with your code that have to be handled first. These problems have the potential to break your code even if the logic would otherwise seem correct.

  • Variables that are shared between the interrupt service routine (ISR) and the main program code should be declared volatile. In your case that is all three of them:

     volatile int compt = 0;
     volatile int seconds = 10;
     volatile int enable = 0;
    

Volatile tells the compiler that a variable can change in between different accesses even if the code does not appear to modify it. This prevents certain reads and writes being optimized away because the optimizer does not understand there's an ISR working with those same variables asynchronously.

  • The C standard requires a int to be at least 16-bit. Your PIC, however, is a 8-bit device. This means manipulating a int will be a non-atomic operation. Non-atomic operations require multiple CPU instructions to complete. A interrupt could be triggered in the middle of such a operation which would cause either the ISR to see a (corrupt) intermediate value of the variable or the ISR modifying the variable while the main code was in the middle of doing an operation on it, possibly corrupting it.

A possible solution to this is to use single byte variables instead, which can be manipulated in a single instruction or turn off interrupts during any relevant non-atomic operation.