Menu

Pin Change Interrupt on Mega328P

joe rocci
2014-06-18
2014-06-18
  • joe rocci

    joe rocci - 2014-06-18

    When I insert this line in my code:
    On interrupt PinChange6 Call Encoder_Interrupt

    I get this error:
    Error: Invalid interrupt event: PINCHANGE6

    Also, the Mega328P supports pin change interrupts up to PCINT23. I only see On Interrupt definitions up to PCINT7 in the Help. How far does GCB go with these interrupts?

    Joe

     
  • joe rocci

    joe rocci - 2014-06-18

    This was my misunderstanding on how the AVR Pin Change interrupts work. I think I know what I'm doing wrong.

    Sorry

     
  • joe rocci

    joe rocci - 2014-06-18

    Well, I guess I really don't understand how this works on the AVR. I'm trying to set up pin change interrupts for a rotary encoder on an Arduino Uno, pins D0 and D1. Any suggestions?

    Joe

     
  • joe rocci

    joe rocci - 2014-06-18

    Hmmm..seems that all interrupts except INT0 and INT1 are level triggered...not good for an encoder app. My Arduino LCD shield already uses the INT0 and INT1 pins (PortB.0 & PortB.1), so I guess I'm going back to the drawing board on this one.

     
  • joe rocci

    joe rocci - 2014-06-18

    OK, got it, after a lot of fumbling! Pin change interrupts DO work on state change as advertised.

    Turns out there aren't many free I/O pins left on the Uno board after you plug in an LCD shield, a serial I/O channel and an I2C device. I was using pins that already had signals on them, causing unexpected interrupts. I moved my encoder to pins D2 and D3. Here's the interrupt setup code that made it work:

    PCICR.PCIE2 = 1 'Enables interrupts on pin group PCINT23..16 & maps them to PCINT2
    PCMSK2.PCINT18 = 1 'Enables specific interrupt PCINT18, PORTD.2
    PCMSK2.PCINT19 = 1 'Enables specific interrupt PCINT19, PORTD.3
    On interrupt PinChange2 Call Encoder_Interrupt 'Calls encoder sub on interrpt
    

    Now off to write an interrupt-driven encoder handler!

    Joe

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.