[综合信息] 【华大测评】02+ GPIO_LED(附完整工程)

[复制链接]
 楼主| caizhiwei 发表于 2020-7-2 08:58 | 显示全部楼层 |阅读模式
     任何一款开发板,都要从硬件原理,再到软件编程开始,那么首先开始的肯定是LED。

458885efd2d3d32dfe.png
1. 电路上还是非常满意,首先,IO通过灌电流驱动一个LED足够有余,但是板子并没有节省成本去这样做,还是采用传统的三极管放大电路,去驱动LED。
2. 关于GPIO的配置,需要的注意如下:
1575efd2ebfccf40.png
官方历程的配置很随意,明明是LED输出,使能外部中断干啥呢?该有配置却没有;
修改后如下:
驱动能力配置为最高,且非开漏(CMOS)输出。
  1. stc_port_init_t stcPortInit;

  2.     /* configuration structure initialization */
  3.     MEM_ZERO_STRUCT(stcPortInit);

  4.     stcPortInit.enPinMode = Pin_Mode_Out;
  5.         stcPortInit.enPinDrv =  Pin_Drv_H;
  6.     stcPortInit.enPinOType = Pin_OType_Cmos;
  7.     stcPortInit.enPullUp = Enable;

  8.     /* LED0 Port/Pin initialization */
  9.     PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);

  10.     /* LED1 Port/Pin initialization */
  11.     PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);

  12.     /* LED2 Port/Pin initialization */
  13.     PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);

  14.     /* LED3 Port/Pin initialization */
  15.     PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
  16.         


对于初始化后的输出IO口,必须给一个初始电平,这是一个良好的编程习惯。
  1. ///< LED关闭
  2.         PORT_ResetBits(LED0_PORT, LED0_PIN);
  3.     PORT_ResetBits(LED1_PORT, LED1_PIN);
  4.     PORT_ResetBits(LED2_PORT, LED2_PIN);
  5.         PORT_ResetBits(LED3_PORT, LED3_PIN);
自己编写一个bsp驱动,添加进工程即可。
效果图:
607825efd30c11f7c7.png

工程文件: hc32f46x_ddl.rar (1.69 MB, 下载次数: 29)
martinhu 发表于 2020-7-2 09:34 | 显示全部楼层
可以先给初始化电平,再使能输出
 楼主| caizhiwei 发表于 2020-7-7 22:19 | 显示全部楼层
今天对上次测评的代码进行了优化。
 楼主| caizhiwei 发表于 2020-7-7 22:20 | 显示全部楼层
caizhiwei 发表于 2020-7-7 22:19
今天对上次测评的代码进行了优化。

代码贴上来,新增了一个led反转接口函数。
  1. #include "bsp_led.h"

  2. void BSP_Led_Init(void)
  3. {

  4.         stc_port_init_t stcPortInit;

  5.     /* configuration structure initialization */
  6.     MEM_ZERO_STRUCT(stcPortInit);

  7.     stcPortInit.enPinMode = Pin_Mode_Out;
  8.         stcPortInit.enPinDrv =  Pin_Drv_H;
  9.     stcPortInit.enPinOType = Pin_OType_Cmos;
  10.     stcPortInit.enPullUp = Enable;

  11.     /* LED0 Port/Pin initialization */
  12.     PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);

  13.     /* LED1 Port/Pin initialization */
  14.     PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);

  15.     /* LED2 Port/Pin initialization */
  16.     PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);

  17.     /* LED3 Port/Pin initialization */
  18.     PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
  19.        
  20.         ///< LED关闭
  21.         PORT_ResetBits(LED0_PORT, LED0_PIN);
  22.     PORT_ResetBits(LED1_PORT, LED1_PIN);
  23.     PORT_ResetBits(LED2_PORT, LED2_PIN);
  24.         PORT_ResetBits(LED3_PORT, LED3_PIN);

  25. }


  26. void BSP_led_blink(en_led_t LED)
  27. {
  28.         if(LED == LED_RED)
  29.         {
  30.                 PORT_Toggle(LED0_PORT, LED0_PIN);
  31.         }
  32.         if(LED == LED_GREEN )
  33.         {
  34.                 PORT_Toggle(LED1_PORT, LED1_PIN);;
  35.         }
  36.         if(LED == LED_YELLOW)
  37.         {
  38.                 PORT_Toggle(LED2_PORT, LED2_PIN);
  39.         }
  40.         if(LED == LED_BLUE)
  41.         {
  42.                 PORT_Toggle(LED3_PORT, LED3_PIN);
  43.         }
  44.         return;
  45. }







 楼主| caizhiwei 发表于 2020-7-7 22:21 | 显示全部楼层
头文件:
  1. #ifndef __BSP_LED_H__
  2. #define __BSP_LED_H__

  3. #include "hc32f46x_gpio.h"

  4. /* LED0 Port/Pin definition */
  5. #define  LED0_PORT        (PortE)
  6. #define  LED0_PIN         (Pin06)

  7. /* LED1 Port/Pin definition */
  8. #define  LED1_PORT        (PortA)
  9. #define  LED1_PIN         (Pin07)

  10. /* LED2 Port/Pin definition */
  11. #define  LED2_PORT        (PortB)
  12. #define  LED2_PIN         (Pin05)

  13. /* LED3 Port/Pin definition */
  14. #define  LED3_PORT        (PortB)
  15. #define  LED3_PIN         (Pin09)

  16. /* LED0~3 toggle definition */
  17. #define  LED0_TOGGLE()    (PORT_Toggle(LED0_PORT, LED0_PIN))
  18. #define  LED1_TOGGLE()    (PORT_Toggle(LED1_PORT, LED1_PIN))
  19. #define  LED2_TOGGLE()    (PORT_Toggle(LED2_PORT, LED2_PIN))
  20. #define  LED3_TOGGLE()    (PORT_Toggle(LED3_PORT, LED3_PIN))

  21. #define  DLY_MS           (100ul)

  22. typedef enum led
  23. {
  24.     LED_RED  = 0u,                 
  25.     LED_YELLOW  = 1u,              
  26.     LED_BLUE  = 2u,                 
  27.     LED_GREEN  = 3u,               
  28. }en_led_t;


  29. void BSP_Led_Init(void);

  30. void BSP_led_blink(en_led_t LED);


  31. #endif


guojunhope 发表于 2020-7-9 21:05 | 显示全部楼层
能不盲从原厂示例代码,值得学习!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

857

帖子

16

粉丝
快速回复 返回顶部 返回列表