UART Test Sample

 

#include <stdlib.h>
#define rULCON0     (*(volatile unsigned *)0x50000000) //UART 0 Line control
#define rUCON0      (*(volatile unsigned *)0x50000004) //UART 0 Control
#define rUFCON0     (*(volatile unsigned *)0x50000008) //UART 0 FIFO control
#define rUMCON0     (*(volatile unsigned *)0x5000000c) //UART 0 Modem control
#define rUTRSTAT0   (*(volatile unsigned *)0x50000010) //UART 0 Tx/Rx status
#define rUERSTAT0   (*(volatile unsigned *)0x50000014) //UART 0 Rx error status
#define rUFSTAT0    (*(volatile unsigned *)0x50000018) //UART 0 FIFO status
#define rUMSTAT0    (*(volatile unsigned *)0x5000001c) //UART 0 Modem status
#define rUBRDIV0    (*(volatile unsigned *)0x50000028) //UART 0 Baud rate divisor
#define rUTXH0 (*(volatile unsigned char *)0x50000020) //UART 0 Transmission Hold
#define rURXH0 (*(volatile unsigned char *)0x50000024) //UART 0 Receive buffer
#define RXD0READY 1<<0
#define TXD0READY 1<<2
#define PCLK            50000000   
#define UART_BAUD_RATE  115200     

#define UART_BRD        ((PCLK  / (UART_BAUD_RATE * 16)) - 1)

void putc(char c)
{
    while (!(rUTRSTAT0 & TXD0READY));
    rUTXH0 = c;
}
unsigned char getc(void)
{
    while (!(rUTRSTAT0 & RXD0READY));
    return rURXH0;
}


void Uart_Init()
{
    rGPHCON |= 0xa0;    // GPH2,GPH3用作TXD0,RXD0
    rULCON0  = 0x03;     // 8N1(8个数据位,无较验,1个停止位)
    rUCON0   = 0x05;     // 查询方式,UART时钟源为PCLK
    rUBRDIV0 = UART_BRD; // 波特率为115200

 
}
int main(int argc, char *argv[])
{
 char *a = "Hello";
 Uart_Init();
 while(*a != NULL)
 {
 putc(*a);
 a++;
 }
 return 0;   //裸机需要用while(1),而在内存中则不需要(看了反汇编有句回跳的代码,而在内存中则没有作用??) 

}

程序写到flash则需要添加时钟设置,因为在SDRAM运行时bootloader已经设置好了时钟 这里设置时钟也很简单:

void Clock_Init()
{
 /* close watch dog*/
 rWTCON = 0x0;
 /* close all Interrupt */
 rINTMSK = 0xffffffff;
 rINTSUBMSK = 0x7fff;
 /* clock settings */
 rMPLLCON = 0x5c<<12 | 0x4<<4 | 0x2;
 rCLKDIVN = 1 ;
 __asm {
 
 mrc p15,0,r0,c1,c0,0  
 orr r0,r0,#0xc0000000  
 mcr p15,0,r0,c1,c0,0   //cpu总线模式 essential

        }

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值