TQ2440裸板---串口中断(详细注释)

#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);
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值