、
UART0初始化
初始化配置内容:波特率(115200);8bit数据位;1bit停止位;无奇偶校验位;无流控制
uart.c:
void uart0_init()
{
/*配置GPH2,3为TXD,RXD,并且将这两个引脚内部拉高*/
/*GPHCON(0x56000070)*/
/*GPHUP(0x56000078)*/
GPHCON &= ~((0x3 << 4) | (0x3 << 6)); //[5:4][7:6]置零
GPHCON |= ((0x2 << 4) | (0x2 << 6));
GPHUP &= ~((0x1 << 2) | (0x1 << 3));//[3:2]置零
/*设置波特率115200*/
/*UBRDIV0(0x50000028) = 21*/
UBRDIV0 = 26;
/*设置数据位,停止位,流控制*/
/*ULCON0(0x50000000) = 0x3*/
/* 8n1: 8个数据位, 无较验位, 1个停止位 */
ULCON0 = 0x3;
/*设置UCON0 = 0x5 设置为接收/发送模式*/
UCON0 = 0x5;
}
输出字符函数
int putchar(int c)
{
while(!(UTRSTAT0 & (1 << 2))); //根据状态标志位确定
UTXH0 = c;
}
输入字符
int getchar(void)
{
while(!(UTRSTAT0 & (1 << 0))); //根据状态标志位确定
return URXH0;
}
输出字符串(根据输出字符改写)
int puts(const char *s)
{
while(*s)
{
putchar(*s);
s++;
}
}
UART.h
#ifndef __uart_H_
#define __uart_H_
void uart0_init();
int putchar(int c);
int getchar(void);
int puts(const char *s);
#endif
main.c
#include "s3c2440_soc.h"
#include "uart.h"
int main(void)
{
unsigned char c;
uart0_init();
puts("Hello, world!\n\r");
while(1)
{
c = getchar();
//下面几行代码的作用:当按下回车(‘\r\n’)时,输出起到换行,和光标移动到最前面的效果
if (c == '\r')
{
putchar('\n');
}
if (c == '\n')
{
putchar('\r');
}
putchar(c);
}
return 0;
}