Author Topic: microcontroller Pin Interrupt circuit  (Read 1334 times)

0 Members and 1 Guest are viewing this topic.

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
microcontroller Pin Interrupt circuit
« on: May 17, 2024, 12:05:45 am »
Hi,
I would like to ask you that I want to write code in C language using the microcontroller PIC16F84, when I applicate a voltage on RA4/T0CKI of PORTA an interrupt hapens making the program switchs or jumps to second sub function or loop void interrupt() doing some instructions and back after it has finished or done to the main program void main() that means switching or moving between two functions by interrupt.
is it possible?.

    thank you.
« Last Edit: May 30, 2024, 09:42:02 am by yalect »
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3422
  • Country: nl
Re: microcontroller Interrupt circuit
« Reply #1 on: May 17, 2024, 12:25:51 am »
Usually there are a lot of ways in which ISR's can be triggered by software, but they tend to have various side effects. With your method, you loos an I/O pin, and when it's shorted it does not work at all. Another method is to pre-load a timer and let it overflow after command.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5998
  • Country: es
Re: microcontroller Interrupt circuit
« Reply #2 on: May 17, 2024, 01:48:46 am »
In that PIC only RB7:RB4 pins have interrupt on change.
Which compiler? XC8?
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
Re: microcontroller Interrupt circuit
« Reply #3 on: May 17, 2024, 05:40:05 am »
thank you for your replies.
you mean we can't use interrupts on PORTA with PIC16F84? to switch or move to sub function or routine for example interrupt () by interrupt, maybe with TMR0 timer and How?.
« Last Edit: May 17, 2024, 05:43:52 am by yalect »
 

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 342
  • Country: gb
    • Woofys Place
Re: microcontroller Interrupt circuit
« Reply #4 on: May 17, 2024, 08:09:58 am »
First, unless yo have a good reason to use this ancient device, you should choose something newer with more features for less money.

If you do have to use it then yes, you can use the timer to generate the interrupt from a  change on RA4. You should set T0CS to 1 to select external clk, PSA to 0 to bypass the prescaler and TMR0 to 0xFF. Now a clk input will cause the timer to rollover to 0x00 and set T0IF. Remember to set TMR0 back to 0xFF in your interrupt routine.

Offline woody

  • Frequent Contributor
  • **
  • Posts: 295
  • Country: nl
Re: microcontroller Interrupt circuit
« Reply #5 on: May 17, 2024, 08:19:47 am »
you mean we can't use interrupts on PORTA with PIC16F84?
No you cannot. See datasheet page 48
to switch or move to sub function or routine for example interrupt () by interrupt, maybe with TMR0 timer and How?.
In principle you first select the chip frequency and timer0 settings to fire every x ms. x depending on how often you need to check the state of the input pin. For the 16F84 this is done in hardware (crystal/resonator or RC) and in an init() routine that precedes main(), where you set fuses and timer.
Then you write a (maybe empty) main() and an interrupt() routine that handles (reads) the input pin every time the interrupt fires. If you only want to light a led or switch a relay depending on the input state you can do that in the interrupt routine. For more complicated reactions you need signaling to main() to keep the interrupt() routine short.

 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3541
  • Country: us
Re: microcontroller Interrupt circuit
« Reply #6 on: May 17, 2024, 08:28:13 am »
On the 16F84A (I didn't check the plain 16F84):

1) Poll the pin and make a software interrupt or even a branch.  No interrupt needed.
2) That pin is T0CLKI (TMR0 input) and TMR0 is writable.  Set TMR0 to 255.   On first pulse TMR0 will overflow and can be made to cause an interrupt.  You can also select the edge to cause increment.  Of course, on return from the ISR, you need to re-arm TMR0.

Not knowing how complex your project is, if it is relatively simple, I would just poll the pin and not use an interrupt.
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
Re: microcontroller Pin Interrupt circuit
« Reply #7 on: May 27, 2024, 09:32:27 am »
Hi, thank you for your reply.
but Can I know how is interrupt this Pic microcontroller?.

  thanks.
« Last Edit: May 27, 2024, 02:00:49 pm by yalect »
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3541
  • Country: us
Re: microcontroller Interrupt circuit
« Reply #8 on: May 27, 2024, 09:44:01 am »
It's been 10 days since your last post.  Several posts have told you how to create an interrupt with that chip.  Do you understand the principle?  What don't you understand?  Why do you even need an interrupt?  Why not use polling? 

Maybe most important, do you know how write code in C?  Several contributors have asked questions.  You have not answered a single one.  Addressing those questions would be a good place to start.
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
Re: microcontroller Interrupt circuit
« Reply #9 on: May 27, 2024, 02:09:50 pm »
Sorry, that it was mistake, yes I know about to put 255 value in the timer to make it overflow after, I meant how is it's fast is, that interrupts or speed of response, maybe there are a ways to make it more response?.
Thank you in advance.
« Last Edit: May 27, 2024, 02:20:11 pm by yalect »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5998
  • Country: es
Re: microcontroller Interrupt circuit
« Reply #10 on: May 27, 2024, 02:31:41 pm »
Thats it. It will take a single clock cycle, almost instant response (1-4us maybe).
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3541
  • Country: us
