导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→串口接收问题

* 72173: 串口接收问题

   zhaiy 
zhaiy发表的帖子 

 串口接收问题
void InitSio(void)
{
 u16 RELOAD_COUNT = 0;
 
 //使用独立波特率发生器作为波特率发生器 
    S2CON    =   0x50;   //0101,0000 8位可变波特率,无奇偶校验位,允许接收

 RELOAD_COUNT = (int)(XTAL/BaudRateVal/32/12 + 0.5);

    RELOAD_COUNT = 256 - RELOAD_COUNT;
 //BRT = 0xFA;//22.118400MHz,12T,SMOD=0,9600bps
      BRT = RELOAD_COUNT;
   AUXR = 0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS 
//    ES      =   1;    //允许串口1中断
// ES2 = 1
 IE2 = 0x01; //允许串口2中断,ES2=1
    EA      =   1;    //开总中断
  
}
//----------------------------------------------------------------------------------------------
void WrRiBuf(unsigned char c) 
{
   
    disjoin_send(c);
     //ribuf[rirear] = c;  
    // rirear++;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    if(rirear >  = RIBUFMAX)
 { 
     rirear = 0; 
     Rs485dislow++;
 }
    if(ribufcount != RIBUFMAX) ribufcount ++;
 
}

//-----------------------------------------------------------------------------
unsigned char RdRiBuf() 
{
    char p;
    unsigned char c;   
    p = rirear - ribufcount;
    if(p   < 0) p = RIBUFMAX  + p;
 
    ribufcount--;
    c = ribuf[p];
    return c;
}

//----------------------------------------------------------------------------------------------
void IntComm() interrupt 8 
{
 u16   k   =   0;
 
 k = S2CON ; 
 k = k & 0x01;  
    if(k==1) //RI
    {
   IE2 = 0x00; //允许串口2中断,ES2=1
      S2CON = S2CON & 0xFE; //1111,1110
      WrRiBuf(S2BUF);   
   IE2 = 0x01; //允许串口2中断,ES2=1
      S2CON = S2CON & 0xFE; //1111,1110 
 }
   else
   {
      S2CON = S2CON&0xfd;    
   }

}  
void handlecomm()
{
 char c;
 //char i;
    if (ribufcount)
     c = RdRiBuf();

 if(ribuf[0] == 'F'&&ribuf[1] == 'F'){RIBUFMAX = 20;Rs485dislow++;}
 if(ribuf[0] == 'A'&&ribuf[1] == '0'){RIBUFMAX = 23;Rs485dislow++;}

 manage_key485DataTestdislowSub();  //显示接收到的数据  
}

//-----------------------------------------------------
void disjoin_send( u8 hex)//把十六进制数拆开ASCII显示在屏上
{
  //把十六进制数拆开ASCII
  u8 high;
  u8 low;
  high = hex>  >  4;//去掉低四位,直接得到高四位
  low = hex&0x0f;
  high = CharHex(high);//转换为高位的ASCII码
  low = CharHex(low);
 
  ribuf[rirear] = high;
  delayAny(2);
  rirear++;
  ribuf[rirear] = low;
  delayAny(2);
  rirear++;
  ribuf[rirear]= 0x20;
  rirear++;  
  //if(rirear>  RIBUFMAX*3-1)rirear1 = 0;
  //if(rirear >  =20) Rs485dislow = Rs485dislow+1;
}

//把一个十六进制转换为一个ASCII字符
unsigned char CharHex(char ch) 
{
  if((ch>  =0)&&(ch  <=9))
     return ch+0x30;
   
  else if((ch>  =10)&&(ch  <=15))
  
  return ch+'A'-10;//即变为从0开始对应字符A

  else return (-1);

}
main()
{
 handlecomm();
}
我的函数怎么得不到我想要的东西.我想接收一个字符串转一行.并且串是长度不一样的.


发表时间:2009年7月8日9:59:12

  
回复该帖

本主题共有 1 帖,分页:>>>>>该主题的所有内容[1]条

 *树形目录 只列出部分跟帖的标题以及简单的摘要信息 该主题的部分跟帖如下:

[上一篇帖子]:ARM的32.768不振我的也不振  后来换了块ARM就起振了  BR
[下一篇帖子]:电子工程师的收藏夹 可以重新访问了[原创]电子工程师的收藏夹 可以重新访问了 B