串口收发数据:
115200,8N1。
每一位的时间是t=1/115200。
传输一个字节需要10位:包括起始位1位,数据位8位,停止位1位,需要的时间是t=10/115200。
每秒传输的的字节数: 1/t = 115200/10 = 11520byte。
回环模式:
一发出数据就立刻收到,用于测试。
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();
if(c == '\r')
{
putchar('\n');
}
if(c == '\n')
{
putchar('\r');
}
putchar(c);
}
return 0;
}
uart.c
#include"s3c2440_soc._h"
void uart0_init()
{
//引脚设置
//GPH2/3 TXD0 RXD0
GPHCON &= ~((3<<4) | (3 <<6));
GPHCON |= ((2<<4) | (2<< 6));
//要想平时处于高电平,需要启动内部上拉
GPHUP &= ~((1<<2) | (1<<3));
//设置波特率
UCON0 = 0x00000005;/*PCLK,中断/查询模式*/
//计算公式UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
//例如串口时钟频率为40MUBRDIVn = (int)(40000000 / (115200 x 16) ) -1
//= (int)(21.7) -1 [round to the nearest whole number]
//= 22 -1 = 21
//50M时 UBRDIVn = (int)(50000000 / (115200 * 16))-1 = 26
UBRDIV0 = 26;
//设置数据模式
ULCON0 = 0X00000003;
//设置波特率
//设置数据格式
}
int getchar(void)
{
while(!(UTRSTAT0 & (1<<2)));
UTXH0 = (unsigned char )c;//发送寄存器
}
int putchar(int c)
{
while(!(UTRSTAT0 & (1<<0)) //状态查询寄存器
return URXH0; //接收寄存器
}
int puts(const char *s)
{
while(*s)
{
putchar(*s);
s++;
}
}
makefile
all:
arm-linux-gcc -c -o led.o led.c
arm-linux-gcc -c -o uart.o uart.c
arm-linux-gcc -c -o start.o start.S
arm-linux-ld -Ttext 0 start.o led.o uart.o main.o -o uart.elf
arm-linux-objcopy -O binary -S uart.elf uart.bin
arm-linux-objdump -D uart.elf > uart.dis
clean:
rm *.bin *.o *.elf *.dis