OK6410的UART0串口程序简单测试
mohanzb 2013-01-12
/*
此文件主要学习如何配置UART0,怎样单字符输入和输出, 怎样进行字符串的输入和输出;
*/
#include <stdarg.h>
#include "def.h"
#include "system.h"
#include "sysc.h"
#include "uart.h"
#define Inp32(ADDR) *((volatile u32 *)ADDR)
#define Outp32(ADDR,data) *((volatile u32 *)ADDR)=data
#define UART0 ((volatile UART_REGS*)0X7F005000)
void Uart_Port_Init(int channal);
void Uart_Init(u32 baud, int channal);
void Uart_Putc(char ch);
void Delay(int time);
void Uart_Puts(char *str);
void Uart_Printf(char *fmt,...);
char* Uart_Gets(char *);
char Uart_Getc(void);
char Uart_Getch(void);
char str[256];
void main(void)
{
SYSC_GetClkInform();
Uart_Init(115200, 0);
Uart_Puts("注意:按 ECS再按回车键 退出测试!! \n");
Uart_Printf("首先输出字符串:The world is currut be 5\n");
Uart_Printf("The world is currut be %d \n",5);
while(str[0] != 0x1b)
{
Uart_Gets(str);
Uart_Printf("你输入的是: %s \n",str);
}
Uart_Puts("你已经推出了程序,别做无谓针扎了....\n");
while(1);
}
//延时函数;
void Delay(int time)
{
int i;
for(;time > 0; time--)
for(i=0;i < 3000; i++);
}
//端口初始化;
void Uart_Port_Init(int channal)
{
if(channal == 0)
{
u32 uConValue;
#define rGPACON 0X7F008000
uConValue = Inp32(rGPACON);
uConValue = (uConValue & ~0xff) | 0x22; //配置为UART0功能;
Outp32(rGPACON,uConValue);
#define rGPAPUD 0X7F008008
uConValue = Inp32(rGPAPUD);
uConValue &= ~0xf; //低4位配置为0000,上下拉电阻除能;
Outp32(rGPAPUD,uConValue);
}
}
//uart初始化;
void Uart_Init(u32 baud, int channal)
{
u32 pclk;
u32 uConValue;
//if(pclk == 0)
pclk = g_PCLK;
Uart_Port_Init(channal);
//配置UART0寄存器;
if(channal == 0)
{
UART0->rUlCon = 0x3;//配置为8位数据,1位停止位,0位校验位;
UART0->rUCon = 0x805;//配置为中断或轮询模式,使用pclk作为波特率;
UART0->rUfCon = 0x0;//配置为非FIFO模式;
UART0->rUmCon = 0x0;//配置为非中断,非流控模式;
uConValue = (int)(pclk/16./baud) - 1;
UART0->rUbrDiv = uConValue;//配置波特率;
Delay(100);
}
}
//单字符输出;
void Uart_Putc(char ch)
{
if(ch == '\n')
{
while(!((UART0->rUtrStat) & 0x2));
UART0->rUtxh = '\r';
}
while(!((UART0->rUtrStat) & 0x2));
UART0->rUtxh = ch;
}
//字符串输出
void Uart_Puts(char *str)
{
while(*str)
{
Uart_Putc(*str++);
}
}
//格式化输出;
void Uart_Printf(char *fmt,...)
{
char str[256];
va_list ap;
va_start(ap,fmt); //va_start() 与 va_end() 需要对称
vsprintf(str,fmt,ap);
Uart_Puts(str);
va_end(ap);
}
//单字符阻塞型输入;
char Uart_Getc(void)
{
while(!((UART0->rUtrStat) & 0x1));
return (UART0->rUrxh);
}
//无阻塞型字符输入;
char Uart_Getch(void)
{
//if((UART0->rUtrStat) & 0x1)
return(UART0->rUrxh);
}
//字符寸输入;
char* Uart_Gets(char *str)
{
char *ptr = str;
char c;
while((c = Uart_Getc()) != '\r')
{
if(c == '\b')
{
if(ptr < str)
{
Uart_Putc('\b');
Uart_Putc(' ');
Uart_Putc('\b');
str--;
}
}
else
{
*str++ = c;
Uart_Putc(c);
}
}
Uart_Putc(c);
Uart_Putc('\n');
*str = '\0';
return (ptr);
}