Instruction programming in C language - Page 1
Forum section: electronics / general

author: yalect
author_rank: Regular Contributor
timestamp: May 30, 2024, 11:05:19 pm
content: Hi,I would like to ask that I want to write code in C language that can to read flag interrupt of PIC microcontroller 16F84 when the timer register to 255 and overflow to 00 this register is INTCON and bit 2nd TOIF.How can I write this instruction for read this bit or carry its value and clear it in one instead read and clear frequently for example :Let's say : int overflow;overflow = INTCON,2 & 1;Rest the way to clear the bit after reading in the same line or instruction, maybe with (or) or (and) logic, I hope that will take no more then one or two cycle which means no much time but one line?.Thank you.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: May 30, 2024, 11:13:33 pm
content: The PIC compilers have bit definitions for all register bits, so it would be justOverflow=T0IF;although maybe more type-correctlyOverflow=T0IF?1:0;(may be TMR0IF on later parts - not sure if they have retro-actively named registers on older parts )

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 31, 2024, 04:02:43 am
content: Code:if(T0IF){ // Check if T0IF flag is activeT0IF=0; // Yes, clear flag// Do whatever}Your compiler might use INTCONbits.T0IF instead just T0IF.

author: yalect
author_rank: Regular Contributor
timestamp: May 31, 2024, 07:02:35 am
content: Hi, Thank you.but that will not read or detect the flag and clear it in the same line or condition?.- For mikeselectricstuff (?0:1) will clear the flag after detect it?.Thank you.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: May 31, 2024, 09:17:02 am
content: Quote from: yalect on May 31, 2024, 07:02:35 amno you have to explicitly clear it by setting it to 0

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 31, 2024, 01:53:28 pm
content: You can't detect and clear it at the same time.What are you trying to do? Are you learning C and PIC at the same time?

author: AndyC_772
author_rank: Super Contributor
timestamp: May 31, 2024, 06:10:49 pm
content: Nothing wrong with that, it's how and why I started learning C.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 31, 2024, 06:12:43 pm
content: I know, im just asking

author: Nominal Animal
author_rank: Super Contributor
timestamp: June 01, 2024, 11:44:08 am
content: Quote from: yalect on May 30, 2024, 11:05:19 pmFor PICs, or in standard C, you cannot.On the architectures where this is possible, it is only possible via dedicated functions, either built-in or implemented in assembly.ForIntel Itaniumaka IA-64 architecture support, GCC added a set of__sync built-in functions, but they are deprecated nowadays. Instead, GCC, Clang, Intel Compiler Collection, and many other C and C++ compilers –– but not all! –– provide__atomic built-in functions using C++11 memory modelsas an extension, on architectures where those are possible. On those where they are not, they compile to actual function calls, for the runtime support to implement them as best it can.SDCC, for example, does not provide such built-ins.The useful built-in here would be__atomic_fetch_and(&variable, ~bitmask) &bitmask, which reads the value ofvariablereturning the state of the bits inbitmask, also clearing those bits, atomically, as if in a single operation. (To set those bits, you'd use__atomic_fetch_or(&variable,bitmask) &bitmask; to toggle,__atomic_fetch_xor(&variable,bitmask) &bitmask.)Most architectures don't have such bit manipulation functions, but do have either compare-exchange (CAS) or load-linked store-conditional (LL/SC) instructions, which allow this to be done in a tight loop: the value is loaded, the replacement value computed, but the replacement value is only stored if the original value is still unchanged. (CAS detects changes by value, LL/SC by access.) Because of technical reasons, such loops normally only do one iteration, two iterations max, so they don't really "spin". All processes, threads, or interrupts will see either the original value, or the replacement value, until the next modification; it truly is "atomic", indivisible in this sense. The only real "cost" is that the exact number of cycles taken varies, otherwise it is genuinely atomic read-and-modify operation.Lockless data structures in C use these to implement atomic counters, bit masks, and so on, so you can also see them used in low-level libraries used in Linux/BSD/Android/other POSIXy systems. On microcontrollers they are rarer; for example, Raspberry Pi Zero (RP2040) does not support those either, but does have a hardwarespinlocksupport, for implementing fast mutually exclusive locks.

author: Nominal Animal
author_rank: Super Contributor
timestamp: June 01, 2024, 02:52:35 pm
content: I'm not sure I understand your last post.PIC16F84 can cause an interrupt on on Timer 0 overflow; see T0IE bit in the same register as T0IF. Then, whenever an overflow occurs, that interrupt function will be called by the PIC hardware. It does need to clear the T0IF bit, but it does not need to examine it, because the fact that the interrupt has occurred is itself enough evidence for the T0IF bit to be set.If you want to extend Timer 0 in software to 16 bits (or more) by counting the number of overflows, the interrupt only needs to clear the T0IF bit and increment the high byte. It is best written in assembly. The trick is that reading the 16-bit counter properly becomes a bit complicated, because we need to know if the overflow byte has been incremented since last overflow or not, and handle the case where an overflow occurs during the time we read the 16-bit counter.The same problem occurs an all hardware where the hardware does not "shadow" the multi-byte/multi-word counters (so that reading the low part also hides changes to the high part until after it has also been read; or vice versa), and there are various approaches how that can be done. I'm not familiar enough to say which one is best for PIC16F84, but I suppose the variant that disables interrupts, reads the timer value and overflow byte, and increments the local overflow byte part by one if T0IF is set (but does not update the global overflow byte value), and finally re-enables interrupts, should work well.PIC16F84 PORTA pins do not support generating an interrupt on state change, but PORTB pins 7, 6, 5, and 4 do.So, if you want the PIC hardware to generate an interrupt (running a specific function you can write in C) whenever an input changes state, you need to use one of those four pins.

author: DavidAlfa
author_rank: Super Contributor
timestamp: June 01, 2024, 03:07:00 pm
content: Why are you doing two post for the same thing?You got already answered:https://www.eevblog.com/forum/microcontrollers/microcontroller-interrupt-circuit/