• ARM——综合作业


     实验要求:串口输入相应的命令,控制对应的硬件进行工作

    例如:

    串口工具输入led1on----->板子led1点亮

    在串口工具输入led1off----->板子led1熄灭

    在串口工具输入led2on----->板子led2点亮

    在串口工具输入led2off----->板子led2熄灭

    在串口工具输入led3on----->板子led3点亮

    在串口工具输入led3off----->板子led3熄灭

    测试代码

    1. #include "port.h"
    2. extern void printf(const char *fmt, ...);
    3. void delay_ms(int ms)
    4. {
    5. int i,j;
    6. for(i = 0; i < ms;i++)
    7. for (j = 0; j < 1800; j++);
    8. }
    9. int main()
    10. {
    11. hal_led_init();
    12. cmd_t* cmd_string = 0;
    13. while(1)
    14. {
    15. cmd_string = find_command(get_string());
    16. if(cmd_string == 0)
    17. {
    18. printf("command is not find");
    19. }
    20. else
    21. {
    22. cmd_string->leds_status_t(cmd_string->led,cmd_string->status);
    23. }
    24. }
    25. return 0;
    26. }
    1. #include "port.h"
    2. char data[40];
    3. void hal_led_init()
    4. {
    5. RCC->MP_AHB4ENSETR |= (0x1<<4);
    6. RCC->MP_AHB4ENSETR |= (0x1<<5);
    7. RCC->MP_AHB4ENSETR |= (0x1<<1);
    8. RCC->MP_AHB4ENSETR |= (0x1<<6);
    9. RCC->MP_APB1ENSETR |= (0x1<<16);
    10. GPIOE->MODER &=(~(0x3<<20));
    11. GPIOE->MODER |=(0x1<<20);
    12. GPIOF->MODER &=(~(0x3<<20));
    13. GPIOF->MODER |=(0x1<<20);
    14. GPIOE->MODER &=(~(0x3<<16));
    15. GPIOE->MODER |=(0x1<<16);
    16. GPIOB->MODER &=(~(0x3<<4));
    17. GPIOB->MODER |=(0x1<<5);
    18. GPIOG->MODER &=(~(0x3<<22));
    19. GPIOG->MODER |=(0x1<<23);
    20. GPIOE->OTYPER &=(~(0x1<<10));
    21. GPIOF->OTYPER &=(~(0x1<<10));
    22. GPIOE->OTYPER &=(~(0x1<<8));
    23. GPIOE->OSPEEDR &=(~(0x3<<20));
    24. GPIOE->OSPEEDR &=(~(0x3<<16));
    25. GPIOF->OSPEEDR &=(~(0x3<<20));
    26. GPIOE->PUPDR &=(~(0x3<<20));
    27. GPIOE->PUPDR &=(~(0x3<<16));
    28. GPIOF->PUPDR &=(~(0x3<<20));
    29. GPIOB->AFRL &=(~(0xf<<8));
    30. GPIOB->AFRL |=(0x1<<11);
    31. GPIOG->AFRH &=(~(0xf<<12));
    32. GPIOG->AFRH |=(0x3<<13);
    33. /***********UART4章节初始化****/
    34. //0需要判断串口UE位是否使能
    35. if(USART4->CR1 & 0x1)
    36. {
    37. delay_ms(500);
    38. USART4->CR1 &= (~(0x1<<0));
    39. }
    40. //1、设置UART4串口8位数据位,无奇偶校验位,CR1[28][12]
    41. USART4->CR1 &=(~(0x1<<28));
    42. USART4->CR1 &=(~(0x1<<12));
    43. USART4->CR1 &=(~(0x1<<10));
    44. //2、设置uart4串口1为停止位
    45. USART4->CR2 &=(~(0x3<<12));
    46. //3、设置UART4串口16倍采样率
    47. USART4->CR1 &=(~(0x1<<15));
    48. //4、设置UART4串口不分频
    49. USART4->PRESC &=(~(0xf));
    50. //5、设置UART4波特率为115200
    51. USART4->BRR =0x22b;
    52. //6、设置UART4串口接收器/发送器使能
    53. USART4->CR1 |=(0x1<<3);
    54. USART4->CR1 |=(0x1<<2);
    55. //7、设置UART4串口使能
    56. USART4->CR1 |=(0x1);
    57. }
    58. void led_status(led_t led,status_t status)
    59. {
    60. switch(led)
    61. {
    62. case LED1:
    63. if(status == LED_ON)
    64. {
    65. GPIOE->ODR |=(0x1<<10);
    66. printf("led1点亮\n");
    67. }
    68. else
    69. {
    70. GPIOE->ODR &=(~(0x1<<10));
    71. printf("led1熄灭\n");
    72. }
    73. break;
    74. case LED2:
    75. if(status == LED_ON)
    76. {
    77. GPIOF->ODR|=(0x1<<10);
    78. printf("led2点亮\n");
    79. }
    80. else
    81. {
    82. GPIOF->ODR&=(~(0x1<<10));
    83. printf("led2熄灭\n");
    84. }
    85. break;
    86. case LED3:
    87. if(status == LED_ON)
    88. {
    89. GPIOE->ODR |=(0x1<<8);
    90. printf("led3点亮\n");
    91. }
    92. else {
    93. GPIOE->ODR &=(~(0x1<<8));
    94. printf("led3熄灭\n");
    95. }
    96. break;
    97. }
    98. }
    99. cmd_t cmd_arr[MAX] = {
    100. [0] = {
    101. .cmd_arr = "led1on",
    102. .led = LED1,
    103. .status = LED_ON,
    104. .leds_status_t = led_status,
    105. },
    106. [1] = {
    107. .cmd_arr = "led1off",
    108. .led = LED1,
    109. .status = LED_OFF,
    110. .leds_status_t = led_status,
    111. },
    112. [2] = {
    113. .cmd_arr = "led2on",
    114. .led = LED2,
    115. .status = LED_ON,
    116. .leds_status_t = led_status,
    117. },
    118. [3] = {
    119. .cmd_arr = "led2off",
    120. .led = LED2,
    121. .status = LED_OFF,
    122. .leds_status_t = led_status,
    123. },
    124. [4] = {
    125. .cmd_arr = "led3on",
    126. .led = LED3,
    127. .status = LED_ON,
    128. .leds_status_t = led_status,
    129. },
    130. [5] = {
    131. .cmd_arr = "led3off",
    132. .led = LED3,
    133. .status = LED_OFF,
    134. .leds_status_t = led_status,
    135. },
    136. };
    137. cmd_t* find_command(const char* str)
    138. {
    139. int i;
    140. for(i=0;i
    141. {
    142. if(!my_strcmp(str,cmd_arr[i].cmd_arr))
    143. {
    144. return &cmd_arr[i];
    145. }
    146. }
    147. return 0;
    148. }
    149. //比较函数
    150. int my_strcmp(const char *s1,const char *s2)
    151. {
    152. int result;
    153. while(*s1==*s2 &&*s1!='\0' &&*s2!='\0')
    154. {
    155. s1++;
    156. s2++;
    157. }
    158. result = *s1-*s2;
    159. return result;
    160. }
    161. //发送一个字符
    162. void put_char(const char ch)
    163. {
    164. //判断发送寄存器状态位
    165. while(!(USART4->ISR&(0x1<<7)));
    166. USART4->TDR = ch;
    167. //判断发送数据是否完成
    168. while(!(USART4->ISR&(0x1<<6)));
    169. }
    170. //发送一个字符串
    171. void put_string(const char* str)
    172. {
    173. while(*str)
    174. {
    175. put_char(*str++);
    176. }
    177. put_char('\n');
    178. put_char('\r');
    179. }
    180. //接收一个字符
    181. char get_char()
    182. {
    183. //判断接收数据是否有数据
    184. char ch;
    185. while(!(USART4->ISR&(0x1<<5)));
    186. ch = USART4->RDR;
    187. return ch;
    188. }
    189. //接收一个字符串
    190. char* get_string()
    191. {
    192. int i=0;
    193. for(i=0;i<sizeof(data)-1;i++)
    194. {
    195. data[i]=get_char();
    196. put_char(data[i]);
    197. if(data[i]=='\r')
    198. {
    199. break;
    200. }
    201. }
    202. data[i]='\0';
    203. put_char('\n');
    204. return data;
    205. }
    1. #ifndef __PORT_H__
    2. #define __PORT_H__
    3. #include "../common/include/stm32mp1xx_gpio.h"
    4. #include "../common/include/stm32mp1xx_rcc.h"
    5. #include "../common/include/stm32mp1xx_uart.h"
    6. #define MAX 6
    7. typedef enum
    8. {
    9. LED1=1,
    10. LED2,
    11. LED3,
    12. }led_t;
    13. typedef enum
    14. {
    15. LED_ON,
    16. LED_OFF,
    17. }status_t;
    18. typedef struct
    19. {
    20. char * cmd_arr;
    21. led_t led;
    22. status_t status;
    23. void (*leds_status_t)(led_t led,status_t status);
    24. }cmd_t;
    25. //初始化
    26. void hal_led_init();
    27. //比较函数
    28. //发送一个字符
    29. void put_char(const char ch);
    30. //发送一个字符串
    31. void put_string(const char* string);
    32. //接收一个字符
    33. char get_char();
    34. //接收一个字符串
    35. char *get_string();
    36. cmd_t* find_command(const char* str);
    37. void led_status(led_t led,status_t status);
    38. int my_strcmp(const char *s1,const char *s2);
    39. extern void printf(const char *fmt, ...);
    40. void delay_ms(int ms);
    41. #endif

     

  • 相关阅读:
    ESP32网络编程-TCP客户端数据传输
    C#的Array 类使用说明
    qt关于在linux系统中使用qmake编译项目遇到的问题
    Java计算机毕业设计 基于SpringBoot+Vue的毕业生信息招聘平台的设计与实现 Java实战项目 附源码+文档+视频讲解
    Linux 进程线程
    立足小餐饮,“新名酒”江小白能走多远?
    linux搭建java部署环境-docker
    FineReport填报设计-填报设置-填报校验
    (二十六)admin-boot项目之基于注解的数据字段脱敏
    力扣:103. 二叉树的锯齿形层序遍历(Python3)
  • 原文地址:https://blog.csdn.net/ww1106/article/details/127602520