忘记发代码了,全部代码如下:
- #include "STC\STC8H.h"
- #include "INTRINS.H"
- #include <STRING.H>
-
- #define uchar unsigned char
- #define uint unsigned int
-
- bit send_busy;
- uchar recv_timeout;
- struct BUFF_DATA
- {
- uchar rptr;
- uchar wptr;
- char buff[256];
- };
- struct BUFF_DATA xdata uart_buff,xdata str_buff;
-
- #define BUFF_POP(x) x.buff[x.rptr++]
- #define BUFF_PUSH(x,d) x.buff[x.wptr++] = d
- #define BUFF_HAS_DATA(x) (x.rptr != x.wptr)
- #define BUFF_RESET(x) x.rptr=0;x.wptr=0
-
- #define PBUFF_POP(x) x->buff[x->rptr++]
- #define PBUFF_PUSH(x,d) x->buff[x->wptr++] = d
- #define PBUFF_HAS_DATA(x) (x->rptr != x->wptr)
- #define PBUFF_RESET(x) x->rptr=0;x->wptr=0
-
- void Delay1ms(void) //@5.5296MHz
- {
- unsigned char data i, j;
-
- _nop_();
- _nop_();
- i = 8;
- j = 43;
- do
- {
- while (--j);
- } while (--i);
- }
-
- void Delay_ms(uint times) //@5.5296MHz
- {
- unsigned char data i, j;
- while (times-->0)
- {
- _nop_();
- _nop_();
- i = 8;
- j = 43;
- do
- {
- while (--j);
- } while (--i);
- }
- }
-
- void Uart1_Isr(void) interrupt 4
- {
- if (TI)
- {
- TI = 0;
- send_busy = 0;
- }
- if (RI)
- {
- RI = 0;
- BUFF_PUSH(uart_buff,SBUF);
- recv_timeout = 5;
- }
- }
-
- void Uart1_Init(void) //19200bps@5.5296MHz
- {
- SCON = 0x50; //8位数据,可变波特率
- AUXR &= 0xBF; //定时器时钟12T模式
- AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
- TMOD &= 0x0F; //设置定时器模式
- TL1 = 0xFA; //设置定时初始值
- TH1 = 0xFF; //设置定时初始值
- ET1 = 0; //禁止定时器中断
- TR1 = 1; //定时器1开始计时
- ES = 1; //使能串口1中断
- EA = 1;
-
- send_busy = 0;
- BUFF_RESET(uart_buff);
- }
-
- void UartSend(char dat)
- {
- while(send_busy);
- send_busy = 1;
- SBUF = dat;
- }
-
- void UartSendStr(char *p)
- {
- while(*p)
- UartSend(*p++);
- }
-
- /*
- void UartSendBuff(struct BUFF_DATA xdata *d)
- {
- while(PBUFF_HAS_DATA(d))
- {
- UartSend(PBUFF_POP(d));
- }
- }
- */
-
- void DealString(char *p)
- {
- if(strcmp(p,"hello") == 0)
- {
- UartSendStr("world!");
- }
- else if(strcmp(p,"reboot") == 0)
- {
- Delay_ms(2000);
- UartSendStr("bye!");
- IAP_CONTR = 0x60;
- }
- else
- {
- UartSendStr(p);
- }
- }
-
- void main()
- {
- uchar xdata tmp;
- Uart1_Init();
-
- UartSendStr("Uart Test!\r\n");
-
- while(1)
- {
- if(recv_timeout>0)
- {
- BUFF_RESET(str_buff);
- while(recv_timeout>0)
- {
- while(BUFF_HAS_DATA(uart_buff))
- {
- tmp = BUFF_POP(uart_buff);
- BUFF_PUSH(str_buff,tmp);
- }
- recv_timeout--;
- Delay1ms();
- }
- BUFF_PUSH(str_buff,0);
- DealString(str_buff.buff);
- }
- /*if(BUFF_HAS_DATA(uart_buff))
- {
- UartSend(BUFF_POP(uart_buff));
- }*/
- }
- }
复制代码
|