15单片机串口2只能接受数据,不能发数据
时间:10-02
整理:3721RD
点击:
我还用了串口1,波特率9600但没有用中断,串口2相关程序如下,现在问题是函数 Uart2_Sendstring("The bedroom light has been turned off.\r\n");不执行
void Uart2Init(void) //9600
{
S2CON = 0x50; //8位数据,可变波特率
AUXR|= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xE0; //设定定时初值
T2H = 0xFE; //设定定时初值
AUXR|= 0x10; //启动定时器2
EA=1;
IE2=IE2|0x01;
}
void Uart2_Sendstring(uchar *str)
{
uchar *p;
IE2=IE2&0xFE;
S2CON=S2CON&0xFD;
p = str;
while(*p != '\0')
{
S2BUF = *p;
while(S2CON&0x02== 0); //等待发送标志位置位
S2CON=S2CON&0xFD;
p++;
}
IE2=IE2|0x01;
}
void BlueTooth_INT() interrupt 8
{
uchar receivechar; //接受的字符
if(S2CON&0X01==1)
{
S2CON=S2CON&0XFE;
receivechar=S2BUF;
switch(receivechar)
{
case '1': //收到字符1,指示灯0灭
LED0=1;
Uart2_Sendstring("The bedroom light has been turned off.\r\n");
break;
case '2': //收到字符2,指示灯1灭
LED0=0;
Uart2_Sendstring("The bedroom light has been turned on.\r\n");
break;
default: //输入其他数据返回error
Uart2_Sendstring("error\r\n");
break;
}
}
}
void Uart2Init(void) //9600
{
S2CON = 0x50; //8位数据,可变波特率
AUXR|= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xE0; //设定定时初值
T2H = 0xFE; //设定定时初值
AUXR|= 0x10; //启动定时器2
EA=1;
IE2=IE2|0x01;
}
void Uart2_Sendstring(uchar *str)
{
uchar *p;
IE2=IE2&0xFE;
S2CON=S2CON&0xFD;
p = str;
while(*p != '\0')
{
S2BUF = *p;
while(S2CON&0x02== 0); //等待发送标志位置位
S2CON=S2CON&0xFD;
p++;
}
IE2=IE2|0x01;
}
void BlueTooth_INT() interrupt 8
{
uchar receivechar; //接受的字符
if(S2CON&0X01==1)
{
S2CON=S2CON&0XFE;
receivechar=S2BUF;
switch(receivechar)
{
case '1': //收到字符1,指示灯0灭
LED0=1;
Uart2_Sendstring("The bedroom light has been turned off.\r\n");
break;
case '2': //收到字符2,指示灯1灭
LED0=0;
Uart2_Sendstring("The bedroom light has been turned on.\r\n");
break;
default: //输入其他数据返回error
Uart2_Sendstring("error\r\n");
break;
}
}
}
我将串口2换成串口1,程序模式不变,没有任何问题,用串口2就是不能发送字符串
主函数 void main()
{
Uart2Init();
while(1);
}