正如开箱贴所述,借助STM32CubeMX工具可以快速构建工程框架,此次通过对TIM3的控制,PWM输出驱动42步进电机。同样的,首先使用STM32CubeMX构建一个基于TIM3输出PWM的基础工程,这里选择四通道作为PUL输出。
设置好Prescaler预分频及Period周期的初始参数,PUL脉冲信号通道设置好了,但驱动步进电机还需要提供使能信号与方向信号,因此在GPIO口设置中增加两个GPIO,即用于ENA使能信号控制,DIR方向控制。
导出工程后,稍加修改,即可驱动步进电机。这里还引入OLED显示模块,采用GPIO口模拟IIC通讯。以及结合板上的LED、按键,可以实测一下C031C6的实时驱动能力。整体硬件连接示意图及实物连接如下图所示。另:为了注意LED4的及时响应,这里增加了一个蜂鸣器。
对照官方提供的原理图,这里值得注意的是,笔者采用的PB11引脚在原理图上标识错误,特此说明。
工程中采用循环读取USER按键的状态,改变TIM3的预分频系数,从而修改脉冲信号输出,工程中的部分代码展示如下。
#include "main.h" #include "oled.h" #include "bmp.h" I2C_HandleTypeDef hi2c1; TIM_HandleTypeDef htim3; UART_HandleTypeDef huart1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); static void MX_TIM3_Init(uint16_t Prescaler,uint16_t period); static void MX_USART1_UART_Init(void); int main(void) { uint8_t t; uint32_t i=40; static uint32_t num = 0; HAL_Init(); SystemClock_Config(); HAL_NVIC_SetPriority(SysTick_IRQn,0,0); MX_GPIO_Init(); MX_I2C1_Init(); MX_TIM3_Init(42-1,1000); MX_USART1_UART_Init(); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4); OLED_Init(); OLED_Clear(); t=' '; while (1) { if(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13))//如果按键按下 { i=i+20; if(i>1000) {i=0; HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);} //停止PWM MX_TIM3_Init(i,1000); //PWM重新配置,周期为1000,高电平持续时间为i HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4); //启动PWM while(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));//等待按键释放 } HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET); HAL_Delay(50); HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_RESET); HAL_Delay(200); HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET); HAL_Delay(50); HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_RESET); OLED_Clear(); OLED_ShowCHinese(10,0,25);//欢 OLED_ShowCHinese(18+10,0,26);//迎 OLED_ShowCHinese(36+10,0,27);//使 OLED_ShowCHinese(54+10,0,28);//用 OLED_ShowString(6,3,(uint8_t *)"STM32C031C6",16); OLED_ShowString(6,6,(uint8_t *)"2023-03-11",16); HAL_Delay(1500); OLED_Clear(); OLED_ShowCHinese(10,0,20);//意 OLED_ShowCHinese(18+10,0,21);//法 OLED_ShowCHinese(36+10,0,22);//半 OLED_ShowCHinese(54+10,0,23);//导 OLED_ShowCHinese(72+10,0,24);//体 OLED_ShowString(16,3,(uint8_t *)"NUCLEO Board",16); OLED_ShowChar(48,6,t,16);//显示ASCII字符 HAL_Delay(50); t++; if(t>'~')t=' '; OLED_ShowNum(103,6,t,3,16);//显示ASCII字符的码值 HAL_Delay(1500); OLED_Clear(); OLED_DrawBMP(0,0,128,8,BMP2); HAL_Delay(1500); } }
static void MX_TIM3_Init(uint16_t Prescaler,uint16_t period) { TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; htim3.Instance = TIM3; htim3.Init.Prescaler = Prescaler; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = period; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim3) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if(HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } if(HAL_TIM_PWM_Init(&htim3) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if(HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 500; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if(HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } HAL_TIM_MspPostInit(&htim3); } static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(ENA_GPIO_Port, ENA_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET); /*Configure GPIO pin : User_Button_Pin */ GPIO_InitStruct.Pin = User_Button_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(User_Button_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : Led_Pin */ GPIO_InitStruct.Pin = Led_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(Led_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : ENA_Pin DIR_Pin */ GPIO_InitStruct.Pin = ENA_Pin|DIR_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); }
#ifndef __MAIN_H #define __MAIN_H #ifdef __cplusplus extern "C" { #endif #include "stm32c0xx_hal.h" void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); void Error_Handler(void); #define User_Button_Pin GPIO_PIN_13 #define User_Button_GPIO_Port GPIOC #define User_Button_EXTI_IRQn EXTI4_15_IRQn #define Led_Pin GPIO_PIN_5 #define Led_GPIO_Port GPIOA #define ENA_Pin GPIO_PIN_10 #define ENA_GPIO_Port GPIOB #define DIR_Pin GPIO_PIN_11 #define DIR_GPIO_Port GPIOB #ifdef __cplusplus } #endif #endif
#ifndef __OLED_H #define __OLED_H #include "stm32c0xx_hal.h" #include "stm32c0xx_hal_gpio.h" #include "stm32c0xx.h" #include "stdlib.h" #include "stdint.h" #define OLED_MODE 0 #define SIZE 8 #define XLevelL 0x00 #define XLevelH 0x10 #define Max_Column 128 #define Max_Row 64 #define Brightness 0xFF #define X_WIDTH 128 #define Y_WIDTH 64 //-----------------OLED IIC端口定义---------------- #define OLED_SCLK_Clr() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,0)//SCL PA0 #define OLED_SCLK_Set() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,1) #define OLED_SDIN_Clr() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,0)//SDA PA1 #define OLED_SDIN_Set() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1) #define OLED_CMD 0 //写命令 #define OLED_DATA 1 //写数据 typedef uint32_t u32; typedef uint16_t u16; typedef uint8_t u8; //OLED控制用函数 void OLED_WR_Byte(unsigned dat,unsigned cmd); void OLED_Display_On(void); void OLED_Display_Off(void); void OLED_Init(void); void OLED_Clear(void); void OLED_DrawPoint(u8 x,u8 y,u8 t); void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot); void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size); void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size); void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size); void OLED_Set_Pos(unsigned char x, unsigned char y); void OLED_ShowCHinese(u8 x,u8 y,u8 no); void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]); void Delay_50ms(unsigned int Del_50ms); void Delay_1ms(unsigned int Del_1ms); void fill_picture(unsigned char fill_Data); void Picture(void); void IIC_Start(void); void IIC_Stop(void); void Write_IIC_Command(unsigned char IIC_Command); void Write_IIC_Data(unsigned char IIC_Data); void Write_IIC_Byte(unsigned char IIC_Byte); void IIC_Wait_Ack(void); #endif
#include "oled.h" #include "stdlib.h" #include "oledfont.h" void IIC_Start() { OLED_SCLK_Set(); OLED_SDIN_Set(); OLED_SDIN_Clr(); OLED_SCLK_Clr(); } void IIC_Stop() { OLED_SCLK_Set(); OLED_SDIN_Clr(); OLED_SDIN_Set(); } void IIC_Wait_Ack() { OLED_SCLK_Set() ; OLED_SCLK_Clr(); } void Write_IIC_Byte(unsigned char IIC_Byte) { unsigned char i; unsigned char m,da; da=IIC_Byte; OLED_SCLK_Clr(); for(i=0;i<8;i++) { m=da; m=m&0x80; if(m==0x80) {OLED_SDIN_Set();} else OLED_SDIN_Clr(); da=da<<1; OLED_SCLK_Set(); OLED_SCLK_Clr(); } } void Write_IIC_Command(unsigned char IIC_Command) { IIC_Start(); Write_IIC_Byte(0x78); //Slave address,SA0=0 IIC_Wait_Ack(); Write_IIC_Byte(0x00); //write command IIC_Wait_Ack(); Write_IIC_Byte(IIC_Command); IIC_Wait_Ack(); IIC_Stop(); } void Write_IIC_Data(unsigned char IIC_Data) { IIC_Start(); Write_IIC_Byte(0x78); //D/C#=0; R/W#=0 IIC_Wait_Ack(); Write_IIC_Byte(0x40); //write data IIC_Wait_Ack(); Write_IIC_Byte(IIC_Data); IIC_Wait_Ack(); IIC_Stop(); } void OLED_WR_Byte(unsigned dat,unsigned cmd) { if(cmd) { Write_IIC_Data(dat); } else { Write_IIC_Command(dat); } } void fill_picture(unsigned char fill_Data) { unsigned char m,n; for(m=0;m<8;m++) { OLED_WR_Byte(0xb0+m,0); //page0-page1 OLED_WR_Byte(0x00,0); //low column start address OLED_WR_Byte(0x10,0); //high column start address for(n=0;n<128;n++) { OLED_WR_Byte(fill_Data,1); } } } void OLED_Set_Pos(unsigned char x, unsigned char y) { OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD); } void OLED_Display_On(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON } void OLED_Display_Off(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF } void OLED_Clear(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7) OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址 OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); } } void OLED_On(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7) OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址 OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址 for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA); } } void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size) { unsigned char c=0,i=0; c=chr-' '; if(x>Max_Column-1){x=0;y=y+2;} if(Char_Size ==16) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8[c][i],OLED_DATA); } } u32 oled_pow(u8 m,u8 n) { u32 result=1; while(n--)result*=m; return result; } void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2) { u8 t,temp; u8 enshow=0; for(t=0;t<len;t++) { temp=(num/oled_pow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { OLED_ShowChar(x+(size2/2)*t,y,' ',size2); continue; }else enshow=1; } OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); } } void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size) { unsigned char j=0; while (chr[j]!='\0') { OLED_ShowChar(x,y,chr[j],Char_Size); x+=8; if(x>120){x=0;y+=2;} j++; } } void OLED_ShowCHinese(u8 x,u8 y,u8 no) { u8 t,adder=0; OLED_Set_Pos(x,y); for(t=0;t<16;t++) { OLED_WR_Byte(Hzk[2*no][t],OLED_DATA); adder+=1; } OLED_Set_Pos(x,y+1); for(t=0;t<16;t++) { OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA); adder+=1; } } void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]) { unsigned int j=0; unsigned char x,y; if(y1%8==0) y=y1/8; else y=y1/8+1; for(y=y0;y<y1;y++) { OLED_Set_Pos(x0,y); for(x=x0;x<x1;x++) { OLED_WR_Byte(BMP[j++],OLED_DATA); } } } void OLED_Init(void) { GPIO_InitTypeDef configStruct; /* enable the OLED GPIO clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* configure OLED GPIO pin */ configStruct.Pin = (GPIO_PIN_0 | GPIO_PIN_1); configStruct.Mode = GPIO_MODE_OUTPUT_PP; configStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(GPIOA, &configStruct); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0|GPIO_PIN_1,1); HAL_Delay(800); OLED_WR_Byte(0xAE,OLED_CMD);//--display off OLED_WR_Byte(0x00,OLED_CMD);//---set low column address OLED_WR_Byte(0x10,OLED_CMD);//---set high column address OLED_WR_Byte(0x40,OLED_CMD);//--set start line address OLED_WR_Byte(0xB0,OLED_CMD);//--set page address OLED_WR_Byte(0x81,OLED_CMD);// contract control OLED_WR_Byte(0xFF,OLED_CMD);//--128 OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64) OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0xD5,OLED_CMD);//set osc division OLED_WR_Byte(0x80,OLED_CMD); OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off OLED_WR_Byte(0x05,OLED_CMD); OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period OLED_WR_Byte(0xF1,OLED_CMD); OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion OLED_WR_Byte(0x12,OLED_CMD); OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh OLED_WR_Byte(0x30,OLED_CMD); OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable OLED_WR_Byte(0x14,OLED_CMD); OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel }
至于采用“PCtoLCD2002”取字模软件生成的十六进制数据不再粘贴出来,编译工程后,下载到开发板,可通过按下按键改变步进电机的转速。视频后续经EEPW审核后再跟帖附上。综合体验,STM32C031C6控制外设实时可靠,相较于八位单片机,完全可以胜任在电机控制方面的产品开发。加之可使用STM32CubeMX工具方便构建工程,是一款不错的MCU。