Atmel AVR ADMUX寄存器未取指定值

时间:2019-01-30 19:45:45

标签: c avr cpu-registers atmel adc

我有以下枚举和结构:

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的值,如果我没记错的话,它只是{{1} }设置,而REFS0MUX0都没有。

该寄存器是8位的,因此它应该能够保存最多255个值。

出于完整性考虑,这是我的MUX1

main.c

之前已经使用过USART库和实用程序库,所以我可以肯定它们还可以。

有什么想法我要去哪里吗?

1 个答案:

答案 0 :(得分:0)

结果证明我的ADC设置完全没有问题。

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

之所以无法获得价值,是因为我忘记了从库中运行InitialiseUART()命令。

故事的寓意,请确保您初始化了所有外围设备。


评论中的其他建议:

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