|
本人最近在學(xué)習(xí)使用TIM來觸發(fā)ADC采樣,使用的是TIM1_CC4事件來觸發(fā)ADC1通道4的采樣,TIM_CH4通道對(duì)應(yīng)的是PA11引腳,ADC1_CH4對(duì)應(yīng)的是PA4引腳,由變量DC_ADC來存儲(chǔ)ADC1通道4的采樣值。使用ST-Link進(jìn)行仿真,由示波器測(cè)量得到TIM1_CH4的輸出波形是50%占空比的方波,符合預(yù)期,說明TIM1的輸出信號(hào)(即ADC1的觸發(fā)信號(hào))是沒有問題的。但是ADC1_DR寄存器存放的值一直是校準(zhǔn)完成后校準(zhǔn)因子的值,且程序一直運(yùn)行在主函數(shù)的等待EOC轉(zhuǎn)換完成標(biāo)志循環(huán)內(nèi),說明ADC1并沒有被觸發(fā),求高人指點(diǎn)這是哪里出了問題,萬分感謝!
程序如下:
#include "main.h"
uint32_t DC_ADC = 0;
void MyGPIO_Init(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); //開啟GPIO時(shí)鐘
GPIO_InitTypeDef GPIO_InitStruct1;
GPIO_InitTypeDef GPIO_InitStruct2;
GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_AN; //設(shè)置PA4為模擬輸入
GPIO_InitStruct1.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct1);
GPIO_InitStruct2.GPIO_Mode = GPIO_Mode_AF; //設(shè)置PA11為復(fù)用功能
GPIO_InitStruct2.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct2.GPIO_OType = GPIO_OType_PP; //設(shè)置輸出為推挽輸出
GPIO_InitStruct2.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStruct2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_2); //PC6設(shè)置為復(fù)用功能AF2(TIM1)
}
void MyADC1_Init(void)
{
/*==================1.開啟ADC1時(shí)鐘===============*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
/*==================2.復(fù)位ADC1===============*/
ADC_DeInit(ADC1)
/*==================3.ADC1初始化===============*/
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b ; //分辨率12位
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC4; //外部觸發(fā)源為TIM1_CC4
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; //不開啟連續(xù)轉(zhuǎn)換模式
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; //向前掃描
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //外部觸發(fā)為上升沿
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; //數(shù)據(jù)對(duì)齊模式:右對(duì)齊
ADC_Init(ADC1,&ADC_InitStruct);
ADC_ChannelConfig(ADC1,ADC_Channel_4,ADC_SampleTime_239_5Cycles); //設(shè)置通道4和采樣周期
/*==================4.校準(zhǔn)并使能ADC1===============*/
ADC_GetCalibrationFactor(ADC1); //ADC打開校準(zhǔn)并等待完成
ADC_Cmd(ADC1, ENABLE); //開啟ADC
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); //等待ADC準(zhǔn)備完成
}
void MyTIM1_Init(uint16_t arr,uint16_t psc)
{
/*==============1.打開TIM1時(shí)鐘=============*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
/*==============2.初始化TIM1=============*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
TIM_TimeBaseInitStruct.TIM_Period = arr - 1;
TIM_TimeBaseInitStruct.TIM_Prescaler = psc -1 ;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct);
/*==============3.配置TIM1 PWM1模式=============*/
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //設(shè)置輸出模式:PWM1模式
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //設(shè)置比較輸出使能:
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //設(shè)置ref為有效電平時(shí) 輸出高電平
TIM_OCInitStruct.TIM_Pulse = arr/2; //輸出占空比:50%的PWM波
TIM_OC4Init(TIM1,&TIM_OCInitStruct); //通道4輸出
TIM_CtrlPWMOutputs(TIM1,ENABLE); //PWM輸出使能
TIM_Cmd(TIM1,ENABLE); //使能定時(shí)器 輸出上升沿時(shí)觸發(fā)ADC采樣
int main()
{
MyGPIO_Init();
MyADC1_Init();
MyTIM1_Init(10000,8);
while(1)
{
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET) ; //等待轉(zhuǎn)換完成標(biāo)志
DC_ADC = ADC_GetConversionValue(ADC1)*3300/0xfff ; //由ADC采樣轉(zhuǎn)換值計(jì)算出真實(shí)電壓
}
}
|
|