第三次实验 串口实验
- 格式:doc
- 大小:156.50 KB
- 文档页数:6
第三次实验 串口实验
一、实验报告内容:
1、实验目的和内容;
要求:通过串口0从PC机接收数据,再发给PC机显示出来,再结合流水灯显
示。
2、自己编写的程序;
3、实验现象记录和相关截图;
4、实验中出现问题和调试分析。
二、实验任务:
1. 看懂原理图的接口连接;
2. 端口初始化函数Port_Init(),找到和串口相关的初始化语句,解释;
3. 串口初始化函数Uart_Init(0,115200),对其记录(截图,截和自己实验相
关的串口),并进行注释;计算出波特率;
4. 函数Uart_Select(0)是什么?解释清楚;
5. 编写接收一个字节的函数aa= Uart_Getch();对其进行详细的记录和解
释;
6. 完成:如何发送一个字符?
7. 画接收过程的流程图。
三、实验步骤
1.新建工程文件。
2.定义与UART 有关的各个寄存器地址和一些特殊的位命令,加以解释。
3.编写串口驱动函数:
4.在主函数中实现将从串口0 接收到的数据发送到串口0(Main.c):
实验参考程序:
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#include
#include
//================================
#define rUTRSTAT0 (*(volatile unsigned *)0x50000010)
#define rUTRSTAT1 (*(volatile unsigned *)0x50004010)
#define WrUTXH0(ch) (*(volatile unsigned char *)0x50000020)=(unsigned char)(ch)
#define WrUTXH1(ch) (*(volatile unsigned char *)0x50004020)=(unsigned char)(ch)
#define RdURXH0() (*(volatile unsigned char *)0x50000024)
#define RdURXH1() (*(volatile unsigned char *)0x50004024)
void Uart_Init(int mclk,int baud);
void Uart_Select(int ch);
void Uart_TxEmpty(int ch);
char Uart_Getch(void);
char Uart_GetKey(void);
void Uart_GetString(char *string);
int Uart_GetIntNum(void);
void Uart_SendByte(int data);
void Uart_SendString(char *pt);
void Uart_Printf(char *fmt,...);
void dely(U32 tt)
{
U32 i;
for(;tt>0;tt--)
{
for(i=0;i<10000;i++){}
}
}
int Main(int argc, char **argv)
{
char c1[1];
char err;
U8 aa;
ChangeMPllValue(68,1,1);
Port_Init();
Uart_Select(0);
Uart_Init(0,115200);
MMU_DisableICache();
MMU_DisableDCache();
while(1)
{
aa= Uart_Getch();
Uart_SendByte(aa );
Uart_SendByte(10);
switch(aa)
{
case 'a':
Uart_Printf("\nYou Pressed 'a'");
点流水灯
break;
case 'b':
Uart_Printf("\nYou Pressed 'b'");
点流水灯
break;
case 'c':
Uart_Printf("\nYou Pressed 'c'");
点流水灯
break;
default :
break;
}
}
return 0;
}
//*************************[ MPLL ]*******************************
void ChangeMPllValue(int mdiv,int pdiv,int sdiv)
{
rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv; // 计算PCLK的大小?
}
在下面的函数中那条语句完成了对串口用到的端口的初始化?
void Port_Init(void)
{
rGPACON = 0x7fffff;
rGPBCON = 0x015550;
rGPBUP = 0x7ff;
rGPCCON = 0xaaa956aa;
rGPCUP = 0xffff;
rGPDCON = 0xaaaaaaaa;
rGPDUP = 0xffff;
rGPECON = 0xa02aa800;
rGPEUP = 0xffff;
rGPFCON = 0x55aa;
rGPFUP = 0xff;
rGPGCON = 0x00a2aaaa;
rGPGUP = 0xffff;
rGPHCON = 0x00faaa;
rGPHUP = 0x7ff;
rGPJCON = 0x02aaaaaa;
rGPJUP = 0x1fff;
//External interrupt will be falling edge triggered.
rEXTINT0 = 0x22222222;
rEXTINT1 = 0x22222222;
rEXTINT2 = 0x22222222;
}
void Uart_Select(int ch)
{
whichUart = ch;
}
逐条注释语句
void Uart_Init(int pclk,int baud)
{
int i;
if(pclk == 0)
pclk = 38000000;
// pclk = PCLK;
rUFCON0 = 0x0;
rUFCON1 = 0x0;
rUFCON2 = 0x0;
rUMCON0 = 0x0;
rUMCON1 = 0x0;
//UART0
rULCON0 = 0x3;
rUCON0 = 0x245;
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 );
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 );
for(i=0;i<100;i++);
}