导航

第六周-星期二-详细解说LPC12xx例程之LPC1220_GPIOInt

main.c文件内容如下:


  1     /****************************************Copyright (c)****************************************************
  2    **                            Guangzhou ZLGMCU Development Co., LTD
  3    **
  4    **                                 http://www.zlgmcu.com
  5    **
  6    **--------------File Info---------------------------------------------------------------------------------
  7    ** File name:           main.c
  8    ** Last modified Date:  2010-11-04
  9    ** Last Version:        V1.0
10    ** Descriptions:        The main() function example template
11    **
12    **--------------------------------------------------------------------------------------------------------
13    ** Created by:          He Zengfu
14    ** Created date:        2010-11-20
15    ** Version:             V1.00
16    ** Descriptions:        整理模板,添加用户应用程序
17    **
18    **--------------------------------------------------------------------------------------------------------
19    ** Modified by:         He Zengfu
20    ** Modified date:       2010-11-25
21    ** Version:             V1.00
22    ** Descriptions:        编写GPIO中断例程
23    **
24    **--------------------------------------------------------------------------------------------------------
25    ** Modified by:         Wu yuanlang
26    ** Modified date:       2010-12-24
27    ** Version:             V1.00
28    ** Descriptions:        检查、测试程序,并添加、修改注释和程序风格
29    **
30    ** Rechecked by:
31    *********************************************************************************************************/
32    #include "lpc12xx_libcfg.h"
33   
34    /*********************************************************************************************************
35      宏定义
36    *********************************************************************************************************/
37    #define LED_PORT    LPC_GPIO1                                           /* LED引脚端口P1                */
38    #define LED_PINS    2                                                   /* LED引脚P1.2                  */
39   
40   
41    #define KEY_PORT    LPC_GPIO0                                           /* KEY引脚端口P0                */
42    #define KEY_PINS    9                                                   /* KEY引脚P0.9                  */
43   
44    /*********************************************************************************************************
45      全局变量定义
46    *********************************************************************************************************/
47    static uint16_t GusGPIO1Port[7] = {                                     /* GPIO1端口引脚类型            */
48        IOCON_PIO_1_0,
49        IOCON_PIO_1_1,
50        IOCON_PIO_1_2,
51        IOCON_PIO_1_3,
52        IOCON_PIO_1_4,
53        IOCON_PIO_1_5,
54        IOCON_PIO_1_6
55    };
56   
57    static uint16_t GusGPIO0Port[32] ={                                     /* GPIO0端口引脚类型            */
58        IOCON_PIO_0_0,
59        IOCON_PIO_0_1,
60        IOCON_PIO_0_2,
61        IOCON_PIO_0_3,
62        IOCON_PIO_0_4,
63        IOCON_PIO_0_5,
64        IOCON_PIO_0_6,
65        IOCON_PIO_0_7,
66        IOCON_PIO_0_8,
67        IOCON_PIO_0_9,
68        IOCON_PIO_0_10,
69        IOCON_PIO_0_11,
70        IOCON_PIO_0_12,
71        IOCON_PIO_0_13,
72        IOCON_PIO_0_14,
73        IOCON_PIO_0_15,
74        IOCON_PIO_0_16,
75        IOCON_PIO_0_17,
76        IOCON_PIO_0_18,
77        IOCON_PIO_0_19,
78        IOCON_PIO_0_20,
79        IOCON_PIO_0_21,
80        IOCON_PIO_0_22,
81        IOCON_PIO_0_23,
82        IOCON_PIO_0_24,
83        IOCON_PIO_0_25,
84        IOCON_PIO_0_26,
85        IOCON_PIO_0_27,
86        IOCON_PIO_0_28,
87        IOCON_PIO_0_29,
88        IOCON_PIO_0_30,
89        IOCON_PIO_0_31
90    };
91   
92    /*********************************************************************************************************
93    ** Function name:       myDelay
94    ** Descriptions:        软件延时(ms)
95    ** input parameters:    无
96    ** output parameters:   无
97    ** Returned value:      无
98    *********************************************************************************************************/
99    void myDelay (uint32_t ulTime)
100    {
101        uint32_t i = 0;
102       
103        while (ulTime--) {
104            for (i = 0; i < 5000; i++);
105        }
106    }
107   
108    /*********************************************************************************************************
109    ** Function name:       ledOn
110    ** Descriptions:        点亮LED(输出低电平)
111    ** input parameters:    无
112    ** output parameters:   无
113    ** Returned value:      无
114    *********************************************************************************************************/
115    void ledOn (void)
116    {
117        GPIO_SetLowLevel(LED_PORT, LED_PINS, ENABLE);                       /* 输出低电平,点亮LED          */
118    }
119   
120    /*********************************************************************************************************
121    ** Function name:       ledOff
122    ** Descriptions:        熄灭LED(输出高电平)
123    ** input parameters:    无
124    ** output parameters:   无
125    ** Returned value:      无
126    *********************************************************************************************************/
127    void ledOff (void)
128    {
129        GPIO_SetHighLevel(LED_PORT, LED_PINS, ENABLE);                      /* 输出高电平,熄灭LED          */
130    }
131   
132    /*********************************************************************************************************
133    ** Function name:       ledInit
134    ** Descriptions:        LED初始化
135    ** input parameters:    无
136    ** output parameters:   无
137    ** Returned value:      无
138    *********************************************************************************************************/
139    void ledInit (void)
140    {
141        IOCON_PIO_CFG_Type PIO_mode;
142   
143        IOCON_StructInit(&PIO_mode);                                        /* 初始化端口模式               */
144        PIO_mode.type = GusGPIO1Port[LED_PINS];
145        IOCON_SetFunc(&PIO_mode);                                           /* 设置LED引脚为GPIO功能        */
146   
147        GPIO_SetDir(LED_PORT, LED_PINS, GPIO_DIR_OUTPUT);                   /* 设置LED引脚为输出            */
148       
149        ledOff();                                                           /* 熄灭LED                      */
150    }
151   
152    /*********************************************************************************************************
153    ** Function name:       PIOINT0_IRQHandler
154    ** Descriptions:        端口0中断服务函数
155    **
156    ** input parameters:    无
157    ** output parameters:   无
158    ** Returned value:      无
159    *********************************************************************************************************/
160    void PIOINT0_IRQHandler (void)
161    {
162        uint32_t regVal;
163   
164        regVal = GPIO_IntGetMaskStatus(KEY_PORT, KEY_PINS);
165   
166        if (regVal) {
167            GPIO_IntClear(KEY_PORT, KEY_PINS, ENABLE);                      /* 清除中断标志                 */
168           
169            ledOn();                                                        /* LED1闪烁一次                 */
170            myDelay(100);
171            ledOff();
172            myDelay(100);
173        }
174    }
175   
176    /*********************************************************************************************************
177    ** Function name:       GPIOINTInit
178    ** Descriptions:        GPIO中断初始化
179    ** input parameters:    无
180    ** output parameters:   无
181    ** Returned value:      无
182    *********************************************************************************************************/
183    void GPIOINTInit (void)
184    {
185        IOCON_PIO_CFG_Type PIO_mode;
186   
187        IOCON_StructInit(&PIO_mode);
188        PIO_mode.type =  GusGPIO0Port[KEY_PINS];                            /* 配置KEY引脚为GPIO功能        */
189        IOCON_SetFunc(&PIO_mode);
190   
191        GPIO_SetDir(KEY_PORT, KEY_PINS, GPIO_DIR_INPUT);                    /* 配置为输入                   */
192        GPIO_IntSetType(KEY_PORT, KEY_PINS, GPIO_INTERRUPT_FALLING);        /* 配置为下降沿中断             */
193        GPIO_IntSetMask(KEY_PORT, KEY_PINS, GPIO_MASK_NOT);                 /* 不屏蔽中断                   */
194   
195        NVIC_EnableIRQ(EINT0_IRQn);                                         /* 使能外部中断0中断            */
196    }
197   
198    /*********************************************************************************************************
199    ** Function name:       main
200    ** Descriptions:        主函数(函数入口)
201    **                      GPIO下降沿中断控制LED1
202    **     跳线连接:       P0.9接按键(按键按下输出低电平,按键与TinyM0 T12(M)需要共地)
203    **     操作方法:       运行程序,按动按键,观察LED1状态
204    **     现    象:       每按一次按键,LED1闪烁一次
205    ** input parameters:    无
206    ** output parameters:   无
207    ** Returned value:      无
208    *********************************************************************************************************/
209    int main (void)
210    {
211        SystemInit();                                                       /* 系统时钟初始化               */
212   
213        SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_IOCON, ENABLE);                     /* IOCON 模块使能               */
214        SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_GPIO0, ENABLE);                     /* GPIO0时钟使能                */
215        SYS_ConfigAHBCLK(SYS_AHBCLKCTRL_GPIO1, ENABLE);                     /* GPIO1时钟使能                */
216   
217        ledInit();
218        GPIOINTInit();
219   
220        while(1) {
221   
222        }
223    }
224   
225    #ifdef  DEBUG
226    /*********************************************************************************************************
227    ** Function name:       check_failed
228    ** Descriptions:
229    ** input parameters:    无
230    ** output parameters:   无
231    ** Returned value:      无
232    *********************************************************************************************************/
233    void check_failed (uint8_t *file, uint32_t line)
234    {
235        while(1);                                                           /* Infinite loop                */
236    }
237   
238    #endif
239   
240    /*********************************************************************************************************
241    End Of File
242    *********************************************************************************************************/


