2022.12.27---UART

本文介绍STM32微控制器中UART4模块的初始化配置及数据收发功能实现。通过具体的C语言代码示例,展示了如何设置引脚、波特率及寄存器等,实现了字符与字符串的发送和接收。

实现串口的数据收发

#include "uart4.h"


//uart4初始化
void hal_uart4_init()
{
	RCC->MP_AHB4ENSETR |= (0x1<<1);
	RCC->MP_AHB4ENSETR |= (0x1<<6);
	RCC->MP_APB1ENSETR |= (0x1<<16);
	//PB2设置为复用功能
	GPIOB->MODER &= (~(0X3<<4));
	GPIOB->MODER |= (0X2<<4);
	GPIOB->AFRL &= (~(0XF<<8));
	GPIOB->AFRL |= (0X8<<8);

	//PG11设置为复用功能
	GPIOG->MODER &= (~(0X3<<22));
	GPIOG->MODER |= (0X2<<22);
	GPIOG->AFRH &= (~(0XF<<12));
	GPIOG->AFRH |= (0X6<<12);

	//设置CR1
	USART4->CR1 &= (~(0x1 << 28));
	USART4->CR1 &= (~(0x1 << 12));
	USART4->CR1 &= (~(0x1 << 10));
	USART4->CR1 &= (~(0x1 << 15));
	
	//设置CR2
	USART4->CR2 &= (~(0x3 << 12));

	//设置PRESC
	USART4->PRESC &= (~(0xF << 0));

	//设置BRR
	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 *str)
{
	while(*str)
	{
		put_char(*str++);
	}
	put_char('\n');
	put_char('\r');
}

//接收一个字符
char get_char()
{
	char ch=0;
	while(!(USART4->ISR & (0x1 << 5)));
	ch = USART4->RDR;//2.接收到数据
	return ch;
}

//接收一个字符串
char str[16]="";
char *get_string()
{
	int i=0;
	for(i=0;i<16;i++)
	{
		str[i] = get_char(); //接收一个字符
		put_char(str[i]); //发送一个字符 
		if(str[i] == '\r') //判断键盘enter键是否按下
			break;
	}
	str[i] = '\0';
	put_char('\n');
	return str;
}

Error-[IND] Identifier not declared /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 54 Identifier 'tx_data' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 Error-[IND] Identifier not declared /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 58 Identifier 'tx_data' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 Error-[MFNF] Member not found /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 67 "this.read_word." Could not find member 'rdata' in class 'read_word_seq', at "/iccad/home/lvr/work/base_env/uart/dv/seq/apb_master_wr_sequence.sv", 44. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 Error-[IND] Identifier not declared /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 68 Identifier 'tx_data' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 Error-[IND] Identifier not declared /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 55 Identifier 'tx_data' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 Error-[IND] Identifier not declared /iccad/home/lvr/work/base_env/uart/dv/tests/tc_uart_test_case.sv, 71 Identifier 'tx_data' has not been declared yet. If this error is not expected, please check if you have set `default_nettype to none. Instance stack trace: $unit /edatools/synopsys/vcs/T-2022.06-SP2/etc/uvm-1.2/uvm_pkg.sv, 1 报错了怎么改正
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值