找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 104|回复: 2
收起左侧

基于STC8H单片机实验箱程序修改的PWM控制舵机,舵机没反应,请老师们帮忙看看是哪....

[复制链接]
ID:479134 发表于 2024-10-15 10:04 | 显示全部楼层 |阅读模式
基于STC8G试验箱  PWM程序修改的 舵机控制程序,现在情况是 舵机没有反应,想请老师们看看程序有没有问题。

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序            */
/*---------------------------------------------------------------------*/

#include        "config.h"
#include        "timer.h"
#include        "GPIO.h"
#include        "PWM.h"

/*************        功能说明        **************

本例程基于STC8H8K64U为主控芯片的实验箱8进行编写测试,STC8H系列芯片可通用参考.

高级PWM定时器 PWM1P/PWM1N,PWM2P/PWM2N,PWM3P/PWM3N,PWM4P/PWM4N 每个通道都可独立实现PWM输出,或者两两互补对称输出.

8个通道PWM设置对应P6的8个端口.

通过P6口上连接的8个LED灯,利用PWM实现呼吸灯效果.

PWM周期和占空比可以根据需要自行设置,最高可达65535.

下载时, 选择时钟 24MHZ (用户可在"config.h"修改频率).

******************************************/

/*************        本地常量声明        **************/


/*************        本地变量声明        **************/

PWMx_Duty PWMA_Duty;

/*************        本地函数声明        **************/

int dutyCycle;

/*************  外部函数和变量声明 *****************/
// 简单的延时函数  
void delayMs(int ms) {  
    int i, j;  
    for (i = 0; i < ms; i++)  
        for (j = 0; j < 120; j++);  // 调整内部循环计数以获得所需的延时  
}  


/************************ IO口配置 ****************************/
void        GPIO_config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;                                //结构定义
        GPIO_InitStructure.Pin  = GPIO_Pin_0;                                //指定要初始化的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);        //初始化

        GPIO_InitStructure.Pin  = GPIO_Pin_All;                        //指定要初始化的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_P1,&GPIO_InitStructure);        //初始化
}

/************************ 定时器配置 ****************************/
void        Timer_config(void)
{
        TIM_InitTypeDef                TIM_InitStructure;                                                //结构定义
        TIM_InitStructure.TIM_Mode      = TIM_16BitAutoReload;        //指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
        TIM_InitStructure.TIM_Priority    = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
        TIM_InitStructure.TIM_Interrupt = ENABLE;                                        //中断是否允许,   ENABLE或DISABLE
        TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;                //指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
        TIM_InitStructure.TIM_ClkOut    = DISABLE;                                //是否输出高速脉冲, ENABLE或DISABLE
        TIM_InitStructure.TIM_Value     = 65536UL - (MAIN_Fosc / 1000);                //中断频率, 1000次/秒
        TIM_InitStructure.TIM_Run       = ENABLE;                                        //是否初始化后启动定时器, ENABLE或DISABLE
        Timer_Inilize(Timer0,&TIM_InitStructure);                                        //初始化Timer0          Timer0,Timer1,Timer2,Timer3,Timer4
}

