ZHCAER6A August 2024 – August 2025 MSPM0C1105 , MSPM0C1106 , MSPM0G1105 , MSPM0G1106 , MSPM0G1107 , MSPM0G1505 , MSPM0G1506 , MSPM0G1507 , MSPM0G3105 , MSPM0G3106 , MSPM0G3107 , MSPM0G3505 , MSPM0G3506 , MSPM0G3507 , MSPM0L1105 , MSPM0L1106 , MSPM0L1303 , MSPM0L1304 , MSPM0L1304-Q1 , MSPM0L1305 , MSPM0L1305-Q1 , MSPM0L1306 , MSPM0L1306-Q1 , MSPM0L1343 , MSPM0L1344 , MSPM0L1345 , MSPM0L1346
此示例演示了如何将模拟信号转换为 4kHz PWM 输出。使用 MSPM0 集成式 ADC 对模拟输入信号进行采样。PWM 输出的占空比根据 ADC 读数进行更新。本示例需要两个计时器;一个用于触发 ADC 读取,另一个用于生成 PWM 输出。下载此示例的代码。
图 1-1 显示了该示例中使用的外设的功能方框图。
此应用需要 2 个计时器、1 个集成 ADC 和 2 个器件引脚。
子块功能 | 外设 用法 | 注释 |
|---|---|---|
采样触发 | (1x) 计时器 G | 在代码中称为 TIMER_0_INST |
PWM 生成 | (1 个)计时器 G | 在代码中称为 PWM_0_INST |
模拟信号捕获 | 1 个 ADC 通道 | 在代码中称为 ADC12_0_INST |
IO | 2 引脚 | (1x) ADC 输入 (1x) PWM 输出 |
此应用的 PWM 输出具有 10 位分辨率。不过,ADC 样本是 12 位,因此我们必须将 12 位 ADC 读数转换为 10 位值,以便设置 PWM 计时器的比较值。根据应用要求,可能需要不同的调节。
此外,可能需要对传入数据进行更先进的信号处理。例如,限制、平均值或其他滤波在不同情况下可能很重要。可以在以下函数中执行这些类型的操作。
void updatePWMfromADCvalue(uint16_t adcValue) {
// Check to see if the adc value is above our minimum threshold
if (adcValue > PWM_DEADBAND)
{
// Convert 12bit adcValue into 10bit value by right
// shifting by 2 because the PWM resolution is 10bit
uint16_t adcValue_10bit = adcValue >> 2;
// PWM timer is configured as a down counter (i.e it
// starts counting down from PWM_LOAD_VAL) and its
// initial state is high therefore we must perform
// the following operation so that small values of
// adcValue_10bit result in small duty cycles
uint16_t ccv = PWM_LOAD_VAL - adcValue_10bit;
// Write the new ccv value into the corresponding timer
// register
DL_TimerG_setCaptureCompareValue(PWM_0_INST,
ccv,
DL_TIMER_CC_0_INDEX);
// Start the timer if it is not already running
if ( !DL_TimerG_isRunning(PWM_0_INST) ) {
DL_TimerG_startCounter(PWM_0_INST);
}
}
else {
// If adcResult is not above deadband value then disable timer
DL_TimerG_stopCounter(PWM_0_INST);
}
}
图 1-3 当 ADC 输入低于死区时,禁用 PWM 输出. 当输入电压低于预设的死区值时,输出被禁用,如图 1-3 所示。
图 1-4 PWM 输出占空比对应于输入电压. 在图 1-4 中,输入电压为 2.26V。测得的占空比为 67.93%。快速计算确认预期占空比为 68.4%。