LKS32MC081评测+GCC下外设测试 (2) 基于开发环境搭建的平台,继续进行MC081芯片外设的评测。由于部分文件官方提供的是二进制格式的,在GCC环境下使用不了,所有ADC的测试没有使用官方的校准数据。 1、 外设驱动测试 1) MCPWM的测试 MCPWM是系统中用于电机控制中产生PWM信号的主力定时器,支持最多4组互补输出的PWM信号。这里测试前三组PWM信号输出,即P1.4-P1.9 。 A. GPIO的端口初始化 IO端口初始化: //GPIO_P1.4到P1.11设置为输出模式,MCPWM复用 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIO1, &GPIO_InitStruct); GPIO_PinAFConfig(GPIO1,GPIO_PinSource_4, AF3_MCPWM); //GPIO_PinSource_4表示P1的第四引脚 GPIO_PinAFConfig(GPIO1,GPIO_PinSource_5, AF3_MCPWM); GPIO_PinAFConfig(GPIO1,GPIO_PinSource_6, AF3_MCPWM); GPIO_PinAFConfig(GPIO1,GPIO_PinSource_7, AF3_MCPWM); GPIO_PinAFConfig(GPIO1,GPIO_PinSource_8, AF3_MCPWM); GPIO_PinAFConfig(GPIO1,GPIO_PinSource_9, AF3_MCPWM); B. MCPWM的初始化 初始化为中心对称互补方式输出,PWM频率为22kHz,设置的死区为1.15uS。 void MCPWM_init(void) { MCPWM_InitTypeDef MCPWM_InitStructure; MCPWM_StructInit(&MCPWM_InitStructure); MCPWM_InitStructure.CLK_DIV = PWM_PRSC; /* MCPWM时钟分频设置 */ MCPWM_InitStructure.MCLK_EN = ENABLE; /* 模块时钟开启 */ MCPWM_InitStructure.MCPWM_Cnt_EN = ENABLE; /* 主计数器开始计数使能开关 */ MCPWM_InitStructure.MCPWM_WorkModeCH0 = CENTRAL_PWM_MODE; /* MCPWM CH0工作模式:中心对齐PWM模式 */ MCPWM_InitStructure.MCPWM_WorkModeCH1 = CENTRAL_PWM_MODE; /* 通道工作模式设置,中心对齐或边沿对齐 */ MCPWM_InitStructure.MCPWM_WorkModeCH2 = CENTRAL_PWM_MODE; MCPWM_InitStructure.MCPWM_WorkModeCH3 = CENTRAL_PWM_MODE; MCPWM_InitStructure.GPIO_BKIN_Filter = 12; /* 急停事件(来自IO口信号)数字滤波器时间设置 */ MCPWM_InitStructure.CMP_BKIN_Filter = 12; /* 急停事件(来自比较器信号)数字滤波器时间设置 */ MCPWM_InitStructure.MCPWM_PERIOD = PWM_PERIOD; /* 计数周期设置即MCPWM输出周期*/ MCPWM_InitStructure.TriggerPoint0 = (u16)(10 - PWM_PERIOD); /* MCPWM_TMR0 ADC触发事件T0 设置 */ MCPWM_InitStructure.TriggerPoint1 = (u16)(PWM_PERIOD - 1); /* MCPWM_TMR1 ADC触发事件T1 设置 */ MCPWM_InitStructure.DeadTimeCH0N = DEADTIME; MCPWM_InitStructure.DeadTimeCH0P = DEADTIME; MCPWM_InitStructure.DeadTimeCH1N = DEADTIME; MCPWM_InitStructure.DeadTimeCH1P = DEADTIME; MCPWM_InitStructure.DeadTimeCH2N = DEADTIME; MCPWM_InitStructure.DeadTimeCH2P = DEADTIME;/* 死区时间设置 */ MCPWM_InitStructure.DeadTimeCH3N = DEADTIME; MCPWM_InitStructure.DeadTimeCH3P = DEADTIME;/* 死区时间设置 */ /* CHxP 高有效, CHxN低电平有效 */ MCPWM_InitStructure.CH0N_Polarity_INV = DISABLE; /* CH0N通道输出极性设置 | 取反输出*/ MCPWM_InitStructure.CH0P_Polarity_INV = DISABLE; /* CH0P通道输出极性设置 | 正常输出 */ MCPWM_InitStructure.CH1N_Polarity_INV = DISABLE; MCPWM_InitStructure.CH1P_Polarity_INV = DISABLE; MCPWM_InitStructure.CH2N_Polarity_INV = DISABLE; MCPWM_InitStructure.CH2P_Polarity_INV = DISABLE; MCPWM_InitStructure.CH3N_Polarity_INV = DISABLE; MCPWM_InitStructure.CH3P_Polarity_INV = DISABLE; MCPWM_InitStructure.Switch_CH0N_CH0P = DISABLE; /* 通道交换选择设置 | CH0P和CH0N是否选择信号交换 */ MCPWM_InitStructure.Switch_CH1N_CH1P = DISABLE; /* 通道交换选择设置 */ MCPWM_InitStructure.Switch_CH2N_CH2P = DISABLE; /* 通道交换选择设置 */ MCPWM_InitStructure.Switch_CH2N_CH2P = DISABLE; /* 通道交换选择设置 */ /* 默认电平设置 默认电平输出不受MCPWM_IO01和MCPWM_IO23的 BIT0、BIT1、BIT8、BIT9、BIT6、BIT14通道交换和极性控制的影响,直接控制通道输出 */ MCPWM_InitStructure.CH0P_default_output = LOW_LEVEL; MCPWM_InitStructure.CH0N_default_output = HIGH_LEVEL; MCPWM_InitStructure.CH1P_default_output = LOW_LEVEL; /* CH1P对应引脚在空闲状态输出低电平 */ MCPWM_InitStructure.CH1N_default_output = HIGH_LEVEL; /* CH1N对应引脚在空闲状态输出高电平 */ MCPWM_InitStructure.CH2P_default_output = LOW_LEVEL; MCPWM_InitStructure.CH2N_default_output = HIGH_LEVEL; MCPWM_InitStructure.CH3P_default_output = LOW_LEVEL; MCPWM_InitStructure.CH3N_default_output = HIGH_LEVEL; MCPWM_InitStructure.DebugMode_PWM_out = ENABLE; /* 在接上仿真器debug程序时,暂停MCU运行时,选择各PWM通道正常输出调制信号 还是输出默认电平,保护功率器件 ENABLE:正常输出 DISABLE:输出默认电平*/ MCPWM_InitStructure.MCPWM_T0_UpdateEN = ENABLE; /* MCPWM T0事件更新使能 */ MCPWM_InitStructure.MCPWM_T1_UpdateEN = DISABLE; /* MCPWM T1事件更新 禁止*/ #if(CURRENT_SAMPLE_TYPE == CURRENT_SAMPLE_1SHUNT) MCPWM_InitStructure.T1_Update_INT_EN = ENABLE; /* T1更新事件 中断使能或关闭 */ #else #if (CURRENT_SAMPLE_TYPE ==CURRENT_SAMPLE_2SHUNT) MCPWM_InitStructure.T0_Update_INT_EN = DISABLE; /* T0更新事件 中断使能或关闭 */ #else #if ((CURRENT_SAMPLE_TYPE ==CURRENT_SAMPLE_3SHUNT) || (CURRENT_SAMPLE_TYPE == CURRENT_SAMPLE_MOSFET)) MCPWM_InitStructure.T0_Update_INT_EN = DISABLE; /* T0更新事件 中断使能或关闭 */ #endif #endif #endif MCPWM_InitStructure.FAIL0_INT_EN = ENABLE; /* FAIL0事件, 中断使能或关闭 */ MCPWM_InitStructure.FAIL0_INPUT_EN = DISABLE; /* FAIL0 输入功能使能*/ MCPWM_InitStructure.FAIL0_Signal_Sel = FAIL_SEL_CMP; /* FAIL0事件信号选择,比较器或IO口 */ MCPWM_InitStructure.FAIL0_Polarity = HIGH_LEVEL_ACTIVE; /* FAIL0事件极性选择,高有效 */ MCPWM_InitStructure.FAIL1_INT_EN = ENABLE; /* FAIL1事件 中断使能或关闭 */ MCPWM_InitStructure.FAIL1_INPUT_EN = ENABLE; /* FAIL1通道急停功能打开或关闭 */ MCPWM_InitStructure.FAIL1_Signal_Sel = FAIL_SEL_CMP; /* FAIL1事件信号选择,比较器或IO口 */ MCPWM_InitStructure.FAIL1_Polarity = HIGH_LEVEL_ACTIVE; /* FAIL1事件极性选择,高有效或低有效 */ MCPWM_InitStructure.T0_Update_INT_EN = ENABLE; /* T0更新事件中斷使能 */ MCPWM_Init(&MCPWM_InitStructure); MCPWM_TH00 = (u16)(-(PWM_PERIOD >> 2)); MCPWM_TH01 = (PWM_PERIOD >> 2); MCPWM_TH10 = (u16)(-(PWM_PERIOD >> 2)); MCPWM_TH11 = (PWM_PERIOD >> 2); MCPWM_TH20 = (u16)(-(PWM_PERIOD >> 2)); MCPWM_TH21 = (PWM_PERIOD >> 2); MCPWM_TH30 = (u16)(-(PWM_PERIOD >> 2)); MCPWM_TH31 = (PWM_PERIOD >> 2); } C. 操作函数 Shell下的MCPWM控制函数: //mcpwm测试函数 int mcpwm_ctl(int argc, char *argv[]) { int res = 0; /* API resultcode */ if(argc < 2) { shellPrint(&shell,"mcpwm_ctl on !\n\r"); return -1; } if(strcmp(argv[1], "on") == 0) { PWMOutputs(ENABLE); } else if(strcmp(argv[1], "off") == 0) { PWMOutputs(DISABLE); } return res; } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), mcpwm_ctl, mcpwm_ctl, mcpwmcontrll on/off); D. 测试效果
从截图中可以看到,P和N的输出是存在死区的,死区时间大约为1uS,pwm频率为21.95KHz。
|