|
在你的代碼中,你只啟動(dòng)并讀取了一個(gè)ADC通道(hadc1),然后兩次打印出這個(gè)通道的值。所以,你看到的兩個(gè)數(shù)值是一樣的,因?yàn)樗鼈兌紒碜酝粋(gè)ADC通道。
如果你想從兩個(gè)不同的ADC通道獲取值,你需要分別啟動(dòng)和讀取每個(gè)通道。例如,假設(shè)你有兩個(gè)ADC通道:hadc1和hadc2,你可以這樣修改你的代碼:
c
int main(void)
{
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
printf("this is adc\n");
while (1)
{
// Start ADC for channel 1
HAL_ADC_Start(&hadc1);
while((hadc1.Instance->DR)==RESET);
temp1=HAL_ADC_GetValue(&hadc1);
printf("light adc=%d\n",temp1);
// Start ADC for channel 2
HAL_ADC_Start(&hadc2); // Assuming you have hadc2 for the second channel
while((hadc2.Instance->DR)==RESET);
temp2=HAL_ADC_GetValue(&hadc2);
printf("ht adc2=%d\n",temp2);
HAL_Delay(1000);
}
}
請(qǐng)注意,你需要確保你已經(jīng)正確地初始化了第二個(gè)ADC通道(在這個(gè)例子中是hadc2)。此外,我使用了兩個(gè)不同的變量(temp1和temp2)來存儲(chǔ)每個(gè)通道的值,以避免混淆。 |
|