ARM串口通信编程实验

2023-12-25 23:44:54

完成:从终端输入选项,完成点灯关灯,打开风扇关闭风扇等操作

#include "gpio.h"
int main()
{
 	  char a;
	//char buf[128];
	uart4_config();
	gpio_config();
    while(1)
    {
       //接收一个字符数据
	   a = getchar();
	   //发送接收的字符
	    putchar(a);
	   switch(a)
	   {
		   case '1':
		   	{
				   light1_on();
			}break;
			case '2':
			{
					light2_on();
			}break;
			case '3':
			{
					light3_on();
			}break;
			case '4':
			{
					fans_on();
			}break;
			case '0':
			{
					light_off();
			}break;
	   }
    }
    return 0;
}
#include "gpio.h"
 void uart4_config()
 {
	 //使能GPIOB\GPIOG\UART4外设时钟
	RCC->MP_AHB4ENSETR |= (0x1<<6);  //gpioB
	RCC->MP_AHB4ENSETR |= (0x1<<1);  //gpioG
	RCC->MP_APB4ENSETR |= (0x1<<16); //uart4
	 //设置PB2、PG11用于UART4管脚复用
	GPIOB->MODER &=~(0x3<<4);
	GPIOB->MODER |= (0x2<<4);
	GPIOB->AFRL &=~(0xf<<8);
	GPIOB->AFRL |= (0x8<<8);


	GPIOG->MODER &= ~(0x3<<22);
	GPIOG->MODER |= (0x2<<22);
	GPIOG->AFRH &=~(0xf<<12);
	GPIOG->AFRH |= (0x6<<12);
	//设置串口禁用
	USART4->CR1 &= ~(0x1);
	 //设置数据位宽为8位
	USART4->CR1 &=(~(0x1<<12));
	USART4->CR1 &=(~(0x1<<28));
	//设置无奇偶校验位
	USART4->CR1 &=(~(0x1<<10));
	//设置16倍过采样
	USART4->CR1 &=(~(0x1<<15));
	//设置1位停止位
	USART4->CR2 &=(~(0x3<<12));
	//设置不分频
	USART4->PRESC &= (~0xf);
	//设置波特率为115200
	USART4->BRR = 0x22B;
	//使能发送器
	USART4->CR1 |= (0x1<<3);
	//使能接收器
	USART4->CR1 |= (0x1<<2);
	//使能串口
	USART4->CR1 |= (0x1);
 }

void putchar(char a)
{
	while(!(USART4->ISR &(0x1<<7)));
	USART4->TDR=a;
	while(!(USART4->ISR &(0x1<<6)));
}

char getchar()
{
	char a;
	while(!(USART4->ISR &(0x1<<5)));
	a=USART4->RDR;
	return a;
}

void puts(char *s)
{
	while(*s)
	{
		putchar(*s);
		s++;
	}
	putchar('\r');
	putchar('\n');
}

void gets(char *s)
{
	while(1)
	{
		*s = getchar();
		putchar(*s);
		if(*s == 'r')
			break;
		s++;
	}
	*s = '\0';
	putchar('\n');
}

void gpio_config()
{
	RCC_GPIO |=(0x3<<4);
	GPIOE->MODER &=(~(0x3<<20));
	GPIOE->MODER |=(0x1<<20);
	GPIOE->OTYPER &=(~(0x1<<10));
	GPIOE->OSPEEDR &=(~(0x3<<20));
	GPIOE->PUPDR &=(~(0x3<<20));

	GPIOF->MODER &=(~(0x3<<20));
	GPIOF->MODER |=(0x1<<20);
	GPIOF->OTYPER &=(~(0x1<<10));
	GPIOF->OSPEEDR &=(~(0x3<<20));
	GPIOF->PUPDR &=(~(0x3<<20));

	GPIOE->MODER &=(~(0x3<<16));
	GPIOE->MODER |=(0x1<<16);
	GPIOE->OTYPER &=(~(0x1<<8));
	GPIOE->OSPEEDR &=(~(0x3<<16));
	GPIOE->PUPDR &=(~(0x3<<16));

	GPIOE->MODER &=(~(0x3<<18));
	GPIOE->MODER |=(0x1<<18);
	GPIOE->OTYPER &=(~(0x1<<9));
	GPIOE->OSPEEDR &=(~(0x3<<18));
	GPIOE->PUPDR &=(~(0x3<<18));

}

void light1_on()
{
	GPIOE->ODR |= (0x1<<10);
}