Re: microcontroller Interrupt circuit
« Reply #11 on: May 27, 2024, 02:56:58 pm »
I agree with David.  It is quite fast relative to anything else you do.  The datasheet for the 16F84A in Section 6 describes interrupt latency.  For an IOC on PORTB,  it is 3 to 4 instruction cycles (Tcy).  An instruction cycle is 4 oscillator cycles.  Thus, at 8 MHz, an instruction cycle is 2 MHz (500 ns).  I didn't see a specific latency for reading Timer 0 overflow which sets TOIF.  I suspect it is at least that fast.

That chip does not save "context,"  i.e., WREG, STATUS and other important working values.  It does save the return point on the STACK.  Depending on your program and the ISR, you may and probably do want to save context, then restore context before returning from the interrupt. Saving context will take several more Tcy and there are efficient examples of how to do that in the datasheet.  That may be somewhat automatic in C, I don't know.
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
Re: microcontroller Interrupt circuit
« Reply #12 on: May 29, 2024, 11:36:24 am »
Hi, thank you for replies.
Is it correct when we read the timer or tmr0 directlly?.
 for example :
Timer or tmr0 = 132;
or
if (tmr0 = 132);
shortly, is that affect on the timer fonction?.

   Thanks.
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3541
  • Country: us
Re: microcontroller Interrupt circuit
« Reply #13 on: May 29, 2024, 12:16:08 pm »
Please read the datasheet for the device you are using.  Here's an excerpt from the 16F84A datasheet:

Quote
5.0 TIMER0 MODULE
The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• Internal or external clock select
• Edge select for external clock
• 8-bit software programmable prescaler
• Interrupt-on-overflow from FFh to 00h
Figure 5-1 is a simplified block diagram of the Timer0
module.
Additional information on timer modules is available in
the PIC® Mid-Range Reference Manual (DS33023).
5.1 Timer0 Operation
Timer0 can operate as a timer or as a counter.
Timer mode is selected by clearing bit T0CS
(OPTION_REG<5>). In Timer mode, the Timer0 module
will increment every instruction cycle (without prescaler).
If the TMR0 register is written, the increment is
inhibited for the following two instruction cycles. The
user can work around this by writing an adjusted value
to the TMR0 register.
Counter mode is selected by setting bit T0CS
(OPTION_REG<5>). In Counter mode, Timer0 will
increment, either on every rising or falling edge of pin
RA4/T0CKI. The incrementing edge is determined by
the Timer0 Source Edge Select bit, T0SE

First, it rolls over from 255 (0xFF) on the next count.  It can count from the RA4/T0CK1 pin and create an interrupt.  It's not the easiest way to do it, but since you wanted an interrupt by a pin on PORTA, in theory, it can do that.  I cannot help you with C code.  I don't know why you apparently want to preset to 132.  Note, Timer0 also has prescaler, so you have to set a few things up in code, including edge.  I believe if you set it as a timer based on the instruction clock, it runs continuously after you do that.  That is, you cannot turn it on and off easily.  Look at the requirement for synchronization.

An easier way would be to use PORTB and IOC as other have said.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12904
Re: microcontroller Interrupt circuit
« Reply #14 on: May 29, 2024, 03:31:28 pm »
However Port B IOC on older PIC16 and PIC18 parts including the PIC16F84 & PIC16F84A is buggy.  It can loose edges due to a race condition in the change detection logic.  See bug details at the old xargs.com site (via Internet Archive).

Its OK for waking from SLEEP, but if running, makes it very difficult to use any other pins on port B without risking missing a change on the IOC capable pin you are interested in.

Your best bet for an easy to use external interrupt on that PIC is the RB0/INT pin as that pin has a simple edge triggered interrupt, with the edge selectable by OPTION_REG.INTEDG, and the usual enable and interrupt flags in INTCON.
« Last Edit: May 29, 2024, 03:40:31 pm by Ian.M »
 

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 132
Re: microcontroller Pin Interrupt circuit
« Reply #15 on: May 30, 2024, 06:11:06 pm »
Hi,
we can set TMR0 to 255.   On first pulse TMR0 will overflow and can be made to cause an interrupt.as they response above  and we can read the statuse of the flag in the register INTCON TOIF to detect
That.
is there a way to read TOIF or carry its value and clear it again in software in the same way maybe with (and) logic or (or) in one instruction instead read and clear frequently?.

    Thank you.
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3541
  • Country: us
Re: microcontroller Pin Interrupt circuit
« Reply #16 on: May 30, 2024, 06:33:23 pm »
Look on page 10 of the datasheet.  The T0IF bit of INTCON is readable and writable.  If that doesn't convince you , then this section will:
Quote
The TMR0 interrupt is generated when the TMR0 register
overflows from FFh to 00h. This overflow sets bit
T0IF (INTCON<2>). The interrupt can be masked by
clearing bit T0IE (INTCON<5>). Bit T0IF must be
cleared in software by the Timer0 module Interrupt Service
[
Not only can you clear it, but you must clear it in software.

I feel like we are going in circles.  You wanted an interrupt and didn't want to poll a bit.  Yes, you can poll it (if T0IE is clear), but why go such a circuitous route if you do not want an interrupt?

As a general rule, with PICs, interrupt flags are set regardless of whether the corresponding interrupt is enabled.  That can be an important consideration when re-enabling interrupts. 
« Last Edit: May 30, 2024, 06:36:36 pm by jpanhalt »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf