简体   繁体   English

我可以将两个端口的位相加以形成新的位序列吗?

[英]Can I add the bits of two ports to make a new bit sequence?

In my lab I'm supposed to test a 9 bit value but each port only contains 8 bits. 在我的实验室中,我应该测试9位值,但是每个端口仅包含8位。 According to the instructions, I use all bits on PORTD including PB0 to make this 9 bit value, but I have no plan on how I can do that and that's essentially the whole challenge of this problem. 根据说明,我使用PORTD上的所有位(包括PB0)来设置这9位值,但是我没有计划如何做到这一点,这实质上就是此问题的全部挑战。 The rest of it should be easy, I only need to store a 9 bit value "borrowing" a bit from another port. 其余的应该很容易,我只需要存储一个9位值即可从另一个端口“借用”。

Microcontroller: ATmega1284 微控制器:ATmega1284

Problem: (Challenge): A car's passenger-seat weight sensor outputs a 9-bit value (ranging from 0 to 511) and connects to input PD7..PD0PB0 on the microcontroller. 问题:(挑战):汽车的乘客座位重量传感器输出9位值(范围从0到511),并连接到微控制器上的输入PD7..PD0PB0。 If the weight is equal to or above 70 pounds, the airbag should be enabled by setting PB1 to 1. If the weight is above 5 but below 70, the airbag should be disabled and an "Airbag disabled" icon should light by setting PB2 to 1. (Neither B1 nor B2 should be set if the weight is 5 or less, as there is no passenger). 如果重量等于或大于70磅,则应通过将PB1设置为1来启用安全气囊。如果重量大于5但低于70磅,则应禁用安全气囊,并且将PB2设置为可以点亮“安全气囊禁用”图标。 1.(如果重量不超过5,则B1和B2都不应设置,因为没有乘客)。

Bits from separate registers can be combined in any way necessary using the bit-wise operators: 可以使用按位运算符以任何必要的方式组合来自单独寄存器的位:

&  – Bitwise AND
|  – Bitwise OR
~  – Bitwise NOT
^  – Bitwise XOR
<< – Left Shift
>> – Right Shift  

The somewhat vague notation " PD7..PD0PB0 " suggests that PB0 is the LSB or the weight value. 有点模糊的符号“ PD7..PD0PB0 ”表明PB0是LSB或重量值。 In that case, given: 在这种情况下,给出:

PORTD DDDDDDDD
PORTB xxxxxxxB

then: 然后:

uint16_t weight = ((uint16_t)PORTD << 1u) | (PORTB & 0x01) ;

will result in a value for weight composed of bits: 将产生一个由位组成的weight值:

0000000DDDDDDDDB

The sub-expression ((uint16_t)PORTD << 1u) shifts the value of PORTD left by 1 bit: 子表达式((uint16_t)PORTD << 1u)将PORTD的值左移1位:

0000000DDDDDDDD0

(PORTB & 0x01) zeros all but bit-0, leaving bit-0 unchanged: (PORTB & 0x01) 0位以外的所有位清零,而使第0位保持不变:

        0000000B

The two sub-expressions are then bit-wise OR'ed: 然后将两个子表达式按位进行“或”运算:

   0000000DDDDDDDD0
OR         0000000B
   ----------------
 = 0000000DDDDDDDDB

Note that it might be possible (safety considering) to simplify the task by using just the 8 bit value in PORTD and halving the limit values. 请注意,通过仅使用PORTD中的8位值并将限制值减半,可能(出于安全考虑)可以简化任务。 Currently you have: "above 5 but below 70" , if you just read PORTD, the limits would be 3 >= PORTD < 35 . 当前,您具有: “高于5但低于70” ,如果您仅阅读PORTD,则限制为3> = PORTD <35 This may in fact be safer - when reading a single value from two separate registers you need to be certain the values you have are for the same sample, and that the value in the first register read did not change before you read the second. 实际上,这可能更安全-从两个单独的寄存器读取单个值时,您需要确定您拥有的值与同一样本相同,并且在读取第二个寄存器之前读取的第一个寄存器中的值没有改变。 With only the one LSB in the PORTB register this will have little impact, but also makes it of limited value unless the data is somehow latched while it is read. PORTB寄存器中只有一个LSB​​时,这几乎没有影响,但是除非在读取数据时以某种方式锁存了数据,否则它的作用将是有限的。

It depends on if PB0 is the MSB or LSB of register B. But what you cand do is using uint16_t variable and some masks to get the value you need. 这取决于PB0是寄存器B的MSB还是LSB。但是您可以使用uint16_t变量和一些掩码来获取所需的值。 For example, you could do one of the following things. 例如,您可以做以下事情之一。

PB0 is MSB PB0是MSB

uint16_t sensor;
sensor = (PORTD << 1) | (PORTB >> 7);

PB0 is LSB PB0是LSB

uint16_t sensor;
sensor = (PORTD << 1 ) | (PORTB & 0x01);

It can be done in many ways, and it is just one of them. 它可以通过多种方式完成,并且只是其中之一。 In both solutions, all bits of register D are the most significant bits, and LSB, in the first one, would be the MSB of register B, and in the second one, would be the LSB of register B. 在这两种解决方案中,寄存器D的所有位都是最高有效位,第一个是LSB,它是寄存器B的MSB;第二个是LSB,它是寄存器B的LSB。

If you want your code is MISRA-C compliant you should initialize sensor variable. 如果您希望代码符合MISRA-C标准,则应初始化传感器变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM