c语言串口测试,STC89xx系列单片机串行口的测试程序(C程序)

串行口的测试程序(C程序及汇编程序)

1. C程序:

/*------------------------------------------------------------------------------------*/

/* --- STC MCU International Limited ---------------------------------------*/

/* --- 演示STC89xx系列单片机串行口功能 (8-bit/9-bit) ----------------*/

/*-------------------------------------------------------------------------------------*/

#include "reg51.h"

#include "intrins.h"

typedef unsigned char BYTE;

typedef unsigned int WORD;

#define FOSC 18432000L //System frequency

#define BAUD 9600 //UART baudrate

/*Define UART parity mode*/

#define NONE_PARITY 0 //None parity

#define ODD_PARITY 1 //Odd parity

#define EVEN_PARITY 2 //Even parity

#define MARK_PARITY 3 //Mark parity

#define SPACE_PARITY 4 //Space parity

#define PARITYBIT EVEN_PARITY //Testing even parity

sbit bit9 = P2^2; //P2.2 show UART data bit9

bit busy;

void SendData(BYTE dat);

void SendString(char *s);

void main()

{

#if (PARITYBIT == NONE_PARITY)

SCON = 0x50; //8-bit variable UART

135

#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)

SCON = 0xda; //9-bit variable UART, parity bit initial to 1

#elif (PARITYBIT == SPACE_PARITY)

SCON = 0xd2; //9-bit variable UART, parity bit initial to 0

#endif

TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode

TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule

TR1 = 1; //Timer1 start run

ES = 1; //Enable UART interrupt

EA = 1; //Open master interrupt switch

SendString("STC89-90xx\r\nUart Test !\r\n");

while(1);

}

/*----------------------------

UART interrupt service routine

----------------------------*/

void Uart_Isr() interrupt 4 using 1

{

if (RI)

{

RI = 0; //Clear receive interrupt flag

P0 = SBUF; //P0 show UART data

bit9 = RB8; //P2.2 show parity bit

}

if (TI)

{

TI = 0; //Clear transmit interrupt flag

busy = 0; //Clear transmit busy flag

}

}

/*----------------------------

Send a byte data to UART

Input: dat (data to be sent)

Output:None

----------------------------*/

void SendData(BYTE dat)

{

while (busy); //Wait for the completion of the previous data is sent

ACC = dat; //Calculate the even parity bit P (PSW.0)

if (P) //Set the parity bit according to P

{

#if (PARITYBIT == ODD_PARITY)

136

TB8 = 0; //Set parity bit to 0

#elif (PARITYBIT == EVEN_PARITY)

TB8 = 1; //Set parity bit to 1

#endif

}

else

{

#if (PARITYBIT == ODD_PARITY)

TB8 = 1; //Set parity bit to 1

#elif (PARITYBIT == EVEN_PARITY)

TB8 = 0; //Set parity bit to 0

#endif

}

busy = 1;

SBUF = ACC; //Send data to UART buffer

}

/*----------------------------

Send a string to UART

Input: s (address of string)

Output:None

----------------------------*/

void SendString(char *s)

{

while (*s) //Check the end of the string

{

SendData(*s++); //Send current char and increment string ptr

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值