使用的单片机芯片是STC15W408AS,程序是STC下载器软件的的例子上改了一点,都因为我希望能接收一段字串
现象是,通电 可以收到发过一来的字串,用串口软件发一个字符及接收回显都没有问题,但发两个及以上字符,第一个字符显示正常,之后就是乱码了。请指教。
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC15F4K60S4 ?? ???2????1?????????------------*/
- /* --- Mobile: (86)13922805190 ----------------------------------------*/
- /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
- /* --- Tel: 86-0513-55012928,55012929,55012966-------------------------*/
- /* ????????????,??????????STC?????? */
- /* ????????????,??????????STC?????? */
- /*---------------------------------------------------------------------*/
- //????Keil????????Intel?8058????????
- //??????,???????11.0592MHz
- #include "reg51.h"
- #include "intrins.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- #define FOSC 11059200L //????
- #define BAUD 115200 //?????
- #define NONE_PARITY 0 //???
- #define ODD_PARITY 1 //???
- #define EVEN_PARITY 2 //???
- #define MARK_PARITY 3 //????
- #define SPACE_PARITY 4 //????
- #define PARITYBIT EVEN_PARITY //?????
- sfr P0M1 = 0x93;
- sfr P0M0 = 0x94;
- sfr P1M1 = 0x91;
- sfr P1M0 = 0x92;
- 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;
- sfr AUXR = 0x8e; //?????
- sfr T2H = 0xd6; //???2?8?
- sfr T2L = 0xd7; //???2?8?
- sfr P_SW1 = 0xA2; //?????????1
- #define S1_S0 0x40 //P_SW1.6
- #define S1_S1 0x80 //P_SW1.7
- sbit P22 = P2^2;
- bit busy;
- unsigned char date;
- void SendData(BYTE dat);
- void SendString(char *s);
- void main()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- ACC = P_SW1;
- ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0
- P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD)
- // 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;
- //
- // ACC = P_SW1;
- // ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=1
- // ACC |= S1_S1; //(P1.6/RxD_3, P1.7/TxD_3)
- // P_SW1 = ACC;
- #if (PARITYBIT == NONE_PARITY)
- SCON = 0x50; //8??????
- #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
- SCON = 0xda; //9??????,??????1
- #elif (PARITYBIT == SPACE_PARITY)
- SCON = 0xd2; //9??????,??????0
- #endif
- T2L = (65536 - (FOSC/4/BAUD)); //????????
- T2H = (65536 - (FOSC/4/BAUD))>>8;
- AUXR = 0x14; //T2?1T??, ??????2
- AUXR |= 0x01; //?????2???1???????
- ES = 1; //????1??
- EA = 1;
- SendString("STC15F2K60S2\r\nUart Test !\r\n");
- while(1);
- }
- /*----------------------------
- UART ??????
- -----------------------------*/
- void Uart() interrupt 4 using 1
- {
- if (RI)
- {
- RI = 0; //??RI?
- P1 = SBUF; //P0??????
- date=P1;
- P22 = RB8; //P2.2?????
- SendData(date);
- //SBUF=P1;
- }
- if (TI)
- {
- TI = 0; //??TI?
- busy = 0; //????
- }
- }
- /*----------------------------
- ??????
- ----------------------------*/
- void SendData(BYTE dat)
- {
- while (busy); //???????????
- ACC = dat; //?????P (PSW.0)
- if (P) //??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
- }
- busy = 1;
- SBUF = ACC; //????UART?????
- }
- /*----------------------------
- ?????
- ----------------------------*/
- void SendString(char *s)
- {
- while (*s) //?????????
- {
- SendData(*s++); //??????
- }
- }
复制代码
|