ARM串口通信编程实验

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

c 复制代码
#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;
}
c 复制代码
#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));
}
c 复制代码
#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));
}


相关推荐
最后一个bug3 小时前
STM32MP1linux根文件系统目录作用
linux·c语言·arm开发·单片机·嵌入式硬件
最后一个bug11 小时前
rt-linux中使用mlockall与free的差异
linux·c语言·arm开发·单片机·嵌入式硬件·算法
李先静12 小时前
用 gdbserver 调试 arm-linux 上的 AWTK 应用程序
linux·arm开发·awtk
MrJson-架构师14 小时前
4.银河麒麟V10(ARM) 离线安装 MySQL
arm开发·mysql
qq_459730031 天前
4-3 MCU中ARM存储器的作用
arm开发·单片机·嵌入式硬件
轩辰~1 天前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
ARM&开发(Haidong)1 天前
ARM 获取cpu个数
arm开发
憧憬一下3 天前
PCIe_Host驱动分析_设备枚举
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发
7yewh3 天前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
憧憬一下5 天前
PCIe_Host驱动分析_地址映射
arm开发·嵌入式硬件·嵌入式·linux驱动开发·pci/pcie