1234ͨʵ飺
1ʵǴ1(P3.6 P3.7)ʹ2(P1.0 P1.1)Լ3(P0.0 P0.1)ʹ4(P0.2 P0.3)ͬʱͨʵ;
2سƵѡ11.0592MHZ
3 򿪴ڵ֣ѡ񴮿1ӵUSBתģӦĴںţΪ9600ڷUART1ɹ۲쵽ݽյUART1 CHECK OK!ַ
4 򿪴ڵ֣ѡ񴮿2ӵUSBתģӦĴںţΪ9600ڷUART2ɹ۲쵽ݽյUART2 CHECK OK!ַ
5 򿪴ڵ֣ѡ񴮿3ӵUSBתģӦĴںţΪ9600ڷUART3ɹ۲쵽ݽյUART3 CHECK OK!ַ
6 򿪴ڵ֣ѡ񴮿4ӵUSBתģӦĴںţΪ9600ڷUART4ɹ۲쵽ݽյUART4 CHECK OK!ַ

עʹUSBתģʱؽģGND뿪GNDUSBתģ뿪֮3

#include	"15W4KxxS4.H"
#include <intrins.h>					// ͷļ,ʹ_nop_⺯
#include <string.h>           // ͷļ,ʹstrstr⺯

#define  uint8 unsigned char
#define  uint16  unsigned int 

#define BAUD 9600			    // 
#define TM (65536 - (MAIN_Fosc/4/BAUD))	
#define Buf_Max 50
#define S2_S  0x01	
#define S2RI 0x01
#define S2TI 0x02
#define S3RI 0x01
#define S3TI 0x02
#define S4RI 0x01
#define S4TI 0x02
//#define S1_S0 0x40              // S1_S0 = 1;
//#define S1_S1 0x00              // S1_S1 = 0; Ѵ1ת3.6 3.7 


uint8 xdata Rec_Buf1[Buf_Max];     //մ1
uint8 xdata Rec_Buf2[Buf_Max];     //մ2
uint8 xdata Rec_Buf3[Buf_Max];       //մ3
uint8 xdata Rec_Buf4[Buf_Max];       //մ4
uint8 i = 0; 		 	
uint8 j = 0; 
uint8 m = 0; 		 	
uint8 n = 0; 
uint16 k=10;

sbit LED1=P2^7;
sbit LED2=P2^6;
sbit LED3=P2^5;
sbit LED4=P2^4;
sbit LED5=P2^3;
sbit LED6=P2^2;
sbit LED7=P2^1;
sbit LED8=P2^0; 
sbit S1=P3^5;
sbit S2=P3^4;
sbit S3=P3^3;
sbit S4=P3^2; 
/********************************************************************
ʱ
*********************************************************************/
void delay(uint8 t)
{
uint16 i,j;
for(i=0;i<t;i++)
{
for(j=30000;j>0;j--);
{ ;
}
}
}
/***************************************************************************
*    : 1/2/3/4ʼ
*    : 
* ֵ : 
ע9600bps   11.0592MHz
**************************************************************************/
void Uart1234_Init(void)
{	
//  P_SW1|=0X80;        //ѡP16 P17Ϊ1
//	P_SW1&=0XBF;        //ѡP16 P17Ϊ1
//	P_SW2|=S2_S;        //ѡP46 P47Ϊ2
//  ACC = P_SW1;
//  ACC &= ~(S1_S0 | S1_S1);    //S1_S0=1 S1_S1=0
//  ACC |= S1_S0;               //(P3.6/RxD_2, P3.7/TxD_2)
P_SW1 = ACC;  
//1	
PCON &= 0x3f;		    //1ʲ٣пڹʽSM0SM1
SCON = 0x50;		    //18λ,ɱ䲨ʣн
AUXR |= 0x01;		    //1ѡʱ2Ϊʷ
//2	
S2CON = 0x50;		    //28λ,ɱ䲨	
//3	
S3CON |= 0x10;		  //3н
S3CON &= 0x30;		  //3ѡʱ2Ϊʷ8λ,ɱ䲨
//4	
S4CON |= 0x10;		  //н	
S4CON &= 0x30;		  //8λ,ɱ䲨ʣ4ѡʱ2Ϊʷ

AUXR |= 0x04;		    //ʱ2ʱΪFosc,1T
T2L = 0xE0;		      //趨ʱֵ
T2H = 0xFE;         //趨ʱֵ
AUXR |= 0x10;       //ʱ2
}
//PWMC 
/***************************************************************************
*    : 1ݺ
*    : uint8 
* ֵ : 
**************************************************************************/
void SendDataByUart1(uint8 dat)
{
SBUF = dat;                 //дݵUARTݼĴ
while(TI == 0);             //ֹͣλûзʱTIΪ0һֱȴ
TI = 0;                     //TIλλ㣩
}

