STC15W4K单片机串口2 RS485通信问题
时间:10-02
整理:3721RD
点击:
我用STC15W4K单片机串口2进行485通信时,能正常接收却不能发送,用串口助手收不到我要它回复的内容,收发控制线设置也都正确,有谁知道可能是什么原因吗?
附程序(注释部分复制过来乱码了):
#include "STC15Fxxxx.H"
#include "intrins.h"
#include <string.h>
sbit m485_TX2=P1^2;
#define FOSC 11059200L
#define BAUD 9600
#define TM (65536-(FOSC/4/BAUD)) //??êy3??μ=65536-Fosc/ò?3??꣨ò?3??ù?ê£?,ò?3??ê=1/?¨ê±ê±??
#define S2RI 0X01
#define S2TI 0X02
char code uart2_resp[]="OK\r\n";
unsigned char xdata uart2_re[10];
unsigned char xdata Recestr_2[10];
u8 xdata uart2_m,uart2_n=0,re_i=0;
void Uart2_Response(char *Resp_2) //?ìó|?óê?μ?μ??úèY£?Txd·¢?í
{
IE2 &= ~1; //1?±?′??ú?D??
S2CON&=~S2RI;
m485_TX2=1;
uart2_m=0;
while(Resp_2[uart2_m]!= '\0')
{
S2BUF = Resp_2[uart2_m]; //SUBF?óêü/·¢?í?o3??÷(ó??D′?DDí¨D?ì?êa1|?ü??′??÷)
while(S2CON&S2TI==0); // μèì?êy?Y′??í (TI·¢?í?D??±ê??)
P20=0;
S2CON&=~S2TI; // ??3yêy?Y′??í±ê??
uart2_m++; // ??ò???×?·?
}
m485_TX2=0;
IE2|=0X01;
}
void Uart2_Check(unsigned char *Recestr)
{
re_i=0;
while(*Recestr)
{
Recestr_2[re_i]=*Recestr;
Recestr++;
re_i++;
}
Recestr_2[re_i]='\0';
if(strcmp(Recestr_2,"hello")==0)
{
Uart2_Response(uart2_resp);
}
}
void init_uart2_485(void) //′??ú1?¢212ó?1??2¨ì??ê·¢éú?÷T2
{
// IO_init(); //????IAP15W4K61S4 IO?ú3?ê??ˉ
m485_TX2=0; //485????μ???1 è?485′|óú·¢?í×′ì? P1.2
S2CON = 0x50; //S2REN=1?êDí′?DD?óê???????£?S2SM0=0,S2SM1=1,1¤×÷·?ê?0,8??UART£?2¨ì??ê?é±?=T2ò?3??ê/4
//′??ú2???ü????T2×÷?a2¨ì??ê·¢éú?÷
T2L=TM; //2¨ì??ê?3?μ
T2H=TM>>8; // è?óD2??÷°×??2é STC15ê?2áé?óD?ê???μ?÷
AUXR|=0X14; //?a??1T?£ê?£?2?·??μ,?¨ê±?÷2?êDí??DD
IE2|=0X01; //ES2=1,?a′??ú2?D??
}
void Uart2() interrupt 8 using 2
{
if(S2CON&S2RI)
{
if(S2BUF=='#')
{
uart2_re[uart2_n]='\0';
Uart2_Check(uart2_re);
uart2_n=0;
}
else
{
uart2_re[uart2_n]=S2BUF;
uart2_n++;
}
if(uart2_n>10) uart2_n=0;
S2CON&=~S2RI;
}
}
附程序(注释部分复制过来乱码了):
#include "STC15Fxxxx.H"
#include "intrins.h"
#include <string.h>
sbit m485_TX2=P1^2;
#define FOSC 11059200L
#define BAUD 9600
#define TM (65536-(FOSC/4/BAUD)) //??êy3??μ=65536-Fosc/ò?3??꣨ò?3??ù?ê£?,ò?3??ê=1/?¨ê±ê±??
#define S2RI 0X01
#define S2TI 0X02
char code uart2_resp[]="OK\r\n";
unsigned char xdata uart2_re[10];
unsigned char xdata Recestr_2[10];
u8 xdata uart2_m,uart2_n=0,re_i=0;
void Uart2_Response(char *Resp_2) //?ìó|?óê?μ?μ??úèY£?Txd·¢?í
{
IE2 &= ~1; //1?±?′??ú?D??
S2CON&=~S2RI;
m485_TX2=1;
uart2_m=0;
while(Resp_2[uart2_m]!= '\0')
{
S2BUF = Resp_2[uart2_m]; //SUBF?óêü/·¢?í?o3??÷(ó??D′?DDí¨D?ì?êa1|?ü??′??÷)
while(S2CON&S2TI==0); // μèì?êy?Y′??í (TI·¢?í?D??±ê??)
P20=0;
S2CON&=~S2TI; // ??3yêy?Y′??í±ê??
uart2_m++; // ??ò???×?·?
}
m485_TX2=0;
IE2|=0X01;
}
void Uart2_Check(unsigned char *Recestr)
{
re_i=0;
while(*Recestr)
{
Recestr_2[re_i]=*Recestr;
Recestr++;
re_i++;
}
Recestr_2[re_i]='\0';
if(strcmp(Recestr_2,"hello")==0)
{
Uart2_Response(uart2_resp);
}
}
void init_uart2_485(void) //′??ú1?¢212ó?1??2¨ì??ê·¢éú?÷T2
{
// IO_init(); //????IAP15W4K61S4 IO?ú3?ê??ˉ
m485_TX2=0; //485????μ???1 è?485′|óú·¢?í×′ì? P1.2
S2CON = 0x50; //S2REN=1?êDí′?DD?óê???????£?S2SM0=0,S2SM1=1,1¤×÷·?ê?0,8??UART£?2¨ì??ê?é±?=T2ò?3??ê/4
//′??ú2???ü????T2×÷?a2¨ì??ê·¢éú?÷
T2L=TM; //2¨ì??ê?3?μ
T2H=TM>>8; // è?óD2??÷°×??2é STC15ê?2áé?óD?ê???μ?÷
AUXR|=0X14; //?a??1T?£ê?£?2?·??μ,?¨ê±?÷2?êDí??DD
IE2|=0X01; //ES2=1,?a′??ú2?D??
}
void Uart2() interrupt 8 using 2
{
if(S2CON&S2RI)
{
if(S2BUF=='#')
{
uart2_re[uart2_n]='\0';
Uart2_Check(uart2_re);
uart2_n=0;
}
else
{
uart2_re[uart2_n]=S2BUF;
uart2_n++;
}
if(uart2_n>10) uart2_n=0;
S2CON&=~S2RI;
}
}
乱码看得人头晕
问题解决了吗, 我怎么没找到main函数,? 把完成的代码贴出了,或者 上传上来,呗
不看乱码就行,看代码啊,就不会乱了