测试2:(正常)
1、修改程序main函数中不发送"Hooo" 运行程序
2、将RXD、TXD短接不可以点亮P1.0,同样证明串口工作正常
测试3:(为何不正常)
1、运行程序
2、//测试3时使用该行:send_string_com(inbuf1,INBUF_LEN);具体见main函数
用串口调试助手V2.1发送字符接收也正常,如发送 90 90 90 90 能接收 90 90 90 90
3、//测试3时(不)使用该行:send_string_com(inbuf1,INBUF_LEN);具体见main函数
为何用串口调试助手V2.1发送字符接收也正常,如发送 90 90 90 90 能接收 90 90 90 90 显然不正确
因为在main 函数中已不再转发接收到的字符,为何还能接收到呢??????????
请高手们多多指点!!!!在此先谢啦!!!
#include <REG52.H>
#include <STDIO.H>
#define INBUF_LEN 4
#define on 0
#define off 1
#define LED00 P0_0
#define LED0 P1_0
#define LED1 P1_1
#define LED3 P1_3
#define LED7 P1_7
sbit P0_0=P0^0;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_3=P1^3;
sbit P1_7=P1^7;
short int z;
unsigned char inbuf1[INBUF_LEN];
unsigned char checksum,count3;
bit read_flag=0;
void delay(short int timeEx);
void send_char_com(unsigned char ch);
void send_string_com(unsigned char *str,unsigned int strlen);
/*---------------------------------------------------------------------*/
/* 程序主函数(main) */
/*---------------------------------------------------------------------*/
void main(){
#ifndef MONITOR51
/*-----------------------------------------------------------------------
中断优先级 (List) :
-----------------------------------------------------------------------*/
IE=0x91;
IP=0x00;
IT0=0; /*低电平触发*/
SM0=0,SM1=1; /*串行口运行方式(1)*/
SCON=0x50; /*方式(1) 8位数据位 波特 可变*/
PCON=0x00; /*电源控制器:单倍波特率;非降低功消耗;非芯片闲置;*/
/*-----------------------------------------------------------------------
计时器0 方式(2) 8位自动载入计时/计数器;
计时器1 方式(2) 8位自动载入计时/计数器;
-----------------------------------------------------------------------*/
TMOD|=0x22;
TH1=252; /*晶振频率12(MHZ);波特率9600bps*/
TR1=1; /*定时器(1)已启动*/
//TI=1; /*发送中断标志*/
#endif
/*------TOADD:以下空白请输入控制代码------*/
z=55;
while(1){
send_string_com("Hooo",INBUF_LEN);
LED3=on;
delay(z);
LED3=off;
delay(z);
LED7=on;
delay(z);
LED7=off;
delay(z);
if(z> 1)
z--;
if(z==1)
z=55;
if(read_flag) //如果取数标志已置位,就将读到的数从串口发出
{
read_flag=0; //取数标志清0
//测试3时使用该行:send_string_com(inbuf1,INBUF_LEN);
delay(100);
LED0=off;
delay(100);
}
}
}
void delay(short int timeEx){
short int x=0;
short int y=0;
for(x=0;x <timeEx;x++)
for(y=0;y <timeEx;y++);
}
//向串口发送一个字符
void send_char_com(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
//向串口发送一个字符串,strlen为该字符串长度
void send_string_com(unsigned char *str,unsigned int strlen)
{
unsigned int k=0;
do
{
send_char_com(*(str + k));
k++;
} while(k < strlen);
}
//串口接收中断函数
void serial () interrupt 4
{
if(RI)
{
unsigned char ch;
RI = 0;
ch=SBUF;
if(ch=='H'){
LED0=on; //#define LED0 P1_0//sbit P1_0=P1^0以上已声明
//测试用:如果捕获到ch=='H'则点亮P1。01
}
if(ch> 127)
{
count3=0;
inbuf1[count3]=ch;
checksum= ch-128;
}
else
{
count3++;
inbuf1[count3]=ch;
checksum ^= ch;
if( (count3==(INBUF_LEN-1)) && (!checksum) )
{
read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错,
//就置位取数标志
}
}
}
}
/*---------------------------------------------------------------------*/
/* 外中断INT0(中断函数) */
/*---------------------------------------------------------------------*/
void C_Int0() interrupt 0{
/*------TOADD:以下空白请输入控制代码------*/
//if(LED00==on){
int i=0;
for(i=0;i <5;i++){
LED0=on;
delay(100);
LED0=off;
delay(100);
LED1=on;
delay(100);
LED1=off;
delay(100);
}
}