[开发板] 【CW32L031CxTx StartKit评估板测评】GPIO输入和输出测试

[复制链接]
 楼主| tlled 发表于 2023-10-26 23:38 | 显示全部楼层 |阅读模式
GPIO, IO, cw, gp
测试下开发板的GPIO输入与输出测试,开发板上的按键采集输入,LED指示灯显示GPIO的输入变化。

一、硬件电路图

按键和LED灯部分电路图
001.jpg

二、测试程序

2.1、key.c
  1. #include "main.h"
  2. #include "key/key.h"
  3. #include "led/led.h"
  4. #include "usart/usart.h"

  5. void init_key(void)
  6. {       
  7.         GPIO_InitTypeDef GPIO_InitStructure;
  8.        
  9.         //KEY1
  10.   GPIO_InitStructure.Pins = KEY1_GPIO_PIN;
  11.   GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  12.         GPIO_InitStructure.IT = GPIO_IT_FALLING;
  13.   GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);

  14.   //KEY2
  15.   GPIO_InitStructure.Pins = KEY2_GPIO_PIN;
  16.   GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  17.         GPIO_InitStructure.IT = GPIO_IT_FALLING;
  18.   GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
  19.                
  20. }

  21. uint8_t key_scan(uint8_t mode)
  22. {
  23.         static uint8_t key_up = 1;
  24.         uint8_t keyval = 0;
  25.        

  26.         if (mode) key_up = 1;
  27.                

  28.         if (key_up && ((PA01_GETVALUE() == 0)||(PA02_GETVALUE() == 0)))  
  29.         {
  30.                         SysTickDelay(10);
  31.                         key_up = 0;
  32.                        
  33.                         if (PA01_GETVALUE() == 0) keyval = KEY2_PRES;
  34.                         if (PA02_GETVALUE() == 0) keyval = KEY1_PRES;
  35.         }
  36.         else if ((PA01_GETVALUE()!=0) && (PA02_GETVALUE()!=0))      
  37.         {
  38.                         key_up = 1;
  39.         }

  40.         return keyval;            
  41. }

  42. void key_test(void)
  43. {
  44.         uint8_t key;
  45.         key = key_scan(0);               
  46.         if (key)
  47.         {
  48.                         switch (key)
  49.                         {
  50.                                         case KEY1_PRES:   
  51.                                                         led1_tog();         
  52.                                                         break;
  53.                                         case KEY2_PRES:
  54.                                                         led2_tog();         
  55.                                                         break;
  56.                         }
  57.         }
  58. }




2.2、led.c
  1. #include "main.h"
  2. #include "led/led.h"

  3. #define LED_GPIO_PORT CW_GPIOB
  4. #define LED_GPIO_PINS GPIO_PIN_8 | GPIO_PIN_9

  5. void init_led(void)
  6. {
  7.         GPIO_InitTypeDef GPIO_InitStructure;
  8.        
  9.         //LED1
  10.         GPIO_InitStructure.Pins = LED1_GPIO_PIN;
  11.   GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  12.         GPIO_InitStructure.IT = GPIO_IT_NONE;
  13.   GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
  14.        
  15.         //LED2
  16.         GPIO_InitStructure.Pins = LED2_GPIO_PIN;
  17.   GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  18.         GPIO_InitStructure.IT = GPIO_IT_NONE;
  19.   GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);

  20. }


2.3、main.c
  1. #include "main.h"
  2. #include "led/led.h"
  3. #include "key/key.h"
  4. #include "usart/usart.h"

  5. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  6. /******************************************************************************
  7. * Local function prototypes ('static')
  8. ******************************************************************************/
  9. void RCC_Configuration(void);
  10. void GPIO_Configuration(void);
  11. void NVIC_Configuration(void);


  12. /******************************************************************************
  13. * Local variable definitions ('static')                                      *
  14. ******************************************************************************/
  15.        
  16. //KEY
  17. volatile uint8_t gKey1Status,gKey2Status;  /* set to 1 after User Button interrupt  */
  18.        

  19. int32_t main(void)
  20. {
  21.   RCC_Configuration();  
  22.   NVIC_Configuration();
  23.         init_led();
  24.         init_key();  
  25.        
  26.         gKey1Status = 0;       
  27.         gKey2Status = 0;       
  28.        
  29.         printf("zhang\r\n");
  30.         while(1)
  31.         {               
  32.                 key_test();
  33.         }
  34. }



三、程序运行

gpio.gif

您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部