- 打卡等级:偶尔看看III
- 打卡总天数:50
- 最近打卡:2026-02-27 09:09:39
已绑定手机
中级会员
- 积分
- 200
|
发表于 2025-11-13 17:45:45
|
显示全部楼层
尝试了串口3和串口2打印都正常,但是对无刷电机的控制就不对了,不能减速,加速按一下就最大。不知道问题出现在哪里!
/******************* IO配置函数 *******************/
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
GPIO_InitStructure.Pin = GPIO_Pin_6 | GPIO_Pin_7; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
GPIO_Inilize(GPIO_P4,&GPIO_InitStructure); //初始化
}
/*************** 串口初始化函数 *****************/
void UART_config(void)
{
COMx_InitDefine COMx_InitStructure; //结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
// COMx_InitStructure.UART_BRT_Use = BRT_Timer2; //选择波特率发生器, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2, 所以不用选择)
COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 110 ~ 115200
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
UART_Configuration(UART2, &COMx_InitStructure); //初始化串口2 USART1,USART2,USART3,USART4
NVIC_UART2_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART2_SW(UART2_SW_P46_P47); //UART2_SW_P10_P11,UART2_SW_P46_P47
}
/**********************************************/
void main(void)
{
WTST = 0;
CKCON = 0;
EAXFR = 1; // SFR enable, 允许访问扩展寄存器
Stc_Foc_Init(Foc_User_Init, Foc_Pwm_Swx); // 调用初始化,固定形式
EA = 1; // 打开总中断
Io_Init(); // 用户的其他初始化部分
GPIO_config();
UART_config();
set_duty = 250; // 占空比最高可达1024(超过1024进入过调制状态,最高可达1177,允许再放大1.15倍)
#if START_MODE == 0 // 是否上电启动
{
ReStart_Moto();
} // 已启动
#else
{
Stop_Moto();
} // 未启动
#endif
PrintString2("STC32G UART2 Test Programme!\r\n"); // UART2发送一个字符串
while (1)
{
// 用户的其他循环程序
// set_duty = 1177;//过调制状态最大占空比,此时接近方波的电压利用率
if (P32 == 0 && set_duty > 10)
set_duty--; // 更高的占空比
if (P33 == 0 && set_duty < 1024)
set_duty++; // 满占空比
if (P34 == 0) // 正反转切换
{
if (CW_CCW == 0)
CW_CCW = 1;
else
CW_CCW = 0; // 取反正反转标志位
if (B_Timer3_OverFlow < Ref_OVER_TIME) // 判断如果还在转
{
while (B_Timer3_OverFlow > Ref_OVER_TIME)
;
ReStart_Moto(); // 等待停转,然后重新启动
}
while (P34 == 0)
;
};
if (P54 == 0)
{
if (B_Timer3_OverFlow > Ref_OVER_TIME)
{
ReStart_Moto();
} // 启动/停止控制
// else{Fast_Stop_Moto();while(P54==0);}}//可选快速刹车
else
{
Stop_Moto();
while (P54 == 0)
;
}
} // 可选慢速刹车(自由滑行刹停)
}
}
|
|