1.宏定义

定义了LED灯引脚为P1.2,KEY引脚为P0.9

 

37    #define LED_PORT    LPC_GPIO1                                           /* LED引脚端口P1                */
38    #define LED_PINS    2                                                   /* LED引脚P1.2                  */
39   
40   
41    #define KEY_PORT    LPC_GPIO0                                           /* KEY引脚端口P0                */
42    #define KEY_PINS    9                                                   /* KEY引脚P0.9                  */

2.全局变量

GPIO1端口引脚类型

GPIO0端口引脚类型

47    static uint16_t GusGPIO1Port[7] = {                                     /* GPIO1端口引脚类型            */
    48        IOCON_PIO_1_0,
    49        IOCON_PIO_1_1,
    50        IOCON_PIO_1_2,
    51        IOCON_PIO_1_3,
    52        IOCON_PIO_1_4,
    53        IOCON_PIO_1_5,
    54        IOCON_PIO_1_6
    55    };
    56   
    57    static uint16_t GusGPIO0Port[32] ={                                     /* GPIO0端口引脚类型            */
    58        IOCON_PIO_0_0,
    59        IOCON_PIO_0_1,
    60        IOCON_PIO_0_2,
    61        IOCON_PIO_0_3,
    62        IOCON_PIO_0_4,
    63        IOCON_PIO_0_5,
    64        IOCON_PIO_0_6,
    65        IOCON_PIO_0_7,
    66        IOCON_PIO_0_8,
    67        IOCON_PIO_0_9,
    68        IOCON_PIO_0_10,
    69        IOCON_PIO_0_11,
    70        IOCON_PIO_0_12,
    71        IOCON_PIO_0_13,
    72        IOCON_PIO_0_14,
    73        IOCON_PIO_0_15,
    74        IOCON_PIO_0_16,
    75        IOCON_PIO_0_17,
    76        IOCON_PIO_0_18,
    77        IOCON_PIO_0_19,
    78        IOCON_PIO_0_20,
    79        IOCON_PIO_0_21,
    80        IOCON_PIO_0_22,
    81        IOCON_PIO_0_23,
    82        IOCON_PIO_0_24,
    83        IOCON_PIO_0_25,
    84        IOCON_PIO_0_26,
    85        IOCON_PIO_0_27,
    86        IOCON_PIO_0_28,
    87        IOCON_PIO_0_29,
    88        IOCON_PIO_0_30,
    89        IOCON_PIO_0_31
    90    };

