Author Topic: Instruction programming in C language  (Read 614 times)

0 Members and 2 Guests are viewing this topic.

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 140
Instruction programming in C language
« on: May 30, 2024, 11:05:19 pm »
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.
« Last Edit: May 31, 2024, 07:30:39 am by yalect »
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13834
  • Country: gb
    • Mike's Electric Stuff
Re: Instruction programming in C language
« Reply #1 on: May 30, 2024, 11:13:33 pm »
The PIC compilers have bit definitions for all register bits, so it would be just
Overflow=T0IF;
although maybe more type-correctly
Overflow=T0IF?1:0;

(may be  TMR0IF on later parts - not sure if they have retro-actively named registers on older parts )
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6081
  • Country: es
Re: Instruction programming in C language
« Reply #2 on: May 31, 2024, 04:02:43 am »
Code: [Select]
if(T0IF){          // Check if T0IF flag is active
  T0IF=0;         // Yes, clear flag
  // Do whatever
}

Your compiler might use INTCONbits.T0IF instead just T0IF.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 140
Re: Instruction programming in C language
« Reply #3 on: May 31, 2024, 07:02:35 am »
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.
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13834
  • Country: gb
    • Mike's Electric Stuff
Re: Instruction programming in C language
« Reply #4 on: May 31, 2024, 09:17:02 am »
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.
no you have to explicitly clear it by setting it to 0
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6081
  • Country: es
Re: Instruction programming in C language
« Reply #5 on: May 31, 2024, 01:53:28 pm »
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?
« Last Edit: May 31, 2024, 06:13:01 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4255
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: Instruction programming in C language
« Reply #6 on: May 31, 2024, 06:10:49 pm »
Nothing wrong with that, it's how and why I started learning C.

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6081
  • Country: es
Re: Instruction programming in C language
« Reply #7 on: May 31, 2024, 06:12:43 pm »
I know, im just asking  ;)
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6514
  • Country: fi
    • My home page and email address
Re: Instruction programming in C language
« Reply #8 on: June 01, 2024, 11:44:08 am »
How can I write this instruction for read this bit or carry its value and clear it in one
For 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.

For Intel Itanium aka 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 models as 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 of variable returning the state of the bits in bitmask, 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 hardware spinlock support, for implementing fast mutually exclusive locks.
« Last Edit: June 01, 2024, 11:46:32 am by Nominal Animal »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6514
  • Country: fi
    • My home page and email address
Re: Instruction programming in C language
« Reply #9 on: June 01, 2024, 02:52:35 pm »
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.
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6081
  • Country: es
Re: Instruction programming in C language
« Reply #10 on: June 01, 2024, 03:07:00 pm »
Why are you doing two post for the same thing?
You got already answered:

https://www.eevblog.com/forum/microcontrollers/microcontroller-interrupt-circuit/
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf