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));
}


相关推荐
TeYiToKu7 小时前
笔记整理—linux驱动开发部分(9)framebuffer驱动框架
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件·arm
w微信150135078121 天前
小华一级 代理商 HC32F005C6PA-TSSOP20 HC32F005系列
c语言·arm开发·单片机·嵌入式硬件
憧憬一下1 天前
Pinctrl子系统中Pincontroller和client驱动程序的编写
arm开发·嵌入式·c/c++·linux驱动开发
上海知从科技1 天前
知从科技受邀出席ARM日产技术日
arm开发·科技
极客小张2 天前
基于STM32的智能温室环境监测与控制系统设计(代码示例)
c语言·arm开发·stm32·单片机·嵌入式硬件·物联网·毕业设计
TeYiToKu3 天前
笔记整理—linux驱动开发部分(6)platform平台总线
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件
飞腾开发者3 天前
飞腾平台Arm ComputeLibrary编译安装指南
linux·服务器·arm开发·后端·性能优化
CodingCos3 天前
【ARM Linux 系统稳定性分析入门及渐进 1.1 -- Crash 工具功能概述】
linux·arm开发·crash tools·linux crash·crash 工具使用
@haihi5 天前
IIC和SPI的区别和相同之处
arm开发·stm32·mcu
@@庆5 天前
FreeRTOS 数据传输方法(环形buffer,队列的本质)队列实验—多设备玩游戏
arm开发·stm32·单片机·嵌入式硬件·freertos