/***************************************************************************
*    : 1ַ
*    : ַ
* ֵ : 
**************************************************************************/
void SendStringByUart1(uint8 *s)
{
while(*s)
{
SendDataByUart1(*s++);       //ַеַһһ
}
}

/**************************************
ֳɹ
ڲuint8 *a
ֵλ
***************************************/
bit Hand1(uint8 *a)
{ 
if(strstr(Rec_Buf1,a)!=NULL)     //жַaǷַRec_Buf1Ӵ
return 1;                      //ַaַRec_Buf1Ӵ
else
return 0;                      //ַaַRec_Buf1Ӵ
}

/**************************************
1ݺ
ڲ
ֵ
***************************************/
void CLR_Buf1(void)
{
uint8 k;
for(k=0;k<Buf_Max;k++)      //1ֵΪ
{
Rec_Buf1[k] = 0;
}
i = 0;                    
}

/***************************************************************************
*    : 1жϷ
*    : 
* ֵ : 
**************************************************************************/
void Uart1() interrupt UART1_VECTOR using 1
{
ES = 0;                       // 1жϹر
if (RI)                       //нյֹͣλмʱʱλ1
{
RI = 0;                   //RIλ λ㣩
Rec_Buf1[i] = SBUF;       //Ѵ1SBUFĴδŵRec_Buf1
i++;                      
if(i>Buf_Max)             //ڶʱǽ֮ǰֵ
{
i = 0;                 
}           
}
if (TI)                    //ֹͣλʼʱλ1
{
TI = 0;                 //TIλλ㣩
}
ES =  1;                   // 1жϴ
}

/***************************************************************************
*    : 2ݺ
*    : uint8 
* ֵ : 
**************************************************************************/
void SendDataByUart2(uint8 dat)
{
S2BUF = dat;                 //дݵUARTݼĴ
while(!(S2CON&S2TI));        //ֹͣλûзʱS2TIΪ0һֱȴ
S2CON&=~S2TI;                //S2CONĴӦS2TIλλ㣩
}

/***************************************************************************
*    : 2ַ
*    : ַ
* ֵ : 
**************************************************************************/
void SendStringByUart2(uint8 *s)
{
IE2 &= 0xFE; 			          // 2жϹر
while (*s)                  //ַ־
{
SendDataByUart2(*s++);         //͵ǰַ
}
IE2 |= 0x01;                     // 2жϴ
}

/**************************************
ֳɹ
ڲuint8 *a
ֵλ
***************************************/
bit Hand2(uint8 *a)
{ 
if(strstr(Rec_Buf2,a)!=NULL)      //жַaǷַRec_Buf2Ӵ
return 1;                      //ַaַRec_Buf2Ӵ
else
return 0;                      //ַaַRec_Buf2Ӵ
}

/**************************************
2ݺ
ڲ
ֵ
***************************************/
void CLR_Buf2(void)
{
unsigned char k;
for(k=0;k<Buf_Max;k++)      //2ֵΪ
{
Rec_Buf2[k] = 0;
}
j = 0;                    
}

/***************************************************************************
*    : 2жϷ
*    : 
* ֵ : 
**************************************************************************/
void Uart2() interrupt UART2_VECTOR using 1
{
IE2 &= 0xFE; 			                         // 2жϹر
if (S2CON & S2RI)                          //нյֹͣλмʱʱλ1
{
S2CON &= ~S2RI;                        //S2CONĴӦS2RIλλ㣩
Rec_Buf2[j] = S2BUF;                   //Ѵ2SBUFĴδŵRec_Buf2
j++;                                   
if(j>Buf_Max)                          //ڶʱǽ֮ǰֵ
{
j = 0;
}           
}
if (S2CON & S2TI)                       //ֹͣλʼʱλ1
{
S2CON &= ~S2TI;   			              //S2CONĴӦS2TIλλ㣩
}
IE2 |= 0x01;                               // 2жϴ	
}

