|
- /**********************************************************
- STC12C5204
- 串口工作方式 : 方式1 8位UART
- 定時(shí)器的選取 : T1
- 定時(shí)器工作方式 : 方式2
- 波特率選取 : 9600
- **********************************************************/
- #include "reg51.h"
- #include "intrins.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- bit busy;
- BYTE temp=0;
- BYTE flag=0;
- void SendData(BYTE dat);
- void SendString(char *s);
- void main()
- {
- SCON = 0x50; //方式1
- TMOD = 0x20; //8位自動重裝載定時(shí)/計(jì)數(shù)器
- TH1 = TL1 = 0xfd; //9600
- TR1 = 1;
- ES = 1;
- EA = 1;
- while(1)
- {
- if(flag)
- {
- flag=0;
- SendData(temp+1);
- }
- }
- }
- /*----------------------------
- UART interrupt service routine
- ----------------------------*/
- void Uart_Isr() interrupt 4 using 1
- {
- if (RI)
- {
- RI = 0; //Clear receive interrupt flag
- temp = SBUF; //P0 show UART data
- flag=1;
- }
- // if (TI)
- // {
- // TI = 0; //Clear transmit interrupt flag
- // busy = 0; //Clear transmit busy flag
- // }
- }
- /*----------------------------
- Send a byte data to UART
- Input: dat (data to be sent)
- Output:None
- ----------------------------*/
- void SendData(BYTE dat)
- {
- while (flag); //Wait for the completion of the previous data is sent
-
- SBUF = dat;
- while(!TI);
- TI=0; //Send data to UART buffer
- }
- /*----------------------------
- Send a string to UART
- Input: s (address of string)
- Output:None
- ----------------------------*/
- void SendString(char *s)
- {
- while (*s) //Check the end of the string
- {
- SendData(*s++); //Send current char and increment string ptr
- }
- }
復(fù)制代碼
改了一下,可以實(shí)現(xiàn)發(fā)送1,返回2 |
|