void light2_on()
{
	GPIOF->ODR |= (0x1<<10);
}

void light3_on()
{
	GPIOE->ODR |= (0x1<<8);
}

void fans_on()
{
	GPIOE->ODR |= (0x1<<9);
}

void light_off()
{
	GPIOE->ODR &= (~(0x1<<10));
	GPIOF->ODR &= (~(0x1<<10));
	GPIOE->ODR &= (~(0x1<<8));
	GPIOE->ODR &= (~(0x1<<9));
}

#include "gpio.h"
 void uart4_config()
 {
	 //使能GPIOB\GPIOG\UART4外设时钟
	RCC->MP_AHB4ENSETR |= (0x1<<6);  //gpioB
	RCC->MP_AHB4ENSETR |= (0x1<<1);  //gpioG
	RCC->MP_APB4ENSETR |= (0x1<<16); //uart4
	 //设置PB2、PG11用于UART4管脚复用
	GPIOB->MODER &=~(0x3<<4);
	GPIOB->MODER |= (0x2<<4);
	GPIOB->AFRL &=~(0xf<<8);
	GPIOB->AFRL |= (0x8<<8);


	GPIOG->MODER &= ~(0x3<<22);
	GPIOG->MODER |= (0x2<<22);
	GPIOG->AFRH &=~(0xf<<12);
	GPIOG->AFRH |= (0x6<<12);
	//设置串口禁用
	USART4->CR1 &= ~(0x1);
	 //设置数据位宽为8位
	USART4->CR1 &=(~(0x1<<12));
	USART4->CR1 &=(~(0x1<<28));
	//设置无奇偶校验位
	USART4->CR1 &=(~(0x1<<10));
	//设置16倍过采样
	USART4->CR1 &=(~(0x1<<15));
	//设置1位停止位
	USART4->CR2 &=(~(0x3<<12));
	//设置不分频
	USART4->PRESC &= (~0xf);
	//设置波特率为115200
	USART4->BRR = 0x22B;
	//使能发送器
	USART4->CR1 |= (0x1<<3);
	//使能接收器
	USART4->CR1 |= (0x1<<2);
	//使能串口
	USART4->CR1 |= (0x1);
 }

void putchar(char a)
{
	while(!(USART4->ISR &(0x1<<7)));
	USART4->TDR=a;
	while(!(USART4->ISR &(0x1<<6)));
}

char getchar()
{
	char a;
	while(!(USART4->ISR &(0x1<<5)));
	a=USART4->RDR;
	return a;
}


void gpio_config()
{
	RCC_GPIO |=(0x3<<4);
	GPIOE->MODER &=(~(0x3<<20));
	GPIOE->MODER |=(0x1<<20);
	GPIOE->OTYPER &=(~(0x1<<10));
	GPIOE->OSPEEDR &=(~(0x3<<20));
	GPIOE->PUPDR &=(~(0x3<<20));

	GPIOF->MODER &=(~(0x3<<20));
	GPIOF->MODER |=(0x1<<20);
	GPIOF->OTYPER &=(~(0x1<<10));
	GPIOF->OSPEEDR &=(~(0x3<<20));
	GPIOF->PUPDR &=(~(0x3<<20));

	GPIOE->MODER &=(~(0x3<<16));
	GPIOE->MODER |=(0x1<<16);
	GPIOE->OTYPER &=(~(0x1<<8));
	GPIOE->OSPEEDR &=(~(0x3<<16));
	GPIOE->PUPDR &=(~(0x3<<16));

	GPIOE->MODER &=(~(0x3<<18));
	GPIOE->MODER |=(0x1<<18);
	GPIOE->OTYPER &=(~(0x1<<9));
	GPIOE->OSPEEDR &=(~(0x3<<18));
	GPIOE->PUPDR &=(~(0x3<<18));

}

void light1_on()
{
	GPIOE->ODR |= (0x1<<10);
}

void light2_on()
{
	GPIOF->ODR |= (0x1<<10);
}

void light3_on()
{
	GPIOE->ODR |= (0x1<<8);
}

void fans_on()
{
	GPIOE->ODR |= (0x1<<9);
}

void light_off()
{
	GPIOE->ODR &= (~(0x1<<10));
	GPIOF->ODR &= (~(0x1<<10));
	GPIOE->ODR &= (~(0x1<<8));
	GPIOE->ODR &= (~(0x1<<9));
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_57071955/article/details/135208208
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。