/***************  串口初始化函数 *****************/
void        PWM_config(void)
{
        PWMx_InitDefine                PWMx_InitStructure;
      
        PWMx_InitStructure.PWM1_Mode    =        CCMRn_PWM_MODE1;        //模式,                CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
//        PWMx_InitStructure.PWM2_Mode    =        CCMRn_PWM_MODE1;        //模式,                CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
//        PWMx_InitStructure.PWM3_Mode    =        CCMRn_PWM_MODE1;        //模式,                CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
//        PWMx_InitStructure.PWM4_Mode    =        CCMRn_PWM_MODE1;        //模式,                CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2

        PWMx_InitStructure.PWM1_SetPriority  = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
//        PWMx_InitStructure.PWM2_SetPriority  = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
//        PWMx_InitStructure.PWM3_SetPriority  = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
//        PWMx_InitStructure.PWM4_SetPriority  = Priority_0;                        //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
      
        PWMx_InitStructure.PWM_Period   = 20000 - 1;                                                        // 20ms周期对应50hz频率
        PWMx_InitStructure.PWM1_Duty    = 1500;                                                                                //1.5ms为中位位置
//        PWMx_InitStructure.PWM2_Duty    = PWMA_Duty.PWM2_Duty;        //PWM2占空比时间, 0~Period
//        PWMx_InitStructure.PWM3_Duty    = PWMA_Duty.PWM3_Duty;        //PWM3占空比时间, 0~Period
//        PWMx_InitStructure.PWM4_Duty    = PWMA_Duty.PWM4_Duty;        //PWM4占空比时间, 0~Period
//        PWMx_InitStructure.PWM_DeadTime = 0;                                                                //死区发生器设置, 0~255
      
        PWMx_InitStructure.PWM_EnoSelect   = ENO1P ;        //输出通道选择,        ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
        PWMx_InitStructure.PWM_PS_SW       = PWM1_SW_P10_P11;        //切换端口,                PWM1_SW_P10_P11,PWM1_SW_P20_P21,PWM1_SW_P60_P61
                                                                                                                                                                                                                        //                                                PWM2_SW_P12_P13,PWM2_SW_P22_P23,PWM2_SW_P62_P63
                                                                                                                                                                                                                        //                                                PWM3_SW_P14_P15,PWM3_SW_P24_P25,PWM3_SW_P64_P65
                                                                                                                                                                                                                        //                                                PWM4_SW_P16_P17,PWM4_SW_P26_P27,PWM4_SW_P66_P67,PWM4_SW_P34_P33

        PWMx_InitStructure.PWM_CC1Enable   = ENABLE;                                //开启PWM1P输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC1NEnable  = ENABLE;                                //开启PWM1N输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC2Enable   = ENABLE;                                //开启PWM2P输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC2NEnable  = ENABLE;                                //开启PWM2N输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC3Enable   = ENABLE;                                //开启PWM3P输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC3NEnable  = ENABLE;                                //开启PWM3N输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC4Enable   = ENABLE;                                //开启PWM4P输入捕获/比较输出,  ENABLE,DISABLE
//        PWMx_InitStructure.PWM_CC4NEnable  = ENABLE;                                //开启PWM4N输入捕获/比较输出,  ENABLE,DISABLE
      
        PWMx_InitStructure.PWM_MainOutEnable= ENABLE;                                //主输出使能, ENABLE,DISABLE
        PWMx_InitStructure.PWM_CEN_Enable   = ENABLE;                                //使能计数器, ENABLE,DISABLE
        PWM_Configuration(PWMA, &PWMx_InitStructure);                                //初始化PWM,  PWMA,PWMB
}



void setServoAngle(int angle) {  
    // 检查角度是否在有效范围内,防止错误输入导致不正确操作  
    if (angle < 0) angle = 0;  
    if (angle > 180) angle = 180;  

    // 将角度(0-180度)转换为对应的PWM占空比值  
    // 假设0度对应500μs(占空比2.5%),180度对应2500μs(占空比12.5%)  
    dutyCycle = 500 + ((angle * 2000) / 180);  
    PWMA_Duty.PWM1_Duty = dutyCycle;  
    UpdatePwm(PWMA, &PWMA_Duty);  
}

/******************** 主函数**************************/
void main(void)
{
        GPIO_config();
        Timer_config();
        PWM_config();
        EA = 1;
        P10 = 0;                //打开LED电源
      
        setServoAngle(0);

        while (1)
        {
        setServoAngle(0);   // Move servo to 0 degrees  
        delayMs(5000);      // Delay to observe position  

        setServoAngle(90);  // Move servo to 90 degrees (center)  
        delayMs(5000);      // Delay to observe position  

//        setServoAngle(180); // Move servo to 180 degrees  
//        delayMs(5000);      // Delay to observe position                 
        }
}


回复

使用道具 举报

ID:161164 发表于 2024-10-15 16:26 | 显示全部楼层
1. 舵机用什么电源?
2. 共地了吗?
3. PWM信号符合舵机的要求吗?
回复

使用道具 举报

ID:479134 发表于 2024-10-16 08:52 | 显示全部楼层
lkc8210 发表于 2024-10-15 16:26
1. 舵机用什么电源?
2. 共地了吗?
3. PWM信号符合舵机的要求吗?

找到问题了 谢谢老师 原因是 PWM信号周期不符合舵机的要求,谢谢老师
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表