|
#define UART0CLKENB *((volatile unsigned int *)0xC00A9000)
#define UART0CLKGEN0L *((volatile unsigned int *)0xC00A9004)
#define GPIOD_ALTFN0 *((volatile unsigned int *)0xC001D020)
#define GPIOD_ALTFN1 *((volatile unsigned int *)0xC001D024)
#define GPIOD_PULLENB *((volatile unsigned int *)0xC001D060)
#define ULCON0 *((volatile unsigned int *)0xC00A1000)
#define UCON0 *((volatile unsigned int *)0xC00A1004)
#define UFCON0 *((volatile unsigned int *)0xC00A1008)
#define UTRSTAT0 *((volatile unsigned int *)0xC00A1010)
#define UTXH0 *((volatile unsigned int *)0xC00A1020)
#define URXH0 *((volatile unsigned int *)0xC00A1024)
#define UBRDIV0 *((volatile unsigned int *)0xC00A1028)
#define UFRACVAL0 *((volatile unsigned int *)0xC00A102C)
void uart_init(void)
{
//先禁止时钟
UART0CLKENB &= ~(1<<2);
//选择时钟源为PPL1=800MHZ
//[4:2]=001 先请零
UART0CLKGEN0L &= ~(7<<2);
UART0CLKGEN0L |= (1<<2);
//设置分频系数
//[12:5] = 1111 1111,先请零
UART0CLKGEN0L &= ~(0xff<<5);
//UART0CLKGEN0L |= (0x0f<<5); //800/(0xof+1)=50MHz
UART0CLKGEN0L |= (0x13<<5); //800/(0x13+1)=40MHz
//配置管脚的功能 功能1
GPIOD_ALTFN0 &= ~(3<<28);
GPIOD_ALTFN0 |= (1<<28);
GPIOD_ALTFN1 &= ~(3<<4);
GPIOD_ALTFN1 |= (1<<4);
//uart控制器的配置
ULCON0 = 0x03; //7-bit/1-bit停止位
UCON0 = 0x05; //[3:0]=0101
//UBRDIV0 = 26; //uart 50MHz
//UFRACVAL0 = 2;
UBRDIV0 = 20; //uart 40MHz
UFRACVAL0 = 11;
//使能时钟
UART0CLKENB |= (1<<2);
}
void uart_putc(char c)
{
/*轮询发送缓冲区是否为空,
UARTSTATO[1]=1 空*/
while( !(UTRSTAT0 & (1<<1)) ) ;
UTXH0 = c;//发送一个字符
if(c == '\n')
uart_putc('\r');
}
void uart_puts(char *str)
{
while(*str)
{
uart_putc(*str);
str++;
}
}
|
|