作业:
1、通过操作Cortex-A7核,串口输入相应的命令,控制LED灯进行工作--->上传CSDN 1.例如在串口输入led1on,开饭led1灯点亮 2.例如在串口输入led1off,开饭led1灯熄灭 3.例如在串口输入led2on,开饭led2灯点亮 4.例如在串口输入led2off,开饭led2灯熄灭 5.例如在串口输入led3on,开饭led3灯点亮 6.例如在串口输入led3off,开饭led3灯熄灭
2、 检测中断到来时,让LED灯状态取反,并且在串口工具上打印一句话--->上传CSDN 例如:当按键1按下之后,让LED1状态取反,并打印“LED1 down” 当按键2按下之后,让LED2状态取反,并打印“LED2 down” 当按键3按下之后,让LED3状态取反,并打印“LED3 down” 火焰传感器/人体红外/光电开关实验要求如上 只需要上传编写的代码,和实验现象截图
1、
头文件:
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//串口初始化
void uart_init();
//发送一个字符
void uart_put_char(const char str);
//发送一个字符串
void uart_put_string(const char* str);
//接收一个字符
char uart_get_char();
//接收一个字符串
char* uart_get_string();
//LED1---->PE10
void led11_init();
void led11_on();
void led11_off();
//LED2---->PF10
void led22_init();
void led22_on();
void led22_off();
//LED3---->PE8
void led33_init();
void led33_on();
void led33_off();
#endif
源文件:
#include "uart4.h"
extern void delay_ms(int ms);
//串口初始化
void uart_init()
{
/*********RCC章节初始化************/
//通过MP_AHB4ENSETR设置GPIOB/GPIOG控制器使能 GPIOB[1] = 1 GPIOG[6] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//通过MP_APB1ENSETR设置UART4控制器使能 UART4[16] = 1
RCC->MP_APB1ENSETR |= (0x1 << 16);
/*********GPIO章节初始化************/
//PB2---->UART4_RX
//1.通过MODER寄存器设置PB2引脚为复用功能 MODER[5:4] = 10
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x1 << 5);
//2.通过AFRL寄存器设置PB2引脚复用功能为UART4_RX AFRL[11:8] = 1000
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x1 << 11);
//PG11---->UART4_TX
//1.通过MODER寄存器设置PG11引脚为复用功能 MODER[23:22] = 10
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x1 << 23);
//2.通过AFRH寄存器设置PG11引脚复用功能为UART4_TX AFRH[15:12] = 0110
GPIOG->AFRH &= (~(0xf << 12));
GPIOG->AFRH |= (0x3 << 13);
/*********UART4章节初始化************/
//0.判断UE为是否为0
if(USART4->CR1 & (0x1 << 0))
{
delay_ms(500);
USART4->CR1 &= (~(0x1 << 0));
}
//1.串口初始化 8位数据位 无奇偶校验位 CR1[28][12] = 00 CR1[10] = 0
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
USART4->CR1 &= (~(0x1 << 10));
//2.1位停止位 CR2[13:12] = 00
USART4->CR2 &= (~(0x3 << 12));
//3.设置串口16倍采样率 CR1[15] = 0
USART4->CR1 &= (~(0x1 << 15));
//4.设置串口不分频 PRESC[3:0] = 0000
USART4->PRESC &= (~(0xf << 0));
//5.设置串口波特率为115200 BRR = 0x22B
USART4->BRR |= 0x22B;
//6.设置串口发送器使能 CR1[3] = 1
USART4->CR1 |= (0x1 << 3);
//7.设置串口接受器使能 CR1[2] = 1
USART4->CR1 |= (0x1 << 2);
//8.设置串口使能 CR1[0] = 1
USART4->CR1 |= (0x1 << 0);
}
//发送一个字符
//putchar
void uart_put_char(const char str)
{
//1.判断发送数据寄存器是否为空,为空,才可以发送下一个字节
//ISR[7]
//读0:发送数据寄存器满,需要等待
//读1:发送数据寄存器空,才可以发送下一位数据
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送的字符,写到发送数据寄存器中
USART4->TDR = str;
//3.判断发送数据是否完成 ISR[6]
//读0:发送数据没有完成,需要等待
//读1:发送一帧数据完成,可以发送下一帧数据
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void uart_put_string(const char* str)
{
//判断是否为'\0',一个字符一个字符发送
while(*str)
{
uart_put_char(*str++);
}
uart_put_char('\n');
uart_put_char('\r');
}
//接收一个字符
//getchar
char uart_get_char()
{
char ch;
//1.判断接收数据寄存器是否有数据可读 ISR[5]
//读0:表示接收数据寄存器有数据可读
//读1:表示接收数据寄存器中没有数据
while(!(USART4->ISR & (0x1 << 5)));
//2.将接收到的数据读出来
ch = USART4->RDR;
return ch;
}
char buffer[50] = {0};
//接收一个字符串
char* uart_get_string()
{
int i;
//for循环
//当键盘的回车键'\r'按下之后,字符串输入完成
for(i=0;i<49;i++)
{
buffer[i] = uart_get_char();//接收一个字符
uart_put_char(buffer[i]);//发送一个字符
if(buffer[i] == '\r') //判断字符串是否输入完成
{
switch(buffer[3])
{
case '1':
if(buffer[5] == 'n')
{
led11_on();
}
else if(buffer[5] == 'f')
{
led11_off();
}
break;
case '2':
if(buffer[5] == 'n')
{
led22_on();
}
else if(buffer[5] == 'f')
{
led22_off();
}
break;
case '3':
if(buffer[5] == 'n')
{
led33_on();
}
else if(buffer[5] == 'f')
{
led33_off();
}
break;
}
break;
}
}
//字符串补'\0'
buffer[i] = '\0';
uart_put_char('\n');
return buffer;
}
void led11_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//1.设置PE10引脚为输出模式 MODER[21:20] = 01
GPIOE->MODER = GPIOE->MODER & (~(0x3<<20));
GPIOE->MODER = GPIOE->MODER | (0x1<<20);
//2.设置PE10引脚为推挽输出模式 OTYPER[10] = 0
GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1<<10));
//GPIOE->OTYPER &= (~(0x1 << 10));
//3.设置PE10引脚为低速输出 OSPEEDR[21:20] = 00
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x3<<20));
//4.设置PE10引脚禁止上下拉电阻 PUPDR[21:20] = 00
GPIOE->PUPDR = GPIOE->PUPDR &(~(0x3<<20));
}
void led11_on()
{
//设置PE10引脚输出高电平 ODR[10] = 1
GPIOE->ODR = GPIOE->ODR | (0x1<<10);
}
void led11_off()
{
//设置PE10引脚输出低电平 ODR[10] = 0
GPIOE->ODR = GPIOE->ODR & (~(0x1<<10));
}
void led22_init() //PF10
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//1.设置PF10引脚为输出模式 MODER[21:20] = 01
GPIOF->MODER = GPIOF->MODER & (~(0x3<<20));
GPIOF->MODER = GPIOF->MODER | (0x1<<20);
//2.设置PF10引脚为推挽输出模式 OTYPER[10] = 0
GPIOF->OTYPER = GPIOF->OTYPER & (~(0x1<<10));
//GPIOF->OTYPER &= (~(0x1 << 10));
//3.设置PF10引脚为低速输出 OSPEEDR[21:20] = 00
GPIOF->OSPEEDR = GPIOF->OSPEEDR & (~(0x3<<20));
//4.设置PF10引脚禁止上下拉电阻 PUPDR[21:20] = 00
GPIOF->PUPDR = GPIOF->PUPDR &(~(0x3<<20));
}
void led22_on()
{
//设置PF10引脚输出高电平 ODR[10] = 1
GPIOF->ODR = GPIOF->ODR | (0x1<<10);
}
void led22_off()
{
//设置PF10引脚输出低电平 ODR[10] = 0
GPIOF->ODR = GPIOF->ODR & (~(0x1<<10));
}
void led33_init() //PE8
{
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//1.设置PE8引脚为输出模式 MODER[17:16] = 01
GPIOE->MODER = GPIOE->MODER & (~(0x3<<16));
GPIOE->MODER = GPIOE->MODER | (0x1<<16);
//2.设置PE8引脚为推挽输出模式 OTYPER[8] = 0
GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1<<8));
//GPIOE->OTYPER &= (~(0x1 << 8));
//3.设置PE8引脚为低速输出 OSPEEDR[17:16] = 00
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x3<<16));
//4.设置PE8引脚禁止上下拉电阻 PUPDR[17:16] = 00
GPIOE->PUPDR = GPIOE->PUPDR &(~(0x3<<16));
}
void led33_on()
{
//设置PE10引脚输出高电平 ODR[10] = 1
GPIOE->ODR = GPIOE->ODR | (0x1<<8);
}
void led33_off()
{
//设置PE10引脚输出低电平 ODR[10] = 0
GPIOE->ODR = GPIOE->ODR & (~(0x1<<8));
}
主函数调用:
#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++);
}
int main()
{
//GPIO初始化
led11_init();
led22_init();
led33_init();
//串口初始化
uart_init();
uart_put_string("uart4 test!!!!\n");
//实现串口数据收发
while(1)
{
// uart_put_char(uart_get_char() + 1);
uart_put_string(uart_get_string());
}
return 0;
}
实验现象:
2、
重写中断函数:
//重写中断函数
/* USER CODE BEGIN 2 */
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
//KEY1--->LED1
case GPIO_PIN_9:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_8);
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_8))
{
printf("key1 on\n");
}
else
{
printf("key1 off\n");
}
break;
//KEY2--->LED2
case GPIO_PIN_7:
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
if(HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_10))
{
printf("key2 on\n");
}
else
{
printf("key2 off\n");
}
break;
//KEY3--->LED3
case GPIO_PIN_8:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_10);
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_10))
{
printf("key3 on\n");
}
else
{
printf("key3 off\n");
}
break;
}
}
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
//
case GPIO_PIN_5:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_8);
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_8))
{
printf("flame\n");
}
else
{
printf("flame off\n");
}
break;
//KEY2--->LED2
case GPIO_PIN_12:
HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
if(HAL_GPIO_ReadPin(GPIOF,GPIO_PIN_10))
{
printf("infrare\n");
}
else
{
printf("infrare off\n");
}
break;
//KEY3--->LED3
case GPIO_PIN_15:
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_10);
if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_10))
{
printf("ptotoe\n");
}
else
{
printf("ptotoe off\n");
}
break;
}
}
/* USER CODE END 2 */
重写fputc函数:
/* USER CODE BEGIN 0 */
/*实现数据发送一个字符*/
int fputc(int ch,FILE *STREAM)
{
/*
判断发送数据寄存器是否为空;
将要发送的字符放入到发送寄存器中
判断是否为'\n'
*/
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 */
实验现象: