Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3298787
  • 博文数量: 754
  • 博客积分: 10132
  • 博客等级: 上将
  • 技术积分: 7780
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-14 23:36
文章分类

全部博文(754)

文章存档

2012年(3)

2011年(39)

2010年(66)

2009年(167)

2008年(479)

我的朋友

分类: LINUX

2008-04-21 01:26:20

串行通信时微计算机之间一种常见的近距离通信手段,因使用方便,编程简单而广泛使用,几乎所有的微控制器,PC都提供串行通信接口。
开始前,线路处于空闲状态,送出连续“1”。传送开始时首先发一个“0”作为起始位,然后出现在通信线桑的时字符的二进制编码数据。
每个字符的数据位长可以约定为:5位、6位、7位或8位,一般采用ASCII编码,后面时奇偶校验位,根据约定,用奇偶校验位将所传的字符中为“1”的位数凑成奇数个或偶数个。也可以约定不要奇偶校验,这样就取消奇偶校验位。
最后时表示停止位的“1”信号,这个停止位可以约定连续1位、1.5位或2位的时间宽度。
至此一个字符传送完毕,线路又进入空闲,持续为“1”。经过一段随机的时间后,下一个字符开始传送。

6.1
串口初始化
rUFCON0=0xf7;          //设置串口的FIFO属性,并清除FIFO中的内容
rUMCON0=0x0;         //关闭控制流功能
rULCON0=0x3;  //设置8位数据,1位停止位,无奇偶校验位串口模式
rUCON0=0x345;         //设置收发的中断模式位电平触发关闭超时功能
rUBRDIV0=int)(PCLK/(bps*16))-1;            //设置波特率,pclkARM时钟频率,baud为传//输波特率
 
6.2接收数据
whilerUTRSTAT0&0x1                   //检查状态寄存器,是否有数据到来,启动接收过程
data=rURXH0;   //将数据写到数据端口
 
6.3发送数据
whilerUTRSTAT0&0x2;                  //等待发送缓存区为空
rURXH0=data;//发送数据
 
6.4发送数据主函数
int Main()
{
    char aa;        
    SetClockDivider(1, 1);
         SetSysFclk(DFT_FCLK_VAL);
         Port_Init();
        
         myUart_Send("My UART0 is OK!\n");
        
        while(1);
}
 
6.5接收并发送主函数
int Main(void)
{
         char aa;
         char *str;        
    char *string;
    SetClockDivider(1, 1);
         SetSysFclk(DFT_FCLK_VAL);
         Port_Init();
                  
    myUart_Send("Please Input a string:\n");
    myUart_receive(string);
    *str=*string;
    Delay(500);
    myUart_Send(str);
    while(1);     
}
 
//------------------------------------
#include "2410lib.h"
#include "2410addr.h"
#include "timer.h"
static int UartNum=0;
//===============================================================
//对Uart进行初始化,以所需要的波特率为输入参数
void myUart_Init(int whichuart, int baud)
{
   if(whichuart==0)
   {
        UartNum=0;
       
  rGPHCON = rGPHCON & (~(0xffff)) ;
  rGPHCON = rGPHCON | (0xaaa0) ;
     rGPHUP  = 0x0;    // The pull up function is enable
 
  rUFCON0=0x00;   //不使用FIFO
        rUMCON0=0x00;   //不使用自动流控制
        rULCON0=0x03;   //不采用红外线传输模式,无奇偶校验位,1个停止位,8个数据位
        rUCON0=0x245;   //发送中断为电平方式,接收中断为边沿方式,禁止超时中断,允许产生错误状态中断,禁止回送模式,禁止中止  
                        //信号,传输模式为中断请求模式,接收模式也为中断请求模式。
        rUBRDIV0=((int)(PCLK/(baud*16))-1); //根据波特率计算UBRDIV0的值
  Delay(10);
    }
    else if(whichuart==1)
    {
        UartNum=1;
       
  rGPHCON = rGPHCON & (~(0xffff)) ;
  rGPHCON = rGPHCON | (0xaaa0) ;
     rGPHUP  = 0x0;    // The pull up function is enable
 
  rUFCON1=0x0;   
        rUMCON1=0x0;  
        rULCON1=0x3;
        rUCON1=0x245;
        rUBRDIV0=((int)(PCLK/(baud*16))-1);
        Delay(10);
    }
}
/*******************************************************************/
void myUart_SendByte(char ch)
{
 if (UartNum ==0)
    {
  if(ch=='\n')
  {
      while(!(rUTRSTAT0 & 0x2));//等待,直到发送缓冲区为空
      //Delay(10); //超级中断的响应速度较慢
      WrUTXH0('\r');//发送回车符
  }
  while(!(rUTRSTAT0 & 0x2)); //等待,直到发送缓冲区为空
  Delay(10);
  WrUTXH0(ch);//发送字符
    }
 else
    {
  if(ch=='\n')
     {
      while(!(rUTRSTAT1 & 0x2));
      Delay(10); //because the slow response of hyper_terminal
      rUTXH1='\r';
     }
     while(!(rUTRSTAT1 & 0x2));  //Wait until THR is empty.
     Delay(10);
     WrUTXH1(ch);
    } 
}
//====================================================================
void myUart_Send (char *str)
{
    myUart_Init(0,115200);
 while (*str)
 myUart_SendByte(*str++);

/********************************************************************/
char myUart_ReceiveByte(void)
{
    if(UartNum==0)
    {      
        while(!(rUTRSTAT0 & 0x1)); //Receive data ready
        return RdURXH0();
    }
    else if(UartNum==1)
    {      
        while(!(rUTRSTAT1 & 0x1)); //Receive data ready
        return RdURXH1();
    }
    return 0;
}
//===================================================================
//#if 0
void myUart_receive(char *string)
{
  char *string2 = string;
     char c;
     myUart_Init(0,115200);
     while((c = myUart_ReceiveByte())!='\r')
     {
        if(c=='\b')
        {
            if( (int)string2 < (int)string )
            {
                printf("\b \b");
                string--;
            }
        }
        else
        {
            *string++ = c;
            myUart_SendByte(c);
        }
     }
     *string='\0';
     myUart_SendByte('\n');
}
//#endif
//==================================================================
#if 0
myUart_receive(char *string)
{
  char *string2 = string;
     char c;
     myUart_Init(0,115200);
     while((c = myUart_ReceiveByte())!='\r')
     {
        if(c=='\b')
        {
            if( (int)string2 < (int)string )
            {
                printf("\b \b");
                string--;
            }
        }
        else
        {
            *string++ = c;
            return string;
        }
     }
     *string='\0';
    return string;
}
#endif
/**********************************************************************/
#if 0
int Main()
{
    char aa;        
    SetClockDivider(1, 1);
 SetSysFclk(DFT_FCLK_VAL);
 Port_Init();
 
 myUart_Send("My UART0 is OK!\n");
 
        while(1);
 
}
#endif
//======================================================================
//#if 0
int Main(void)
{
 char aa;
 char *str;        
    char *string;
    SetClockDivider(1, 1);
 SetSysFclk(DFT_FCLK_VAL);
 Port_Init();
  
    myUart_Send("Please Input a string:\n");
    myUart_receive(string);
    *str=*string;
    Delay(500);
    myUart_Send(str);
    while(1); 
}
//#endif
 


 
阅读(979) | 评论(0) | 转发(0) |
0

上一篇:SD卡应用实验

下一篇:红外模块控制实验

给主人留下些什么吧!~~