LPC1114外部中断程序

  • Post author:
  • Post category:其他



main.c

如下:

#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "main.h"
#include "Key.h"
#include "LCD.h"

void Delay_Sec ( unsigned char Time ) { /* “1s * Time”延时函数 */
    u16 i;

    while ( Time ) {
        for ( i = 0; i < 3000000; i++ );

        Time--;
    }
}

void Delay_Ms ( unsigned int Time ) { /* “1ms * Time”延时函数 */
    u16 i;

    while ( Time ) {
        for ( i = 0; i < 3000; i++ );

        Time--;
    }
}

void Delay_Us ( unsigned int Time ) { /* “1us * Time”延时函数 */
    u8 i;

    while ( Time ) {
        for ( i = 0; i < 3; i++ );

        Time--;
    }
}

int main ( void ) {
    // SystemInit(); /* 系统初始化,主要是设置主时钟 */
    // GPIOInit(); /* IO口初始化,主要是开启GPIO时钟 */
    // GPIOSetDir( 0, 3, 1 ); /* 设置P0.3为输出(LPC1114单片机输出需设置为1,输入需设置为0) */
    LPC_GPIO0->DIR |= ( 0x1 << 3 ); /* 设置P0.3为输出 */
    LPC_GPIO2->DIR |= ( 0x1 << 9 );
    Key_GPIO_Init();
    Key_Intrrupt_Init();

    while ( 1 ) {
        // GPIOSetValue( 0, 3, 0 ); /* 给P0.3位写0 */
        // LPC_GPIO0->DATA &= ~(0x1 << 3); /* 给P0.3位写0 */
        LCD_RW_High;
        Delay_Ms ( 100 );
        LCD_RW_Low;
        Delay_Ms ( 100 );
    }
}


key.c

如下:

#include "LPC11XX.h"
#include "gpio.h"
#include "main.h"
#include "Key.h"

void Key_GPIO_Init ( void ) {
    LPC_GPIO1->DIR &= ~ ( 0x1 << 8 ); /* 设置P1.8为输入 */
}

void Key_Intrrupt_Init ( void ) { /* 配置中断 */
    GPIOSetInterrupt ( 1, 8, 1, 0, 0 ); /* P1.8低电平触发中断 */
    GPIOIntEnable ( 1, 8 ); /* 使能中断 */
    NVIC_EnableIRQ ( EINT1_IRQn ); /* 使能中断入口 */
}


gpio.c

如下:

#include "LPC11xx.h" /* LPC11xx Peripheral Registers */
#include "gpio.h"

void PIOINT1_IRQHandler ( void ) {
    uint32_t regVal;
    gpio1_counter++;
    regVal = GPIOIntStatus ( 1, 8 );

    if ( regVal ) {
        LPC_GPIO0->MASKED_ACCESS[ ( 1 << 3 )] = ( 0 << 3 );
        Delay_Ms ( 2000 );
        LPC_GPIO0->MASKED_ACCESS[ ( 1 << 3 )] = ( 1 << 3 );
        Delay_Ms ( 2000 );
        LPC_GPIO0->MASKED_ACCESS[ ( 1 << 3 )] = ( 0 << 3 );
        Delay_Ms ( 2000 );
        LPC_GPIO0->MASKED_ACCESS[ ( 1 << 3 )] = ( 1 << 3 );
        Delay_Ms ( 2000 );
        GPIOIntClear ( 1, 8 ); /* 清除中断标志位 */
    }

    return;
}