打印

LPC932A1串口发送问题,高手请进!

[复制链接]
1138|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
LPC300|  楼主 | 2010-4-28 16:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大虾,小弟使用LPC932A1单片机,通信使用中断方式,当收到地址为0x28并且数据为21个字节时,发送一组数据(共13字节),可我用串口软件接收,实际只接收到11个。我监视了一下发送计数(send_num)没问题是13,可怎么我接收就是少了两个字节呢,请高手帮忙看一下程序到底是什么地方出了问题!谢谢
#include <reg932.h>
#define uchar unsigned char
#define ulong unsigned long
#define uint  unsigned int
#define addr 0x28
sbit RUN=P3^1;
sbit EN485=P3^0;
uchar read[22];  //接收缓冲区
uchar buf[22];   //发送缓冲区  
int tx_time,Led_count,dis_time;
int xdata  data_change[10],dis_no[9],dis_sign,dis_mark,dis_choice,rec_num,send_num;
void init()
{
P0M1=0;   
P0M2=0x80;
P2M1=0xff;   //P2口设置为开漏  
P2M2=0xff;
P3M1=3;
P3M2=3;
P1M1=0;
P1M2=0xd0;
tx_time=0;
rec_num=0;
send_num=0;
IP0=0x90;   //设置串口中断为最高权限
IP0H=0x90;
IP1=0xc0;
IP1H=0x60;
SADDR=0x28;
TAMOD=0x00;
TMOD=0X01;    //定时器0,方式1
TH0=0xbb;//77;    //定时器0 ,10ms中断一次
TL0=0xa4;//48;
SCON=0xf0;            /*串行口工作在方式3*/
SSTAT=0xa0;//E0;      //启用串行通信使用双缓冲区
BRGCON=0x00;     //关闭波特率发生器,准备给波特率发生器赋初值
BRGR1=0x02;   //波特率9600  ,7.373MHz
BRGR0=0xC9;
BRGCON=0x03;     //开启波特率发生器
IEN0=0x92;   //允许接收中断   允许定时器0中断     开启全局中断
IEN1=0x40;   //允许发送中断

}

void Delays(uchar no)
{
int i,j;
for(;no>0;no--)
{
  for(i=0;i<500;i++)
   for(j=0;j<500;j++);
}
}



void receive ()          /*接收数据部分*/
{      
   uchar i;
   rec_num++;
   if(rec_num==1)
   {
    read[0]=SBUF;
   if (read[0]>0x80)      /*去掉校验*/
    read[0]=read[0]-0x80;
   if (read[0]!=SADDR)  /*判断是否是本机地址*/
    rec_num=0;
   else
    SM2=0;
   }
   if(rec_num>1 && rec_num<22)
          read[rec_num-1]=SBUF;
   RI=0;
}  

void send()               /*发送数据部分*/
{    uchar i,j=1;
  TI=0;
  EN485=1;
     pf=0;
  buf[0]=0x01;
  buf[1]=0x02;
  buf[2]=0x03;
  buf[3]=0x04;
  buf[4]=0x05;
  buf[5]=0x06;
  buf[6]=0x07;
  buf[7]=0x08;
  buf[8]=0x09;
  buf[9]=0x0a;
  buf[10]=0x0b;
  buf[11]=0x0c;
  SBUF=addr;            /*发送地址*/
  send_num=1;
}            
void io_int() interrupt 4    //串口中断
{   
     ES=0;
  tx_time=0;
     receive();
  clrDog();  
     if (rec_num==21)
      {
    rec_num=0;
  send_num=0;
     send();
  SM2=1;
      }  
  ES=1;
}
void TI_int() interrupt  13   //串口发送中断
{
ES=0;
TI=0;
    if(send_num>0&&send_num<13)
{
   SBUF=buf[send_num-1];
   send_num++;
}
  if(send_num>=13)
  {
  EN485=0;
//     send_num=0;
  }
  ES=1;
}
void main()
{

init();
RUN=0;
EN485=0;

while(1)
{

}

}

相关帖子

沙发
歪 歪| | 2010-4-29 11:59 | 只看该作者
用这个软件监视一下,可能是工具误导了你。
注意:一定按照下面详细使用方法里说明的顺序操作。

下载地址:
http://download.cnet.com/ComTrac ... 6892.html?tag=mncol

感觉好别忘了给个review(评论),谢谢!

FAQ
How to use it?
1.Run ComTrace
2.Select serial port name(e.g. COM1)
3.Run serial port application program that you want to debug(e.g. Windows HyperTerminal)
4.debug......(e.g. press any key in Windows HyperTerminal, then watch the data that is captured.)
5.Close application program(e.g. Windows HyperTerminal)
6.Close ComTrace

Notice: You must operate by the order, only support Win2000 and WinXp(32 bit) system!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:30——驱动高手、流利的英语

107

主题

525

帖子

0

粉丝