这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 高手求救———请大家快快帮帮我吧

共2条 1/1 1 跳转至

高手求救———请大家快快帮帮我吧

院士
2006-09-17 18:14:16     打赏
高手求救———请大家快快帮帮我吧



关键词: 高手     求救     大家     快快     帮帮     我吧    

院士
2006-12-22 22:43:00     打赏
2楼
问 由于学妹初学单片机,我花了一夜时间,用89S52调串口发送程序,但由于发送和接收的时序有问题,大家有没有什么见意,或有什么程序请发给我吧,不甚感激,
   xiexie  1: 供参考,可到我网站下载/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  Copyright (c)  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$                                                肖遥电子(广州)研发中心
$                                    Shawyoo Electronics (GuangZhou) R&D Center
$                          http://www.shawyoo.com         Email: autoele@qq.com
$                         Q Q群:8502321/9069471/9069433/9524253        All rights reserved.      
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  Copyright (c)  $$$$$$$$$$$$$$$$$$$$$$$$$$$ */
/*  
                                      文件描述
    文件名:      UART0.c
    功能描述:串口0初始化,数据链路层等相关程序
    编译环境:Keil C 51
    目标CPU:  8051
    作者:
    创建时间:
    建议编辑环境:UltraEdit 11.00a+  制表符宽度为三个字符,程序可读性更好。
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*/                                            

#define        UART0_GLOBALS

#include    "UART0.h"




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: PutString
    功能: 从串口0发送一块数据(字符串)
    入口: 待发送的字符串数据指针
    出口: 发送成功与否标志,成功为0,不成功为非0    
*/
uchar         PutString(uchar  *p)
{
   
    //RS485_TRANSMIT_ENABLE;          
   if(Txd.Sending)
       return 1;         // 发送失败,需要等待
    Txd.Length = strlen(p);
    if(Txd.Length==0)
        return 1;
    TxdP = p;
    Txd.Sending = 1;
    SBUF = *TxdP;    
    return 0;
}   // End of <PutString> Function



/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: PutBuffer
    功能: 从串口0发送一块数据(16进制)
    入口: 初始检验和,数据长度,待发送的数据指针
    出口: 发送成功与否标志,成功为0,不成功为非0
*/
uchar         PutBuffer(uchar  InitSum,uint L,uchar *p)
{
    uint16        i;
    if(Txd.Sending)
       return 1;         // 发送失败,需要等待
   Txd.Length = L;
    TxdP = p;
    Txd.CheckSum = InitSum;
    for(i=Txd.Length;i;i--)
    {
       Txd.CheckSum ^= *p;
       p++;
    }
    *p = Txd.CheckSum;        // Add ChechSum at the end of the buffer
    Txd.Length += 1;               
    Txd.Sending = 1;
    SBUF = *TxdP;
    return 0;
}  // End of <PutBuffer> Function




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: UART0_INT
    功能:   UART0中断函数,实现UART0数据链路层数据通讯
    入口:    无
    出口:    无
*/
void            UART0_INT(void)   interrupt 4     // UART0 interrupt function
{
   static   uchar   counter;    
    uchar     i,c;
   NOP();   NOP();        
    // Transmit interrupt $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$      
   if(TI)
    {  
        NOP();    NOP();
       if(Txd.Length==0)
       {
           Txd.Sending = 0;
           //os_evt_set(UART0_TRANSMIT_OK_EVT_FLAG,tidUART0_TxdOK);   //           
       }
         if(Txd.Length)       
      {
          //RS485_TRANSMIT_ENABLE;
            Txd.Length--;          
             TxdP++;
          SBUF = *TxdP;           
          Txd.Sending = 1;
       }
       else
       {
         Txd.Sending = 0;
         //RS485_RECEIVE_ENABLE
       }  
    }
    // Receive interrupt $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    if(RI)
    {  
        c = SBUF;
        // ####################################################        
       NOP();   NOP();
       if(c=='{')                     // 开始符
       {
           counter = 0;
           for(i=0;i<UART0_REC_LENGTH;i++)
           {
               Rec.Buffer[i] = 0;
           }
       }
       else
           counter++;         
       Rec.Buffer[counter] = c;      
       NOP();   NOP();
       if(((c=='}')&&(Rec.Buffer[0]=='{'))||(counter>UART0_REC_LENGTH))
       {
           counter = 0;
           isr_send_signal(TASK_ID_UART);
       }            
    }
    NOP();   NOP();
    RI = TI = 0;
    NOP();    NOP();
}  // End of UART0 Interrupt function



