不好意思剛剛發錯了 那個是串口1的,串口2如下:
#include <stdio.h>
#include <string.h>
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define S2RI 0x01 //S2CON.1
#define S2TI 0x02 //S2CON.2
#define ES2 0x01
unsigned char buf[7]={'S','t','r','i','n','g'};
unsigned char *TextStr;
unsigned char *RecStr;
void PutString(char *TextStr)
{
IE2=0x00;
while(*TextStr!=0)
{
S2BUF=*TextStr;
while(!(S2CON&S2TI));
S2CON &= ~S2TI;
TextStr++;
}
IE2=0x01;
}
void ReceiveString(char *RecStr)
{
char num=0;
unsigned char count=0;
*RecStr=S2BUF;
count=0;
S2CON &= ~S2RI;
}
void init(void)
{
// AUXR |= 0x08; //????????S2SMOD
// S2CON = 0x50; //8???,?????
// AUXR &= 0xFB; //???????????Fosc/12,?12T
// BRT = 0xFA; //?????????????
// AUXR |= 0x10; //??????????
AUXR &= 0xf7; //??????
S2CON = 0x50; //8???,?????
BRT = 0xDC; //?????????????
AUXR |= 0x04; //???????????Fosc,?1T
AUXR |= 0x10; //??????????
IE2=0x01; //????2??ES2=1
EA=1; //????
}
void main()
{
init();
PutString(&buf);
ReceiveString(&buf);
while(1);
}
void UART2(void) interrupt 8
{
if(S2CON & S2RI)
{
S2CON &= ~S2RI;
*RecStr = S2BUF;
S2BUF = *TextStr;
}
} |