周日在Eclipse IDE中加载了GPIO Example 。并调试下,
- /* **** Includes **** */
- #include
- #include
- #include "mxc_config.h"
- #include "board.h"
- #include "gpio.h"
- #include "tmr_utils.h"
- /* **** Definitions **** */
- #define GPIO_PORT_IN PORT_0
- #define GPIO_PIN_IN PIN_12
- #define GPIO_PORT_OUT PORT_0
- #define GPIO_PIN_OUT PIN_13
- #define GPIO_PORT_INTERRUPT_IN PORT_0
- #define GPIO_PIN_INTERRUPT_IN PIN_3
- #define GPIO_PORT_INTERRUPT_STATUS PORT_0
- #define GPIO_PIN_INTERRUPT_STATUS PIN_2
- /* **** Globals **** */
- /* **** Functions **** */
- void gpio_isr(void *cbdata)
- {
- GPIO_OutToggle((gpio_cfg_t*)cbdata);
- }
- int main(void)
- {
- gpio_cfg_t gpio_in;
- gpio_cfg_t gpio_out;
- gpio_cfg_t gpio_interrupt;
- gpio_cfg_t gpio_interrupt_status;
- printf("nn***** GPIO Example ******nn");
- printf("1. This example reads P0.12 (S1) and outputs the same state onto P0.13 (DS1).n");
- printf("2. An interrupt is set up on P0.3. P0.2 toggles when that interrupt occurs.nn");
- /* Setup interrupt status pin as an output so we can toggle it on each interrupt. */
- gpio_interrupt_status.port = GPIO_PORT_INTERRUPT_STATUS;
- gpio_interrupt_status.mask = GPIO_PIN_INTERRUPT_STATUS;
- gpio_interrupt_status.pad = GPIO_PAD_NONE;
- gpio_interrupt_status.func = GPIO_FUNC_OUT;
- GPIO_Config(&gpio_interrupt_status);
- /* Set up interrupt on P0.3. */
- /* Switch on EV kit is open when non-pressed, and grounded when pressed. Use an internal pull-up so pin
- reads high when button is not pressed. */
- gpio_interrupt.port = GPIO_PORT_INTERRUPT_IN;
- gpio_interrupt.mask = GPIO_PIN_INTERRUPT_IN;
- gpio_interrupt.pad = GPIO_PAD_PULL_UP;
- gpio_interrupt.func = GPIO_FUNC_IN;
- GPIO_Config(&gpio_interrupt);
- GPIO_RegisterCallback(&gpio_interrupt, gpio_isr, &gpio_interrupt_status);
- GPIO_IntConfig(&gpio_interrupt, GPIO_INT_EDGE, GPIO_INT_FALLING);
- GPIO_IntEnable(&gpio_interrupt);
- NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(GPIO_PORT_INTERRUPT_IN));
- /* Setup input pin. */
- /* Switch on EV kit is open when non-pressed, and grounded when pressed. Use an internal pull-up so pin
- reads high when button is not pressed. */
- gpio_in.port = GPIO_PORT_IN;
- gpio_in.mask = GPIO_PIN_IN;
- gpio_in.pad = GPIO_PAD_PULL_UP;
- gpio_in.func = GPIO_FUNC_IN;
- GPIO_Config(&gpio_in);
- /* Setup output pin. */
- gpio_out.port = GPIO_PORT_OUT;
- gpio_out.mask = GPIO_PIN_OUT;
- gpio_out.pad = GPIO_PAD_NONE;
- gpio_out.func = GPIO_FUNC_OUT;
- GPIO_Config(&gpio_out);
- while (1) {
- /* Read state of the input pin. */
- if (GPIO_InGet(&gpio_in)) {
- /* Input pin was high, set the output pin. */
- GPIO_OutSet(&gpio_out);
- } else {
- /* Input pin was low, clear the output pin. */
- GPIO_OutClr(&gpio_out);
- }
- }
- }
发现这个程序第一部分是读PIN_12到 PIN_13引脚。
与孩子商量能否用手触开关控制台灯。
于是找个电容式触摸模块和光隔继电器制作器件。
准备触摸模块到PIN_12, PIN_13保存状态到继电器到台灯。
- /* **** Includes **** */
- #include
- #include
- #include "mxc_config.h"
- #include "board.h"
- #include "gpio.h"
- #include "tmr_utils.h"
- #include "mxc_delay.h"
- /* **** Definitions **** */
- #define GPIO_PORT_IN PORT_0
- #define GPIO_PIN_IN PIN_12
- /*LED Control PIN */
- #define GPIO_PORT_OUT PORT_0
- #define GPIO_PIN_OUT PIN_13
- /* **** Globals **** */
- /* **** Functions **** */
- int main(void)
- {
- gpio_cfg_t gpio_in;
- gpio_cfg_t gpio_out;
- int count = 0;
- /* Setup input pin. ta */
- gpio_in.port = GPIO_PORT_IN;
- gpio_in.mask = GPIO_PIN_IN;
- gpio_in.pad = GPIO_PAD_NONE;
- gpio_in.func = GPIO_FUNC_IN;
- /* Setup output pin FOR lamp. */
- gpio_out.port = GPIO_PORT_OUT;
- gpio_out.mask = GPIO_PIN_OUT;
- gpio_out.pad = GPIO_PAD_NONE;
- gpio_out.func = GPIO_FUNC_OUT;
- GPIO_Config(&gpio_out);
- /* Setup output 1 FOR lamp. */
- GPIO_OutSet(&gpio_out);
- printf("nn***** GPIO Example ******nn");
- printf("1. This example reads P0.12 (S1) and outputs the same state onto P0.13 (lamp).n");
- /*printf("2. An interrupt is set up on P0.3. P0.2 toggles when that interrupt occurs.nn");*/
- while (1) {
- /* Read state of the input pin. */
- if (GPIO_InGet(&gpio_in)) {
- /* Input pin was high, set the output pin. */
- count = GPIO_OutGet(&gpio_out);
- printf("count = %dn", count);
- if(count) {
- GPIO_OutClr(&gpio_out);
- printf("TO OFF = %dn", count);
- }
- else {
- GPIO_OutSet(&gpio_out);
- printf("TO ON = %dn", count);
- }
- while (GPIO_InGet(&gpio_in)) {
- mxc_delay(MXC_DELAY_MSEC(200));
- }
- }
- }
- }
调试时 GPIO_OutGet(&gpio_out)读为0或8192。
为2**13,为13脚号。
实测成功