单片机/MCU论坛
直播中

卢松涛

12年用户 419经验值
擅长:嵌入式技术
私信 关注
[文章]

【MAX32660试用体验】触摸台灯

周日在Eclipse IDE中加载了GPIO Example 。并调试下, 1.png
  1. /* **** Includes **** */
  2. #include
  3. #include
  4. #include "mxc_config.h"
  5. #include "board.h"
  6. #include "gpio.h"
  7. #include "tmr_utils.h"

  8. /* **** Definitions **** */
  9. #define GPIO_PORT_IN                PORT_0
  10. #define GPIO_PIN_IN                 PIN_12

  11. #define GPIO_PORT_OUT               PORT_0
  12. #define GPIO_PIN_OUT                PIN_13

  13. #define GPIO_PORT_INTERRUPT_IN      PORT_0
  14. #define GPIO_PIN_INTERRUPT_IN       PIN_3

  15. #define GPIO_PORT_INTERRUPT_STATUS  PORT_0
  16. #define GPIO_PIN_INTERRUPT_STATUS   PIN_2

  17. /* **** Globals **** */

  18. /* **** Functions **** */
  19. void gpio_isr(void *cbdata)
  20. {
  21.     GPIO_OutToggle((gpio_cfg_t*)cbdata);
  22. }

  23. int main(void)
  24. {
  25.     gpio_cfg_t gpio_in;
  26.     gpio_cfg_t gpio_out;
  27.     gpio_cfg_t gpio_interrupt;
  28.     gpio_cfg_t gpio_interrupt_status;

  29.     printf("nn***** GPIO Example ******nn");
  30.     printf("1. This example reads P0.12 (S1) and outputs the same state onto P0.13 (DS1).n");
  31.     printf("2. An interrupt is set up on P0.3. P0.2 toggles when that interrupt occurs.nn");

  32.     /* Setup interrupt status pin as an output so we can toggle it on each interrupt. */
  33.     gpio_interrupt_status.port = GPIO_PORT_INTERRUPT_STATUS;
  34.     gpio_interrupt_status.mask = GPIO_PIN_INTERRUPT_STATUS;
  35.     gpio_interrupt_status.pad = GPIO_PAD_NONE;
  36.     gpio_interrupt_status.func = GPIO_FUNC_OUT;
  37.     GPIO_Config(&gpio_interrupt_status);

  38.     /* Set up interrupt on P0.3. */
  39.     /* Switch on EV kit is open when non-pressed, and grounded when pressed.  Use an internal pull-up so pin
  40.        reads high when button is not pressed. */
  41.     gpio_interrupt.port = GPIO_PORT_INTERRUPT_IN;
  42.     gpio_interrupt.mask = GPIO_PIN_INTERRUPT_IN;
  43.     gpio_interrupt.pad = GPIO_PAD_PULL_UP;
  44.     gpio_interrupt.func = GPIO_FUNC_IN;
  45.     GPIO_Config(&gpio_interrupt);
  46.     GPIO_RegisterCallback(&gpio_interrupt, gpio_isr, &gpio_interrupt_status);
  47.     GPIO_IntConfig(&gpio_interrupt, GPIO_INT_EDGE, GPIO_INT_FALLING);
  48.     GPIO_IntEnable(&gpio_interrupt);
  49.     NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(GPIO_PORT_INTERRUPT_IN));

  50.     /* Setup input pin. */
  51.     /* Switch on EV kit is open when non-pressed, and grounded when pressed.  Use an internal pull-up so pin
  52.        reads high when button is not pressed. */
  53.     gpio_in.port = GPIO_PORT_IN;
  54.     gpio_in.mask = GPIO_PIN_IN;
  55.     gpio_in.pad = GPIO_PAD_PULL_UP;
  56.     gpio_in.func = GPIO_FUNC_IN;
  57.     GPIO_Config(&gpio_in);

  58.     /* Setup output pin. */
  59.     gpio_out.port = GPIO_PORT_OUT;
  60.     gpio_out.mask = GPIO_PIN_OUT;
  61.     gpio_out.pad = GPIO_PAD_NONE;
  62.     gpio_out.func = GPIO_FUNC_OUT;
  63.     GPIO_Config(&gpio_out);

  64.     while (1) {
  65.         /* Read state of the input pin. */
  66.         if (GPIO_InGet(&gpio_in)) {
  67.             /* Input pin was high, set the output pin. */
  68.             GPIO_OutSet(&gpio_out);
  69.         } else {
  70.             /* Input pin was low, clear the output pin. */
  71.             GPIO_OutClr(&gpio_out);
  72.         }
  73.     }
  74. }

