这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 51 请教51串口通讯问题

共2条 1/1 1 跳转至

51 请教51串口通讯问题

院士
2006-09-17 18:14:16     打赏
51 请教51串口通讯问题



关键词: 请教     串口     通讯     问题    

院士
2006-12-22 22:43:00     打赏
2楼
问 我用单片机与电脑串行通讯,p1.0接发光二极管,用来观测是否收到数据!单片机发的数电脑能收到!而电脑发的数
单片机没有反应!
但是我把单片机的rx 与tx接到以前p1.0有反映!
计算机的发送接收接到一起!也能收发成功!
请问为啥单片机没有收到 计算机发的数据哪?

#include<reg51.h>

#define uint unsigned int
sbit P1_0=P1^0;
sbit P1_1=P1^1;
main()
{
unsigned char a;
unsigned char b;
uint j;
b=0x05;
TMOD=0x20;
TL1=0xff;
TH1=0xff;
SCON=0x50;
PCON=0x80;
TR1=1;
// REN=1;
P1_1=0;
SBUF=b;
while(1){

while(RI==0);
P1_0=!P1_0;//
RI=0;
a=SBUF;
for(j=0;j<12500;j++);
SBUF=a+1;
while(TI==0);
TI=0;
}
}
那里有问题请指点 1: 这一句换换试一下while(TI==0);
SBUF=a+1;
TI=0;


电平转换芯片用没 2: 有可能波特率不准。你用多少的晶振?计算波特率误差是多少?   另外问题可能出在线路方面,比如RS232电平转换芯片,或者是232连接线太长等等。
   你可以把波特率降下来试试,比如240bps。 3: 我用的是1.8432m晶振,波特率9600计算后没有误差!
用到max232芯片! 4: 这样换了也没有用!?while(TI==0);
SBUF=a+1;
TI=0;

5: 是不是我用的方式不对?我用的 是方式1 6: 两片单片机 之间都能通为啥 计算机发的 单片机收不到?
郁闷
我波特率都改到2400了 7: 一个实用串口程序#ifndef _debug_h
#include "Define.h"
#define _debug_h

#define MAX_RS232_BUF_LEN    10

/**********************
* EXPORTED DATA       *
**********************/
extern BYTE idata gbRS232WPtr;  // RS232 buffer write pointer
extern BYTE idata gbRS232RPtr;  // RS232 buffer read pointer
extern BYTE idata gpbRS232Buf[];  // RS232 buffer
extern BYTE idata gpbTmpBuf[];

/**********************
* FUNCTION PROTOTYPES *
**********************/
extern void InitRS232(void);
extern void RS232_WriteDec(INT32 tdwInNum);
extern void RS232_WriteHex8(BYTE tbInNum);
extern void RS232_WriteHex16(UINT16 twInNum);
extern void RS232_WriteHex32(UINT32 tdwInNum);
extern void RS232_WriteStr(BYTE *tpStr);
extern void RS232_Write_data(void);

#endif

/**********************
* INCLUDE FILES       *
**********************/
/* Standard include files */

/* Project  include files */
#include "global.h"
#include "Define.h"
#include "Debug.h"
#ifdef RS232_DEBUG

/**********************
* EXPORTED DATA       *
**********************/
BYTE idata gbRS232WPtr;    // RS232 buffer write pointer
BYTE idata gbRS232RPtr; // RS232 buffer read pointer
BYTE idata gpbRS232Buf[MAX_RS232_BUF_LEN]; // RS232 buffer
BYTE idata gpbTmpBuf[8];// = { 0,0,0,0,0,0,0,0 }

/**********************
* FUNCTION PROTOTYPES *
**********************/

/*********************************************************
* NAME    :    InitRS232
*           Initialize serial port and buffer.
*
* Returns : none
*
* Parameter        Description
* --------------------------------------------------------
* none
*
* Externals        Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*     none
*********************************************************/
void InitRS232(void)
{
    BYTE IDATA i;

    // reset read/write pointer
    gbRS232WPtr = 0;
    gbRS232RPtr = 0;

    // clear buffer
    for(i = 0; i < MAX_RS232_BUF_LEN; i++)
        gpbRS232Buf[i] = 0;

    // setup serial port
    PCON |= 0x80;    // SMOD=1, 2x baud rate
    SCON = 0x40; // serial port mode 1
//-----------------------------------
    //RCAP2H=0xff;// baud rate:2400
    //RCAP2L=0x63;// baud rate:2400
//-----------------------------------
    //RCAP2H=0xff;// baud rate:4800
    //RCAP2L=0xb1;// baud rate:4800
//------------------------------------------
    //RCAP2H=0xff;// baud rate: 9600
    //RCAP2L=0xd8;// baud rate:9600
//-------------------------------------------
    RCAP2H=0xff;// baud rate:19200
    RCAP2L=0xec;// baud rate:19200
//--------------------------------------------------    
    //RCAP2H=0xff;// baud rate:38400
    //RCAP2L=0xf6;// baud rate:38400
//------------------------------------------
    //RCAP2H=0xff;// baud rate:56000
    //RCAP2L=0xf9;// baud rate:56000
//------------------------------------------
    T2CON=0x30; //0011 0000b, T2 as UART timer
    T2MOD=0x01;
    TR2= 1; // start Timer2
    TI = 1;
    RS232_WriteStr("RS232 OK\r");
    //RS232_Write_data();
}

