我的目的是采樣外部連接PA1,PA2兩個(gè)模擬信號,以及內(nèi)部16通道的內(nèi)部溫度。
使用注入方式采樣,由外部定時(shí)器事件觸發(fā),然后ADC連續(xù)采樣3個(gè)通道,最后觸發(fā)ADC_IT_JEOC中斷進(jìn)行處理
?
實(shí)驗(yàn)的初始化代碼如下
void ad_init(void) {
??? memset(&ad, 0, sizeof(ad));
??? ADC_InitTypeDef ADC_InitStructure = { 0 };
??? GPIO_InitTypeDef GPIO_InitStructure = { 0 };
??? NVIC_InitTypeDef NVIC_InitStructure = { 0 };
?
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
??? RCC_ADCCLKConfig(RCC_PCLK2_Div8);
?
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
?
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
?
//??? ADC1->CTLR1 = 0x00000180;
//??? ADC1->CTLR2 = 0x00808801;
//??? ADC1->ISQR = 0x00204020;
//??? ADC1->SAMPTR1 = 0x00080000;
//??? ADC1->SAMPTR2 = 0x00000012;
?
??? ADC_DeInit(ADC1);
??? ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
??? ADC_InitStructure.ADC_ScanConvMode = ENABLE;
??? ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
??? ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigInjecConv_T1_TRGO;
??? ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
??? ADC_InitStructure.ADC_NbrOfChannel = 0;
??? ADC_Init(ADC1, &ADC_InitStructure);
?
??? ADC_InjectedDiscModeCmd(ADC1, DISABLE);
??? ADC_ExternalTrigInjectedConvCmd(ADC1, ENABLE);
?
??? ADC_InjectedSequencerLengthConfig(ADC1, 3);
??? ADC_InjectedChannelConfig(ADC1, ADC_Channel_1, 0,ADC_SampleTime_28Cycles5);
??? ADC_InjectedChannelConfig(ADC1, ADC_Channel_2, 1,ADC_SampleTime_28Cycles5);
??? ADC_InjectedChannelConfig(ADC1, ADC_Channel_16, 2,ADC_SampleTime_28Cycles5);
??? ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
?
??? ADC_Cmd(ADC1, ENABLE);
??? Delay_Us(50);
??? ADC_TempSensorVrefintCmd(ENABLE);
??? ADC_ResetCalibration(ADC1);
??? while(ADC_GetResetCalibrationStatus(ADC1));
??? ADC_StartCalibration(ADC1);
??? while(ADC_GetCalibrationStatus(ADC1));
??? Calibrattion_Val = Get_CalibrationValue(ADC1);
?
??? NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
??? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
??? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
??? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
??? NVIC_Init(&NVIC_InitStructure);
?
}
void timer_init(void) {
??? TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = { 0 };
//定時(shí)器1的刷新時(shí)間觸發(fā)AD采樣
??? RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE);
??? TIM_TimeBaseInitStructure.TIM_Period = 56250; //=72000000/12800
??? TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //不分頻
??? TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4;
??? TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
??? TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
??? TIM_CtrlPWMOutputs(TIM1, ENABLE);
??? TIM_ARRPreloadConfig(TIM1, ENABLE);
??? TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
TIM_Cmd(TIM1, ENABLE);
}
?
首先發(fā)現(xiàn)了一個(gè)bug
ADC_InjectedChannelConfig函數(shù)的描述內(nèi)rank的解釋有問題他的取值范圍說明是1~4,但實(shí)測是0~3.
?
在中斷內(nèi)讀取ADC1->IDATAR1~ADC1->IDATAR3,發(fā)現(xiàn)他們的值居然不隨外部的電壓變化,經(jīng)使用demo測試判斷硬件是沒有問題的。
這個(gè)問題已經(jīng)折騰2天,目前沒有任何進(jìn)展,望技術(shù)支持
?