/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: ClearReceiveBuffer
    功能: 清除接收缓冲区及相关标志
    入口: 是否清缓冲区的命令,非0则清除缓冲区,否则只清除相关标志
    出口: 无
*/
void            ClearReceiveFlag(void)
{     
    NOP();    NOP();      
    Rec.CheckSum = Rec.Error = 0;
    Rec.Command = 0;
    Rec.Counter  = Rec.Length   = Rec.Receiving = 0;        
} // End of Function                 




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: ClearTransmitBuffer
    功能: 清除发送缓冲区及相关标志
    入口: 是否清缓冲区的命令,非0则清除缓冲区,否则只清除相关标志
    出口: 无
*/
void            ClearTransmitFlag(void)
{
    
    NOP();    NOP();         
    Txd.CheckSum = Txd.Sending = 0;    
    Txd.Length = 0;    
} // End of Function                  




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名: UART0_Init
    功能: 初始化UART0
    入口: 无
    出口: 无
*/
void            UART0_Init(void)
{
    //uint16  Fdiv;        
    ///////////////  
    NOP();    NOP();
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 使用定时器1      
   #if(USE_TIMER==1)            
    SCON=0x50;      // 方式1,8 bits     
   PCON |= 0x80;   // SMOD =1;
   TMOD |= 0x20;   // 8 bit auto reload
   TH1 = 256 - 2*FOSC/UART0_BPS/32/12;
   TR1=1;    
    #elif(USE_TIMER==2)        // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 使用定时器1    
    SCON=0x50;               // 方式1
   T2CON=0x30;              // 用T2作波特率发生器    
    TH2 = (65536 - FOSC/UART0_BPS/32)/256;
    TL2 = (65536 - FOSC/UART0_BPS/32)%256;   
    RCAP2H = (65536 - FOSC/UART0_BPS/32)/256;
    RCAP2L = (65536 - FOSC/UART0_BPS/32)%256;      
   SM2=0;
   TR2=1;      
    #endif
    #if(U0_PROIRITY)            // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 中断优先级    
    PS = 1;
    #endif

   ES=1;

}  // End of <UART0_Init> initialize function




/*
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  End of File $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*/ 2: 可以用串口通V3.21作串口通讯调试    串口通V3.21,超强功能...

   
    最新功能1: 接收区能混合显示ASCII和中文,ASCII和16进制,
    最新功能2: 能自定义设置5个换行符,显示纯16进制显示时能自动换行
    最新功能3: 发送前可选择自动换行或清空接收区...增加接收时间


    自动保存的路径可选,自动保存文件的大小可选。方便很多调试场合,更好地对事后的数据进行分析......
    自动保存最后配置,即打开软件时自动加载上次配置,方便...
    自动清空长度可选...
    此外,还可选择TB8作为多机通讯位...   
         
    下载地址: http://www.shawyoo.com下载中心一栏    
    特别功能: 发送数据支持 16进制、10进制和ASCII码的混合输入,方便更多场合调试
    两个频率和占空比可调的低频简易矩形波发生器

     发送数据举例:
     例1: \x01\x23 \x90 \180\238 \xf0 1234567890 \n\r \xaa abcd可输出中文\r\n
     例2: AT+CIPSTART="TCP","168.168.168.168","1688" \r\n
     例3: AT+CIPSEND=1024\r\n
           输入发送命令和长度后,可直接输入10进制、16进制、ASCII和中文,到预定长度时自动发送...

     自发自收测试:115200波特率,无校验,每隔100ms发送第一次开机默认发送区的数据(546字节)。测试过程中,界面操作反应正常,接收显示与发送基本同步,一直测试到发送约200KB(接收区不清空),发送字节数与接收字节数相同。

     做串口软件也是一种享受,不用刻意去做,需要某个功能的时候就增加,感觉真好...

共2条 1/1 1 跳转至

回复

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