main.c
资源名称:arm_exam.rar [点击查看]
上传用户:mhstny
上传日期:2022-08-05
资源大小:793k
文件大小:3k
源码类别:
微处理器开发
开发平台:
Unix_Linux
- #include "2410lib.h"
- #include "2410addr.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;
- }
- //===================================================================
- 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=' ';
- myUart_SendByte('n');
- }
- //======================================================================
- int Main(void)
- {
- char aa;
- char *str;
- char *string;
- ChangeClockDivider(1,1);
- ChangeMPllValue(0xa1,0x3,0x1);
- Port_Init();
- myUart_Send("Please Input a string:n");
- myUart_receive(string);
- *str=*string;
- Delay(500);
- myUart_Send(str);
- while(1);
- }