/***************************************************************************
*    : 3ݺ
*    : uint8 
* ֵ : 
**************************************************************************/
void SendDataByUart3(uint8 dat)
{
S3BUF = dat;                 //дݵUARTݼĴ
while(!(S3CON&S3TI));        //ֹͣλûзʱS3TIΪ0һֱȴ
S3CON&=~S3TI;                //S3CONĴӦS3TIλλ㣩
}

/***************************************************************************
*    : 3ַ
*    : ַ
* ֵ : 
**************************************************************************/
void SendStringByUart3(char *s)
{
IE2 &= 0xF7; 			               // 3жϹر
while (*s)                       //ַ־
{
SendDataByUart3(*s++);         //͵ǰַ
}
IE2 |= 0x08;                     // 3жϴ
}

/**************************************
ֳɹ
ڲuint8 *a
ֵλ
***************************************/
bit Hand3(uint8 *a)
{ 
if(strstr(Rec_Buf3,a)!=NULL)     //жַaǷַRec_Buf3Ӵ
return 1;                      //ַaַRec_Buf3Ӵ
else
return 0;                      //ַaַRec_Buf3Ӵ
}

/**************************************
ݺ
ڲ
ֵ
***************************************/
void CLR_Buf3(void)
{
uint8 k;
for(k=0;k<Buf_Max;k++)      //3ֵΪ
{
Rec_Buf3[k] = 0;
}
m = 0;                    
}

/***************************************************************************
*    : 3жϷ
*    : 
* ֵ : 
**************************************************************************/
void Uart3() interrupt UART3_VECTOR using 1
{
IE2 &= 0xF7; 			               // 3жϹر
if (S3CON & S3RI)                //нյֹͣλмʱʱλ1
{
S3CON &= ~S3RI;              //S3CONĴӦS3RIλλ㣩
Rec_Buf3[m] = S3BUF;         //Ѵ3SBUFĴδŵRec_Buf3
m++;               
if(m>Buf_Max)                //ڶʱǽ֮ǰֵ
{
m = 0;
}     
}
if (S3CON & S3TI)                //ֹͣλʼʱλ1
{
S3CON &= ~S3TI;   			     //S3CONĴӦS3TIλλ㣩
}
IE2 |= 0x08;                     // 3жϴ
}

/***************************************************************************
*    : 4ݺ
*    : uint8 
* ֵ : 
**************************************************************************/
void SendDataByUart4(uint8 dat)
{
S4BUF = dat;                 //дݵUARTݼĴ
while(!(S4CON&S4TI));        //ֹͣλûзʱS4TIΪ0һֱȴ
S4CON&=~S4TI;                //S4CONĴӦS4TIλλ㣩
}

/***************************************************************************
*    : 4ַ
*    : ַ
* ֵ : 
**************************************************************************/
void SendStringByUart4(char *s)
{
IE2 &= 0xEF; 			               //4жϹر
while (*s)                       //ַ־
{
SendDataByUart4(*s++);         //͵ǰַ
}
IE2 |= 0x10;                     //4жϴ
}

/**************************************
ֳɹ
ڲunsigned char *a
ֵλ
***************************************/
bit Hand4(unsigned char *a)
{ 
if(strstr(Rec_Buf4,a)!=NULL)       //жַaǷַRec_Buf4Ӵ
return 1;                      //ַaַRec_Buf4Ӵ
else
return 0;                      //ַaַRec_Buf4Ӵ
}

/**************************************
ݺ
ڲ
ֵ
***************************************/
void CLR_Buf4(void)
{
unsigned char k;
for(k=0;k<Buf_Max;k++)         //4ֵΪ
{
Rec_Buf4[k] = 0;
}
n = 0;                    
}

