ATMEGA32 UART通讯 [英] ATMEGA32 UART Communication

查看:222
本文介绍了ATMEGA32 UART通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ATMEGA32中进行串行通信,并且我有一个问题:

I am trying to do serial communication in ATMEGA32 and I have a question:

在异步串行通信中,UBRRHUCSRC寄存器都位于同一位置.我不知道该位置的哪些条件将充当UBRRH,对于哪些条件,它将充当UCSRC.根据分配给这些寄存器的工作,我需要为每个寄存器使用不同的值

In asynchronous serial communication both UBRRH and UCSRC registers have same location. I don't know which conditions that location will act as UBRRH and for which conditions, it will act as UCSRC. I need different values for each register according to the work assigned to those registers

在数据表中,他们提到在两个寄存器之间使用URSEL位进行选择,但不知何故.

In the datasheet, they have mentioned the use of URSEL bit for selection betweem two registers but somehow I am not getting that.

推荐答案

答案是:是的URSEL位.根据数据表:

The answer is: Yes, the URSEL bit. According to the datasheet:

对此I/O位置进行写访问时, 写入的值(USART寄存器选择(URSEL)位)控制 将要写入的两个寄存器之一.如果在此期间URSEL为零 进行写操作时,UBRRH值将被更新.如果URSEL为1, UCSRC设置将被更新.

When doing a write access of this I/O location, the high bit of the value written, the USART Register Select (URSEL) bit, controls which one of the two registers that will be written. If URSEL is zero during a write operation, the UBRRH value will be updated. If URSEL is one, the UCSRC setting will be updated.

这意味着,当您写入UCSRC时,无论您要在其中写入什么值,都要同时设置URSEL位(确保URSEL1):

This means, when you write to UCSRC, regardless of what value you want to put there, also set the URSEL bit (make sure that URSEL is 1):

UCSRC = (1<<URSEL)| ... whatever else ...

写入UBRRH时,请确保URSEL位必须为零.这是一些不同的方法:

When you write to UBRRH, make sure that URSEL bit must is zero. Here are some different ways of doing that:

UBRRH = (0<<URSEL)| ... whatever else ...  // just showing that URSEL isn't set
UBRRH = ...some value...                   // simple not setting URSEL
UBRRH = (someValue)&(~(1<<URSEL)           // Ensuring that URSEL isn't set

URSEL位只是高位.因此,无论您写入UCSRC的哪个值,都将其高位(位7)设置(打开,使1).并且在写入UBRRH时,请确保清除了位7.另一种思考的方式是,您写入UBRRH的每个值都必须小于128.要将您要写入UCSRC的每个值都加上128:它将打开位7.一种解释方式,上面的代码更加清晰.

URSEL bit is just a high bit. So whatever value you write to UCSRC, set (turn on, make 1) the high bit (bit 7). And when writing to UBRRH, make sure that bit 7 is cleared. Another way of thinking about it, every value you write to UBRRH must be under 128. And every value that you want to write to UCSRC, add 128 to it: this will turn on bit 7. This is just as a way of explanation, the code above is clearer.

这是怎么做的?我不知道,我不是uC设计师.似乎有可能将相同的IO位置映射到处理器中的两个不同的寄存器.假设您有一个名为foo的寄存器,当您向其中写入一个值时,uC会检查高位是否已设置.如果是,则将值写入内部存储器位置1;如果不是,则将值写入内部存储器位置2.

How is this done? I don't know, I am not a uC designer. What seems likely is that the same IO location location is mapped to two different registers in the processor. Say you have a register named foo, and when you write a value to it the uC checks if the high bit is set. If it is it writes the value to internal memory location 1 and if it isn't it writes the value to internal memory location 2.

如果正确使用了URSEL位,则表示正在正确写入值.您的测试未显示正确的值,因为您没有正确阅读它们.数据表的第162页:

If you are using the URSEL bit correctly, then the values are being written correctly. Your testing not showing the correct values because you are not reading them propertly. Page 162 of the datasheet:

对UBRRH或UCSRC寄存器进行读访问更是如此 操作复杂.但是,在大多数应用中,很少 读取这些寄存器中的任何一个都是必需的.

Doing a read access to the UBRRH or the UCSRC Register is a more complex operation. How- ever, in most applications, it is rarely necessary to read any of these registers.

读取访问由定时序列控制.读取I/O 位置一旦返回UBRRH寄存器的内容.如果注册 在先前的系统时钟周期中读取了位置,并读取了寄存器 在当前时钟周期中,将返回UCSRC内容.注意 读取UCSRC的定时序列是原子操作. 因此必须控制中断(例如,通过禁用 读取操作期间全局中断.

The read access is controlled by a timed sequence. Reading the I/O location once returns the UBRRH Register contents. If the register location was read in previous system clock cycle, reading the register in the current clock cycle will return the UCSRC contents. Note that the timed sequence for reading the UCSRC is an atomic operation. Interrupts must therefore be controlled (for example by disabling interrupts globally) during the read operation.

因此,当您第一次读取UBRRH/UCSRC时,您会得到UBRRH.如果您立即再次阅读,则阅读UCSRC.但是,正如文档所建议的那样,没有真正的理由来读取这些寄存器.似乎您不信任数据表,但这是一个错误:数据表是有关此类问题的最佳信息来源:没有数据表,我们将无处可寻.

So when your read UBRRH / UCSRC for the first time you get UBRRH. If you immediately read again you read UCSRC. But as the documentation suggests, there is no real reason to read these registers. It seems that you do not trust the datasheet, but this is a mistake: the datasheet is the best source of information about such matters: without datasheets we would be nowhere.

这篇关于ATMEGA32 UART通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