基于 STM8MC-KIT v1.0 库;
问题:使用ADC1通道3,5,测试发现通道5使用正常,通道3随通道5电压改变,示波器测量通道3的电压为固定300mV值;为什么通道3会随通道5改变呢?通道3非悬空状态,实际上是经过电阻分压测量输出电源电压用的。上代码
初始化:
- void Init_ADC( void )
- {
- u8 value;
- u16 ADC_TDR_tmp;
- //ADC_Sync_State = ADC_USER_SYNC_INIT;
- ADC_Sync_State = ADC_CURRENT_INIT;
- ADC_State = ADC_SYNC;
- ADC1->CSR = 0;
-
- //select 4MHz clock based on 16MHz fMaster (div4), single mode
- // 6MHz clock based on 24MHz fMaster (div4)
- ADC1->CR1 = BIT5|BIT6;
- //select internal trigger TIM1 TRGO
- ADC1->CR2 = 0;
- //select phase input
- // ADC1->CSR = PHASE_C_BEMF_ADC_CHAN;
- ADC1->CSR = ADC_USER_ASYNC_CHANNEL;
- ADC_TDR_tmp = 0;
- // ADC_TDR_tmp |= (u16)(1) << PHASE_A_BEMF_ADC_CHAN;
- // ADC_TDR_tmp |= (u16)(1) << PHASE_B_BEMF_ADC_CHAN;
- // ADC_TDR_tmp |= (u16)(1) << PHASE_C_BEMF_ADC_CHAN;
- ADC_TDR_tmp |= (u16)(1) << ADC_CURRENT_CHANNEL;
- // ADC_TDR_tmp |= (u16)(1) << ADC_USER_SYNC_CHANNEL;
- ADC_TDR_tmp |= (u16)(1) << ADC_BUS_CHANNEL;
- // ADC_TDR_tmp |= (u16)(1) << ADC_NEUTRAL_POINT_CHANNEL;
- // ADC_TDR_tmp |= (u16)(1) << ADC_TEMP_CHANNEL;
- ADC_TDR_tmp |= (u16)(1) << ADC_USER_ASYNC_CHANNEL;
- ToCMPxH( ADC1->TDRH, ADC_TDR_tmp);
- ToCMPxL( ADC1->TDRL, ADC_TDR_tmp);
-
- //enable trigger
- ADC1->CR2 |= BIT6;
- //enable ADC
- ADC1->CR1 |= BIT0;
- //allow ADC to stabilize
- value=30;
- while(value--);
- //clear interrupt flag
- ADC1->CSR &= (u8)(~BIT7);
- ADC1->CSR |= BIT5;
- }
复制代码
中断:
- #ifdef HALL
- @near @interrupt @svlreg void ADC2_IRQHandler (void)
- {
- u16 data;
-
- data = ADC1->DRH;
- data <<= 2;
- data |= (ADC1->DRL & 0x03);
- //clear interrupt flag
- ADC1->CSR &= (u8)(~BIT7);
- //ADC_Async_State = ADC_USER_ASYNC_SAMPLE;
- // Manage async sampling
- switch (ADC_Async_State)
- {
- default:
- case ADC_BUS_SAMPLE:
- ADC_Buffer[ ADC_BUS_INDEX ] = data;
- //ADC_Async_State = ADC_TEMP_INIT;
- ADC_Async_State = ADC_USER_ASYNC_INIT;
- break;
- case ADC_TEMP_SAMPLE:
- ADC_Buffer[ ADC_TEMP_INDEX ] = data;
- ADC_Async_State = ADC_NEUTRAL_POINT_INIT;
- break;
- case ADC_NEUTRAL_POINT_SAMPLE:
- ADC_Buffer[ ADC_NEUTRAL_POINT_INDEX ] = data;
- ADC_Async_State = ADC_USER_ASYNC_INIT;
- break;
- case ADC_USER_ASYNC_SAMPLE:
- ADC_Buffer[ ADC_USER_ASYNC_INDEX ] = data;
- ADC_Async_State = ADC_BUS_INIT;
- break;
- }
- // Set the Async sampling channel
- switch (ADC_Async_State)
- {
- default:
- case ADC_BUS_INIT:
- ADC1->CSR = (ADC_BUS_CHANNEL|BIT5);
- ADC_Async_State = ADC_BUS_SAMPLE;
- break;
-
- case ADC_TEMP_INIT:
- ADC1->CSR = (ADC_TEMP_CHANNEL|BIT5);
- ADC_Async_State = ADC_TEMP_SAMPLE;
- break;
- case ADC_NEUTRAL_POINT_INIT:
- ADC1->CSR = (ADC_NEUTRAL_POINT_CHANNEL|BIT5);
- ADC_Async_State = ADC_NEUTRAL_POINT_SAMPLE;
- break;
-
- case ADC_USER_ASYNC_INIT:
- ADC1->CSR = (ADC_USER_ASYNC_CHANNEL|BIT5);
- ADC_Async_State = ADC_USER_ASYNC_SAMPLE;
- break;
- }
- // Configure syncronous sampling
- #ifdef DEV_CUT_1
- // Enable ext. trigger
- ADC1->CR2 |= BIT6;
- //turn on ADC fix bug on cut1 device
- ADC1->CR1 |= BIT0;
- #else
- // Enable ext. trigger
- ADC1->CR2 |= BIT6;
- #endif
- }
- #endif
复制代码
|
谢谢