|
#include <reg51.h>
#include "main.h"
#include "intrins.h"
//使用T2定時器2 ,控制串口1 波特率
#define FOSC 18432000L //系統頻率
#define BAUD 9600 //串口1波特率
#define TM2 (65536-(FOSC/4/BAUD))
#define NONE_PARITY 0 //無校驗
#define ODD_PARITY 1 //奇校驗
#define EVEN_PARITY 2 //偶校驗
#define MARSK_PARITY 3 //標記校驗
#define SPACE_PARITY 4 //空白校驗
#define PARITYBIT NONE_PARITY //定義校驗位
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;//使用定時器2 作為串口1的波特率發生器
ES = 1;
EA = 1;
uart1_SendString("\r\n stc15f2k08s uart1 test \r\n");
while(1);
}
/*********************************
串口1應用
**********************************/
/*UART1 中斷服務程序*/
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; //清忙標志
}
}
void uart1_SendData(BYTE dat)
{
while(busy1); //等待前面的數據發送完成
ACC = dat; // 獲取校驗位
if(P)
{
#if (PARITYBIT == ODD_PARITY)
{
TB8 = 0; //設置校驗位為0
}
#elif (PARITYBIT == EVEN_PARITY)
{
TB8 = 1; //設置校驗位為1
}
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
{
TB8 = 1; //設置校驗位為1
}
#elif (PARITYBIT == EVEN_PARITY)
{
TB8 = 0; //設置校驗位為0
}
#endif
}
busy1 = 1;
SBUF = ACC; //寫數據到UART 數據寄存器
}
void uart1_SendString(char *s)
{
while(*s) //檢測字符串結束標識
{
uart1_SendData(*s++); //發送當前字符
}
}
|
-
-
uart_project.zip
2019-10-29 16:54 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
23.76 KB, 下載次數: 10, 下載積分: 黑幣 -5
stc15 串口程序
評分
-
查看全部評分
|