Linux之串口知识

串口怎样发送一个字节?
1.双方先约定好发送一个字节所用的时间,即波特率。
2.TXD引脚原来是高电平,ARM开发板拉低电平,保持1bit的时间。
3.PC在低电平时开始计时。
4.ARM根据数据驱动TXD电平,TXD=Data[0]…发送数据。
PC在计时期间,在每一数据位的中间读取引脚状态。Data[0]=TXD[t0]…Data[i]=TXD[tn]
5.以前数据传输没有那么稳定,所以在数据位传输完成后要添加校验位。奇偶校验:就是说“数据位+校验位”中为1的个数是奇数个还是偶数个。
比如说奇校验,前面数据位中1的个数是两位,那么这个校验位必须是1,这样就是奇数个。不过现在用的少了。
6.最后一个位是停止位,一般是高电平,目的是恢复到原来的状态,方便开始下一轮传输。双方可以约定停止位占据多少位的时间,可以是1位,1.5位,2位。
在这里插入图片描述
注意:逻辑电平这里,RS232刚好反过来
在这里插入图片描述
一般的逻辑电平都是正常
在这里插入图片描述

RS232相比TTL传输距离更远。

ARM和PC机之间的数据的发送流程
ARM内存——>FIFO缓冲区——>发送移位器——>PC
PC——>接收移位器——>FIFO缓冲区——>ARM内存
PC给ARM就反过来

然后开始写代码
在这里插入图片描述
显示TXD0,RXD0是GH2和GH3引脚
查sc2440的技术手册知道,这两个位都要配置成10
在这里插入图片描述

c查看这个寄存器的名字
在这里插入图片描述
所以代码这样写
在这里插入图片描述

在这里插入图片描述by byte好像表示只能发送字节

在uart.c文件中写的代码:

#include"S3C2440_soc.h"
void uart0_init()
{
/设置引脚用于串口/
/GPH2,GPH3用于TXD,RXD/
GPHCON &= ~((3<<4)|(3<<6)); //这一步是将4567位清零
GPHCON |=((2<<4)|(2<<6)); //将两位变成10,成为串口的引脚

GPHUP &=~((1<<2)|(1<<3));      //传输一开始不是需要上拉电平么,就是这个代码干的活

/*设置波特率*/
/*注意波特率不是直接设置出来的,他说计算出分频用分频的寄存器表示出来的*/
/*UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
*	UART clock=50M(在.s文件中有设置过),buad rate115200
*计算得26.126...
*/

UCON0=0x00000005;/*PCLK  中断模式/查询模式*/
UBRDIV0=26;

/*设置数据格式,设置8位数据位(有12,14,16等位),去相应寄存器修改*/

ULCON0 =0x00000003;			/*8个数据位,无校验位,一个停止位*/

}

int putchar(int c)
{
/UTRSTAT0寄存器用于查看传送buff是否为空/
/UTXH0,它是传送buff寄存器,怎么把内存数据发送给uart,就是靠这个寄存器,也能把接收到的数据读回来/
while(!(UTRSTAT0&(1<<2)));//如果该寄存器不为空,就让他一直执行发送,直到为空执行下面的
UTXH0 = (unsiggned char)c;
}

int getchar(void) //串口接收数据
{
while(!(UTRSTAT0&(1<<0)));//如果接收寄存器为空,就让它死循环
return URXH0;
}

int puts(const char *s)
{
while (*s)
{
putchar(*s);
s++;
}

}

写的main函数如下
#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©;
}
return 0;
}

有不懂的地方咨询我
最后说下start.S文件,因为这个文件我不是自己写的,最后是去调用的,一直不知道这个文件的意义,不过下面这个回答说的很好
start.s is the first program to run after power up. It does some initialization, then jumps to your main function in C language. In the lingo of C language, start.s is called run time library.

The C run time library is usually written in assembly language, and takes the name like crt0.s or startup.s. As its name implies, it needs to set up the run time environment before the code in main() function can be executed, which includes:
(1) Initialize the hardware, such as CPU cache and MMU, if necessary
(2) Setup the Stack
(3) Initialize the .bss section to all zero in the memory
(4) Calling the main() function
For application developers, the compiler would provide a default run time library during link stage, which is good enough in most cases. However, for programs like the bootloader, the developer has to provide its own run time library in order to set up the environment correctly. It will be the first code to run after power reset.

As for your question regarding those “b loop”. It really depends on where you link script puts them. But most likely, your link script will put them into the designated address for IVT (Interrupt Vector Table). So these are just interrupt vectors. As you can see, many vectors are left un-handled. (b loop and endless loop). It is really up to you (or the OS) to set up those interrupt vectors since the main job of start.s is just like a bootloader to bring up the system.**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值