微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM8 MCKIT1.0 BLDC无感控制中的采集实现分析

STM8 MCKIT1.0 BLDC无感控制中的采集实现分析

时间:11-28 来源:互联网 点击:
ST写的STM8电机库中,可以用无感方式驱动BLDC。STM8芯片只有一个AD转换器,而且是8位机。库中要对反

电势采集,比较处理,还要进行母线电压,母线电流、散热片温度、电位器等模拟信号进行采集、运算和处理。

这优先级处理说起来是很重要的。不过,ST把程序写的很好,至少我这样认为。它把不同的任务放到准确的时间段里进行采集处理。
先把它核心AD采集处理帖出来再分析 。

#ifdef SENSORLESS
@near @interrupt @svlreg void ADC2_IRQHandler (void)
{
if (ADC_State == ADC_SYNC)
{
// Syncronous sampling

u16 data;
u8 delay;
u16 bemf_threshold;

// Reset bit
bComHanderEnable = 0;

//clear interrupt flag
ADC2->CSR &= (u8)(~BIT7);

//left align - read DRH first
data = ADC2->DRH;
data <= 2;
data |= (ADC2->DRL & 0x03);

switch( ADC_Sync_State )
{
case ADC_BEMF_INIT:
ADC2->CSR = (u8)((Current_BEMF_Channel|BIT5));
BEMF_Sample_Debounce = 0;
Zero_Sample_Count = 0;
ADC_Sync_State = ADC_BEMF_SAMPLE;
SetSamplingPoint_BEMF();
break;

case ADC_BEMF_SAMPLE:
//detect zero crossing
if( Current_BEMF == BEMF_FALLING )
{
if( Z_Detection_Type == Z_DETECT_PWM_OFF )
{
bemf_threshold = BEMF_FALLING_THRESHOLD;
}
else
{
bemf_threshold = hNeutralPoint;
}

if (Ramp_Step > FORCED_STATUP_STEPS)
{
if( data bemf_threshold)
{
Zero_Sample_Count++;
BEMF_Sample_Debounce++;
if( BEMF_Sample_Debounce >= BEMF_SAMPLE_COUNT )
{
hTim3Th -= hTim3Cnt;
GetStepTime();

SpeedMeasurement();

bComHanderEnable = 1;

BEMF_Sample_Debounce = 0;
}
}
else
{
BEMF_Sample_Debounce = 0;
}
}
}
else
{
if( Z_Detection_Type == Z_DETECT_PWM_OFF )
{
bemf_threshold = BEMF_RISING_THRESHOLD;
}
else
{
bemf_threshold = hNeutralPoint;
}

if (Ramp_Step > FORCED_STATUP_STEPS)
{
if( data > bemf_threshold )
{
Zero_Sample_Count++;
BEMF_Sample_Debounce++;
if( BEMF_Sample_Debounce >= BEMF_SAMPLE_COUNT )
{
hTim3Th -= hTim3Cnt;
GetStepTime();

SpeedMeasurement();

bComHanderEnable = 1;

BEMF_Sample_Debounce = 0;
}
}
else
{
BEMF_Sample_Debounce = 0;
}
}
}
break;

case ADC_CURRENT_INIT:
ADC2->CSR = (ADC_CURRENT_CHANNEL|BIT5);
ADC_Sync_State = ADC_CURRENT_SAMPLE;
SetSamplingPoint_Current();
break;

default:
case ADC_AVCURRENT_INIT:
ADC2->CSR = (ADC_AVCURRENT_CHANNEL|BIT5);
ADC_Sync_State = ADC_AVCURRENT_CHANNEL;// ADC_USER_SYNC_SAMPLE;
SetSamplingPoint_AVCURRENT();
break;


case ADC_CURRENT_SAMPLE:
ADC_Buffer[ ADC_CURRENT_INDEX ] = data;
break;

case ADC_AVCURRENT_SAMPLE:
ADC_Buffer[ ADC_AVCURRENT_INDEX] = data;
break;
}

// Store the current channel selected
bCSR_Tmp = ADC2->CSR;

// Set the Async sampling channel
switch (ADC_Async_State)
{
default:
case ADC_BUS_INIT:
ADC2->CSR = (ADC_BUS_CHANNEL|BIT5);
ADC_Async_State = ADC_BUS_SAMPLE;
break;

case ADC_TEMP_INIT:
ADC2->CSR = (ADC_TEMP_CHANNEL|BIT5);
ADC_Async_State = ADC_TEMP_SAMPLE;
break;

case ADC_USER_ASYNC_INIT:
ADC2->CSR = (ADC_USER_ASYNC_CHANNEL|BIT5);
ADC_Async_State = ADC_USER_ASYNC_SAMPLE;
break;
}

// Disable ext. trigger
ADC2->CR2 &= (u8)(~BIT6);
//Start ADC sample
ADC2->CR1 |= BIT0;

ADC_State = ADC_ASYNC;

if (bComHanderEnable == 1)
{
ComHandler();
}
}
else
{
// Syncronous sampling
u16 data;

data = ADC2->DRH;
data <= 2;
data |= (ADC2->DRL & 0x03);

//clear interrupt flag
ADC2->CSR &= (u8)(~BIT7);

// Restore the sync ADC channel
ADC2->CSR = bCSR_Tmp;

// Enable ext. trigger
ADC2->CR2 |= BIT6;

// Manage async sampling
switch (ADC_Async_State)
{
default:
case ADC_BUS_SAMPLE:
ADC_Buffer[ ADC_BUS_INDEX ] = data;
ADC_Async_State = ADC_TEMP_INIT;
break;

case ADC_TEMP_SAMPLE:
ADC_Buffer[ ADC_TEMP_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;
}

ADC_State = ADC_SYNC;
}
}
#endif

上面的代码我改了一点点,就是多采集了一路平均电流。

AD采集分两种,一个是同步,一个是异步。同步中有三个采集通道,异步中有三个采集通道。同步中的通道为反电势通道、瞬时电流、平均电流。异步采集中通道为母线电压

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top