Interrupt examples by MicroBuilder, CooCox, and Somy

LPC1114 Interrupt - Micro Builder

http://www.microbuilder.eu/Projects/LPC1114ReferenceDesign/CodeBaseDocumentation.aspx

Any GPIO pin on the LPC1114 can be configured as an external interrupt source. The gpioSetInterrupt method allows you to determine which pins act as an interrupt source, and specify the conditions under which an interrupt should be raised (if the pin goes high or low, etc.). Depending on the portNum specified, the interrupt will be handled (by default) by PIOINTx_IRQHandler, where 'x' is the port number in question (PIOINT1_IRQHandler would be entered if an interrupt was raised on GPIO pin 1.8, for example).

void gpioSetInterrupt (uint32_t portNum, uint32_t bitPos, gpioInterruptSense_t sense, gpioInterruptEdge_t edge, gpioInterruptEvent_t event)

Arguments

portNum: the gpio port number [0..3]


bitPos: the bit position for the gpio pin [0..11]


sense: whether the interrupt should be configured as edge (gpioInterruptSense_Edge) or level (gpioInterruptSense_Level) sensitive.


edge: whether an interrupt is triggered on one edge (gpioInterruptEdge_Single) or on both (gpioInterruptEdge_Double).


event: whether the rising edge (gpioInterruptEvent_ActiveHigh) or the falling edge (gpioInterruptEvent_ActiveLow) should be used to trigger the interrupt. ActiveHigh means that a HIGH level on the pin will trigger an interrupt, ActiveLow means that a LOW level on the pin will trigger an interrupt.


void gpioIntEnable (uint32_t portNum, uint32_t bitPos)

Enables an interrupt on the specified GPIO pin.


Arguments


portNum: the gpio port number [0..3]


bitPos: the bit position for the gpio pin [0..11]


See the example for gpioSetInterrupt.


void gpioIntDisable (uint32_t portNum, uint32_t bitPos)

Disables an interrupt on the specified pin.


Arguments


portNum: the gpio port number [0..3]


bitPos: the bit position for the gpio pin [0..11]


See the example for gpioSetInterrupt.


void gpioIntClear (uint32_t portNum, uint32_t bitPos)

Clears the interrupt on the specified pin. This method should only be called from withint the interrupt service routine (ISR) once the interrupt has been raise.


Arguments


portNum: the gpio port number [0..3]


bitPos: the bit position for the gpio pin [0..11]




uint32_t gpioIntStatus (uint32_t portNum, uint32_t bitPos)

Gets the interrupt status for a specific port pin.


Arguments


portNum: the gpio port number [0..3]


bitPos: the bit position for the gpio pin [0..11]


Returns


'1' if an interrupt was raised by the specified pin, otherwise '0'.


See the example for gpioIntClear.


#include "core/cpu/cpu.h"
#include "core/gpio/gpio.h"

int main (void)
{
    cpuInit();
    gpioInit();

    // Set GPIO1.8 to input
    gpioSetDir(1, 8, gpioDirection_Input);

    // Disable the internal pullup/down resistor on P1.8
    gpioSetPullup (&IOCON_PIO1_8, gpioPullupMode_Inactive);

    // Setup an interrupt on GPIO1.8
    gpioSetInterrupt(1,                               // Port
                     8,                               // Pin
                     gpioInterruptSense_Edge,         // Edge Sensitive
                     gpioInterruptEdge_Single,        // Single Edge
                     gpioInterruptEvent_ActiveHigh);  // Active High
    // Enable the interrupt

    gpioIntEnable(1, 8);

    while (1);
}


// IRQ Handler for GPIO Port 1

void PIOINT1_IRQHandler(void)
{
  uint32_t regVal;

  // Check if pin 1.8 raised the interrupt  

  regVal = gpioIntStatus(1, 8);

  if (regVal)
  {
    // Do Something
    ...
    // Clear the interrupt
    gpioIntClear(1, 8);
  }
  return;
}

See the example for gpioSetInterrupt to configure an interrupt.

.END



CooCox NVIC Example - Author: CooCox

http://www.coocox.org/show_exam/NVIC/82.html

Description:

This example describes how to configure the Nested Vectored Interrupt Controller(NVIC)IRQ Channels:

* Enable the following external interrupts in NVIC: LVD, BOD, WDT.

* Configure the priority of each external interrupt.

* Set each external interrupt pending bit during a lower priority ISR(Interrupt Service Routine).

* Show the order of interrupts by LEDs.

This example runs on HT32F175x/275x Development board.

Example Code

#include "ht32f175x_275x.h"
#include "ht32f175x_275x_gpio.h"
#include "ht32f175x_275x_ckcu.h"
#include "ht32f175x_275x_misc.h"

#define DELAY_TIME    0x7FFFFF

void LEDInit(void);
void LVD_IRQHandler(void);
void BOD_IRQHandler(void);
void WDT_IRQHandler(void);
void Delay(vu32 count);

void External_Interrupt(void)
{
    //
    // Config led pins
    //
    LEDInit();
    
    //
    // Enable the Interrupts
    //
    NVIC_EnableIRQ(LVD_IRQn);
    NVIC_EnableIRQ(BOD_IRQn);
    NVIC_EnableIRQ(WDT_IRQn);

    //
    // Configure 2 bits for preemption priority 2 bits for subpriority
    //
    NVIC_SetPriorityGrouping(5);

    //
    // Configure the External Interrupts Priority
    //
    NVIC_SetPriority(LVD_IRQn, NVIC_EncodePriority(5, 2, 0));
    NVIC_SetPriority(BOD_IRQn, NVIC_EncodePriority(5, 1, 0));
    NVIC_SetPriority(WDT_IRQn, NVIC_EncodePriority(5, 0, 0));

    //
    // Generate LVD Interrupt
    //
    NVIC_SetPendingIRQ(LVD_IRQn);

    //
    // Check on the LEDs flash sequence
    //
    while(1);
}

