现需要对两路信号(电压和电流)进行采集,计算功率因数。想设置为每次定时中断触发ad采样,ad采连续采样两个通道后停止。请问怎么设置。
void ADC_Configuration(void)
{
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立转换
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //单通道
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // 外部触发禁止
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //转换通道数
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_41Cycles5);
}
void AD_SJ(void)
{
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC软件转换启动功能
//AD_value=ADC_GetConversionValue(ADC1);//获取转换结果
AD_value[m]=ADC_GetConversionValue(ADC1);//获取转换结果
}
void AD_CY(void)
{
TIM_Cmd(TIM2, ENABLE);//启动定时器
while(n!=4096) //等待256*16次中断
{
if(AD_FLAG==1)
{
AD_SJ(); //开始数据采集
sum=+ AD_value[m];//16次和累加
if(m==16)
{
sum = sum >> 2; //右移两位
AD_DATA[k]=sum;//数据为14位精度
m=0;
}
AD_FLAG=0;//标志位清零
}
}
TIM_Cmd(TIM2, DISABLE); //关闭定时器
n=0;
}
/*******************************************************************************
* Function Name : TIM2_Configuration
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure ;
TIM_DeInit( TIM2);//复位TIM2定时器
/* TIM2 configuration */
TIM_TimeBaseStructure.TIM_Period = 351; //定时值 351/ 72M = 4.875us
TIM_TimeBaseStructure.TIM_Prescaler = 0; //不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM1参数初始化TIMx
/* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter [允许tim2计数]*/
//TIM_Cmd(TIM2, ENABLE);
} |