• day5ARM


     循环点亮三个led灯

    方法1

    1. ------------------led.h----------------
    2. #ifndef __LED_H__
    3. #define __LED_H__
    4. #define RCC (*(volatile unsigned int *)0x50000A28)
    5. #define GPIOE ((GPIO_t *)0x50006000)
    6. #define GPIOF ((GPIO_t *)0x50007000)
    7. //结构体封装
    8. typedef struct
    9. {
    10. volatile unsigned int MODER;
    11. volatile unsigned int OTYPER;
    12. volatile unsigned int OSPEED;
    13. volatile unsigned int PUPDR;
    14. volatile unsigned int IDR;
    15. volatile unsigned int ODR;
    16. }GPIO_t;
    17. //RCC章节初始化
    18. void RCC_INIT();
    19. //LED1 ----> PE10
    20. //LED1 - GPIO章节初始化
    21. void LED1_GPIO_INIT();
    22. //LED1点亮
    23. void LED1_ON();
    24. //LED1熄灭
    25. void LED1_OFF();
    26. //LED2 ----> PF10
    27. //LED2 - GPIO章节初始化
    28. void LED2_GPIO_INIT();
    29. //LED2点亮
    30. void LED2_ON();
    31. //LED2熄灭
    32. void LED2_OFF();
    33. //LED3 ----> PF10
    34. //LED3 - GPIO章节初始化
    35. void LED3_GPIO_INIT();
    36. //LED3点亮
    37. void LED3_ON();
    38. //LED3熄灭
    39. void LED3_OFF();
    40. #endif
    41. ------------------led.c----------------
    42. #include "led.h"
    43. //LED1 ---> PE10
    44. //RCC章节初始化
    45. void RCC_INIT()
    46. {
    47. RCC = RCC | (0x3 << 4);
    48. }
    49. //LED1 - GPIO章节初始化
    50. void LED1_GPIO_INIT()
    51. {
    52. //设置为输出模式 0x50006000[21,20] -> 01
    53. GPIOE->MODER &= (~(0x3 << 20));
    54. GPIOE->MODER |= (0x1 << 20);
    55. //设置为推挽输出 0x50006004[10] -> 0
    56. GPIOE->OTYPER &= (~(0x1 << 10));
    57. //设置为低速模式 0x50006008[21,20] -> 00
    58. GPIOE->OSPEED &= (~(0x3 << 20));
    59. //设置为禁止上下拉电阻 0x5000600c[21,20] -> 00
    60. GPIOE->PUPDR &= (~(0x3 << 20));
    61. }
    62. //LED1点亮
    63. void LED1_ON()
    64. {
    65. //点亮LED1 0x50006014[10] -> 1
    66. GPIOE->ODR |= (0x1 << 10);
    67. }
    68. //LED1熄灭
    69. void LED1_OFF()
    70. {
    71. //熄灭LED1 0x50006014[10] -> 0
    72. GPIOE->ODR &= (~(0x1 << 10));
    73. }
    74. //LED2 ----> PF10
    75. //LED2 - GPIO章节初始化
    76. void LED2_GPIO_INIT()
    77. {
    78. //设置为输出模式 0x50007000[21,20] -> 01
    79. GPIOF->MODER &= (~(0x3 << 20));
    80. GPIOF->MODER |= (0x1 << 20);
    81. //设置为推挽输出 0x50007004[10] -> 0
    82. GPIOF->OTYPER &= (~(0x1 << 10));
    83. //设置为低速模式 0x50007008[21,20] -> 00
    84. GPIOF->OSPEED &= (~(0x3 << 20));
    85. //设置为禁止上下拉电阻 0x5000700c[21,20] -> 00
    86. GPIOF->PUPDR &= (~(0x3 << 20));
    87. }
    88. //LED2点亮
    89. void LED2_ON()
    90. {
    91. //点亮LED2 0x50007014[10] -> 1
    92. GPIOF->ODR |= (0x1 << 10);
    93. }
    94. //LED2熄灭
    95. void LED2_OFF()
    96. {
    97. //熄灭LED2 0x50007014[10] -> 0
    98. GPIOF->ODR &= (~(0x1 << 10));
    99. }
    100. //LED3 ----> PF10
    101. //LED3 - GPIO章节初始化
    102. void LED3_GPIO_INIT()
    103. {
    104. //设置为输出模式 0x50006000[17,16] -> 01
    105. GPIOE->MODER &= (~(0x3 << 16));
    106. GPIOE->MODER |= (0x1 << 16);
    107. //设置为推挽输出 0x50006004[8] -> 0
    108. GPIOE->OTYPER &= (~(0x1 << 8));
    109. //设置为低速模式 0x50006008[17,16] -> 00
    110. GPIOE->OSPEED &= (~(0x3 << 16));
    111. //设置为禁止上下拉电阻 0x5000600c[17,16] -> 00
    112. GPIOE->PUPDR &= (~(0x3 << 16));
    113. }
    114. //LED3点亮
    115. void LED3_ON()
    116. {
    117. //点亮LED3 0x50006014[8] -> 1
    118. GPIOE->ODR |= (0x1 << 8);
    119. }
    120. //LED3熄灭
    121. void LED3_OFF()
    122. {
    123. //熄灭LED3 0x50006014[8] -> 0
    124. GPIOE->ODR &= (~(0x1 << 8));
    125. }
    126. -----------------main.c----------------
    127. #include "led.h"
    128. extern void printf(const char *fmt, ...);
    129. void delay_ms(int ms)
    130. {
    131. int i,j;
    132. for(i = 0; i < ms;i++)
    133. for (j = 0; j < 1800; j++);
    134. }
    135. int main()
    136. {
    137. // LED灯初始化
    138. RCC_INIT();
    139. LED1_GPIO_INIT();
    140. LED2_GPIO_INIT();
    141. LED3_GPIO_INIT();
    142. while(1)
    143. {
    144. //点亮LED1
    145. LED1_ON();
    146. delay_ms(500);
    147. //熄灭LED1
    148. LED1_OFF();
    149. //点亮LED2
    150. LED2_ON();
    151. delay_ms(500);
    152. //熄灭LED2
    153. LED2_OFF();
    154. //点亮LED3
    155. LED3_ON();
    156. delay_ms(500);
    157. //熄灭LED3
    158. LED3_OFF();
    159. }
    160. return 0;
    161. }

    方法2

  • 相关阅读:
    etcd中version,revision,mod_revision,create_revision的区别
    渲染时间过长?这些参数设置学起来
    itextpdf代码生成pdf直接下载
    19.7 Boost Asio 传输序列化数据
    电脑系统重装后如何开启Win11实时辅助字幕
    3D Instance Segmentation via Multi-Task Metric Learning
    virtualbox7_0 ubunt20_04 共享文件且自动加载
    Tuxera NTFS2022mac电脑无法读取写入移动硬盘如何解决?
    npm install 安装总结
    CPT203-Software Engineering
  • 原文地址:https://blog.csdn.net/m0_64146298/article/details/133043401