cortex-M4核综合实验
1.通过配置开发板LED1/LED2/LED3三盏灯
2.当KEY1/KEY2/KEY3/光电开关/火焰传感器/人体红外中断触发,需要完成以下内容
1)中断触发,在串口工具打印一句话
2)中断触发,对应LED灯状态取反
3.需求:实验中的内容,需要在一个工程下配置,代码编写
usart.h
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE *stream){
while(!(huart4.Instance->ISR & (0X1 << 7)));
huart4.Instance->TDR=ch;
if(ch == '\n')
{
while(!(huart4.Instance->ISR & (0X1 << 7)));
huart4.Instance->TDR='\r';
}
return ch;
}
/* USER CODE END 0 */
gpio.c
/* USER CODE BEGIN 2 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
switch(GPIO_Pin){
case GPIO_PIN_15:
//光电开关
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
printf("led3####\n\r");
break;
case GPIO_PIN_5:
//火焰
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
printf("led2####\n\r");
break;
case GPIO_PIN_12:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
printf("led1####\n\r");
break;
}
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
switch(GPIO_Pin){
case GPIO_PIN_9:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);
printf("led3####\n\r");
break;
case GPIO_PIN_7:
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
printf("led2####\n\r");
break;
case GPIO_PIN_8:
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_10);
printf("led1####\n\r");
break;
}
}
/* USER CODE END 2 */
cortex-A7核综合实验
实验需求:在串口输入相应的命令,控制对应的硬件进行操作
在串口输入led1on------>板子LED1灯点亮
在串口输入led1off------>板子LED1灯熄灭
在串口输入led2on------>板子LED2灯点亮
在串口输入led2off------>板子LED2灯熄灭
在串口输入led3on------>板子LED3灯点亮
在串口输入led3off------>板子LED3灯熄灭文章来源:https://www.toymoban.com/news/detail-430439.html
main.c文章来源地址https://www.toymoban.com/news/detail-430439.html
#include "led.h"
#include "uart4.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 1800; j++)
;
}
typedef struct
{
char cmd_arr[20]; //命令字符串
gpio_t *gpiox; //gpio组号
unsigned int pin; //对应引脚编号
gpio_status_t status; //对应引脚状态
void (*gpio_write)(gpio_t *gpiox, unsigned int pin, gpio_status_t status);
} cmd_t;
cmd_t arr[6] = {
[0] = {
.cmd_arr = "led1on",
.gpiox = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[1] = {
.cmd_arr = "led1off",
.gpiox = GPIOE,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
[2] = {
.cmd_arr = "led2on",
.gpiox = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[3] = {
.cmd_arr = "led2off",
.gpiox = GPIOF,
.pin = GPIO_PIN_10,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
[4] = {
.cmd_arr = "led3on",
.gpiox = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_SET_T,
.gpio_write = hal_gpio_write,
},
[5] = {
.cmd_arr = "led3off",
.gpiox = GPIOE,
.pin = GPIO_PIN_8,
.status = GPIO_RESET_T,
.gpio_write = hal_gpio_write,
},
};
//字符串比较函数
int strcmp(const char *str1, const char *str2)
{
while (*str1 && *str2 && *str1 == *str2)
{
str1++;
str2++;
}
return *str1 - *str2;
}
//查找命令的
cmd_t *find_cmd(const char *buffer)
{
int i;
for (i = 0; i < 6; i++)
{
if (strcmp(arr[i].cmd_arr, buffer) == 0)
{
return arr + i;
}
}
return 0;
}
int main()
{
//LED123灯初始化
hal_init_led123();
//UART初始化
hal_uart_init();
char buf[50];
int len = 50;
while (1)
{
hal_recv_string(buf, len); //用来接收串口工具输入的字符串
cmd_t *p = find_cmd(buf);
if (p == 0)
{
printf("cmd not found\n\r");
}
else
{
p->gpio_write(p->gpiox, p->pin, p->status);
}
}
return 0;
}
到了这里,关于20230426 cortex-A7 cortex-M4核综合实验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!