• 基于STC15系列库操作LED灯


    一、准备工作

    1. 基于STC15系列库的工程模板

    参考:51单片机工程模板的建立(基于STC15系列库)-CSDN博客

    2. Keil编译器

    二、程序编写

    1. 新建 led.c 和 led.h 文件并存放于 user/led 文件夹下;

    2. 新建 user.c 和 user.h 文件并存放于 user 文件夹下;

    3. 将 user.c 文件 led.c 文件添加进工程分组 user 组别下;

    4. 将 user.h 文件目录和 led.h 文件目录添加进头文件检索目录内;

    5. 在 main.c 文件中添加以下代码内容;

    1. #include "user.h"
    2. void main()
    3. {
    4. User_Init(); //上电初始化,在该函数内实现上电后所需要的所有初始化操作
    5. while(1)
    6. {
    7. LED_OnOrOff(LED_ON); //打开LED
    8. delay_ms(500);
    9. LED_OnOrOff(LED_OFF); //关闭LED
    10. delay_ms(500);
    11. }
    12. }

     6. 在 led.c 函数中添加以下代码内容;

    1. #include "led.h"
    2. //设置LED电路引脚
    3. #define LED_Px GPIO_P1
    4. #define LED_Py GPIO_Pin_0
    5. //设置LED引脚为推挽输出模式
    6. void LED_Init(void)
    7. {
    8. GPIO_InitTypeDef GPIO_InitStructure;
    9. GPIO_InitStructure.Mode = GPIO_OUT_PP;
    10. GPIO_InitStructure.Pin = LED_Py;
    11. GPIO_Inilize(LED_Px, &GPIO_InitStructure);
    12. }
    13. //制作开关LED的驱动函数
    14. void LED_OnOrOff(unsigned char LED_Sta)
    15. {
    16. if(LED_Sta)
    17. {
    18. GPIO_PIN_Set(LED_Px, LED_Py);
    19. }
    20. else
    21. {
    22. GPIO_PIN_ReSet(LED_Px, LED_Py);
    23. }
    24. }
    25. //制作GPIO的初始化函数
    26. void GPIO_Init(void)
    27. {
    28. LED_Init(); //对LED引脚初始化
    29. }

    7.  在 led.h 文件中添加以下代码内容;

    1. #ifndef __LED_H__
    2. #define __LED_H__
    3. #include "gpio.h"
    4. //宏定义灯的开关标识符号,便于程序理解
    5. #define LED_ON 0
    6. #define LED_OFF 1
    7. //声明所有在led.c文件中定义的函数
    8. void LED_Init(void);
    9. void LED_OnOrOff(unsigned char LED_Sta);
    10. void GPIO_Init(void);
    11. #endif

    8. 在 user.c 文件中添加以下代码内容;

    1. #include "user.h"
    2. void User_Init(void)
    3. {
    4. GPIO_Init(); //通用IO端口初始化
    5. }

    9. 在 user.h 文件中添加以下代码内容;

    1. #ifndef __USER_H__
    2. #define __USER_H__
    3. //文件引用
    4. #include "config.h"
    5. #include "led.h"
    6. #include "delay.h"
    7. //函数声明
    8. void User_Init(void);
    9. #endif

    10. 按正常库设计逻辑,LED驱动工程到此已经结束,用户应该可以编译运行,并下载看程序效果了,很遗憾,本程序出现了报错;因为在 led.c 文件中使用了两个官方库未定义的函数:

    1. //制作开关LED的驱动函数
    2. void LED_OnOrOff(unsigned char LED_Sta)
    3. {
    4. if(LED_Sta)
    5. {
    6. GPIO_PIN_Set(LED_Px, LED_Py); //官方库未定义
    7. }
    8. else
    9. {
    10. GPIO_PIN_ReSet(LED_Px, LED_Py); //官方库未定义
    11. }
    12. }

    这两个函数的功能很简单用于对特定引脚置位或重置操作,即拉高或拉低电平信号;由于在以后的众多功能中都需要这么操作,在该官方库中我自作主张添加了以上两个函数;具体内容见步骤11、步骤12;

    11. 在 gpio.h 文件 u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx); 函数上方添加以下代码内容;

    1. typedef struct
    2. {
    3. u8 Mode; //IO模式, GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
    4. u8 Pin; //要设置的端口
    5. } GPIO_InitTypeDef;
    6. //添加置位与重置函数声明
    7. u8 GPIO_PIN_Set(u8 GPIO, u8 GPIO_Pin_x);
    8. u8 GPIO_PIN_ReSet(u8 GPIO, u8 GPIO_Pin_x);
    9. u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx);

    12. 在 gpio.c 文件 u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx); 函数上方添加以下代码内容;

    1. //添加置位函数定义
    2. u8 GPIO_PIN_Set(u8 GPIO, u8 GPIO_Pin_x)
    3. {
    4. if(GPIO > GPIO_P5) return 1; //空操作
    5. if(GPIO_Pin_x > GPIO_Pin_7) return 2; //空操作
    6. if(GPIO == GPIO_P0)
    7. {
    8. if(GPIO_Pin_x == GPIO_Pin_0) P00 = 1;
    9. if(GPIO_Pin_x == GPIO_Pin_1) P01 = 1;
    10. if(GPIO_Pin_x == GPIO_Pin_2) P02 = 1;
    11. if(GPIO_Pin_x == GPIO_Pin_3) P03 = 1;
    12. if(GPIO_Pin_x == GPIO_Pin_4) P04 = 1;
    13. if(GPIO_Pin_x == GPIO_Pin_5) P05 = 1;
    14. if(GPIO_Pin_x == GPIO_Pin_6) P06 = 1;
    15. if(GPIO_Pin_x == GPIO_Pin_7) P07 = 1;
    16. }
    17. else if(GPIO == GPIO_P1)
    18. {
    19. if(GPIO_Pin_x == GPIO_Pin_0) P10 = 1;
    20. if(GPIO_Pin_x == GPIO_Pin_1) P11 = 1;
    21. if(GPIO_Pin_x == GPIO_Pin_2) P12 = 1;
    22. if(GPIO_Pin_x == GPIO_Pin_3) P13 = 1;
    23. if(GPIO_Pin_x == GPIO_Pin_4) P14 = 1;
    24. if(GPIO_Pin_x == GPIO_Pin_5) P15 = 1;
    25. if(GPIO_Pin_x == GPIO_Pin_6) P16 = 1;
    26. if(GPIO_Pin_x == GPIO_Pin_7) P17 = 1;
    27. }
    28. else if(GPIO == GPIO_P2)
    29. {
    30. if(GPIO_Pin_x == GPIO_Pin_0) P20 = 1;
    31. if(GPIO_Pin_x == GPIO_Pin_1) P21 = 1;
    32. if(GPIO_Pin_x == GPIO_Pin_2) P22 = 1;
    33. if(GPIO_Pin_x == GPIO_Pin_3) P23 = 1;
    34. if(GPIO_Pin_x == GPIO_Pin_4) P24 = 1;
    35. if(GPIO_Pin_x == GPIO_Pin_5) P25 = 1;
    36. if(GPIO_Pin_x == GPIO_Pin_6) P26 = 1;
    37. if(GPIO_Pin_x == GPIO_Pin_7) P27 = 1;
    38. }
    39. else if(GPIO == GPIO_P3)
    40. {
    41. if(GPIO_Pin_x == GPIO_Pin_0) P30 = 1;
    42. if(GPIO_Pin_x == GPIO_Pin_1) P31 = 1;
    43. if(GPIO_Pin_x == GPIO_Pin_2) P32 = 1;
    44. if(GPIO_Pin_x == GPIO_Pin_3) P33 = 1;
    45. if(GPIO_Pin_x == GPIO_Pin_4) P34 = 1;
    46. if(GPIO_Pin_x == GPIO_Pin_5) P35 = 1;
    47. if(GPIO_Pin_x == GPIO_Pin_6) P36 = 1;
    48. if(GPIO_Pin_x == GPIO_Pin_7) P37 = 1;
    49. }
    50. else if(GPIO == GPIO_P4)
    51. {
    52. if(GPIO_Pin_x == GPIO_Pin_0) P40 = 1;
    53. if(GPIO_Pin_x == GPIO_Pin_1) P41 = 1;
    54. if(GPIO_Pin_x == GPIO_Pin_2) P42 = 1;
    55. if(GPIO_Pin_x == GPIO_Pin_3) P43 = 1;
    56. if(GPIO_Pin_x == GPIO_Pin_4) P44 = 1;
    57. if(GPIO_Pin_x == GPIO_Pin_5) P45 = 1;
    58. if(GPIO_Pin_x == GPIO_Pin_6) P46 = 1;
    59. if(GPIO_Pin_x == GPIO_Pin_7) P47 = 1;
    60. }
    61. else if(GPIO == GPIO_P5)
    62. {
    63. if(GPIO_Pin_x == GPIO_Pin_0) P50 = 1;
    64. if(GPIO_Pin_x == GPIO_Pin_1) P51 = 1;
    65. if(GPIO_Pin_x == GPIO_Pin_2) P52 = 1;
    66. if(GPIO_Pin_x == GPIO_Pin_3) P53 = 1;
    67. if(GPIO_Pin_x == GPIO_Pin_4) P54 = 1;
    68. if(GPIO_Pin_x == GPIO_Pin_5) P55 = 1;
    69. if(GPIO_Pin_x == GPIO_Pin_6) P56 = 1;
    70. if(GPIO_Pin_x == GPIO_Pin_7) P57 = 1;
    71. }
    72. return 0;
    73. }
    74. //添加重置函数定义
    75. u8 GPIO_PIN_ReSet(u8 GPIO, u8 GPIO_Pin_x)
    76. {
    77. if(GPIO > GPIO_P5) return 1; //空操作
    78. if(GPIO_Pin_x > GPIO_Pin_7) return 2; //空操作
    79. if(GPIO == GPIO_P0)
    80. {
    81. if(GPIO_Pin_x == GPIO_Pin_0) P00 = 0;
    82. if(GPIO_Pin_x == GPIO_Pin_1) P01 = 0;
    83. if(GPIO_Pin_x == GPIO_Pin_2) P02 = 0;
    84. if(GPIO_Pin_x == GPIO_Pin_3) P03 = 0;
    85. if(GPIO_Pin_x == GPIO_Pin_4) P04 = 0;
    86. if(GPIO_Pin_x == GPIO_Pin_5) P05 = 0;
    87. if(GPIO_Pin_x == GPIO_Pin_6) P06 = 0;
    88. if(GPIO_Pin_x == GPIO_Pin_7) P07 = 0;
    89. }
    90. else if(GPIO == GPIO_P1)
    91. {
    92. if(GPIO_Pin_x == GPIO_Pin_0) P10 = 0;
    93. if(GPIO_Pin_x == GPIO_Pin_1) P11 = 0;
    94. if(GPIO_Pin_x == GPIO_Pin_2) P12 = 0;
    95. if(GPIO_Pin_x == GPIO_Pin_3) P13 = 0;
    96. if(GPIO_Pin_x == GPIO_Pin_4) P14 = 0;
    97. if(GPIO_Pin_x == GPIO_Pin_5) P15 = 0;
    98. if(GPIO_Pin_x == GPIO_Pin_6) P16 = 0;
    99. if(GPIO_Pin_x == GPIO_Pin_7) P17 = 0;
    100. }
    101. else if(GPIO == GPIO_P2)
    102. {
    103. if(GPIO_Pin_x == GPIO_Pin_0) P20 = 0;
    104. if(GPIO_Pin_x == GPIO_Pin_1) P21 = 0;
    105. if(GPIO_Pin_x == GPIO_Pin_2) P22 = 0;
    106. if(GPIO_Pin_x == GPIO_Pin_3) P23 = 0;
    107. if(GPIO_Pin_x == GPIO_Pin_4) P24 = 0;
    108. if(GPIO_Pin_x == GPIO_Pin_5) P25 = 0;
    109. if(GPIO_Pin_x == GPIO_Pin_6) P26 = 0;
    110. if(GPIO_Pin_x == GPIO_Pin_7) P27 = 0;
    111. }
    112. else if(GPIO == GPIO_P3)
    113. {
    114. if(GPIO_Pin_x == GPIO_Pin_0) P30 = 0;
    115. if(GPIO_Pin_x == GPIO_Pin_1) P31 = 0;
    116. if(GPIO_Pin_x == GPIO_Pin_2) P32 = 0;
    117. if(GPIO_Pin_x == GPIO_Pin_3) P33 = 0;
    118. if(GPIO_Pin_x == GPIO_Pin_4) P34 = 0;
    119. if(GPIO_Pin_x == GPIO_Pin_5) P35 = 0;
    120. if(GPIO_Pin_x == GPIO_Pin_6) P36 = 0;
    121. if(GPIO_Pin_x == GPIO_Pin_7) P37 = 0;
    122. }
    123. else if(GPIO == GPIO_P4)
    124. {
    125. if(GPIO_Pin_x == GPIO_Pin_0) P40 = 0;
    126. if(GPIO_Pin_x == GPIO_Pin_1) P41 = 0;
    127. if(GPIO_Pin_x == GPIO_Pin_2) P42 = 0;
    128. if(GPIO_Pin_x == GPIO_Pin_3) P43 = 0;
    129. if(GPIO_Pin_x == GPIO_Pin_4) P44 = 0;
    130. if(GPIO_Pin_x == GPIO_Pin_5) P45 = 0;
    131. if(GPIO_Pin_x == GPIO_Pin_6) P46 = 0;
    132. if(GPIO_Pin_x == GPIO_Pin_7) P47 = 0;
    133. }
    134. else if(GPIO == GPIO_P5)
    135. {
    136. if(GPIO_Pin_x == GPIO_Pin_0) P50 = 0;
    137. if(GPIO_Pin_x == GPIO_Pin_1) P51 = 0;
    138. if(GPIO_Pin_x == GPIO_Pin_2) P52 = 0;
    139. if(GPIO_Pin_x == GPIO_Pin_3) P53 = 0;
    140. if(GPIO_Pin_x == GPIO_Pin_4) P54 = 0;
    141. if(GPIO_Pin_x == GPIO_Pin_5) P55 = 0;
    142. if(GPIO_Pin_x == GPIO_Pin_6) P56 = 0;
    143. if(GPIO_Pin_x == GPIO_Pin_7) P57 = 0;
    144. }
    145. return 0;
    146. }

    13. 保存所有文件并编译即可完成LED驱动例程;

    14. 工程文件:基于STC15系列库的LED工程样例资源-CSDN文库

    三、总结

    使用STC15系列库在小工程上的复杂程度提升了很多倍,这会使的很多同学放弃使用库函数来学习,这是无法避免的;但我们不能忽视使用库所带来的优势,程序的可读性大大加强,程序的统一操作使程序在相互之间的流通性大大提升,也许你们还没有意识到库的优势,但还是希望你们保持一颗热情的心继续深入学习一下,也许很快你就能体会到库的魅力;

  • 相关阅读:
    Java练习22
    asp.net get请求base64解密报错问题
    Linux网络编程(socket本机通信)
    【C++】LeetCode 160 相交链表
    一道桥牌明手题的思路与分析
    1422. 分割字符串的最大得分
    Mac 上可以使用 ping 端口
    MAKEFLAGS += -rR --include-dir=$(CURDIR)的含义
    机器视觉:实现精准农业的关键技术
    lerna在项目中使用
  • 原文地址:https://blog.csdn.net/zwx2415/article/details/137938609