【AutoChips AC7801x电机demo板测评】+关键外设测试-ADC和PWM
[复制链接]
关键外设测试 - ADC和PWM
为了完成电机驱动测试,需要首先完成几个关键外设的驱动:包括AD采样,PWM输出控制,串口等,本次完成ADC采样和PWM驱动测试。
- AD数据采样
为了贴近实际的使用,使用了AD的顺序转换和注入式转换配置。AC7801x中集成了一个12bit的ADC转换器,允许使用24MHz的时钟驱动。此AD支持8中模式配置
本次测试了Mode3注入组扫描模式,通过外部触发方式,规则组扫描采用DMA方式读取转换结果。
通过AutoGen Studio工具配置如下:
规则通道配置了IN8,IN9,IN10和内部温度传感器,注入组配置了IN4,IN5,IN6,IN7 。
DMA的通道配置:
由于DMA仅支持规则组的结果读取,所以只有规则组对应的通道结果才会不读入到指定的内存去中。
生成代码,并编译下载到微控制器中。通过串口终端可以读取到规则组中的4个通道的AD采样值。
其中IN9-电位器对应1组显示数据,通过调节电位器可以观察到采样值的变化。
说明我们的AD转换配置和DMA读取通道都是正常的。
- 定时器触发AD转换
为了测试AD转换的外部定时器触发,初始化了TIMER0定时器通道和采集传输终端CTU。
定时器的溢出信号可以作为AD转换的启动信号,CTU负责完成启动信号路由。
通过AutoGen Studio产生TIMER0配置:
通过AutoGen Studio产生CTU配置:
可见CTU中将Timer Channel 0 Overflow信号路由到ADC Regular Group Trigger Source。
生成驱动代码:
static void ATC_CTU_Init(void)
{
/* USER CODE BEGIN CTU_Init 0 */
/* USER CODE END CTU_Init 0 */
CTU_ConfigType ctuConfig;
/* USER CODE BEGIN CTU_Init 1 */
/* USER CODE END CTU_Init 1 */
ctuConfig.uart0RxFilterEn = DISABLE;
ctuConfig.rtcCaptureEn = DISABLE;
ctuConfig.acmpCaptureEn = DISABLE;
ctuConfig.uart0RxCaptureEn = DISABLE;
ctuConfig.uartTxModulateEn = DISABLE;
ctuConfig.clkPsc = CTU_CLK_PRESCALER_1;
ctuConfig.adcRegularTriggerSource = CTU_TRIGGER_ADC_TIMER_CH0_OVERFLOW;
ctuConfig.delay0Time = 0;
ctuConfig.adcInjectTriggerSource = CTU_TRIGGER_ADC_PWM1_MATCH;
ctuConfig.delay1Time = 10;
ctuConfig.pwdt0In3Source = CTU_PWDT_IN3_SOURCE_UART0_RX;
ctuConfig.pwdt1In3Source = CTU_PWDT_IN3_SOURCE_UART0_RX;
CTU_Init(&ctuConfig);
/* USER CODE BEGIN CTU_Init 2 */
/* USER CODE END CTU_Init 2 */
}
static void ATC_TIMER0_Init(void)
{
/* USER CODE BEGIN TIMER0_Init 0 */
/* USER CODE END TIMER0_Init 0 */
TIMER_ConfigType timerConfig;
/* USER CODE BEGIN TIMER0_Init 1 */
/* USER CODE END TIMER0_Init 1 */
MSP_TIMER_Init(TIMER_CHANNEL0);
timerConfig.timerEn = ENABLE;
timerConfig.interruptEn = ENABLE;
timerConfig.linkModeEn = DISABLE;
timerConfig.periodValue = 119999;
timerConfig.callBack = TIMER_CHANNEL0_IRQHandler_Callback;
TIMER_Init(TIMER_CHANNEL0, &timerConfig);
/* USER CODE BEGIN TIMER0_Init 2 */
/* USER CODE END TIMER0_Init 2 */
}
这样就会完成AD的外部启动信号的配置,必须注意,AD需要选择外部触发信号元。
- 互补式PWM信号输出配置
PWM信号输出是直流无刷电机驱动的基础功能,互补式PWM信号输出是一个特定应用方式,尤其是FOC驱动方式中,不可或缺。
AC7801x微控制器集成了两个PWM发生器,分别为PWM0和PWM1,这里使用PWM1作为目标对象测试。
配置PWM的周期为15KHz,3组互补信号输出,1us死区时间,占空比80%。
通过AutoGen Studio配置:
在Channel0-1 Setup中,必须配置CHannel0 And Channel1 Mode Select为Combine模式才表示通道0和通道1是互补输出模式。
生成驱动代码:
static void ATC_PWM1_Init(void)
{
/* USER CODE BEGIN PWM1_Init 0 */
uint32_t ch1_count,ch2_count;
ch2_count = (MOD_PWM >> 1) * 80 /100;
ch1_count = MOD_PWM - ch2_count;
/* USER CODE END PWM1_Init 0 */
PWM_ConfigType pwmConfig;
PWM_ModulationConfigType initModeStruct;
/* USER CODE BEGIN PWM1_Init 1 */
/* USER CODE END PWM1_Init 1 */
MSP_PWM_Init(PWM1);
pwmConfig.mode = PWM_MODE_MODULATION;
pwmConfig.clkSource = PWM_CLK_SOURCE_APB;
pwmConfig.clkPsc = PWM_PRES;
pwmConfig.initValue = 0;
pwmConfig.maxValue = MOD_PWM-1;
pwmConfig.overflowInterrupEn = DISABLE;
pwmConfig.cntOverflowFreq = 0;
pwmConfig.interruptEn = DISABLE;
initModeStruct.countMode = PWM_UP_COUNT;
initModeStruct.deadtime = 24;
initModeStruct.deadtimePsc = PWM_DEADTIME_DIVID_1;
initModeStruct.initChOutputEn = ENABLE;
initModeStruct.initTriggerEn = DISABLE;
PWM_CombineChConfig combineChConfig[3];
/* combine channel 0 and channel 1 configuration */
combineChConfig[0].pairChannel = PWM_CH_0;
combineChConfig[0].ch1stValue = ch1_count;
combineChConfig[0].ch2ndValue = ch2_count;
combineChConfig[0].levelMode = PWM_HIGH_TRUE;
combineChConfig[0].deadtimeEn = ENABLE;
combineChConfig[0].complementEn = ENABLE;
combineChConfig[0].ch1stMatchDir = PWM_MATCH_DIR_DOWN;
combineChConfig[0].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
combineChConfig[0].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[0].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[0].ch1stInterruptEn = DISABLE;
combineChConfig[0].ch2ndInterruptEn = DISABLE;
combineChConfig[0].ch1stInitLevel = PWM_LOW_LEVEL;
combineChConfig[0].ch2ndInitLevel = PWM_LOW_LEVEL;
combineChConfig[0].ch1stTriggerEn = DISABLE;
combineChConfig[0].ch2ndTriggerEn = DISABLE;
/* combine channel 2 and channel 3 configuration */
combineChConfig[1].pairChannel = PWM_CH_2;
combineChConfig[1].ch1stValue = ch1_count;
combineChConfig[1].ch2ndValue = ch2_count;
combineChConfig[1].levelMode = PWM_HIGH_TRUE;
combineChConfig[1].deadtimeEn = ENABLE;
combineChConfig[1].complementEn = ENABLE;
combineChConfig[1].ch1stMatchDir = PWM_MATCH_DIR_UP;
combineChConfig[1].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
combineChConfig[1].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[1].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[1].ch1stInterruptEn = DISABLE;
combineChConfig[1].ch2ndInterruptEn = DISABLE;
combineChConfig[1].ch1stInitLevel = PWM_HIGH_LEVEL;
combineChConfig[1].ch2ndInitLevel = PWM_HIGH_LEVEL;
combineChConfig[1].ch1stTriggerEn = DISABLE;
combineChConfig[1].ch2ndTriggerEn = DISABLE;
/* combine channel 4 and channel 5 configuration */
combineChConfig[2].pairChannel = PWM_CH_4;
combineChConfig[2].ch1stValue = ch1_count;
combineChConfig[2].ch2ndValue = ch2_count;
combineChConfig[2].levelMode = PWM_HIGH_TRUE;
combineChConfig[2].deadtimeEn = ENABLE;
combineChConfig[2].complementEn = ENABLE;
combineChConfig[2].ch1stMatchDir = PWM_MATCH_DIR_DOWN;
combineChConfig[2].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;
combineChConfig[2].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[2].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
combineChConfig[2].ch1stInterruptEn = DISABLE;
combineChConfig[2].ch2ndInterruptEn = DISABLE;
combineChConfig[2].ch1stInitLevel = PWM_HIGH_LEVEL;
combineChConfig[2].ch2ndInitLevel = PWM_HIGH_LEVEL;
combineChConfig[2].ch1stTriggerEn = DISABLE;
combineChConfig[2].ch2ndTriggerEn = DISABLE;
initModeStruct.combineChannelNum = 3;
initModeStruct.combineChConfig = combineChConfig;
pwmConfig.initModeStruct = &initModeStruct;
PWM_Init(PWM1, &pwmConfig);
/* USER CODE BEGIN PWM1_Init 2 */
/* USER CODE END PWM1_Init 2 */
}
编译下载到微控制器中,通过示波器我们可以观察PWM的一组互补输出波形。
其中蓝色为通道0,黄色为通道1,从图种可以看到,两个波形为互补输出模式,下面的A通道和B通道的输出信息总结都可以看到,信号频率为15KHz,占空比接近80%(由于示波器带宽不足导致测试结果误差)。
这幅图中,放大互补信号波形,我们测量A通道下降沿到B通道上升沿的时间,可见测试时间接近1us的时间,这个时间就是死区时间,验证了我们的配置参数。
通过上面关键外设的测试研究,基本摸清了AC7801x外设的使用方法,对于后面电机驱动的研究扫清了硬件驱动部分的障碍。下一步对霍尔信号的采样进行研究测试。
|