Chinaunix首页 | 论坛 | 博客
  • 博客访问: 508389
  • 博文数量: 76
  • 博客积分: 2535
  • 博客等级: 少校
  • 技术积分: 3130
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-22 11:26
个人简介

京都医院http://www.fjzzled.com/杭州京都医院

文章分类

全部博文(76)

文章存档

2011年(25)

2010年(51)

我的朋友

分类: 嵌入式

2010-11-23 15:51:24

#include "2440lib.h"
#include "2440addr.h"

 int flag = 0;
 
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/16./baud+0.5) -1 );
  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/16./baud+0.5) -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(100);
  WrUTXH0(ch);//发送字符
    }
 else
    {
  if(ch=='\n')
     {
      while(!(rUTRSTAT1 & 0x2));
      Delay(100); //because the slow response of hyper_terminal
      rUTXH1='\r';
     }
     while(!(rUTRSTAT1 & 0x2));  //Wait until THR is empty.
     Delay(100);
     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;
}
//===================================================================
void myUart_receive(char *string)
{
  char *string2 = string;
     char c;
     int i = 0;
     myUart_Init(0,115200);
     while((c = myUart_ReceiveByte())!='\r')
     {
        if(c=='\b')
        {
            if( (int)string2 < (int)string )
            {
                Uart_Printf("\b \b");
                string--;
            }
        }
        else
        {
            *(string + i) = c;
            i++;
            myUart_SendByte(c);
        }
     }
     *(string+i)='\0';
     myUart_SendByte('\n');
}
 
void __irq eint_handler(void)
{  
  rSRCPND |= (0x1<<5);
     rINTPND |= (0x1<<5);//申请 请求 EINT8_23
   
   
    // rEINTPEND |= (0x1<<8);//EINT8 发生中断
   
    if ( rEINTPEND & (0x1<<8))
    {
   
    flag = 8;
   rEINTPEND |= (0x1<<8);
  
     }
    
      if ( rEINTPEND & (0x1<<11))
    {
   
    flag = 11;
   rEINTPEND |= (0x1<<11);
   
    }
     if ( rEINTPEND & (0x1<<13))
    {
   
    flag = 13;
   rEINTPEND |= (0x1<<13);
  
     }
      if ( rEINTPEND & (0x1<<14))
    {
   
    flag = 14;
   rEINTPEND |= (0x1<<14);
  
     }
         if ( rEINTPEND & (0x1<<15))
    {
   
    flag = 15;
   rEINTPEND |= (0x1<<15);
  
     }
         if ( rEINTPEND & (0x1<<19))
    {
   
    flag = 19;
    
   rEINTPEND |= (0x1<<19);
  
     }
}
 
