|
GYJ-0321 某一個寶描述_01.jpg (593.94 KB, 下載次數: 31)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_02.jpg (1.3 MB, 下載次數: 22)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_09.jpg (561.6 KB, 下載次數: 20)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_10.jpg (461.2 KB, 下載次數: 23)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_11.jpg (993.43 KB, 下載次數: 32)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_12.jpg (653.15 KB, 下載次數: 29)
下載附件
2023-7-1 18:45 上傳
GYJ-0321 某一個寶描述_13.jpg (667.91 KB, 下載次數: 29)
下載附件
2023-7-1 18:45 上傳
【聲明】此程序僅用于學習與參考!
*********************************************************************/
/************* 功能說明 **************
4路串口全雙工中斷方式收發通訊程序。
通過PC向MCU發送數據, MCU收到后通過串口把收到的數據原樣返回.
用定時器做波特率發生器,建議使用1T模式(除非低波特率用12T),并選擇可被波特率整除的時鐘頻率,以提高精度。
下載時, 選擇時鐘 11.0592MHz (用戶可自行修改頻率).
******************************************/
#include "STC8H8K64U.h" //包含此頭文件后,里面聲明的寄存器不需要再手動輸入,避免重復定義
#include "stdio.h"
#include "intrins.h"
#define uchar unsigned char//宏定義無符號字符型
#define uint unsigned int //宏定義無符號整型
#define MAIN_Fosc 11059200L //定義主時鐘(精確計算9600波特率)
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
////手動輸入聲明"reg51.h"頭文件里面沒有定義的寄存器
//sfr AUXR = 0x8E;
sfr INT_CLKO = 0x8F;
//sfr P_SW1 = 0xA2;
//sfr P_SW2 = 0xBA;
//sfr IE2 = 0xAF;
//sfr S4CON = 0x84;
//sfr S4BUF = 0x85;
//sfr S2CON = 0x9A;
//sfr S2BUF = 0x9B;
//sfr S3CON = 0xAC;
//sfr S3BUF = 0xAD;
//sfr T2H = 0xD6;
//sfr T2L = 0xD7;
//sfr T3H = 0xD4;
//sfr T3L = 0xD5;
//sfr T4H = 0xD2;
//sfr T4L = 0xD3;
//sfr T4T3M = 0xD1;
//sfr P4 = 0xC0;
//sfr P5 = 0xC8;
//sfr P6 = 0xE8;
//sfr P7 = 0xF8;
//sfr P1M1 = 0x91; //PxM1.n,PxM0.n =00--->Standard, 01--->push-pull
//sfr P1M0 = 0x92; // =10--->pure input, 11--->open drain
//sfr P0M1 = 0x93;
//sfr P0M0 = 0x94;
//sfr P2M1 = 0x95;
//sfr P2M0 = 0x96;
//sfr P3M1 = 0xB1;
//sfr P3M0 = 0xB2;
//sfr P4M1 = 0xB3;
//sfr P4M0 = 0xB4;
//sfr P5M1 = 0xC9;
//sfr P5M0 = 0xCA;
//sfr P6M1 = 0xCB;
//sfr P6M0 = 0xCC;
//sfr P7M1 = 0xE1;
//sfr P7M0 = 0xE2;
//sbit P00 = P0^0;
//sbit P01 = P0^1;
//sbit P02 = P0^2;
//sbit P03 = P0^3;
//sbit P04 = P0^4;
//sbit P05 = P0^5;
//sbit P06 = P0^6;
//sbit P07 = P0^7;
//sbit P10 = P1^0;
//sbit P11 = P1^1;
//sbit P12 = P1^2;
//sbit P13 = P1^3;
//sbit P14 = P1^4;
//sbit P15 = P1^5;
//sbit P16 = P1^6;
//sbit P17 = P1^7;
//sbit P20 = P2^0;
//sbit P21 = P2^1;
//sbit P22 = P2^2;
//sbit P23 = P2^3;
//sbit P24 = P2^4;
//sbit P25 = P2^5;
//sbit P26 = P2^6;
//sbit P27 = P2^7;
//sbit P30 = P3^0;
//sbit P31 = P3^1;
//sbit P32 = P3^2;
//sbit P33 = P3^3;
//sbit P34 = P3^4;
//sbit P35 = P3^5;
//sbit P36 = P3^6;
//sbit P37 = P3^7;
//sbit P40 = P4^0;
//sbit P41 = P4^1;
//sbit P42 = P4^2;
//sbit P43 = P4^3;
//sbit P44 = P4^4;
//sbit P45 = P4^5;
//sbit P46 = P4^6;
//sbit P47 = P4^7;
//sbit P50 = P5^0;
//sbit P51 = P5^1;
//sbit P52 = P5^2;
//sbit P53 = P5^3;
//sbit P54 = P5^4;
//sbit P55 = P5^5;
//sbit P56 = P5^6;
//sbit P57 = P5^7;
#define Baudrate1 9600UL
#define Baudrate2 9600UL
#define Baudrate3 9600UL
#define Baudrate4 9600UL
#define UART1_BUF_LENGTH 64
#define UART2_BUF_LENGTH 64
#define UART3_BUF_LENGTH 64
#define UART4_BUF_LENGTH 64
u8 TX1_Cnt; //發送計數
u8 RX1_Cnt; //接收計數
u8 TX2_Cnt; //發送計數
u8 RX2_Cnt; //接收計數
u8 TX3_Cnt; //發送計數
u8 RX3_Cnt; //接收計數
u8 TX4_Cnt; //發送計數
u8 RX4_Cnt; //接收計數
bit B_TX1_Busy; //發送忙標志
bit B_TX2_Busy; //發送忙標志
bit B_TX3_Busy; //發送忙標志
bit B_TX4_Busy; //發送忙標志
u8 xdata RX1_Buffer[UART1_BUF_LENGTH]; //接收緩沖
u8 xdata RX2_Buffer[UART2_BUF_LENGTH]; //接收緩沖
u8 xdata RX3_Buffer[UART3_BUF_LENGTH]; //接收緩沖
u8 xdata RX4_Buffer[UART4_BUF_LENGTH]; //接收緩沖
uchar receBuf[10];//接收緩沖區
void UART1_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
void UART2_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
void UART3_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
void UART4_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
void PrintString1(u8 *puts);
void PrintString2(u8 *puts);
void PrintString3(u8 *puts);
void PrintString4(u8 *puts);
void delay(uchar dat);//延時程序
void senduart2();//發送函數
void KEY(); //按鍵函數
void LED(); //測試輸出
/**************** IO口定義及聲明 *****************/
/**************** 按鍵輸入口 *****************/
sbit S1=P3^2;//外部中斷輸入0
sbit S2=P3^3;//外部中斷輸入1
sbit S3=P3^4;
sbit S4=P3^5;
sbit S5=P3^6;
sbit S6=P3^7;
/**************** 485通訊換向口 *****************/
sbit RS485_DIR = P0^2; //RS485方向控制
/**************** 外部函數聲明和外部變量聲明 *****************/
bit flag_zx3=0;//串口2標志
uchar UART3_JS=0;//串口2數據計數
bit S1bz=0;//按鍵1標志
bit S2bz=0;//按鍵2標志
bit S3bz=0;//按鍵3標志
bit S4bz=0;//按鍵4標志
bit S5bz=0;//按鍵5標志
bit S6bz=0;//按鍵6標志
bit UART3_FSBZ=0;//串口2發送標志
uchar uart_dat=0;//按鍵數據
uint yssj=1000;
//========================================================================
// 函數: void main(void)
// 描述: 主函數。
// 參數: none.
// 返回: none.
//========================================================================
void main(void)
{
P0M1 = 0x00; P0M0 = 0x00; //設置為準雙向口
P1M1 = 0x00; P1M0 = 0x00; //設置為準雙向口
P2M1 = 0x00; P2M0 = 0x00; //設置為準雙向口
P3M1 = 0x00; P3M0 = 0x00; //設置為準雙向口
P4M1 = 0x00; P4M0 = 0x00; //設置為準雙向口
P5M1 = 0x00; P5M0 = 0x00; //設置為準雙向口
P6M1 = 0x00; P6M0 = 0x00; //設置為準雙向口
P7M1 = 0x00; P7M0 = 0x00; //設置為準雙向口
LED();
UART1_config(2); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
UART2_config(2); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
UART3_config(0); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
UART4_config(0); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
EA = 1; //允許全局中斷
RS485_DIR=1;//發送狀態
PrintString1("STC8H8K64U UART1 Test Programme!\r\n"); //UART1發送一個字符串
PrintString2("STC8H8K64U UART2 Test Programme!\r\n"); //UART2發送一個字符串
PrintString3("STC8H8K64U UART3 Test Programme!\r\n"); //UART3發送一個字符串
PrintString4("STC8H8K64U UART4 Test Programme!\r\n"); //UART4發送一個字符串
RS485_DIR=0;//讀取狀態
while (1)
{
KEY();//按鍵函數
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;
}
if((TX2_Cnt != RX2_Cnt) && (!B_TX2_Busy)) //收到數據, 發送空閑
{
S2BUF = RX2_Buffer[TX2_Cnt];
B_TX2_Busy = 1;
if(++TX2_Cnt >= UART2_BUF_LENGTH) TX2_Cnt = 0;
}
// if((TX3_Cnt != RX3_Cnt) && (!B_TX3_Busy)) //收到數據, 發送空閑
// {
// S3BUF = RX3_Buffer[TX3_Cnt];
// B_TX3_Busy = 1;
// if(++TX3_Cnt >= UART3_BUF_LENGTH) TX3_Cnt = 0;
// }
if((TX4_Cnt != RX4_Cnt) && (!B_TX4_Busy)) //收到數據, 發送空閑
{
S4BUF = RX4_Buffer[TX4_Cnt];
B_TX4_Busy = 1;
if(++TX4_Cnt >= UART4_BUF_LENGTH) TX4_Cnt = 0;
}
}
}
//========================================================================
// 主函數程序結束
//========================================================================
/********************************************************************
延時函數
*********************************************************************/
void delay(uint dat)//延時程序
{
uint m,n,s;
for(m=dat;m>0;m--)
for(n=2;n>0;n--)
for(s=100;s>0;s--);
}
/****************發送函數*********************/
void senduart3()//發送函數
{
RS485_DIR=1;
S3BUF=0xAA;B_TX3_Busy = 1;while(B_TX3_Busy);
S3BUF=uart_dat;B_TX3_Busy = 1;while(B_TX3_Busy);
S3BUF=0xFF;B_TX3_Busy = 1;while(B_TX3_Busy);
S3BUF=0xFF;B_TX3_Busy = 1;while(B_TX3_Busy);
S3BUF=0xBB;B_TX3_Busy = 1;while(B_TX3_Busy);
RS485_DIR=0;
}
/*****************清空發送緩沖區*************************/
void clear_receBuf() //清空發送緩沖區
{
uchar i;
for(i=0;i<5;i++)
{
RX3_Buffer=0;
}
}
/********************************************************************
按鍵函數
*********************************************************************/
void KEY() //按鍵函數
{
if(UART3_FSBZ==1){senduart3();UART3_FSBZ=0;}
if((S1==0)&&(S1bz==0)){delay(10);if((S1==0)&&(S1bz==0)){uart_dat=0X01;senduart3();S1bz=1;}}if((S1==1)&&(S1bz==1)){delay(10);if((S1==1)&&(S1bz==1)){S1bz=0;}}
if((S2==0)&&(S2bz==0)){delay(10);if((S2==0)&&(S2bz==0)){uart_dat=0X02;senduart3();S2bz=1;}}if((S2==1)&&(S2bz==1)){delay(10);if((S2==1)&&(S2bz==1)){S2bz=0;}}
if((S3==0)&&(S3bz==0)){delay(10);if((S3==0)&&(S3bz==0)){uart_dat=0X03;senduart3();S3bz=1;}}if((S3==1)&&(S3bz==1)){delay(10);if((S3==1)&&(S3bz==1)){S3bz=0;}}
if((S4==0)&&(S4bz==0)){delay(10);if((S4==0)&&(S4bz==0)){uart_dat=0X04;senduart3();S4bz=1;}}if((S4==1)&&(S4bz==1)){delay(10);if((S4==1)&&(S4bz==1)){S4bz=0;}}
if((S5==0)&&(S5bz==0)){delay(10);if((S5==0)&&(S5bz==0)){uart_dat=0X05;senduart3();S5bz=1;}}if((S5==1)&&(S5bz==1)){delay(10);if((S5==1)&&(S5bz==1)){S5bz=0;}}
if((S6==0)&&(S6bz==0)){delay(10);if((S6==0)&&(S6bz==0)){uart_dat=0X06;senduart3();S6bz=1;}}if((S6==1)&&(S6bz==1)){delay(10);if((S6==1)&&(S6bz==1)){S6bz=0;}}
}
void LED() //測試輸出
{
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);P40=0;
delay(yssj);P64=0;
delay(yssj);P65=0;
delay(yssj);P66=0;
delay(yssj);P67=0;
delay(yssj);P30=0;
delay(yssj);P31=0;
delay(yssj);P32=0;
delay(yssj);P33=0;
delay(yssj);P34=0;
delay(yssj);P50=0;
delay(yssj);P51=0;
delay(yssj);P35=0;
delay(yssj);P36=0;
delay(yssj);P37=0;
delay(yssj);P70=0;
delay(yssj);P71=0;
delay(yssj);P72=0;
delay(yssj);P73=0;
delay(yssj);P41=0;
delay(yssj);P42=0;
delay(yssj);P43=0;
delay(yssj);P44=0;
delay(yssj);P20=0;
delay(yssj);P21=0;
delay(yssj);P22=0;
delay(yssj);P23=0;
delay(yssj);P24=0;
delay(yssj);P25=0;
delay(yssj);P26=0;
delay(yssj);P27=0;
delay(yssj);P74=0;
delay(yssj);P75=0;
delay(yssj);P76=0;
delay(yssj);P77=0;
delay(yssj);P45=0;
delay(yssj);P46=0;
delay(yssj);P00=0;
delay(yssj);P01=0;
delay(yssj);P02=0;
delay(yssj);P03=0;
delay(yssj);P04=0;
delay(yssj);P52=0;
delay(yssj);P53=0;
delay(yssj);P05=0;
delay(yssj);P06=0;
delay(yssj);P07=0;
delay(yssj);P60=0;
delay(yssj);P61=0;
delay(yssj);P62=0;
delay(yssj);P63=0;
delay(yssj);P10=0;
delay(yssj);P11=0;
delay(yssj);P47=0;
delay(yssj);P14=0;
delay(yssj);P15=0;
delay(yssj);P16=0;
delay(yssj);P17=0;
delay(yssj);P13=0;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
}
//========================================================================
// 函數: void PrintString1(u8 *puts)
// 描述: 串口1發送字符串函數。
// 參數: puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString1(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結束
{
SBUF = *puts;
B_TX1_Busy = 1;
while(B_TX1_Busy);
}
}
//========================================================================
// 函數: void PrintString2(u8 *puts)
// 描述: 串口2發送字符串函數。
// 參數: puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString2(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結束
{
S2BUF = *puts;
B_TX2_Busy = 1;
while(B_TX2_Busy);
}
}
//========================================================================
// 函數: void PrintString3(u8 *puts)
// 描述: 串口3發送字符串函數。
// 參數: puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString3(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結束
{
S3BUF = *puts;
B_TX3_Busy = 1;
while(B_TX3_Busy);
}
}
//========================================================================
// 函數: void PrintString4(u8 *puts)
// 描述: 串口4發送字符串函數。
// 參數: puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString4(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結束
{
S4BUF = *puts;
B_TX4_Busy = 1;
while(B_TX4_Busy);
}
}
//========================================================================
// 函數: SetTimer2Baudraye(u16 dat)
// 描述: 設置Timer2做波特率發生器。
// 參數: dat: Timer2的重裝值.
// 返回: none.
//========================================================================
void SetTimer2Baudraye(u16 dat)
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
T2H = dat / 256;
T2L = dat % 256;
IE2 &= ~(1<<2); //禁止中斷
AUXR |= (1<<4); //Timer run enable
}
//========================================================================
// 函數: void UART1_config(u8 brt)
// 描述: UART1初始化函數。
// 參數: brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
// 返回: none.
//========================================================================
void UART1_config(u8 brt)
{
/*********** 波特率使用定時器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; //UART1 switch to, 0x00: P3.0 P3.1
P_SW1 |= 0x00; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7, 0xC0: P4.3 P4.4
// PCON2 |= (1<<4); //內部短路RXD與TXD, 做中繼, ENABLE,DISABLE
B_TX1_Busy = 0;
TX1_Cnt = 0;
RX1_Cnt = 0;
}
//========================================================================
// 函數: void UART2_config(u8 brt)
// 描述: UART2初始化函數。
// 參數: brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
// 返回: none.
//========================================================================
void UART2_config(u8 brt) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
{
if(brt == 2)
{
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2);
S2CON &= ~(1<<7); // 8位數據, 1位起始位, 1位停止位, 無校驗
IE2 |= 1; //允許中斷
S2CON |= (1<<4); //允許接收
P_SW2 &= ~0x01; //UART2 switch to: 0: P1.0 P1.1
// P_SW2 |= 1; //UART2 switch to: 1: P4.0 P4.2
B_TX2_Busy = 0;
TX2_Cnt = 0;
RX2_Cnt = 0;
}
}
//========================================================================
// 函數: void UART3_config(u8 brt)
// 描述: UART3初始化函數。
// 參數: brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
// 返回: none.
//========================================================================
void UART3_config(u8 brt) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
{
if(brt == 2)
{
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate3);
S3CON = 0x10; //8位數據, 使用Timer2做波特率發生器, 允許接收
}
else
{
S3CON = 0x50; //8位數據, 使用Timer3做波特率發生器, 允許接收
T3H = (65536UL - (MAIN_Fosc / 4) / Baudrate3) / 256;
T3L = (65536UL - (MAIN_Fosc / 4) / Baudrate3) % 256;
T4T3M &= 0xf0;
T4T3M |= 0x0a;
}
IE2 |= 0x08; //允許UART3中斷
P_SW2 &= ~0x02; //UART3 switch bit1 to: 0: P0.0 P0.1
//P_SW2 |= 0x02; //UART3 switch bit1 to: 1: P5.0 P5.1
B_TX3_Busy = 0;
TX3_Cnt = 0;
RX3_Cnt = 0;
}
//========================================================================
// 函數: void UART4_config(u8 brt)
// 描述: UART4初始化函數。
// 參數: brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
// 返回: none.
//========================================================================
void UART4_config(u8 brt) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
{
if(brt == 2)
{
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate4);
S4CON = 0x10; //8位數據, 使用Timer2做波特率發生器, 允許接收
}
else
{
S4CON = 0x50; //8位數據, 使用Timer4做波特率發生器, 允許接收
T4H = (65536UL - (MAIN_Fosc / 4) / Baudrate4) / 256;
T4L = (65536UL - (MAIN_Fosc / 4) / Baudrate4) % 256;
T4T3M &= 0x0f;
T4T3M |= 0xa0;
}
IE2 |= 0x10; //允許UART4中斷
// P_SW2 &= ~0x04; //UART4 switch bit2 to: 0: P0.2 P0.3
P_SW2 |= 0x04; //UART4 switch bit2 to: 1: P5.2 P5.3
B_TX4_Busy = 0;
TX4_Cnt = 0;
RX4_Cnt = 0;
}
//========================================================================
// 函數: void UART1_int (void) interrupt UART1_VECTOR
// 描述: UART1中斷函數。
// 參數: nine.
// 返回: none.
//========================================================================
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;
}
}
//========================================================================
// 函數: void UART2_int (void) interrupt UART2_VECTOR
// 描述: UART2中斷函數。
// 參數: nine.
// 返回: none.
//========================================================================
void UART2_int (void) interrupt 8
{
if((S2CON & 1) != 0)
{
S2CON &= ~1; //Clear Rx flag
RX2_Buffer[RX2_Cnt] = S2BUF;
if(++RX2_Cnt >= UART2_BUF_LENGTH) RX2_Cnt = 0;
}
if((S2CON & 2) != 0)
{
S2CON &= ~2; //Clear Tx flag
B_TX2_Busy = 0;
}
}
//void UART2_int (void) interrupt 8
//{
//if((S2CON & 1) != 0)
//{
//S2CON &= ~1; //接收標志清零
//RX2_Buffer[UART2_JS++&0x0F] = S2BUF; //把接受的數據存儲到BUT數組中
//if(RX2_Buffer[0]!=0xaa){UART2_JS=0;}
//if(UART2_JS>=5){UART2_JS=0;flag_zx2=1;}
//}
//if(flag_zx2==1)
//{
//flag_zx2=0;
//if((RX2_Buffer[0]==0xaa)&&(RX2_Buffer[1]==0x00)&&(RX2_Buffer[2]==0x00)&&(RX2_Buffer[3]==0x00)&&(RX2_Buffer[4]==0xBB))
//{
//// P0=0X55;
//// senduart2();//發送數據
// UART2_FSBZ=1;
//}
// clear_receBuf();
//}
// if((S2CON & 2) != 0)
// {
// S2CON &= ~2; //Clear Tx flag
// B_TX2_Busy = 0;
// }
//}
//========================================================================
// 函數: void UART3_int (void) interrupt UART3_VECTOR
// 描述: UART3中斷函數。
// 參數: nine.
// 返回: none.
//========================================================================
//void UART3_int (void) interrupt 17
//{
// if((S3CON & 0x01) != 0)
// {
// S3CON &= ~0x01; //Clear Rx flag
// RX3_Buffer[RX3_Cnt] = S3BUF;
// if(++RX3_Cnt >= UART3_BUF_LENGTH) RX3_Cnt = 0;
// }
// if((S3CON & 0x02) != 0)
// {
// S3CON &= ~0x02; //Clear Tx flag
// B_TX3_Busy = 0;
// }
//}
void UART3_int (void) interrupt 17
{
if((S3CON & 1) != 0)
{
S3CON &= ~1; //接收標志清零
RX3_Buffer[UART3_JS++&0x0F] = S3BUF; //把接受的數據存儲到BUT數組中
if(RX3_Buffer[0]!=0xaa){UART3_JS=0;}
if(UART3_JS>=5){UART3_JS=0;flag_zx3=1;}
}
if(flag_zx3==1)
{
flag_zx3=0;
if((RX3_Buffer[0]==0xaa)&&(RX3_Buffer[1]==0x00)&&(RX3_Buffer[2]==0x00)&&(RX3_Buffer[3]==0x00)&&(RX3_Buffer[4]==0xBB))
{
// P0=0X55;
// senduart3();//發送數據
UART3_FSBZ=1;
}
clear_receBuf();
}
if((S3CON & 2) != 0)
{
S3CON &= ~2; //Clear Tx flag
B_TX3_Busy = 0;
}
}
//========================================================================
// 函數: void UART4_int (void) interrupt UART4_VECTOR
// 描述: UART4中斷函數。
// 參數: nine.
// 返回: none.
//========================================================================
void UART4_int (void) interrupt 18
{
if((S4CON & 0x01) != 0)
{
S4CON &= ~0x01; //Clear Rx flag
RX4_Buffer[RX4_Cnt] = S4BUF;
if(++RX4_Cnt >= UART4_BUF_LENGTH) RX4_Cnt = 0;
}
if((S4CON & 0x02) != 0)
{
S4CON &= ~0x02; //Clear Tx flag
B_TX4_Busy = 0;
}
}
/********************************************************************
結束
*********************************************************************/
|
|