串口初始化……
main.c 文件
#include "common.h" #include "uart.h" #include "isr.h" void UART4_Init(U32); void delay(long count); void Uart4_SendByte(U8 Char); void main (void) { char str[]="hello cortex-m4 "; U8 i; DisableInterrupts; UART4_Init(57600); EnableInterrupts; while(1) { for(i=0; str[i]!='\0'; i++) Uart4_SendByte(str[i]); delay(7000000); } } void delay(long count) { while(count--) asm("nop"); } void UART4_Init(U32 BaudRate) { U16 ubd, brfa; U8 temp; PORTC_PCR15 = PORT_PCR_MUX(0x3); //引脚使能 PORTC_PCR14 = PORT_PCR_MUX(0x3); SIM_SCGC1 |= SIM_SCGC1_UART4_MASK; //时钟使能 UART4_C2 &= ~((1<<2) | (1<<3)); //禁止发送接收 控制寄存器C2 UART4_C1 = 0; //控制器C1 全部使用默认配备 ubd = (U16)((50000000)/(BaudRate * 16)); //波特率设置 UART4_BDH = (U8)ubd>>8; UART4_BDL = (U8)(ubd); brfa = (((50000000)/(BaudRate * 16)) - (ubd * 32)); //波特率微调 1/32的增量 temp = UART4_C4 & ~(UART_C4_BRFA(0x1F)); UART4_C4 = temp | UART_C4_BRFA(brfa); UART4_C2 |= ((1<<2) | (1<<3)); //允许发送接收 return; } void Uart4_SendByte(U8 Char) { while(!(UART_S1_REG(UART4_BASE_PTR) & UART_S1_TDRE_MASK)); //UARTx_S1状态寄存器 UART4_D = Char; return; }
串口接收函数
U8 Uart4_Receive(void) { U8 Receivechar; while(!(UART_S1_REG(UART4_BASE_PTR) & UART_S1_RDRF_MASK)); //查询是否接受到数据 Receivechar = UART4_D; //接受到8位的数据 return(Receivechar); }