简体   繁体   English

读取和比较32位架构中的10位寄存器

[英]Reading & comparing 10-bit register in 32-bit architecture

I am currently C programming 10-bit ADC inside 32-bit ARM9 based microcontroller. 我目前正在基于32位ARM9的微控制器内编程10位ADC。 This 10-bit ADC is saving digitalised analog value in 10 bit register named "ADC_DATA_REG" that uses bits 9-0 (LSB). 该10位ADC将数字化模拟值保存在名为“ADC_DATA_REG”的10位寄存器中 ,该寄存器使用位9-0(LSB)。 I have to read this register's value and compare it to a 32 bit constant named "CONST" . 我必须读取该寄存器的值并将其与名为“CONST”32位常量进行比较。 My attempt looked like this, but it isn't working. 我的尝试看起来像这样,但它不起作用。 What am i missing here? 我在这里失踪了什么? Should i use shift operations? 我应该使用轮班操作吗? This is my frst time dealing with this so any example will be welcomed. 这是我第一次处理这个问题,所以任何例子都会受到欢迎。

The below code has been edited regarding coments and anwsers and is still not working. 以下代码已针对coments和anwsers进行了编辑,但仍然无效。 I allso added a while statement which checks if ADC_INT_STATUS flag is raized before reading ADC_DATA_REG. 我还添加了一个while语句,用于在读取ADC_DATA_REG之前检查ADC_INT_STATUS标志是否已升级。 The flag mentioned indicates an interrupt which is pending as soon as ADC finishes conversion and data is ready to read from ADC_DATA_REG. 上述标志表示一旦ADC完成转换并且数据准备好从ADC_DATA_REG读取,就会挂起一个中断。 It turns out data remains 0 even after assigning value of register ADC_DATA_REG to it, so that is why my LED is always on. 事实证明,即使将寄存器ADC_DATA_REG的值分配给它,数据仍保持为0,因此这就是我的LED始终打开的原因。 And it allso means i got an interrupt and there should be data in ADC_DATA_REG, instead it seems there isnt... 它也意味着我得到了一个中断,ADC_DATA_REG中应该有数据,相反它似乎没有......

#define CONST 0x1FF
unsigned int data = 0;

while (!(ADC_INT_STATUS_REG & ADC_INT_STATUS))
data = ADC_DATA_REG; 

if ((data & 0x3FF)> CONST){ 
   //code to turn off the LED 
} 
else{ 
   //code to turn on the LED 
}

You dont write how ADC_DATA_REG fetches the 10-bit value. 你不会写ADC_DATA_REG如何获取10位值。 But I assume that it is only a read to some IO-address. 但我认为它只是对某个IO地址的读取。 In this case the read from the address return 32 bits. 在这种情况下,从地址读取返回32位。 In your case only the lower 10 are valid (or interresting). 在您的情况下,只有较低的10有效(或有效)。 The other 22 bit can be anything (eg status bits, rubbish, ...), so before you proceed with the data, you should zero the first 22 bits. 另外22位可以是任何东西(例如状态位,垃圾,......),因此在继续处理数据之前,应该将前22位置零。

In case the 10-bit value is signed, you should also perform a sign extension and correct your datatype (I know the port IO is unsigned, but maybe the 10-bit value the adc returns isnt). 如果10位值已签名,您还应执行符号扩展并更正您的数据类型(我知道端口IO是无符号的,但可能是adc返回的10位值不是)。 Then your comparison should work. 然后你的比较应该工作。

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

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