led_blink_sample.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-09-25 misonyo first edition.
  9. */
  10. /*
  11. * 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
  12. * 例程导出了 led_sample 命令到控制终端
  13. * 命令调用格式:led_sample 41
  14. * 命令解释:命令第二个参数是要使用的PIN脚编号,为空则使用例程默认的引脚编号。
  15. * 程序功能:程序创建一个led线程,线程每隔1000ms改变PIN脚状态,达到控制led灯
  16. * 亮灭的效果。
  17. */
  18. #include <rtthread.h>
  19. #include <rtdevice.h>
  20. #include <stdlib.h>
  21. /* PIN脚编号,查看驱动文件drv_gpio.c确定 */
  22. #define LED_PIN_NUM 41
  23. static int pin_num;
  24. static void led_entry(void *parameter)
  25. {
  26. int count = 0;
  27. /* 设置PIN脚模式为输出 */
  28. rt_pin_mode(pin_num, PIN_MODE_OUTPUT);
  29. while (1)
  30. {
  31. count++;
  32. rt_kprintf("thread run count : %d\r\n", count);
  33. /* 拉低PIN脚 */
  34. rt_pin_write(pin_num, PIN_LOW);
  35. rt_kprintf("led on!\r\n");
  36. /* 延时1000ms */
  37. rt_thread_mdelay(1000);
  38. /* 拉高PIN脚 */
  39. rt_pin_write(pin_num, PIN_HIGH);
  40. rt_kprintf("led off!\r\n");
  41. rt_thread_mdelay(1000);
  42. }
  43. }
  44. static int led_sample(int argc, char *argv[])
  45. {
  46. rt_thread_t tid;
  47. rt_err_t ret = RT_EOK;
  48. /* 判断命令行参数是否给定了PIN脚编号 */
  49. if (argc == 2)
  50. {
  51. pin_num = atoi(argv[1]);
  52. }
  53. else
  54. {
  55. pin_num = LED_PIN_NUM;
  56. }
  57. tid = rt_thread_create("led",
  58. led_entry,
  59. RT_NULL,
  60. 512,
  61. RT_THREAD_PRIORITY_MAX / 3,
  62. 20);
  63. if (tid != RT_NULL)
  64. {
  65. rt_thread_startup(tid);
  66. }
  67. else
  68. {
  69. ret = RT_ERROR;
  70. }
  71. return ret;
  72. }
  73. /* 导出到 msh 命令列表中 */
  74. MSH_CMD_EXPORT(led_sample, led sample);