{"id":10355,"date":"2022-10-15T16:07:33","date_gmt":"2022-10-15T08:07:33","guid":{"rendered":"http:\/\/www.szryc.com\/?p=10355"},"modified":"2022-10-15T16:07:33","modified_gmt":"2022-10-15T08:07:33","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e8%84%89%e5%86%b2%e5%8a%a0%e6%96%b9%e5%90%91%e6%96%b9%e5%bc%8f%e5%af%b9%e6%ad%a5%e8%bf%9b%e7%94%b5%e6%9c%ba%e8%bf%9b%e8%a1%8c%e4%bd%8d%e7%bd%ae%e7%9a%84%e8%87%aa","status":"publish","type":"post","link":"http:\/\/www.szryc.com\/?p=10355","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u8109\u51b2\u52a0\u65b9\u5411\u65b9\u5f0f\u5bf9\u6b65\u8fdb\u7535\u673a\u8fdb\u884c\u4f4d\u7f6e\u7684\u81ea\u7531\u63a7\u5236"},"content":{"rendered":"

\u603b\u4f53\u4ecb\u7ecd<\/strong><\/p>\n

\u5bf9\u6b65\u8fdb\u7535\u673a<\/u>\u7684\u63a7\u5236\u901a\u5e38\u4f7f\u7528PWM\u63a7\u5236\uff0c\u6539\u53d8\u9891\u7387\u6765\u63a7\u5236\u901f\u5ea6\uff0c\u7136\u540e\u7edf\u8ba1\u8109\u51b2\u4e2a\u6570\u77e5\u9053\u7535\u673a<\/u>\u5f53\u524d\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5b9e\u73b0\u52a0\u51cf\u901f\u89c4\u5212\uff0c\u6216\u8005\u4e0d\u8003\u8651\u52a0\u51cf\u901f\u5e73\u7a33\u6027\uff0c\u76f4\u63a5\u4ee5\u5c0f\u4e8e\u6700\u5927\u542f\u52a8\u901f\u5ea6\u7684\u901f\u5ea6\u542f\u52a8\uff0c\u53d1\u5b8c\u7ed9\u5b9a\u4e2a\u8109\u51b2\u540e\u76f4\u63a5\u5173\u95ed\u5b9a\u65f6\u5668<\/u>\u3002\u4ee5\u4e0a\u63a7\u5236\u65b9\u5f0f\u90fd\u6ca1\u5b9e\u73b0\u5bf9\u6b65\u8fdb\u7535\u673a\u7684\u4f4d\u7f6e\u7684\u81ea\u7531\u63a7\u5236\uff0c\u5373\u8ba9\u6b65\u8fdb\u7535\u673a\u8ddf\u968f\u4efb\u610f\u4f4d\u7f6e\u66f2\u7ebf\u8fd0\u52a8\uff0c\u6b64\u9879\u76ee\u662f\u4e3a\u4e86\u5b9e\u73b0\u6b65\u8fdb\u7535\u673a\u7684\u81ea\u7531\u63a7\u5236\uff0c\u80fd\u51c6\u786e\u5b9a\u4f4d\u3002\u53ef\u4ee5\u4f7f\u7528\u7f16\u7801\u5668<\/u>\u6216\u8005\u7535\u4f4d\u5668\u4f5c\u4e3a\u63a7\u5236\u5668\uff0c\u7528\u624b\u62e7\u7f16\u7801\u5668\uff0c\u6b65\u8fdb\u7535\u673a\u53ef\u8ddf\u968f\u4e00\u8d77\u8fd0\u52a8\uff0c\u4e5f\u53ef\u4ee5\u6309\u7167\u51fd\u6570\u66f2\u7ebf\u8fd0\u52a8\u3002<\/p>\n

\u6b65\u8fdb\u7535\u673a\u9a71\u52a8<\/strong><\/p>\n

\u6b65\u8fdb\u7535\u673a\u9a71\u52a8\u5668\u6709\u5f88\u591a\u79cd\uff0c\u5982A4988\u3001TMC2208\u7b49\uff0c\u5e38\u7528\u7684\u9a71\u52a8\u65b9\u5f0f\u662f\u8109\u51b2\u52a0\u65b9\u5411\uff0c\u6709\u7684\u9ad8\u7aef\u70b9\u7684\u4f7f\u7528can<\/u>\u3001\u4e32\u53e3\u7b49\u65b9\u5f0f\u63a7\u5236\u3002\u672c\u7bc7\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8109\u51b2\u52a0\u65b9\u5411\u65b9\u5f0f\u5bf9\u6b65\u8fdb\u7535\u673a\u8fdb\u884c\u4f4d\u7f6e\u7684\u81ea\u7531\u63a7\u5236\uff01<\/p>\n

\u63a7\u5236\u539f\u7406<\/strong><\/p>\n

\u4f7f\u7528\u901a\u4fe1\u534f\u8bae\u65b9\u5f0f\u63a7\u5236\u6b65\u8fdb\u7535\u673a\uff0c\u53ef\u5468\u671f\u6027\u540c\u6b65\u4f4d\u7f6e\u5230\u9a71\u52a8\u5668\uff0c\u5b9e\u73b0\u4f4d\u7f6e\u7684\u81ea\u7531\u63a7\u5236\uff0c\u8109\u51b2\u52a0\u65b9\u5411\u65b9\u5f0f\u4e5f\u53ef\u62bd\u8c61\u4e3a\u4f7f\u7528\u901a\u4fe1\u534f\u8bae\u5728\u4e0e\u9a71\u52a8\u5668\u901a\u4fe1\uff0c\u53ea\u4e0d\u8fc7\u662f\u4f7f\u7528\u589e\u91cf\u65b9\u5f0f\u5728\u901a\u4fe1\uff0c\u901a\u8fc7\u5bf9\u8109\u51b2\u7684\u7d2f\u8ba1\u5f97\u5230\u76ee\u6807\u4f4d\u7f6e\u3002<\/p>\n

\u7a0b\u5e8f\u4f7f\u7528\u4e24\u4e2a\u5b9a\u65f6\u5668\uff0c\u4e00\u4e2a\u4e2d\u65ad\u9891\u7387\u4e3a1k\uff0c\u7528\u4e8e\u5468\u671f\u91c7\u6837\u76ee\u6807\u4f4d\u7f6e\uff0c\u5e76\u8ba1\u7b97\u5f53\u524d\u901f\u5ea6\uff0c\u5f53\u524d\u901f\u5ea6\u503c\u7528\u4e8e\u4fee\u6539\u53e6\u4e00\u4e2a\u5b9a\u65f6\u5668\u4e2d\u65ad\u9891\u7387\uff0c\u6240\u4ee5\u5728\u7b2c\u4e8c\u4e2a\u5b9a\u65f6\u5668\u4e2d\u5224\u65ad\u76ee\u6807\u4f4d\u7f6e\u4e0e\u5f53\u524d\u4f4d\u7f6e\u7684\u504f\u5dee\uff0c\u7136\u540e\u7ffb\u8f6c\u7535\u5e73\uff0c\u5b9e\u73b0\u5bf9\u8109\u51b2\u53d1\u9001\uff0c\u540c\u65f6\u5224\u65ad\u65b9\u5411\uff0c\u5bf9\u5e94\u63a7\u5236\u65b9\u5411\u63a7\u5236IO\u7535\u5e73\u3002<\/p>\n

\u5728\u5176\u4ed6\u51fd\u6570\u4e2d\u53ef\u7ed9\u5b9a\u4efb\u610f\u5f62\u5f0f\u7684\u4f4d\u7f6e\u53d8\u5316\uff0c\u6839\u636e\u91c7\u6837\u5b9a\u7406\uff0c\u5e94\u8be5\u4f4d\u7f6e\u53d8\u5316\u9891\u7387\u4e0d\u5927\u4e8e500Hz\u7684\u90fd\u80fd\u88ab1k\u7684\u5b9a\u65f6\u5668\u4e2d\u65ad\u6b63\u5e38\u91c7\u6837\uff0c\u7531\u4e8e\u53d1\u9001\u8109\u51b2\u9700\u8981\u4ee5\u4e00\u5b9a\u7684\u9891\u7387\u53d1\u9001\uff0c\u6240\u4ee5\u7b2c\u4e8c\u4e2a\u5b9a\u65f6\u5668\u9891\u7387\u6839\u636e\u76ee\u6807\u4f4d\u7f6e\u53d8\u5316\u7387\u800c\u6539\u53d8\uff0c\u53ef\u4ee5\u8ba9\u901f\u5ea6\u5e73\u6ed1\uff0c\u4e5f\u53ef\u4ee5\u51cf\u5c0fCPU<\/u>\u5e26\u5bbd\u5360\u7528\u3002\u4ee5\u6b64\u65b9\u5f0f\u53ef\u5b9e\u73b0\u5bf9\u6b65\u8fdb\u7535\u673a\u7684\u81ea\u7531\u63a7\u5236\uff0c\u53ef\u4f7f\u7528\u7f16\u7801\u5668\u6216\u51fd\u6570\u968f\u610f\u63a7\u5236\u7535\u673a\uff01<\/p>\n

\u4ee3\u7801\u5206\u6790<\/strong><\/p>\n

\u6b64\u7a0b\u5e8f\u53ef\u5b9e\u73b0\u5bf9\u591a\u4e2a\u6b65\u8fdb\u7535\u673a\u7684\u63a7\u5236\uff0c\u4ee5\u4e0b\u662f\u6b65\u8fdb\u7535\u673a\u7c7b<\/p>\n

\/*\u6b65\u8fdb\u7535\u673a\u63a7\u5236\u7c7b*\/<\/p>\n

typedef struct\u00a0<\/p>\n

{<\/p>\n

\u00a0 \u00a0 volaTIle unsigned long\u00a0 *gpio_dir; \/\/\u7535\u673a\u65b9\u5411\u63a7\u5236GPIO<\/p>\n

\u00a0 \u00a0 volaTIle unsigned long\u00a0 *gpio_pluse; \/\/\u7535\u673a\u8109\u51b2GPIO<\/p>\n

\u00a0 \u00a0 int pluse_count;<\/p>\n

\u00a0 \u00a0 int goal_posiTIon;<\/p>\n

\u00a0 \u00a0 int last_posiTIon;<\/p>\n

\u00a0 \u00a0 int cur_position;<\/p>\n

\u00a0 \u00a0 int pos_bias;<\/p>\n

\u00a0 \u00a0 int speed;<\/p>\n

\u00a0 \u00a0 uint8_t status;<\/p>\n

}stepMotor;<\/p>\n

\u6b65\u8fdb\u7535\u673a\u63a7\u5236\u6838\u5fc3\u51fd\u6570\uff0c\u7531\u4e00\u4e2a\u7b80\u6613\u72b6\u6001\u673a\u7ec4\u6210\uff0c\u6b64\u51fd\u6570\u653e\u4e0a\u9762\u63d0\u5230\u7684\u7b2c\u4e8c\u4e2a\u4e2d\u65ad\u51fd\u6570\u6267\u884c\uff0c\u5148\u5f97\u5230\u5f53\u524d\u504f\u5dee\uff0c\u72b6\u6001\u8f6c\u6362\uff0c\u518d\u53d1\u8109\u51b2<\/p>\n

void StepMotorCtrl(stepMotor *motor)<\/p>\n

{<\/p>\n

switch(motor->status)<\/p>\n

{<\/p>\n

case 0:<\/p>\n

if(motor->goal_position != motor->cur_position)\u00a0 \/\/\u626b\u63cf<\/p>\n

{<\/p>\n

motor->pos_bias = motor->goal_position - motor->cur_position; \/\/\u5f97\u5230\u504f\u5dee<\/p>\n

motor->status = 1;<\/p>\n

}<\/p>\n

break;<\/p>\n

case 1:<\/p>\n

if(motor->pos_bias > 0)<\/p>\n

{<\/p>\n

motor->pluse_count ++;<\/p>\n

*(motor->gpio_dir) = 1;\u00a0 \/\/\u6b63\u65b9\u5411<\/p>\n

*(motor->gpio_pluse) = !*(motor->gpio_pluse);<\/p>\n

if(motor->pluse_count == (motor->pos_bias * 2))<\/p>\n

{<\/p>\n

motor->cur_position += motor->pos_bias;<\/p>\n

motor->pluse_count = 0;<\/p>\n

motor->status = 0;<\/p>\n

}<\/p>\n

}<\/p>\n

else<\/p>\n

{<\/p>\n

motor->pluse_count ++;<\/p>\n

*(motor->gpio_dir) = 0;\u00a0 \/\/\u8d1f\u65b9\u5411<\/p>\n

*(motor->gpio_pluse) = !*(motor->gpio_pluse);<\/p>\n

if(motor->pluse_count == ((-motor->pos_bias) * 2))<\/p>\n

{<\/p>\n

motor->cur_position += motor->pos_bias;<\/p>\n

motor->pluse_count = 0;<\/p>\n

motor->status = 0;<\/p>\n

}<\/p>\n

}<\/p>\n

break;<\/p>\n

default:<\/p>\n

break;<\/p>\n

}<\/p>\n

}<\/p>\n","protected":false},"excerpt":{"rendered":"

\u603b\u4f53\u4ecb\u7ecd \u5bf9\u6b65\u8fdb\u7535\u673a\u7684\u63a7\u5236\u901a\u5e38\u4f7f\u7528PWM\u63a7\u5236\uff0c\u6539\u53d8\u9891\u7387\u6765\u63a7\u5236\u901f\u5ea6\uff0c\u7136\u540e\u7edf\u8ba1\u8109\u51b2\u4e2a\u6570\u77e5\u9053\u7535\u673a\u5f53\u524d\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10355"}],"collection":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10355"}],"version-history":[{"count":1,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10355\/revisions"}],"predecessor-version":[{"id":10357,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/10355\/revisions\/10357"}],"wp:attachment":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10355"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}