当前位置:   article > 正文

HT32F52352 点灯+HT32延时函数_ht32f52367的延时

ht32f52367的延时

首先知道在HT32F52352中有两个可用的LED灯

 

可以看出对应为PC14         PC15

当PC15 PC14 为低电平时LED亮

led.h

  1. #ifndef _LED_H
  2. #define _LED_H
  3. #include "ht32f5xxxx_01.h"
  4. void LED_init(void);
  5. #endif

led.c

  1. #include "led.h"
  2. static void RCC_init() //时钟配置
  3. {
  4. CKCU_PeripClockConfig_TypeDef CCLOCK = {{0}}; //不开启外设时钟相应功能无法使用
  5. CCLOCK.Bit.PC = 1; //开启PC时钟
  6. CCLOCK.Bit.AFIO = 1; //开启复用功能时钟
  7. CKCU_PeripClockConfig(CCLOCK, ENABLE); //使能时钟
  8. }
  9. static void LED_GPIO_init()
  10. {
  11. HT_GPIO_TypeDef* LED_gpio;
  12. LED_gpio=HT_GPIOC;
  13. AFIO_GPxConfig(GPIO_PC, GPIO_PIN_14 | GPIO_PIN_15, AFIO_FUN_GPIO); //配置GPIO模式
  14. GPIO_DirectionConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_DIR_OUT);
  15. //配置GPIO输出 输入GPIO_DIR_IN 输出GPIO_DIR_OUT
  16. GPIO_PullResistorConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_PR_DISABLE);
  17. //配置上下拉 上拉­GPIO_PR_UP 下拉­GPIO_PR_DOWN
  18. GPIO_DriveConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_DV_8MA);
  19. //选择GPIO引脚驱动电压 GPIO_DV_4/8/12/16MA
  20. }
  21. void LED_init()
  22. {
  23. RCC_init();
  24. LED_GPIO_init();
  25. }

 main.c

  1. int main(void)
  2. {
  3. GPIO_Configuration();
  4. LED_init();
  5. //0为低电平 1为高电平
  6. //因为低电平LED亮 所以0 LED亮 1 LED灭
  7. while (1)
  8. {
  9. GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,0);
  10. delay_ms(500);
  11. GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,1);
  12. delay_ms(500);
  13. }
  14. }

因为上面会用到延时函数

所以:

delay.h

  1. #ifndef __DELAY_H
  2. #define __DELAY_H
  3. #include "ht32_cm0plus_misc.h"
  4. void delay_s(u16 s);
  5. void delay_ms(u16 ms);
  6. void delay_us(u32 us);
  7. #endif

delay.c

  1. #include "ht32_cm0plus_misc.h"
  2. #include "delay.h"
  3. //mS
  4. void delay_us(u32 us)
  5. {
  6. u32 i;
  7. SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK); //
  8. SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000000); //
  9. SYSTICK_IntConfig(DISABLE); //
  10. SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR); //
  11. SYSTICK_CounterCmd(SYSTICK_COUNTER_ENABLE); //
  12. for( i = 0;i < us;i++ )
  13. {
  14. while( !( (SysTick->CTRL) & (1<<16) ) );
  15. }
  16. SYSTICK_CounterCmd(SYSTICK_COUNTER_DISABLE); //
  17. SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR); //
  18. }
  19. void delay_ms(u16 ms){ //mS
  20. while( ms-- != 0){
  21. delay_us(1000); //1000
  22. }
  23. }
  24. void delay_s(u16 s){ //S
  25. while( s-- != 0){
  26. delay_ms(1000); //1000
  27. }
  28. }

