uart串口 day57

十二:UART

一:正常发送

每个UART通道包含两个的64字节的FIFO给发送和接收

//正常运行(非中断)
#include <s3c2440.h>
#include "uart.h"

void uart0_init(void)
{
	// 配置GPH2、3功能为UART0
	GPHCON &= ~(0xf << 4);
	GPHCON |= (0xa << 4);

	

	ULCON0 = (0x3 << 0);

	UCON0 &= ~(0xf << 12);
	UCON0 |= (14 << 12);	//配置FCLK分频系数为(14 + 6)
	UCON0 |= (0x3 << 10);	//时钟选择:FCLK		
	UCON0 &= ~(0xf << 0);
	UCON0 |= (0x5 << 0);	//irq或polling模式
	UCON2 |= (1 << 15);		//使能FCLK/n
	UCON0 |= (1 << 7);		//Rx超时使能

	UBRDIV0 = 129;	//波特率 9600

	UFCON0 |= (1 << 1);		//复位RxFIFO	
	UFCON0 |= (1 << 0);		//复位TxFIFO
	UFCON0 &= (0x3 << 4);
	UFCON0 |= (0x1 << 4);   //RxFIFO触发深度为8
	UFCON0 |= (1 << 0);  	//使能FIFO

	INTSUBMSK &= ~(1 << 0);
	INTMSK &= ~(1 << 28);
}

unsigned char uart0_recv_byte(void)
{
	unsigned char data = 0;
	while(!(UTRSTAT0 & (1 << 0)));
	data = URXH0;
	return data;
}										   

char uart0_recv(unsigned char * buf, unsigned char len)
{
	unsigned char i = 0;
	unsigned char recv_len = UFSTAT0 & 0x3f;
	unsigned char read_len = (len < recv_len) ? len : recv_len;
	for(i = 0; i < read_len; i++)
	{
		buf[i] = URXH0;
	}
	
	return i;
}

void uart0_send_byte(unsigned char ch)
{
	while(!(UTRSTAT0 & (1 << 2)));
	UTXH0 = ch;	
}

unsigned char uart0_send(const unsigned char * buf, unsigned char len)
{
	unsigned char i = 0;
	for(i = 0; i < len; i++)
	{
		uart0_send_byte(buf[i]);	
	}

	return i;
}
-------------------------------------------------------------
	clk_init();
	uart0_init();
	while(1)
	{
		unsigned char ret = 0;
		ret = uart0_recv(uart_data,sizeof uart_data);
		uart0_send(uart_data,ret);
    }

二:中断形式

void c_deal_irq(void)
{
	// 1. 判断哪个中断源触发中断 并 处理
	// 清中断标志
	unsigned int irq_num = INTOFFSET;
  	
	if(irq_num == 0x1c)
	{
		if(SUBSRCPND & (1 << 0))
		{	
			unsigned char ret = 0;
			unsigned char uart_data[64] = {0};
			ret = uart0_recv(uart_data, sizeof uart_data);	
			uart0_send(uart_data, ret);
			SUBSRCPND |= (1 << 0);
		}
	}

//	irq_handler[irq_num]();

	SRCPND |= (1 << irq_num);
	INTPND = INTPND;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值