/***************************************************************************
*    : 4жϷ
*    : 
* ֵ : 
**************************************************************************/
void Uart4() interrupt UART4_VECTOR
{			
IE2 &= 0xEF; 			          // 4жϹر  
if(S4CON & S4RI)           //нյֹͣλмʱʱλ1
{
S4CON &= ~S4RI;	         //S4CONĴӦS4RIλλ㣩
Rec_Buf4[n] = S4BUF;     //Ѵ4SBUFĴδŵRec_Buf4
n++;               
if(n>Buf_Max)            //ڶʱǽ֮ǰֵ
{
n = 0;
}
}
if(S4CON & S4TI)           //ֹͣλʼʱλ1
{
S4CON &= ~S4TI;          //S4CONĴӦS4TIλλ㣩
}
IE2 |= 0x10;              // 4жϴ
}



/***************************************************************************
*    : 
*    : 
* ֵ : 
**************************************************************************/
int main()
{
/////////////////////////////////////////////////
//ע: STC15W4K32S4ϵеоƬ,ϵPWMصIOھΪ
//      ̬,轫ЩΪ׼˫ڻǿģʽʹ
//IO: P0.6/P0.7/P1.6/P1.7/P2.1/P2.2
//        P2.3/P2.7/P3.7/P4.2/P4.4/P4.5
/////////////////////////////////////////////////

P0M0=0X00;P0M1=0X00;
P1M0=0X00;P1M1=0X00;
P2M0=0X00;P2M1=0X00;
P3M0=0X00;P3M1=0X00;
P4M0=0X00;P4M1=0X00;
Uart1234_Init();                              // 1/2/3/4ʼ
ES = 1;                                       // 1жϴ                               
IE2 |= 0x01;                                  // 2жϴ
IE2 |= 0x08;                                  // 3жϴ
IE2 |= 0x10;                                  // 4жϴ
EA = 1;                                       // жϴ
LED1=0;delay(k);LED1=1;
LED2=0;delay(k);LED2=1;
LED3=0;delay(k);LED3=1;
LED4=0;delay(k);LED4=1;
LED5=0;delay(k);LED5=1;
LED6=0;delay(k);LED6=1;
LED7=0;delay(k);LED7=1;
LED8=0;delay(k);LED8=1;
while(1)
{
if(S1==0){LED1=0;LED2=0;LED3=0;LED4=0;LED5=1;LED6=1;LED7=1;LED8=1;}
if(S2==0){LED1=1;LED2=1;LED3=1;LED4=1;LED5=0;LED6=0;LED7=0;LED8=0;}
if(S3==0){LED1=0;LED2=1;LED3=0;LED4=1;LED5=0;LED6=1;LED7=0;LED8=1;}
if(S4==0){LED1=1;LED2=0;LED3=1;LED4=0;LED5=1;LED6=0;LED7=1;LED8=0;}

if(Hand1("UART1"))                            //1յַUART1
{
ES = 0; 			                              // 1жϹر
CLR_Buf1();                                 //1ֵΪ
SendStringByUart1("UART1 CHECK OK!\r\n");		//1ַUART1 CHECK OK!
ES = 1;							                        // 1жϴ	
} 	
if(Hand2("UART2"))                            //2յַUART2
{
IE2 &= 0xFE; 			                          // 2жϹر
CLR_Buf2();                                 //2ֵΪ
SendStringByUart2("UART2 CHECK OK!\r\n");		//2ַUART2 CHECK OK!
IE2 |= 0x01;                                // 2жϴ			
} 	
if(Hand3("UART3"))                            //  յLED1ָ
{
IE2 &= 0xF7; 			                          // 3жϹر			
CLR_Buf3();                                 //3ֵΪ
SendStringByUart3("UART3 CHECK OK!\r\n");		//3ַUART3 CHECK OK!
IE2 |= 0x08;                                // 3жϴ					
} 
if(Hand4("UART4"))                            //  յLED1ָ
{
IE2 &= 0xEF; 			                          // 4жϹر		
CLR_Buf4();                                 //4ֵΪ
SendStringByUart4("UART4 CHECK OK!\r\n");		//4ַUART4 CHECK OK!
IE2 |= 0x10;                                // 4жϴ				
} 
}
}  