发现这个程序第一部分是读PIN_12到 PIN_13引脚。

与孩子商量能否用手触开关控制台灯。
于是找个电容式触摸模块和光隔继电器制作器件。
准备触摸模块到PIN_12, PIN_13保存状态到继电器到台灯。
  1. /* **** Includes **** */
  2. #include
  3. #include
  4. #include "mxc_config.h"
  5. #include "board.h"
  6. #include "gpio.h"
  7. #include "tmr_utils.h"
  8. #include "mxc_delay.h"
  9. /* **** Definitions **** */
  10. #define GPIO_PORT_IN                PORT_0
  11. #define GPIO_PIN_IN                 PIN_12
  12. /*LED Control PIN */
  13. #define GPIO_PORT_OUT               PORT_0
  14. #define GPIO_PIN_OUT                PIN_13



  15. /* **** Globals **** */

  16. /* **** Functions **** */


  17. int main(void)
  18. {
  19.     gpio_cfg_t gpio_in;
  20.     gpio_cfg_t gpio_out;
  21.     int count = 0;

  22.     /* Setup input pin. ta */

  23.     gpio_in.port = GPIO_PORT_IN;
  24.     gpio_in.mask = GPIO_PIN_IN;
  25.     gpio_in.pad = GPIO_PAD_NONE;
  26.     gpio_in.func = GPIO_FUNC_IN;

  27.     /* Setup output pin  FOR lamp. */
  28.     gpio_out.port = GPIO_PORT_OUT;
  29.     gpio_out.mask = GPIO_PIN_OUT;
  30.     gpio_out.pad = GPIO_PAD_NONE;
  31.     gpio_out.func = GPIO_FUNC_OUT;
  32.     GPIO_Config(&gpio_out);
  33.         /* Setup output  1 FOR lamp. */
  34.         GPIO_OutSet(&gpio_out);

  35.     printf("nn***** GPIO Example ******nn");
  36.     printf("1. This example reads P0.12 (S1) and outputs the same state onto P0.13 (lamp).n");
  37.     /*printf("2. An interrupt is set up on P0.3. P0.2 toggles when that interrupt occurs.nn");*/



  38.     while (1) {
  39.         /* Read state of the input pin. */
  40.         if (GPIO_InGet(&gpio_in)) {
  41.             /* Input pin was high, set the output pin. */

  42.                  count = GPIO_OutGet(&gpio_out);
  43.                  printf("count = %dn", count);
  44.                  if(count) {
  45.                          GPIO_OutClr(&gpio_out);
  46.                          printf("TO OFF = %dn", count);
  47.                  }
  48.                  else {
  49.                          GPIO_OutSet(&gpio_out);
  50.                          printf("TO ON = %dn", count);
  51.                  }
  52.                  while (GPIO_InGet(&gpio_in)) {
  53.                          mxc_delay(MXC_DELAY_MSEC(200));
  54.              }
  55.                 }
  56.         }
  57. }

调试时 GPIO_OutGet(&gpio_out)读为0或8192。
2.png
为2**13,为13脚号。
实测成功

回帖(1)

clenghin

2019-3-8 09:55:42
请问,怎么在eclipse上安装GPIO,网上找不到安装教程啊
举报

更多回帖

发帖
×
20
完善资料,
赚取积分