/*********************************************************
* NAME    :    RS232_WriteHex8
*           Write a 8-bit hexadecimal integer number to the serial port.
*
* Returns : none
*
* Parameter        Description
* --------------------------------------------------------
* tbInNum        8-bit input number
*
* Externals        Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*     none
*********************************************************/
/*void RS232_WriteHex8(BYTE tbInNum)
{
INT8 idata i;

for( i=0;i<2;i++ )
{
    gpbTmpBuf[i] = (tbInNum&0x0f)+'0';
    if( gpbTmpBuf[i] > '9' ) gpbTmpBuf[i] += 7;
    tbInNum >>= 4;
}

gpbRS232Buf[gbRS232WPtr++] = '0';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

gpbRS232Buf[gbRS232WPtr++] = 'x';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

for( i=1;i>=0;i-- )
{
    gpbRS232Buf[gbRS232WPtr++] = gpbTmpBuf[i];
    if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

gpbRS232Buf[gbRS232WPtr++] = '\r';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

/*********************************************************
* NAME    :    RS232_WriteHex16
*           Write a 16-bit hexadecimal integer number to the serial port.
*
* Returns : none
*
* Parameter        Description
* --------------------------------------------------------
* twInNum        16-bit input number
*
* Externals     Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*     none
*********************************************************/
void RS232_WriteHex16(UINT16 twInNum)
{
INT8 idata i;

for( i=0;i<4;i++ )
{
    gpbTmpBuf[i] = (twInNum&0x0f)+'0';
    if( gpbTmpBuf[i] > '9' ) gpbTmpBuf[i] += 7;
    twInNum >>= 4;
}

gpbRS232Buf[gbRS232WPtr++] = '0';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

gpbRS232Buf[gbRS232WPtr++] = 'x';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

for( i=3;i>=0;i-- )
{
    gpbRS232Buf[gbRS232WPtr++] = gpbTmpBuf[i];
    if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

gpbRS232Buf[gbRS232WPtr++] = '\r';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

/*********************************************************
* NAME    :    RS232_WriteHex32
*           Write a 32-bit hexadecimal integer number to the serial port.
*
* Returns : none
*
* Parameter        Description
* --------------------------------------------------------
* tdwInNum        32-bit input number
*
* Externals     Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*     none
*********************************************************/
/*void RS232_WriteHex32(UINT32 tdwInNum)
{
INT8 idata i;

for( i=0;i<8;i++ )
{
    gpbTmpBuf[i] = (tdwInNum&0x0f)+'0';
    if( gpbTmpBuf[i]>'9' ) gpbTmpBuf[i] += 7;
    tdwInNum >>= 4;
}

gpbRS232Buf[gbRS232WPtr++] = '0';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

gpbRS232Buf[gbRS232WPtr++] = 'x';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;

for( i=7;i>=0;i-- )
{
    gpbRS232Buf[gbRS232WPtr++] = gpbTmpBuf[i];
    if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

gpbRS232Buf[gbRS232WPtr++] = '\r';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

/*********************************************************
* NAME    :    RS232_WriteDec()
*           Write decimal integer number to serial port.
*
* Returns : none
*
* Parameter     Description
* --------------------------------------------------------
* tdwInNum        input number
*
* Externals        Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*   Input number is limited to -99999 to +99999
*********************************************************/
void RS232_WriteDec(INT32 tdwInNum)
{
INT8 idata i;

if( tdwInNum < 0 )
{
    gpbRS232Buf[gbRS232WPtr++] = '-';
    tdwInNum *= -1;
}
else gpbRS232Buf[gbRS232WPtr++] = '+';

if( gbRS232WPtr == MAX_RS232_BUF_LEN )
         gbRS232WPtr = 0;

for( i=0;i<8;i++ )
{
    gpbTmpBuf[i] = tdwInNum%10 + '0';
    tdwInNum = tdwInNum/10;
}

for( i=7;i>=0;i-- )
{
    gpbRS232Buf[gbRS232WPtr++] = gpbTmpBuf[i];
    if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}

gpbRS232Buf[gbRS232WPtr++] = '\r';
if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
//RS232_Write_data();
}

/*********************************************************
* NAME    :    RS232_WriteStr
*           Write a string to serial port.
*
* Returns : none
*
* Parameter     Description
* --------------------------------------------------------
* tpStr            input string
*
* Externals     Usage
* --------------------------------------------------------
* gbRS232WPtr    write pointer
* gbRS232RPtr    read pointer
* gpbRS232Buf    buffer pointer
*
* Additional information:
*     String max. is set to 60 char.
*********************************************************/
void RS232_WriteStr(BYTE *tpStr)
{
BYTE idata i;

for( i=0;i<10;i++ )
{
       if( *tpStr == '\0' )
       {
          //gpbRS232Buf[gbRS232WPtr++] = '\r';
        //if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
           return;
       }
       gpbRS232Buf[gbRS232WPtr++] = *tpStr;
       tpStr++;

       if( gbRS232WPtr == MAX_RS232_BUF_LEN ) gbRS232WPtr = 0;
}
// RS232_Write_data();
}
/*-------------------------------------------*/
void RS232_Write_data(void)
{
     if( TI ) // last transfer finished
     {        
         if( gbRS232RPtr != gbRS232WPtr )
             {
                 TI = 0;
                   SBUF = gpbRS232Buf[gbRS232RPtr++];
                   if( gbRS232RPtr == MAX_RS232_BUF_LEN ) gbRS232RPtr = 0;
               }
     }
        /*if(gbRS232RPtr != gbRS232WPtr)
        {    
            while(!TI);// last transfer finished
            TI = 0;
            SBUF = gpbRS232Buf[gbRS232RPtr++];
            if(gbRS232RPtr == MAX_RS232_BUF_LEN) gbRS232RPtr = 0;
        }*/
        //gbRS232WPtr=0;
}
#endif
8: 多谢这程序实现啥功能?

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]