简体   繁体   English

自由运行模式下AVR ADC中的ADIE位

[英]ADIE bit in AVR ADC in Free running mode

I have been writing my own "library" to handle different inits on an AVR chip. 我一直在写自己的“库”来处理AVR芯片上的不同init。 However, I am stuck on the action of the ADIE bit in the ADCSRA register (p.261 in the datasheet). 但是,我对ADCSRA寄存器(数据表中的第261页)中ADIE位的操作感到困惑。 The datasheet doesn't really explain how ADIE works. 数据表并没有真正解释ADIE的工作原理。 However, I haven't been able to get a reading out of the ADC whenever ADIE is set to 0. Why is that? 但是,每当ADIE设置为0时,我都无法读取ADC的读数。为什么? I thought that the ADC worked like the timers- even if the interrupt isn't enabled, it would still be updating the ADC registers with readings. 我以为ADC就像定时器一样工作-即使未启用中断,它仍会使用读数来更新ADC寄存器。

This is the culprit code, whenever "interrupt" is set to anything but 1, the ADC doesn't work. 这是罪魁祸首的代码,只要将“ interrupt”设置为除1以外的任何值,ADC均不起作用。 Interestingly, unlike other interrupts, if there is no code within the ISR(ADC_vect) routine, the chip doesn't get stuck. 有趣的是,与其他中断不同,如果ISR(ADC_vect)例程中没有代码,则芯片不会卡住。

if(interrupt){ADCSRA|=1<<ADIE;}//enable the ADC conversion complete interrupt
else{ADCSRA&=~1<<ADIE;}

This code is missing some parentheses, so it doesn't do what you think it does. 该代码缺少一些括号,因此它没有执行您认为的操作。 It looks like it may be doing the right thing by good luck. 看来运气不错,这可能是对的。

if(interrupt){ADCSRA|=1<<ADIE;}//enable the ADC conversion complete
interrupt else{ADCSRA&=~1<<ADIE;}


~1<<ADIE != ~(1<<ADIE)

The datasheet seems pretty explicit: 数据表似乎非常明确:

ADCSRA Bit 3 – ADIE: ADC Interrupt Enable ADCSRA位3 – ADIE:ADC中断使能

When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete Interrupt is activated. 当该位被写为1且SREG中的I位被置1时,ADC转换完成中断被激活。

This means that if bit 4 ADIF (flag) becomes 1, then the ADC interrupt vector will run (and clear the flag). 这意味着,如果第4位ADIF(标志)变为1,则ADC中断向量​​将运行(并清除标志)。

But if you are not going to use the interrupt, you won't need to use this. 但是,如果您不打算使用该中断,则无需使用此中断。 If you do set it to 1, and there is no interrupt registered, then the device will just reset. 如果确实将其设置为1,并且没有注册中断,则设备将仅复位。

Maybe you need more explanation on how ADC works. 也许您需要更多有关ADC工作原理的解释。

You start a conversion by writing to bit 6 of ADCSRA. 通过写入ADCSRA的第6位开始转换。 The conversion takes some time, and you can tell when it is ready by polling bit 4, if you don't want to use the interrupt. 转换需要一些时间,如果您不想使用中断,则可以通过轮询第4位来判断何时准备就绪。 When it becomes 1, read the result from ADCL and ADCH. 变为1时,从ADCL和ADCH读取结果。 Make sure to clear the flag by writing a 1 to bit 4. 确保通过向位4写入1来清除标志。

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

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