3.软件延时.

4.ledOn,ledOff(设置引脚为低电平点亮LED)

5.LedInit Led初始化

139    void ledInit (void)
140    {
141        IOCON_PIO_CFG_Type PIO_mode;
142   
143        IOCON_StructInit(&PIO_mode);                                        /* 初始化端口模式               */
144        PIO_mode.type = GusGPIO1Port[LED_PINS];
145        IOCON_SetFunc(&PIO_mode);                                           /* 设置LED引脚为GPIO功能        */
146   
147        GPIO_SetDir(LED_PORT, LED_PINS, GPIO_DIR_OUTPUT);                   /* 设置LED引脚为输出            */
148       
149        ledOff();                                                           /* 熄灭LED                      */
150    }

初始化的过程为:

  1. 空的引脚配置结构体IOCON_PIO_CFG_Type
  2. 初始化结构体(恢复默认值)IOCON_StructInit
  3. 结构体成员设置:(1)引脚指定 PIO_mode.type  (2)功能指定 IOCON_SetFunc(3)方向指定.GPIO_SetDir
  4. 熄灭LED ledOff()
  5. 6.中断服务函数

void PIOINT0_IRQHandler (void)

   161    {

   162        uint32_t regVal;

   163   

   164        regVal = GPIO_IntGetMaskStatus(KEY_PORT, KEY_PINS);

   165   //得到引脚的中断状态

   166        if (regVal) { //如果有中断,清除中断,led闪烁一次

   167            GPIO_IntClear(KEY_PORT, KEY_PINS, ENABLE);                      /* 清除中断标志                 */

   168           

   169            ledOn();                                                        /* LED1闪烁一次                 */

   170            myDelay(100);

   171            ledOff();

   172            myDelay(100);

   173        }

   174    }

    7.中断初始化

    183    void GPIOINTInit (void)
    184    {
    185        IOCON_PIO_CFG_Type PIO_mode;
    186   
    187        IOCON_StructInit(&PIO_mode);
    188        PIO_mode.type =  GusGPIO0Port[KEY_PINS];                            /* 配置KEY引脚为GPIO功能        */
    189        IOCON_SetFunc(&PIO_mode);
    190   
    191        GPIO_SetDir(KEY_PORT, KEY_PINS, GPIO_DIR_INPUT);                    /* 配置为输入                   */
    (和步骤5里的相同)
    192        GPIO_IntSetType(KEY_PORT, KEY_PINS, GPIO_INTERRUPT_FALLING);        /* 配置为下降沿中断             */
    193        GPIO_IntSetMask(KEY_PORT, KEY_PINS, GPIO_MASK_NOT);                 /* 不屏蔽中断                   */
    194   
    195        NVIC_EnableIRQ(EINT0_IRQn);                                         /* 使能外部中断0中断            */
    196    }


     

    8.主函数

    系统时钟初始化
    IOCON模块的时钟使能,GPIO0,GPIO1
    LED初始化
    中断初始化
    等待
    2013.4.2
    写于数图,2013年4月3日 00:32:31整理笔记.

posted on 2013-04-03 09:30  淅沥枫  阅读(680)  评论(0编辑  收藏  举报