51单片机 串口通信

UART.h

#ifndef __UART_H__
#define __UART_H__

void UART_Init();
void UART_SendByte(unsigned char Byte);

#endif

UART.H

#include 

/**
  * @brief  串口初始化,[email protected]
  * @param  无
  * @retval 无
  */
void UART_Init()
{
	SCON=0x40;
	PCON |= 0x80;
	TMOD &= 0x0F;		//设置定时器模式
	TMOD |= 0x20;		//设置定时器模式
	TL1 = 0xF3;		//设定定时初值
	TH1 = 0xF3;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
}

/**
  * @brief  串口发送一个字节数据
  * @param  Byte 要发送的一个字节数据
  * @retval 无
  */
void UART_SendByte(unsigned char Byte)
{
	SBUF=Byte;
	while(TI==0);
	TI=0;
}

C

#include 
#include "Delay.h"
#include "UART.h"

unsigned char Sec;


void main()
{
	UART_Init();

	while(1)
	{
			UART_SendByte(Sec++);
			Delay(1000);
	}


}

你可能感兴趣的:(嵌入式,51单片机,单片机,嵌入式硬件)