void LEDInit(void)
{
    //
    // Enable PA,PB clock
    //
    CKCU_APBPerip0ClockConfig(CKCU_APBEN0_PA, ENABLE);
    CKCU_APBPerip0ClockConfig(CKCU_APBEN0_PB, ENABLE);

    GPIO_SetOutBits(GPIOA, GPIO_PIN_15);
    GPIO_SetOutBits(GPIOB, GPIO_PIN_0);
    GPIO_SetOutBits(GPIOB, GPIO_PIN_1);

    //
    // Set PA15,PB0,PB1 as output mode
    //
    GPIO_DirectionConfig(GPIOA, GPIO_PIN_15, GPIO_DIR_OUT);
    GPIO_DirectionConfig(GPIOB, GPIO_PIN_0, GPIO_DIR_OUT);
    GPIO_DirectionConfig(GPIOB, GPIO_PIN_1, GPIO_DIR_OUT);
}

void LVD_IRQHandler(void)
{
    //
    // Generate BOD Interrupt
    //
    NVIC_SetPendingIRQ(BOD_IRQn);

    //
    // Turn on LED3
    //
    GPIO_ClearOutBits(GPIOB, GPIO_PIN_1);
    Delay(DELAY_TIME);
}

void BOD_IRQHandler(void)
{
    //
    // Generate WDT Interrupt
    // 
    NVIC_SetPendingIRQ(WDT_IRQn);

    //
    // Turn on LED2 
    //
    GPIO_ClearOutBits(GPIOB, GPIO_PIN_0);
    Delay(DELAY_TIME);
}

void WDT_IRQHandler(void)
{
    //
    // Turn on LED1
    //
    GPIO_ClearOutBits(GPIOA, GPIO_PIN_15);
    Delay(DELAY_TIME);
}

void Delay(vu32 count)
{
    while(count--);
}


Somy IRQ Example - Somy


#include "lpc1114.h"
#include "typedef.h"

#define  LED2 (1 << 7)    //PIO2_7
#define  LED3 (1 << 8)    //PIO2_8
#define  LED4 (1 << 5)    //PIO2_5
#define  BEEP (1 << 7)    //GPIO0-7
#define  TMR16B0_CLK      (1<<7)
#define  KEY1  (1 << 2)   //PIO0_2
uint8  KEY1_FLAG = 0;

void delay(uint32 time)
{
   uint32 i,j;
   for(i=0; i<time; i++)
      for(j=0; j<50000; j++);
}

sys_AHB_clk_ctrl(uint32 xxx_ckl)
{
     AHBCLKCTRL |= xxx_ckl;
}

sys_ahb_clk_enable(uint32 xxx_ckl)
{
     AHBCLKCTRL |= xxx_ckl;
}

sys_ahb_clk_disable(uint32 xxx_ckl)
{
     AHBCLKCTRL &= ~xxx_ckl;
}
//

void pio2_data_clr(uint32 num)
{
    GPIO2DATA_ALL &= ~num;     // On
}

void pio0_data_clr(uint32 num)
{
    GPIO0DATA_ALL &= ~num;     // Off
}

void pio0_dir_set(uint32 num)
{        
 GPIO0DIR |= num;  
}

void pio2_dir_set(uint32 num)
{        
 GPIO2DIR |= num;  
}

void PIO02_irq_init(uint32 num)
{        
 GPIO0IS &= ~(num); // select PIO0.2 edge trigger
 GPIO0IEV &=~(num);//  select PIO0.2 falling edge trigger 
 GPIO0IM |= (num);  // set PIO0.2 interrupt not maskable  
}

void pio2_data_set(uint32 num)
{
    GPIO2DATA_ALL |= num;     // Off
}

void time0_init(void)

TMR16B0PR = 999; // set prescale for Hz
TMR16B0MCR = 0x03; // match channel match interrupt and reset TOTC
TMR16B0MR0 = 12000; // match value (1 second比
TMR16B0TCR = 0x3; // start and reset 0011
TMR16B0TCR = 0x01; // 0001
}

static __inline void NVIC_EnableIRQ(IRQn_Type IRQn)
{
   NVIC_ISER = (1 << ((uint32)(IRQn) & 0x1F)); 
}

void PIOINT0_IRQHandler(void)

   uint8 temp =0;
 if((GPIO0MIS&0x004)==0x004)    // check if PIO0.2 pin causing interrupt
 {
   delay(4);
   if( (GPIO0DATA_ALL & KEY1) != KEY1 )  // if Key1 pressed
         { 
    while((GPIO0DATA_ALL & KEY1) == 0);
    if( (GPIO2DATA_ALL & LED2) == 0 )  // if Key 1 pressed
       {   
         pio2_data_set( LED2 | LED3 | LED4 );
    }else{
        pio2_data_clr( LED2 | LED3 | LED4 );
    }
      } 
 }

 GPIO0IC = 0x04;  // clear interrupt;
 temp = temp;
}

int main(void)

  pio2_dir_set( LED2 | LED3 | LED4 ); 
  pio2_data_set( LED2 | LED3 | LED4 ); 

 PIO02_irq_init(KEY1);

  NVIC_EnableIRQ(EINT0_IRQn); 

  while(1);
}

.END

No comments:

Post a Comment