#define GLOBAL_CLK 1
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
void uart_sendByte(int data)
{
if(data == '\n')
{
while(!(rUTRSTAT0 &0x2));
WrUTXH0('\r');
}
while(!(rUTRSTAT0 &0x2));
WrUTXH0(data);
}
void uart_sendString(char *p)
{
while(*p)
uart_sendByte(*p++);
}
void uart_printf(char* fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt, ap);
uart_sendString(string);
va_end(ap);
}
char buf[256];
int len = 0;
void __irq uart_interrupt(void)
{
char t;
int i;
rSUBSRCPND |=7;
rSRCPND |= (1<<28);
rINTPND |= (1<<28);
t = rURXH0;
if(t!=13)
{
buf[len]=t;
len++;
}
else
{
for(i=0; i<len; i++)
{
rUTXH0 = buf[i];
while((rUTRSTAT0 &0x2)==0);
}
rUTXH0 = 13;
len = 0;
}
//当串口输入的时候,就打印这段代码
//uart_printf("There will be something inputed → → → ");
}
void init_uart(void)
{
rSUBSRCPND |= 0x7;
rSRCPND |= (1<<28);
rINTPND |= (1<<28);
rINTMSK &= ~(1<<28);
rINTSUBMSK = rINTSUBMSK & (~7) | (1<<1);
rGPHCON = ((2<<6)|(2<<4)|(0<<2)|(0<<0));
rGPHUP = ((1<<3)|(1<<2));
//禁止fifo模式
rUFCON0 = 0x0;
//控制串口触发时钟的相关配置
rUMCON0 = 0x0;
//设置每次传输,传8位的bit
rULCON0 = 0x3;
//设置接受和发送的中断请求
rUCON0 = 0x5;
//设置频率
rUBRDIV0 = ((int)(50000000/16.0/115200+0.5)-1);
}
char uart_getch(void)
{
//当接收到数据的时候,标志位自动设为 1
while(!(rUTRSTAT0 & 0x1));
//返回接收到的数据
return RdURXH0();
}
void uart_getString(char *string)
{
char *string2 = string;
char c;
while((c=uart_getch()) != '\r')
{
if(c == '\b')
{
if((int)string2 < (int)string)
{
uart_printf("\b\b");
string--;
}
}
else
{
*string++ = c;
uart_sendByte(c);
}
}
*string = '\0';
uart_sendByte('\n');
}
void Main(void)
{
//串口中断函数入口
pISR_UART0 = (U32)uart_interrupt;
//初始化串口中断
init_uart();
//打印数据
uart_printf("please input\n");
while(1);
}
TQ2440裸板---串口中断(详细注释)
最新推荐文章于 2020-06-24 21:29:32 发布