|
#define MAIN_Fosc 22118400L //定義主時鐘
#include "STC15Fxxxx.H"
#define Baudrate1 115200L
#define UART1_BUF_LENGTH 32
u8 TX1_Cnt; //發送計數
u8 RX1_Cnt; //接收計數
bit B_TX1_Busy; //發送忙標志
u8 idata RX1_Buffer[UART1_BUF_LENGTH]; //接收緩沖
void UART1_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
void PrintString1(u8 *puts);
//========================================================================
// 函數: void main(void)
// 描述: 主函數。
// 參數: none.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 備注:
//========================================================================
void main(void)
{
P0M1 = 0; P0M0 = 0; //設置為準雙向口
P1M1 = 0; P1M0 = 0; //設置為準雙向口
P2M1 = 0; P2M0 = 0; //設置為準雙向口
P3M1 = 0; P3M0 = 0; //設置為準雙向口
P4M1 = 0; P4M0 = 0; //設置為準雙向口
P5M1 = 0; P5M0 = 0; //設置為準雙向口
P6M1 = 0; P6M0 = 0; //設置為準雙向口
P7M1 = 0; P7M0 = 0; //設置為準雙向口
UART1_config(1); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
EA = 1; //允許總中斷
PrintString1("STC15F2K60S2 UART1 Test Prgramme!\r\n"); //SUART1發送一個字符串
while (1)
{
if((TX1_Cnt != RX1_Cnt) && (!B_TX1_Busy)) //收到數據, 發送空閑
{
SBUF = RX1_Buffer[TX1_Cnt]; //把收到的數據遠樣返回
B_TX1_Busy = 1;
if(++TX1_Cnt >= UART1_BUF_LENGTH) TX1_Cnt = 0;
}
}
}
//========================================================================
// 函數: void PrintString1(u8 *puts)
// 描述: 串口1發送字符串函數。
// 參數: puts: 字符串指針.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 備注:
//========================================================================
void PrintString1(u8 *puts) //發送一個字符串
{
for (; *puts != 0; puts++) //遇到停止符0結束
{
SBUF = *puts;
B_TX1_Busy = 1;
while(B_TX1_Busy);
}
}
//========================================================================
// 函數: SetTimer2Baudraye(u16 dat)
// 描述: 設置Timer2做波特率發生器。
// 參數: dat: Timer2的重裝值.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 備注:
//========================================================================
void SetTimer2Baudraye(u16 dat) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = dat / 256;
TL2 = dat % 256;
IE2 &= ~(1<<2); //禁止中斷
AUXR |= (1<<4); //Timer run enable
}
//========================================================================
// 函數: void UART1_config(u8 brt)
// 描述: UART1初始化函數。
// 參數: brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 備注:
//========================================================================
void UART1_config(u8 brt) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
/*********** 波特率使用定時器2 *****************/
if(brt == 2)
{
AUXR |= 0x01; //S1 BRT Use Timer2;
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate1);
}
/*********** 波特率使用定時器1 *****************/
else
{
TR1 = 0;
AUXR &= ~0x01; //S1 BRT Use Timer1;
AUXR |= (1<<6); //Timer1 set as 1T mode
TMOD &= ~(1<<6); //Timer1 set As Timer
TMOD &= ~0x30; //Timer1_16bitAutoReload;
TH1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) / 256);
TL1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) % 256);
ET1 = 0; //禁止中斷
INT_CLKO &= ~0x02; //不輸出時鐘
TR1 = 1;
}
/*************************************************/
SCON = (SCON & 0x3f) | 0x40; //UART1模式, 0x00: 同步移位輸出, 0x40: 8位數據,可變波特率, 0x80: 9位數據,固定波特率, 0xc0: 9位數據,可變波特率
// PS = 1; //高優先級中斷
ES = 1; //允許中斷
REN = 1; //允許接收
P_SW1 &= 0x3f;
P_SW1 |= 0x80; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7 (必須使用內部時鐘)
// PCON2 |= (1<<4); //內部短路RXD與TXD, 做中繼, ENABLE,DISABLE
B_TX1_Busy = 0;
TX1_Cnt = 0;
RX1_Cnt = 0;
}
//========================================================================
// 函數: void UART1_int (void) interrupt UART1_VECTOR
// 描述: UART1中斷函數。
// 參數: nine.
// 返回: none.
// 版本: VER1.0
// 日期: 2014-11-28
// 備注:
//========================================================================
void UART1_int (void) interrupt 4
{
if(RI)
{
RI = 0;
RX1_Buffer[RX1_Cnt] = SBUF;
if(++RX1_Cnt >= UART1_BUF_LENGTH) RX1_Cnt = 0; //防溢出
}
if(TI)
{
TI = 0;
B_TX1_Busy = 0;
}
}
|
|