uart串口 day57
十二:UART
一:正常发送
每个UART通道包含两个的64字节的FIFO给发送和接收
//正常运行(非中断)
#include <s3c2440.h>
#include "uart.h"void uart0_init(void)
{// 配置GPH2、3功能为UART0GPHCON &= ~(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/nUCON0 |= (1 << 7); //Rx超时使能UBRDIV0 = 129; //波特率 9600UFCON0 |= (1 << 1); //复位RxFIFO UFCON0 |= (1 << 0); //复位TxFIFOUFCON0 &= (0x3 << 4);UFCON0 |= (0x1 << 4); //RxFIFO触发深度为8UFCON0 |= (1 << 0); //使能FIFOINTSUBMSK &= ~(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;
}