main.c(全)

  1. /*********************************************************************************************************//**
  2. * @file GPIO/Output/main.c
  3. * @version $Rev:: 5805 $
  4. * @date $Date:: 2022-04-12 #$
  5. * @brief Main program.
  6. *************************************************************************************************************
  7. * @attention
  8. *
  9. * Firmware Disclaimer Information
  10. *
  11. * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
  12. * code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
  13. * proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
  14. * other intellectual property laws.
  15. *
  16. * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
  17. * code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
  18. * other than HOLTEK and the customer.
  19. *
  20. * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
  21. * only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
  22. * the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
  23. * the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
  24. *
  25. * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
  26. ************************************************************************************************************/
  27. /* Includes ------------------------------------------------------------------------------------------------*/
  28. #include "ht32.h"
  29. #include "ht32_board.h"
  30. #include "led.h"
  31. #include "delay.h"
  32. /** @addtogroup HT32_Series_Peripheral_Examples HT32 Peripheral Examples
  33. * @{
  34. */
  35. /** @addtogroup GPIO_Examples GPIO
  36. * @{
  37. */
  38. /** @addtogroup Output
  39. * @{
  40. */
  41. /* Private function prototypes -----------------------------------------------------------------------------*/
  42. void GPIO_Configuration(void);
  43. void GPIO_OutputBit(void);
  44. void GPIO_OutputData(void);
  45. static void __Delay(u32 count);
  46. /* Global functions ----------------------------------------------------------------------------------------*/
  47. /*********************************************************************************************************//**
  48. * @brief Main program.
  49. * @retval None
  50. ***********************************************************************************************************/
  51. int main(void)
  52. {
  53. GPIO_Configuration();
  54. LED_init();
  55. //
  56. while (1)
  57. {
  58. GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,0);
  59. delay_ms(500);
  60. GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,1);
  61. delay_ms(500);
  62. }
  63. }
  64. /*********************************************************************************************************//**
  65. * @brief Configure the GPIO as output mode.
  66. * @retval None
  67. ***********************************************************************************************************/
  68. void GPIO_Configuration(void)
  69. {
  70. { /* Enable peripheral clock */
  71. CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
  72. CKCUClock.Bit.AFIO = 1;
  73. CKCUClock.Bit.PB = 1;
  74. CKCU_PeripClockConfig(CKCUClock, ENABLE);
  75. }
  76. { /* Configure GPIO as output mode */
  77. /* Configure AFIO mode as GPIO */
  78. AFIO_GPxConfig(GPIO_PB, AFIO_PIN_1, AFIO_FUN_GPIO);
  79. /* Configure GPIO pull resistor */
  80. GPIO_PullResistorConfig(HT_GPIOB, GPIO_PIN_1, GPIO_PR_DOWN);
  81. /* Default value RESET/SET */
  82. GPIO_WriteOutBits(HT_GPIOB, GPIO_PIN_1, RESET);
  83. /* Configure GPIO direction as output */
  84. GPIO_DirectionConfig(HT_GPIOB, GPIO_PIN_1, GPIO_DIR_OUT);
  85. }
  86. }
  87. /*********************************************************************************************************//**
  88. * @brief GPIO Output bit test
  89. * @retval None
  90. ***********************************************************************************************************/
  91. void GPIO_OutputBit(void)
  92. {
  93. GPIO_SetOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = HIGH
  94. __Delay(500000);
  95. GPIO_ClearOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = LOW
  96. __Delay(5000000);
  97. }
  98. /*********************************************************************************************************//**
  99. * @brief GPIO Output data test
  100. * @retval None
  101. ***********************************************************************************************************/
  102. void GPIO_OutputData(void)
  103. {
  104. u16 uOutputData;
  105. uOutputData = GPIO_ReadOutData(HT_GPIOB);
  106. uOutputData |= GPIO_PIN_1; // GPIO = HIGH
  107. GPIO_WriteOutData(HT_GPIOB, uOutputData);
  108. __Delay(500000);
  109. uOutputData = GPIO_ReadOutData(HT_GPIOB);
  110. uOutputData &= ~(GPIO_PIN_1); // GPIO = LOW
  111. GPIO_WriteOutData(HT_GPIOB, uOutputData);
  112. __Delay(5000000);
  113. }
  114. #if (HT32_LIB_DEBUG == 1)
  115. /*********************************************************************************************************//**
  116. * @brief Report both the error name of the source file and the source line number.
  117. * @param filename: pointer to the source file name.
  118. * @param uline: error line source number.
  119. * @retval None
  120. ***********************************************************************************************************/
  121. void assert_error(u8* filename, u32 uline)
  122. {
  123. /*
  124. This function is called by IP library that the invalid parameters has been passed to the library API.
  125. Debug message can be added here.
  126. Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
  127. */
  128. while (1)
  129. {
  130. }
  131. }
  132. #endif
  133. /* Private functions ---------------------------------------------------------------------------------------*/
  134. /*********************************************************************************************************//**
  135. * @brief delay function
  136. * @param count: delay count for loop
  137. * @retval None
  138. ***********************************************************************************************************/
  139. static void __Delay(u32 count)
  140. {
  141. while (count--)
  142. {
  143. __NOP(); // Prevent delay loop be optimized
  144. }
  145. }
  146. /**
  147. * @}
  148. */
  149. /**
  150. * @}
  151. */
  152. /**
  153. * @}
  154. */

实现两个LED灯500ms一闪一灭

注意:烧录程序后,需给单片机断电后,再供电才能实现功能,或者点复位按钮

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/565924
推荐阅读
相关标签
  

闽ICP备14008679号