void delay(unsigned long ms)
{  
    unsigned long  i,j;
 for (i=0;i 
 for (j=0;j<100000;j++);

}
void timmer_init(void)
{
 rTCFG0 &= 0xFF00FF;
 rTCFG0 |= 0xf900; //prescaler 等于249 PCLK ÷ (prescaler+1) ÷ divider  diveder=8  prescaler=249  定时器输出为25KHZ
    rTCFG1 &= ~0xF0000;
    rTCFG1 |= 0x20000l; //divider等于8,则设置定时器4的时钟频率为25kHz
   
    rTCNTB4 = 12500; //让定时器4每隔0.5秒中断一次
   
    rTCON &= ~0xF00000;
    rTCON |= 0x700000;
   
    rTCON &= ~0x200000;  //定时器4开始工作

    
}

void __irq timmer4(void)
{
   
    static int count;
   
    count++;
   
 rSRCPND |= (0x1<<14);
 rINTPND |= (0x1<<14);
 
 //每 1S 响一次
 
 if (count % 2==0)
  {
  
      rGPBDAT &= ~((1<<5));
      
     // rGPBDAT |= ((1<<8));
      flag = 1;
     
    
  
   }
 if (count % 2==1)
   { 
   
       rGPBDAT |= ((1<<5));
       myUart_Send("Please Input a string:\n");
       }
      
    if ((count % 4 == 0)&&(flag == 0))
    {
   
      rGPBDAT &=  ~((1<<8));
   
    } 
}

int xmain(void)
{
 //char *str;        
   // char string[255];
    unsigned long i;
 ChangeClockDivider(3,1);//1:4:8
 ChangeMPllValue(127,2,1);  //405MHZ
 Port_Init();
 Isr_Init();
 
  
    rGPBCON &= ~((0x3<<10)|(0x3<<12)|(0x3<<14)|(0x3<<16)|(0x3<<0));//clear 5 6 7 8 is 00
    rGPBCON |= (1<<10)|(1<<12)|(1<<14)|(1<<16)|(0<<1);//set is  output
   
   
    rGPGCON &= ~((0x3<<0)|(0x3<<6)|(0x3<<10)|(0x3<<12)|(0x3<<14)|(0x3<<22));
    rGPGCON |= (0x2<<0)|(0x2<<6)|(0x2<<10)|(0x2<<12)|(0x2<<14)|(0x2<<22);//配置 GPG0为EINT8
   
 
    rGPBDAT |= ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(0<<0));//全部LED灭,BEEP 不响
   
    rGPBDAT &= ~((1<<7));
    timmer_init();
  
   // rPRIORITY=0x0;
   
    rSRCPND |= (0x1<<14);
 rINTPND |= (0x1<<14);
 
 rINTMSK   &=  ~(1<<14);
   
    // rINTMSK   &=  ~((1<<5)|(1<<14));//EINT8_23服务可用 timmer4可用
   //  rEINTMASK &=  ~((1<<8)|(1<<11)|(1<<13)|(1<<14)|(1<<15)|(1<<19));//打开第8个中断
   
  //  rEXTINT1  &= ~((0x7<<0)|(0x7<<12)|(0x7<<20)|(0x7<<24)|(0x7<<28));
  //  rEXTINT1  |= (0x2<<0)|(0x2<<12)|(0x2<<20)|(0x2<<24)|(0x2<<28);//配置为下降沿
   
   //  rEXTINT2  &= ~(0x7<<12);
  //   rEXTINT2  |= (0x2<<12); //EINT19
   
 //    rSRCPND |= (0x1<<5)|(0x1<<14);
 //    rINTPND |= (0x1<<5)|(0x1<<14);//申请 请求 EINT8_23
   
  
  //  rEINTPEND |= (0x1<<8)|(0x1<<11)|(0x1<<13)|(0x1<<14)|(0x1<<14)|(0x1<<15)|(0x1<<19);//EINT8 发生中断
   
   
  
   
   // pISR_EINT8_23 = (U32)eint_handler;   
   
    pISR_TIMER4 = (U32)timmer4;
  //  rGPBDAT &= ~((1<<7)); 
/*    while(1)
 {
       //flag = 8;
   
      switch (flag)
   // if (flag==8)
   
    {   case 8:
    
              flag = 0;
        
          rGPBDAT &= ~((1<<5));
         
          delay(20);
         
          rGPBDAT |= ((1<<5));
         
          delay(20);
            break;
       
       case 11:
             flag = 0;
          rGPBDAT &= ~((1<<6));
          delay(20);
          
          rGPBDAT |= ((1<<6));
          delay(20);
          break;
   
      case 13:
              flag = 0;
          rGPBDAT &= ~((1<<7));
          delay(20);
          rGPBDAT |= ((1<<7));
          delay(20);
          break;
   
      case 14:
            
             flag = 0;
          rGPBDAT &= ~((1<<8));
          delay(20);
          rGPBDAT |= ((1<<8));
          delay(20);
          break;
       
      case 15:
                
                flag = 0;
            rGPBDAT &= ~((1<<8));
            delay(40);
            rGPBDAT |= ((1<<8));
            delay(40);
            rGPBDAT &= ~((1<<7));
            delay(20);
            rGPBDAT |= ((1<<7));
            delay(20); 
            rGPBDAT &= ~((1<<6));
             delay(20);
             rGPBDAT |= ((1<<6));
             delay(20);
             rGPBDAT &= ~((1<<5));
           delay(20);
           rGPBDAT |= ((1<<5));
         
          delay(20);
             break;
          case 19:
                flag = 0;
          rGPBDAT &= ~((1<<8));
          rGPBDAT &= ~((1<<7));
          rGPBDAT &= ~((1<<6));
          rGPBDAT &= ~((1<<5));
          delay(40);
          rGPBDAT |= ((1<<8)); 
            rGPBDAT |= ((1<<7)); 
             rGPBDAT |= ((1<<6)); 
             rGPBDAT |= ((1<<5));
             delay(40);
 
          break; 
         
    default:
   
          flag = 0 ;
          rGPBDAT |= ((1<<8)|(1<<7)|(1<<6)|(1<<5));
          break;      
       
      }  //switch
   
   
 
 }  //while */
 
 while (1)  
 {
             rGPBDAT &= ~((1<<8));
            delay(40);
            rGPBDAT |= ((1<<8));
            delay(40);
            rGPBDAT &= ~((1<<7));
            delay(20);
            rGPBDAT |= ((1<<7));
            delay(20); 
            rGPBDAT &= ~((1<<6));
             delay(20);
             rGPBDAT |= ((1<<6));
             delay(20);
  
 }
}//main
 
 
 

 
阅读(2598) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~