gpt4 book ai didi

c - Atmel AVR ADMUX 寄存器未分配值

转载 作者:行者123 更新时间:2023-11-30 16:22:24 24 4
gpt4 key购买 nike

我有以下枚举和结构:

typedef enum reference{
AREF = 0, //External on AREF pin
AVCC = true << REFS0, //Analogue supply voltage
I11 = true << REFS1, //Internal 1.1V reference
I256 = true << REFS1 | true <<REFS0 //Internal 2.56 reference
} Reference;

typedef enum channel{
ADC0 = 0,
ADC1 = true << MUX0,
ADC2 = true << MUX1,
ADC3 = true << MUX0 | true << MUX1,
ADC4 = true << MUX2,
ADC5 = true << MUX2 | true << MUX0,
ADC6 = true << MUX2 | true << MUX1,
ADC7 = true << MUX2 | true << MUX1 | true << MUX0
//If higher channels are needed continue using MUXn for n = 0-> 4 in a binary count.
} Channel;

typedef enum leftadjust{
LADisabled = 0,
LAEnabled = true << ADLAR
} LeftAdjust;

//Enums for ADCSRA
typedef enum adcenable{
ADCDisabled = 0,
ADCEnabled = true << ADEN
} ADCEnable;

typedef enum adcautotriggerenable{
AutoTriggerDisabled = 0,
AutoTriggerEnabled = true << ADATE
} ADCAutoTriggerEnable;

typedef enum adcinterruptenable{
ADCIntteruptDisabled = 0,
ADCInterruptEnabled = true << ADIE,
} ADCInterruptEnable;

typedef enum adcclockdivider{
CDHalf = 0,
CDQuarter = true << ADPS1,
CDEighth = true << ADPS1 | true << ADPS0,
CDSixteenth = true << ADPS2,
CDThirtySecond = true << ADPS2 | true << ADPS0,
CDSixtyFourth = true << ADPS2 | true << ADPS1,
CDOneTwoEighth = true << ADPS2 | true << ADPS1| true << ADPS0
} ADCClockDivider;

///structs
typedef struct ADCSettings{
Reference ref;
LeftAdjust leftAdjust;
Channel channel;
ADCAutoTriggerEnable autoTrigger;
ADCInterruptEnable interruptEnable;
ADCClockDivider clockDivider;
} ADCSettings;

这个构造函数:

ADCSettings* NewADCSettings()
{
return (ADCSettings*)malloc(sizeof(ADCSettings));
}

然后用这个函数来设置我的 ADC:

void InitialiseADC(ADCSettings* settings)
{
/*
13CCs for a conversion. 50ns/cc therefore 0 clock division conversion time is 13 * 50ns =650ns.
for 50us; 50us/950ns = 77.
We have 16,32,64 and 128, 64 gives a time of 61us which is in range.
*/
ADMUX = (settings->ref | settings ->leftAdjust | settings->channel);
//ADMUX = true << REFS0 | true << MUX1 | true << MUX0; //REFSn sets voltage ref source, MUXn sets channel

DIDR0 = true << ADC3D; //DIDR is digital input disable (makes a pin analogue)
ADCSRA = true << ADEN | settings ->autoTrigger | settings ->interruptEnable | settings ->leftAdjust | settings->clockDivider;
//ADCSRA = true << ADEN | true << ADPS2 | true << ADPS1; //ADPS1 & ADPS2 gives 64 scale

}

正如你所看到的,我正在重构它,尝试使其成为一个很好的库函数,而不是仅仅按照我需要的方式设置它一次。注释掉的行工作得很好。

当我运行这个时,我相信ADMUX应该被分配67,但它取而代之的是64的值,如果我是没有错误的是只是 REFS0 被设置,而 MUX0MUX1 都没有设置。

寄存器是 8 位的,因此它应该能够保存高达 255 的值。

为了完整性,这是我的 main.c:

#include "misc/utilities.h"
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>
#include "USART/USART.h"
#include "ADC/ADC.h"

int main()
{
ADCSettings* settings = NewADCSettings();
settings->autoTrigger = false;
settings->channel=ADC3;
settings->clockDivider=CDSixtyFourth;
settings->interruptEnable=false;
settings->leftAdjust=false;
settings->ref=AVCC;
InitialiseADC(settings);
while (true)
{
int adcReading = GetADCConversion();
Tx_Line(IntToString(adcReading));
_delay_ms(100);
}
}

USART 库和实用程序库之前已经使用过,所以我很确定它们没问题。

有什么想法我哪里出错了吗?

最佳答案

事实证明我的 ADC 设置根本没有问题。

寄存器的问题似乎只是模拟器更新监 window 口中的值的延迟。

我无法获取值的原因是我忘记从库中运行 InitialiseUART() 命令。

这个故事的寓意是,确保初始化所有外围设备。

<小时/>

评论中的其他建议:

  1. 不要在小型 uC 上使用动态内存分配。
  2. 不要依赖枚举的数值,使用#define

关于c - Atmel AVR ADMUX 寄存器未分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54448390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com