//下面是Uart.h程序 #ifndef __UART_H #define __UART_H
#ifndef NONE #define NONE (0) #endif
#ifndef EVEN #define EVEN (2) #endif
#ifndef ODD #define ODD (1) #endif
typedef struct { uint32 baudrate;//波特率 uint8 databit; //字长度,5/6/7/8可选 uint8 stopbit; //停止位,1/2位可选 uint8 parity; //奇偶校验位,0:无校验,1:奇校验,2,偶校验 uint8 rxinten; //接收中断允许,0禁止,1使能中断接收 }UARTMODE;
uint8 Uart0Init(UARTMODE *set); //查询发送单个字节 void Uart0Send(uint8 dat); //查询发送多个字节 void Uart0SendBytes(uint8 *ptr,uint8 num); //查询发送字符串 void Uart0SendStr(uint8 *ptr);
uint8 Uart1Init(UARTMODE *set); //查询发送单个字节 void Uart1Send(uint8 dat); //查询发送多个字节 void Uart1SendBytes(uint8 *ptr,uint8 num); //查询发送字符串 void Uart1SendStr(uint8 *ptr);
#endif
/***********************************************************/ //下面是Uart.c程序 #include "includes.h" #include "Uart.h"
/********************************************* *Function :uart0非嵌套IRQ中断服务程序 *Create By:shannon **********************************************/ void IRQUart0(void) __irq { receBuf[0]= U0RBR; IO1SET = (1 << (receBuf[0] + 16)); VICVectAddr = 0;//向量中断结束 }
/********************************************* *Function :uart0初始化程序 *Create By:shannon **********************************************/ uint8 Uart0Init(UARTMODE *set) { uint32 bak; pinmap.pin.p00 = GP000TXD0; pinmap.pin.p01 = GP001RXD0; PINSEL0 = pinmap.PinSel[0]; VICIntSelect = VICIntSelect & ~(1 << (UART0_PRI - 1));//设置相应的中断为IRQ中断 #if UART0_PRI == 1 VICVectCntl0 = 0x20 | UART0_INT;//分配中断 VICVectAddr0 = (uint32)IRQUart0;//设置中断服务程序地址 VICIntEnable = 1 << UART0_INT; //使能中断 #elif (UART0_PRI == 2) VICVectCntl1 = 0x20 | UART0_INT; VICVectAddr1 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 3 VICVectCntl2 = 0x20 | UART0_INT; VICVectAddr2 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 4 VICVectCntl3 = 0x20 | UART0_INT; VICVectAddr3 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 5 VICVectCntl4 = 0x20 | UART0_INT; VICVectAddr4 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 6 VICVectCntl5 = 0x20 | UART0_INT; VICVectAddr5 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 7 VICVectCntl6 = 0x20 | UART0_INT; VICVectAddr6 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 8 VICVectCntl7 = 0x20 | UART0_INT; VICVectAddr7 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 9 VICVectCntl8 = 0x20 | UART0_INT; VICVectAddr8 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 10 VICVectCntl9 = 0x20 | UART0_INT; VICVectAddr9 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 11 VICVectCntl10 = 0x20 | UART0_INT; VICVectAddr10 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 12 VICVectCntl11 = 0x20 | UART0_INT; VICVectAddr11 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 13 VICVectCntl12 = 0x20 | UART0_INT; VICVectAddr12 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 14 VICVectCntl13 = 0x20 | UART0_INT; VICVectAddr13 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 15 VICVectCntl14 = 0x20 | UART0_INT; VICVectAddr14 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #elif UART0_PRI == 16 VICVectCntl15 = 0x20 | UART0_INT; VICVectAddr15 = (uint32)IRQUart0; VICIntEnable = 1 << UART0_INT; #endif if ((set->databit > 8) || (set->databit < 5)) return(ERR); if ((set->stopbit == 0) || (set->stopbit > 2)) return(ERR); if (set->parity > 2) return(ERR);
U0LCR = 0x80;//DLAB = 1; bak = (Fpclk >> 4)/(set->baudrate); U0DLM = bak >> 8; U0DLL = bak & 0xff;
bak = 0; bak = set->databit - 5; if (set->stopbit == 2) bak |= 0x04; if (set->parity != NONE) { bak |= 0x08; set->parity--; } bak |= set->parity << 4; U0LCR = 0; U0LCR = bak;//设置串口数据格式 if (set->rxinten) U0IER = 0x01;//接收中断使能
return (1); }
//查询发送单个字节 void Uart0Send(uint8 dat) { U0THR = dat; while((U0LSR & 0x40) == 0);//移位寄存器和发送保持均为空 } //查询发送多个字节 void Uart0SendBytes(uint8 *ptr,uint8 num) { uint8 i; for (i = num; num > 0; i--) { U0THR = *ptr; while((U0LSR & 0x40) == 0);//移位寄存器和发送保持均为空 ptr++; } } //查询发送字符串 void Uart0SendStr(uint8 *ptr) { while (*ptr){ U0THR = *ptr; while((U0LSR & 0x40) == 0);//移位寄存器和发送保持均为空 ptr++; } }
我只需要更改UART0_PRI宏的值就可以实现优先级的改变。pinmap.pin.p00 结构体改变引脚的功能也非常方便,如果哪个兄弟希望共同学习arm的可以加我的MSN:hxdlj@hotmail.com。 |