Thread: Public static class pointer

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Matticus View Post
    Nowhere in the scope of that object is the member variable "distance" updated, so the compiler may optimize away the call to the member function. Since that variable is indeed updated in the ISR, it needs to be declared as volatile.
    Ohhhhh... you're right. Didn't think of that. Even I learned something new today. So thanks.

    An ISR must complete before other interrupts can be processed, so you will not see an interrupt occur in the middle of an ISR.
    That's not necessarily true. It is possible to cause an interrupt within an interrupt, depending in whether interrupts are disabled when processing the ISR or not. I don't know specifically for this hardware, though, but in general that is true.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Okay I check it in a book "C Programming for Microcontrollers", it says the following:
    "Volatile is an implemention specific type qualifier that tells the compiler to suppress optimiation that might, in our case, screw things up if we are using pointers to memory mapped registers. We don't want the compiler to help us by using some other memory address since a register is hardwired into the machine and though addressed like memory, isn't ordinary memory.
    Volatile also tells teh compiler that the so modified varible can change unexpectedly (like by an interrupt) so it needs to be checked each time it is used and not just stored somewhere like on stack."

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Elysia View Post
    That's not necessarily true. It is possible to cause an interrupt within an interrupt, depending in whether interrupts are disabled when processing the ISR or not. I don't know specifically for this hardware, though, but in general that is true.
    Your objection to the quoted line in my post is justified. You are correct that nested interrupts can sometimes occur, but only on certain systems that use prioritized interrupts. It is, however, generally considered bad practice to rely on this behavior. In such systems, the more interrupts that are enabled, the more difficult it is to model the behavior of the program. There may be so many possible variations of actions (if multiple interrupts of different priorities occur around the same time) that it makes predictability tremendously difficult.

    But if a device does not use an interrupt priority scheme, or only interrupts of the same priority occur, then an ISR must complete before another interrupt is processed. If interrupts of the same priority could interrupt the ISR, then the ISR could never be executed, as it would restart the ISR each clock cycle. The ISR must ignore interrupts (of equal or lower priority) to allow the code to clear the necessary interrupt flag(s).

  4. #19
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Matticus View Post
    Your objection to the quoted line in my post is justified. You are correct that nested interrupts can sometimes occur, but only on certain systems that use prioritized interrupts. It is, however, generally considered bad practice to rely on this behavior. In such systems, the more interrupts that are enabled, the more difficult it is to model the behavior of the program. There may be so many possible variations of actions (if multiple interrupts of different priorities occur around the same time) that it makes predictability tremendously difficult.

    But if a device does not use an interrupt priority scheme, or only interrupts of the same priority occur, then an ISR must complete before another interrupt is processed. If interrupts of the same priority could interrupt the ISR, then the ISR could never be executed, as it would restart the ISR each clock cycle. The ISR must ignore interrupts (of equal or lower priority) to allow the code to clear the necessary interrupt flag(s).
    In AVR ATmega family of microcontrollers which I am personally using, when interrupt occours other interrupts are disabled automatically since Interrupt enable bit is cleared in the Status register (SREG), so unless you enable that bit during your ISR, another interrupt won't occur during present ISR execution.

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Out of curiosity, what device are you currently using?

    I can also recommend a book if you're interested. It uses C, but teaches a language-independent concept of implementing sophisticated systems using only a single interrupt. This approach helps avoid the issues with nested interrupts that I mentioned above.

  6. #21
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Matticus View Post
    Out of curiosity, what device are you currently using?

    I can also recommend a book if you're interested. It uses C, but teaches a language-independent concept of implementing sophisticated systems using only a single interrupt. This approach helps avoid the issues with nested interrupts that I mentioned above.
    I am using ATmega 324p and ATmega2560.
    Yes of course.

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "Embedded C" by Michael Pont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have a pointer to a static class member ?
    By techie_san778 in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2015, 01:23 AM
  2. C++ public: static field error
    By Bingo90 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2014, 06:14 AM
  3. C++ pointer arrays as static class members
    By l1F in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2010, 04:55 AM
  4. Public: ~Class() is allowed?
    By Lincoln_Poh in forum C++ Programming
    Replies: 13
    Last Post: 09-27-2008, 10:04 PM
  5. how to initialize a static pointer array in a class?
    By nadamson6 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 10:47 PM

Tags for this Thread