<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Interrupt Handling (pushbuttons) in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126882#M1249</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a circuit built around the HCS08 and I need to handle 3 specific events.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The first 2 are push events from 2 pushbuttons connected to Port A (pins 6 and 7)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and the third is an interrupt from an external chip that signals the micro that its done its work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's how I initalize the registers :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// Enable interrupt on KBI (portA)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// enable pullups&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE4 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE6 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE7 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// Direction in (0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE4 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE6 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE7 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// enable int on 3 pins&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE4=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE6=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE7=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The vector table is set like so :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;kbd_isr, /* vector 22: KBI */&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// here is the interrupt handler&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;interrupt void kbd_isr (void)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// if its really a KBI event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if (KBI1SC_KBF)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// don't allow more kbi interrupts&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = DISABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// clear interrupt flag&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBACK = 1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// make sure everyone knows theres a KBI event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag, KBI_EVENT_PENDING);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;basically in the ISR I'm only setting a flag asking the app to take it from there.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In the app I'm expecting that the specific pin that fired the event should have a 1 on its data line and so if I check PTAD_PTAD{4,6,7} i should know if the interrupt happened. But I don't get anything on the data line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Question 1 : Should I get data on this pin everytime an interrupt is fired?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;as a temporary workaround I use this code (right now I pretty much know when which event is going to fire)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;void HandleKBIEvent(void)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// make sure it was a valid call&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if(GETBIT(KBI_EventFlag, KBI_EVENT_PENDING))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// chip shouldn't have a debounce period&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if (CHIP_INT_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_CHIP);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// debounce for switches&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;delay_ms(500);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if(BUTTON1_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_BTN1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else if(BUTTON2_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_BTN2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// clear event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CLRBIT(KBI_EventFlag, KBI_EVENT_PENDING);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// we took care of this&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_HANDLED);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// reenable interrupts&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Question 2: The interrupts on the pushbutton lines don't always fire. Although I can see with an analyzer that the line does go low. Any idea why this might happen?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;K&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 22 Feb 2006 06:08:24 GMT</pubDate>
    <dc:creator>kaiki</dc:creator>
    <dc:date>2006-02-22T06:08:24Z</dc:date>
    <item>
      <title>Interrupt Handling (pushbuttons)</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126882#M1249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a circuit built around the HCS08 and I need to handle 3 specific events.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The first 2 are push events from 2 pushbuttons connected to Port A (pins 6 and 7)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and the third is an interrupt from an external chip that signals the micro that its done its work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's how I initalize the registers :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// Enable interrupt on KBI (portA)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// enable pullups&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE4 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE6 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE7 = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// Direction in (0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE4 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE6 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;PTAPE_PTAPE7 = IN;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// enable int on 3 pins&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE4=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE6=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1PE_KBI1PE7=ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The vector table is set like so :&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;kbd_isr, /* vector 22: KBI */&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// here is the interrupt handler&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;interrupt void kbd_isr (void)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// if its really a KBI event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if (KBI1SC_KBF)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// don't allow more kbi interrupts&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = DISABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// clear interrupt flag&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBACK = 1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// make sure everyone knows theres a KBI event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag, KBI_EVENT_PENDING);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;basically in the ISR I'm only setting a flag asking the app to take it from there.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In the app I'm expecting that the specific pin that fired the event should have a 1 on its data line and so if I check PTAD_PTAD{4,6,7} i should know if the interrupt happened. But I don't get anything on the data line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Question 1 : Should I get data on this pin everytime an interrupt is fired?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;as a temporary workaround I use this code (right now I pretty much know when which event is going to fire)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;void HandleKBIEvent(void)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// make sure it was a valid call&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if(GETBIT(KBI_EventFlag, KBI_EVENT_PENDING))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// chip shouldn't have a debounce period&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if (CHIP_INT_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_CHIP);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// debounce for switches&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;delay_ms(500);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if(BUTTON1_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_BTN1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else if(BUTTON2_INT == ENABLED)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_BTN2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// clear event&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CLRBIT(KBI_EventFlag, KBI_EVENT_PENDING);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;// we took care of this&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SETBIT(KBI_EventFlag,KBI_EVENT_HANDLED);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;// reenable interrupts&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KBI1SC_KBI1E = ENABLED;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Question 2: The interrupts on the pushbutton lines don't always fire. Although I can see with an analyzer that the line does go low. Any idea why this might happen?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;K&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Feb 2006 06:08:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126882#M1249</guid>
      <dc:creator>kaiki</dc:creator>
      <dc:date>2006-02-22T06:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: Interrupt Handling (pushbuttons)</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126883#M1250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;FONT size="3"&gt;Hello Kai,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Have you checked that the KBI triggering edge matches the push event, rather than the release event, for the pushbuttons?&amp;nbsp; With a single push, both events&amp;nbsp;can occur because of switch&amp;nbsp;bounce, so a mismatch may not be obvious.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;I notice that you disable all further KBI interrupts until 500ms de-bounce delay is complete.&amp;nbsp;So no other events could&amp;nbsp;be detected&amp;nbsp;within this period.&amp;nbsp; I would suggest that you disable only the particular pushbutton&amp;nbsp;input that caused the current interrupt, so that other interrupt events can be detected.&amp;nbsp; Allowing 500ms de-bounce is probably excessive - normally 50-100ms is enough for small pushbuttons.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Mac&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Feb 2006 15:27:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126883#M1250</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2006-02-22T15:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Interrupt Handling (pushbuttons)</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126884#M1251</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;One of your inputs is from an external device that signals when an event is finish. This signal probably does not require any debouncing. Have you consider bring this into one of the external interrupts?&lt;BR /&gt;&lt;BR /&gt;The keyboard logic requires that all enable signals be logic high for an interrupt to be asserted when any one of the lines goes low. In the keyboard interrupt handler, determine which input fire the interrupt and disconect that input from the keyboard logic. This will then allow the other pushbutton(s) to asset an interrupt.&lt;BR /&gt;&lt;BR /&gt;I would not use a delay function for debouncing. In your main loop use a polling technique and check the line for a logic high state. Put a variable counter and verify that its high again for a certain number of consecutive looks (threshold), before that pin is re-enable again as a keyboard interrupt. Or setup the timer to assert an interrupt at some high rate and perform the logic test in the timer interrupt handler; or set a flag in the timer interrupt handler and perform the test in a main loop when the above flag is set.&lt;BR /&gt;&lt;BR /&gt;What I found in my logic is that when a I/O was enabled as a keyboard interrupt, I had to make the corresponding I/O pin be an output to keep a false interrupt from occurring.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 Feb 2006 12:05:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Interrupt-Handling-pushbuttons/m-p/126884#M1251</guid>
      <dc:creator>byteybird</dc:creator>
      <dc:date>2006-02-26T12:05:50Z</dc:date>
    </item>
  </channel>
</rss>

