题目:
题目一:通过操作Cortex-A7核,串口输入相应的命令,控制LED灯进行工作--->上传CSDN
1.例如在串口输入led1on,开饭led1灯点亮
2.例如在串口输入led1off,开饭led1灯熄灭
3.例如在串口输入led2on,开饭led2灯点亮
4.例如在串口输入led2off,开饭led2灯熄灭
5.例如在串口输入led3on,开饭led3灯点亮
6.例如在串口输入led3off,开饭led3灯熄灭
题目二:检测中断到来时,让LED灯状态取反,并且在串口工具上打印一句话--->上传CSDN
例如:
当按键1按下之后,让LED1状态取反,并打印“LED1 down”
当按键2按下之后,让LED2状态取反,并打印“LED2 down”
当按键3按下之后,让LED3状态取反,并打印“LED3 down”
火焰传感器/人体红外/光电开关实验要求如上
代码:
代码一:
mix.h
#ifndef __MIX_H__
#define __MIX_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
void hal_mix_rcc();
void hal_mix_gpio();
void hal_mix_uart();
void func();
#endif
min.c
#include "mix.h"
extern void delay_ms(int ms);
void hal_mix_rcc()
{
RCC->MP_AHB4ENSETR|=(0x1<<1);
RCC->MP_AHB4ENSETR|=(0x1<<4);
RCC->MP_AHB4ENSETR|=(0x1<<5);
RCC->MP_AHB4ENSETR|=(0x1<<6);
RCC->MP_APB1LPENSETR|=(0x1<<16);
}
void hal_mix_gpio()
{
GPIOB->MODER&=(~(0x3<<4));
GPIOB->MODER|=(0x2<<4);
GPIOG->MODER&=(~(0x3<<22));
GPIOG->MODER|=(0x2<<22);
GPIOB->AFRL&=(~(0xf<<8));
GPIOB->AFRL|=(0x8<<8);
GPIOG->AFRH&=(~(0xf<<12));
GPIOG->AFRH|=(0x6<<12);
GPIOE->MODER&=(~(0x3<<20));
GPIOE->MODER|=(0x1<<20);
GPIOF->MODER&=(~(0x3<<20));
GPIOF->MODER|=(0x1<<20);
GPIOE->MODER&=(~(0x3<<16));
GPIOE->MODER|=(0x1<<16);
GPIOE->OTYPER&=(~(0x1<<10));
GPIOF->OTYPER&=(~(0x1<<10));
GPIOE->OTYPER&=(~(0x1<<8));
GPIOE->OSPEEDR&=(~(0x3<<20));
GPIOF->OSPEEDR&=(~(0x3<<20));
GPIOE->OSPEEDR&=(~(0x3<<16));
GPIOE->PUPDR&=(~(0x3<<20));
GPIOF->PUPDR&=(~(0x3<<20));
GPIOE->PUPDR&=(~(0x3<<16));
}
void hal_led_on(int num)
{
switch(num)
{
case 1:
GPIOE->ODR|=(0x1<<10);
break;
case 2:
GPIOF->ODR|=(0x1<<10);
break;
case 3:
GPIOE->ODR|=(0x1<<8);
break;
}
}
void hal_led_off(int num)
{
switch(num)
{
case 1:
GPIOE->ODR&=(~(0x1<<10));
break;
case 2:
GPIOF->ODR&=(~(0x1<<10));
break;
case 3:
GPIOE->ODR&=(~(0x1<<8));
break;
}
}
void hal_mix_uart()
{
delay_ms(20);
USART4->CR1&=(~(0x1<<0));
USART4->CR1&=(~(0x1<<28));
USART4->CR1&=(~(0x1<<12));
USART4->CR1&=(~(0x1<<15));
USART4->CR1&=(~(0x1<<10));
USART4->CR2&=(~(0x3<<12));
USART4->PRESC&=(~(0xf<<0));
USART4->BRR=0x22B;
USART4->CR1|=(0x1<<3);
USART4->CR1|=(0x1<<2);
USART4->CR1|=(0x1<<0);
}
void put_char(const char ch)
{
while(!(USART4->ISR&(0x1<<7)));
USART4->TDR=ch;
while(!(USART4->ISR&(0x1<<6)));
}
void put_string(const char *string)
{
int i=0;
char ch;
while(1)
{
ch=string[i];
put_char(ch);
if(ch=='\0')
{
break;
}
i++;
}
}
char get_char()
{
char ch;
while(!(USART4->ISR&(0x1<<5)));
ch=USART4->RDR;
return ch;
}
char buffer[20];
char *get_string()
{
for(int i=0;i<19;i++)
{
buffer[i]=get_char();
put_char(buffer[i]);
if(buffer[i]=='\r')
{
put_char('\n');
buffer[i]='\0';
break;
}
}
return buffer;
}
int my_strcmp(char *a,char *b)
{
int i=0;
int j=0;
while(*(a+i)!='\0'&&*(b+j)!='\0'&&*(a+i)==*(b+j))
{
i++;
j++;
}
return *(a+i)-*(b+j);
}
void func()
{
char temp[20]={0};
get_string();
if(my_strcmp(buffer,"led1on")==0)
{
hal_led_on(1);
}
else if(my_strcmp(buffer,"led1off")==0)
{
hal_led_off(1);
}
else if(my_strcmp(buffer,"led2on")==0)
{
hal_led_on(2);
}
else if(my_strcmp(buffer,"led2off")==0)
{
hal_led_off(2);
}
else if(my_strcmp(buffer,"led3on")==0)
{
hal_led_on(3);
}
else if(my_strcmp(buffer,"led3off")==0)
{
hal_led_off(3);
}
}
main.c
#include "mix.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()
{
hal_mix_rcc();
hal_mix_gpio();
hal_mix_uart();
printf("abc");
while(1)
{
func();
}
return 0;
}
代码二:
运行结果:
结果一:
结果二: