|
#include <reg51.h>
#include "main.h"
#include "intrins.h"
//使用T2定時(shí)器2 ,控制串口1 波特率
#define FOSC 18432000L //系統(tǒng)頻率
#define BAUD 9600 //串口1波特率
#define TM2 (65536-(FOSC/4/BAUD))
#define NONE_PARITY 0 //無(wú)校驗(yàn)
#define ODD_PARITY 1 //奇校驗(yàn)
#define EVEN_PARITY 2 //偶校驗(yàn)
#define MARSK_PARITY 3 //標(biāo)記校驗(yàn)
#define SPACE_PARITY 4 //空白校驗(yàn)
#define PARITYBIT NONE_PARITY //定義校驗(yàn)位
bit busy1 = 0;
BYTE c_data;
BYTE s_data;
void uart1_SendString(char *s);
void uart1_SendData(BYTE dat);
void main(void)
{
SCON = 0x50; //八位可變波特率
T2L = TM2;
T2H = TM2>>8;
AUXR = AUXR_T2R;
AUXR |= AUXR_T2x12
AUXR |= AUXR_S1ST2;//使用定時(shí)器2 作為串口1的波特率發(fā)生器
ES = 1;
EA = 1;
uart1_SendString("\r\n stc15f2k08s uart1 test \r\n");
while(1);
}
/*********************************
串口1應(yīng)用
**********************************/
/*UART1 中斷服務(wù)程序*/
void uart1_ISR() interrupt 4 using 1
{
uchar r_data;
if(RI)
{
RI = 0;
r_data = SBUF;
c_data = RB8;
uart1_SendData(r_data);
}
if(TI)
{
TI = 0; //清除TI位
busy1 = 0; //清忙標(biāo)志
}
}
void uart1_SendData(BYTE dat)
{
while(busy1); //等待前面的數(shù)據(jù)發(fā)送完成
ACC = dat; // 獲取校驗(yàn)位
if(P)
{
#if (PARITYBIT == ODD_PARITY)
{
TB8 = 0; //設(shè)置校驗(yàn)位為0
}
#elif (PARITYBIT == EVEN_PARITY)
{
TB8 = 1; //設(shè)置校驗(yàn)位為1
}
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
{
TB8 = 1; //設(shè)置校驗(yàn)位為1
}
#elif (PARITYBIT == EVEN_PARITY)
{
TB8 = 0; //設(shè)置校驗(yàn)位為0
}
#endif
}
busy1 = 1;
SBUF = ACC; //寫數(shù)據(jù)到UART 數(shù)據(jù)寄存器
}
void uart1_SendString(char *s)
{
while(*s) //檢測(cè)字符串結(jié)束標(biāo)識(shí)
{
uart1_SendData(*s++); //發(fā)送當(dāng)前字符
}
}
|
-
-
uart_project.zip
2019-10-29 16:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
23.76 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5
stc15 串口程序
評(píng)分
-
查看全部評(píng)分
|