【接口协议】03.UART

【嵌入式八股】一、语言篇https://www.nowcoder.com/creation/manager/columnDetail/mwQPeM

【嵌入式八股】二、计算机基础篇https://www.nowcoder.com/creation/manager/columnDetail/Mg5Lym

【嵌入式八股】三、硬件篇(本专栏)https://www.nowcoder.com/creation/manager/columnDetail/MRVDlM

【嵌入式八股】四、嵌入式Linux篇https://www.nowcoder.com/creation/manager/columnDetail/MQ2bb0

UART

UART基础

简介

通用异步收发器(Universal Asynchronous Receiver/Transmitter),通常称作UART,是一种串行、异步、全双工的通信协议,在嵌入式领域应用的非常广泛。

硬件连接

alt

硬件内部结构

2440和STM32

alt

时序

alt

  • 波特率:一般选波特率都会有9600,115200等选项。其实意思就是每秒传输这么多个比特位数(bit)。
  • 起始位:先发出一个逻辑”0”的信号,表示传输数据的开始。
  • 数据位:可以是5~8位逻辑”0”或”1”。如ASCII码(7位),扩展BCD码(8位)。小端传输。
  • 校验位:数据位加上这一位后,使得“1”的位数应为偶数(偶校验)或奇数(奇校验),以此来校验数据传送的正确性。
  • 停止位:它是一个字符数据的结束标志。固定为高电平。

代码

GPIO模拟串口输出

#include <stdio.h>
#include <wiringPi.h>

// GPIO引脚定义
#define TX_PIN  17

// 初始化GPIO
void setup() {
    wiringPiSetupGpio();  // 使用GPIO编号模式
    pinMode(TX_PIN, OUTPUT);  // 设置TX_PIN为输出模式
}

// 发送一个字节
void sendByte(unsigned char byte) {
    // 发送起始位(低电平)
    digitalWrite(TX_PIN, LOW);
    delayMicroseconds(200);  // 起始位持续时间

    // 发送数据位
    for (int i = 0; i < 8; i++) {
        digitalWrite(TX_PIN, (byte >> i) & 0x01);  // 设置引脚电平
        delayMicroseconds(200);  // 数据位持续时间
    }

    // 发送停止位(高电平)
    digitalWrite(TX_PIN, HIGH);
    delayMicroseconds(200);  // 停止位持续时间
}

int main() {
    setup();

    // 发送数据
    unsigned char data = 'A';
    sendByte(data);

    return 0;
}

串口发送函数

//发送一个字节
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
//发送一个数组
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}
//发送一个字符串
void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}
//发送一个数字
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
	}
}
//重定向输出
int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}
//使用可变函数参数
void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Serial_SendString(String);
}

//发送数据包

串口接收函数

alt

//收发HEX数据包
void Serial_SendPacket(void)
{
	Serial_SendByte(0xFF);
	Serial_SendArray(Serial_TxPacket, 4);
	Serial_SendByte(0xFE);
}

uint8_t Serial_GetRxFlag(void)
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}
void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxPacket = 0;
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		
		if (RxState == 0)
		{
			if (RxData == 0xFF)
			{
				RxState = 1;
				pRxPacket = 0;
			}
		}
		else if (RxState == 1)
		{
			Serial_RxPacket[pRxPacket] = RxData;
			pRxPacket ++;
			if (pRxPacket >= 4)
			{
				RxState = 2;
			}
		}
		else if (RxState == 2)
		{
			if (RxData == 0xFE)
			{
				RxState = 0;
				Serial_RxFlag = 1;
			}
		}		
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}

alt

//收发文本数据包

void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxPacket = 0;
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		
		if (RxState == 0)
		{
			if (RxData == '@' && Serial_RxFlag == 0)
			{
				RxState = 1;
				pRxPacket = 0;
			}
		}
		else if (RxState == 1)
		{
			if (RxData == '\r')
			{
				RxState = 2;
			}
			else
			{
				Serial_RxPacket[pRxPacket] = RxData;
				pRxPacket ++;
			}
		}
		else if (RxState == 2)
		{
			if (RxData == '\n')
			{
				RxState = 0;
				Serial_RxPacket[pRxPacket] = '\0';
				Serial_RxFlag = 1;
			}
		}
		
		USART_ClearITPendingB

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

【嵌入式八股】三、硬件篇 文章被收录于专栏

查阅整理上千份嵌入式面经,将相关资料汇集于此,主要包括: 0.简历面试 1.语言篇 2.计算机基础 3.硬件篇【本专栏】 4.嵌入式Linux (建议PC端查看)

全部评论

相关推荐

4 7 评论
分享
牛客网
牛客企业服务