K60学习笔记二:PORT中断

1.C预备知识
关于断言:
断言其实就是一个宏定义

void assert_failed(char * ,int );  //断言失败执行的函数
#if define(DEBUG) //在DEBUG模式下进行调用 
#define ASSERT(expr) if(!(expr)) \
            assert_failed(_FILE_,_LINE_)  //宏定义,如果断言失败则执行函数
#else
#define ASSERT(exper) //非DEBUG模式下,不执行断言函数
#endif

void assert_failed(char * file,int line){
    printf("Assertion failed in %s at line %d\n",file,line);
    while(1){
        //死循环等待程序员检测为何断言失败
    }
} 

在C的编译器环境下有;

#include <assert.h> 
#include <stdio.h>
#include <stdlib.h>

int main(v0id){

    FILE * fp = NULL;

    fp = fopen("a.txt","w");
    assert(fp);
    fclose(fp);

    fp = fopen("b.txt","r");
    assert(fp); //这里出错,显示文件名和行数
    fclose(fp);

    return 0;

Assertion failed!  //断言失败
Program: C:\Users\    \Desktop\C\  \duanyan.exe
File: C:\Users\duanyan.c, Line 14
Expression: fp
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

}

进入正题:
PORT中断的步骤:
设置中断向量表里的中断服务函数,只有中断向量表位与icf的制定的RAM区域是,此函数才有效

void set_vector_handler(VECTORn_t vector ,中断函数名)
//VECTORn_t vector 为一种枚举类型:是中断向量表声明
{
    extern uint32 _VECTOR_RAM[];
    ASSERT(SCB_VTOR == (uint32)___VECTOR_RAM);//断言,是否在RAM中

    __VECTOR_RAM[vector] = (uint32)pfunc_handler;
}

//VECTORn_t vector 为一种枚举类型:是中断向量表声明

typedef enum
{
   Initial_Stack_Pointer_VECTORn    = 0,                /**< Initial stack pointer */
   Initial_Program_Counter_VECTORn  = 1,                /**< Initial program counter */
   NMI_VECTORn                      = 2,                /**< Non-maskable interrupt */
   Hard_Fault_VECTORn               = 3,                /**< Hard fault exception */
   Reserved4_VECTORn                = 4,                /**< Reserved interrupt 4 */
   Reserved5_VECTORn                = 5,                /**< Reserved interrupt 5 */
   Reserved6_VECTORn                = 6,                /**< Reserved interrupt 6 */
   Reserved7_VECTORn                = 7,                /**< Reserved interrupt 7 */
   Reserved8_VECTORn                = 8,                /**< Reserved interrupt 8 */
   Reserved9_VECTORn                = 9,                /**< Reserved interrupt 9 */
   Reserved10_VECTORn               = 10,               /**< Reserved interrupt 10 */
   SVCall_VECTORn                   = 11,               /**< A supervisor call exception */
   Reserved12_VECTORn               = 12,               /**< Reserved interrupt 12 */
   Reserved13_VECTORn               = 13,               /**< Reserved interrupt 13 */
   PendableSrvReq_VECTORn           = 14,               /**< PendSV exception - request for system level service */
   SysTick_VECTORn                  = 15,               /**< SysTick interrupt */
   DMA0_VECTORn                     = 16,               /**< DMA channel 0 transfer complete/error interrupt */
   DMA1_VECTORn                     = 17,               /**< DMA channel 1 transfer complete/error interrupt */
   DMA2_VECTORn                     = 18,               /**< DMA channel 2 transfer complete/error interrupt */
   DMA3_VECTORn                     = 19,               /**< DMA channel 3 transfer complete/error interrupt */
   Reserved20_VECTORn               = 20,               /**< Reserved interrupt 20 */
   FTFA_VECTORn                     = 21,               /**< FTFA command complete/read collision interrupt */
   LVD_LVW_VECTORn                  = 22,               /**< Low Voltage Detect, Low Voltage Warning */
   LLW_VECTORn                      = 23,               /**< Low Leakage Wakeup */
   I2C0_VECTORn                     = 24,               /**< I2C0 interrupt */
   I2C1_VECTORn                     = 25,               /**< I2C0 interrupt 25 */
   SPI0_VECTORn                     = 26,               /**< SPI0 interrupt */
   SPI1_VECTORn                     = 27,               /**< SPI1 interrupt */
   UART0_VECTORn                    = 28,               /**< UART0 status/error interrupt */
   UART1_VECTORn                    = 29,               /**< UART1 status/error interrupt */
   UART2_VECTORn                    = 30,               /**< UART2 status/error interrupt */
   ADC0_VECTORn                     = 31,               /**< ADC0 interrupt */
   CMP0_VECTORn                     = 32,               /**< CMP0 interrupt */
   TPM0_VECTORn                     = 33,               /**< TPM0 fault, overflow and channels interrupt */
   TPM1_VECTORn                     = 34,               /**< TPM1 fault, overflow and channels interrupt */
   TPM2_VECTORn                     = 35,               /**< TPM2 fault, overflow and channels interrupt */
   RTC_VECTORn                      = 36,               /**< RTC interrupt */
   RTC_Seconds_VECTORn              = 37,               /**< RTC seconds interrupt */
   PIT_VECTORn                      = 38,               /**< PIT timer interrupt */
   I2S0_VECTORn                     = 39,               /**< I2S0 transmit interrupt */
   USB0_VECTORn                     = 40,               /**< USB0 interrupt */
   DAC0_VECTORn                     = 41,               /**< DAC0 interrupt */
   TSI0_VECTORn                     = 42,               /**< TSI0 interrupt */
   MCG_VECTORn                      = 43,               /**< MCG interrupt */
   LPTMR_VECTORn                    = 44,                 /**< LPTimer interrupt */
   Reserved45_VECTORn               = 45,               /**< Reserved interrupt 45 */
   PORTA_VECTORn                    = 46,               /**< Port A interrupt */
   PORTC_PORTD_VECTORn              = 47                /**< Port C and port D interrupt */
} VECTORn_t;
//PORT端口外部中断函数:
void porta_handler(void)
{
    uint8  n = 0;    //引脚号
    n = 6;
    if(PORTA_ISFR & (1 << n))           //PTA6触发中断
    {
        PORTA_ISFR  = (1 << n);        //写1清中断标志位

    }
}

你可能感兴趣的:(K60学习笔记二:PORT中断)