Jump to Navigation

STC单片机串口通讯代码

中断方式的串口收发代码,适用于STC 1T单片机,使用11.0592MHz晶振时串口的波特率为9600bps。UIO_OutBuff[]是发送缓存区,调用UIO_SendBuff()发送整个缓存区的内容。中断方式最大程度地节省CPU的资源。

 

UIO.h代码

extern unsigned char UIO_OutBuff[];

void UIO_Init(void);
void UIO_SendBuff();

UIO.c代码

#include "Hardware.h"

unsigned char UIO_OutBuff[] = "HELLO...";
unsigned char UIO_SendPos;

void OnUARTReceive();

void UIO_Init(void)
{
	// 9600 bps

	TMOD = 0xF & TMOD | 0x20;
	TH1 = 0xfd;
	TL1 = 0xfd;    
	TR1 = 1;      

	SM0 = 0;
	SM1 = 1;     
	REN = 1;   
	ES = 1;     
}

void UIO_Routine(void) interrupt 4
{
	if (TI)
	{
		TI = 0;

		if (UIO_SendPos < sizeof(UIO_OutBuff) - 1)
		{
			SBUF = UIO_OutBuff[UIO_SendPos];
			UIO_SendPos ++;
		}
	}
	else
	{
		RI = 0;
		OnUARTReceive();
	}
}

void UIO_SendBuff()
{
	SBUF = UIO_OutBuff[0];
	UIO_SendPos = 1;
}


Main menu 2

about seo