gpt4 book ai didi

c - 有人为 NXP LPC17xx ADC 编写过工作代码吗?

转载 作者:行者123 更新时间:2023-11-30 15:46:31 25 4
gpt4 key购买 nike

我正在使用 NXP LPC1788 微 Controller ,并且正在尝试编写代码,让我能够在模拟 channel 0-7 上执行 ADC 测量。我现在的代码是:

uint16_t getADCChannelValue(uint8_t adcChannel)
{
uint16_t adc_value;

ADC_ChannelCmd(LPC_ADC, adcChannel, ENABLE);
ADC_StartCmd(LPC_ADC, ADC_START_NOW);

// Wait for measurement to complete.
while (!(ADC_ChannelGetStatus(LPC_ADC, adcChannel, ADC_DATA_DONE)));

adc_value = ADC_ChannelGetData(LPC_ADC, adcChannel);
ADC_ChannelCmd(LPC_ADC, adcChannel, DISABLE);

// With delay - code works. Without delay - channel 0 is correct,
// channels 1-7 have values close to channel 0 (~2150) (incorrect).
//OS_Delay(1);

return adc_value;
}

随着延迟,代码似乎可以工作,但我不希望出现任意延迟。我已经研究了几个小时的代码,无论出于何种原因,当存在延迟时,就会设置溢出标志(也就是说,当函数输出正确的值时,它会提示溢出)。

我仅向模拟 channel 0 施加电压。这是包含延迟时得到的输出:

Channel 0 = 2151
Channel 1 = 35
Channel 2 = 33
Channel 3 = 34
Channel 4 = 32
Channel 5 = 34
Channel 6 = 32
Channel 7 = 31

当不包含它时:

Channel 0 = 2150
Channel 1 = 2151
Channel 2 = 2151
Channel 3 = 2150
Channel 4 = 2150
Channel 5 = 2150
Channel 6 = 2149
Channel 7 = 2150

有没有人编写过任何代码,可以让我运行所有 ADC channel 并尽快记录它们的值,而不会出现任意延迟?

最佳答案

标签,我认为你应该使用“突发模式”。这是我的 ADC 初始化代码:

void adcHandlerInit()
{
// Clear all bits of the analog pin registers except for the filter disable
// bit.
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_23)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_24)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_25)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_26)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_1 * 32
+ BRD_ADC_CH_PIN_30)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_1 * 32
+ BRD_ADC_CH_PIN_31)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_12)*sizeof(uint32_t)))) = 1 << 8;
*((uint32_t *)(LPC_IOCON_BASE + ((BRD_ADC_CH_PORT_0 * 32
+ BRD_ADC_CH_PIN_13)*sizeof(uint32_t)))) = 1 << 8;

PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_23, BRD_ADC_CH_FUNC_NO_1);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_24, BRD_ADC_CH_FUNC_NO_1);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_25, BRD_ADC_CH_FUNC_NO_1);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_26, BRD_ADC_CH_FUNC_NO_1);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_1, BRD_ADC_CH_PIN_30, BRD_ADC_CH_FUNC_NO_3);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_1, BRD_ADC_CH_PIN_31, BRD_ADC_CH_FUNC_NO_3);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_12, BRD_ADC_CH_FUNC_NO_3);
PINSEL_ConfigPin(BRD_ADC_CH_PORT_0, BRD_ADC_CH_PIN_13, BRD_ADC_CH_FUNC_NO_3);

/* Configuration for ADC :
* ADC conversion rate = 400Khz
*/
ADC_Init(LPC_ADC, 400000);

ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_0, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_1, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_2, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_3, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_4, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_5, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_6, DISABLE);
ADC_IntConfig(LPC_ADC, BRD_ADC_INTR_7, DISABLE);

// Start burst mode.
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_0, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_1, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_2, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_3, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_4, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_5, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_6, ENABLE);
ADC_ChannelCmd(LPC_ADC, ADC_CHANNEL_7, ENABLE);
ADC_StartCmd(LPC_ADC, ADC_START_CONTINUOUS);
ADC_BurstCmd(LPC_ADC, ENABLE);
}

底部的部分很重要。它将使微 Controller 对所有 ADC channel 执行重复测量。之后,您可以通过以下方式获取 channel 电压值:

uint16_t getADCChannelValue(uint8_t adcChannel)
{
return (uint16_t)ADC_ChannelGetData(LPC_ADC, adcChannel);
}

这应该可以帮助你。然而,很高兴能找到一种不使用突发模式来做到这一点的方法,因此任何具有不依赖突发模式的正确答案的人都应该代替我获得接受的答案。

关于c - 有人为 NXP LPC17xx ADC 编写过工作代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18315161/

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