找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1132|回復(fù): 4
收起左側(cè)

51單片機(jī)+步進(jìn)馬達(dá)延時(shí)程序問(wèn)題

[復(fù)制鏈接]
ID:143767 發(fā)表于 2023-3-5 13:41 | 顯示全部樓層 |閱讀模式
下面程序是上電馬達(dá)轉(zhuǎn)動(dòng)45度后延時(shí)10秒再轉(zhuǎn)動(dòng)45度,往復(fù)循環(huán),我想在馬達(dá)停止后延時(shí)60秒,我把100000這個(gè)值改多大也不能延時(shí)60秒,請(qǐng)問(wèn)這程序該怎么改,請(qǐng)各位給個(gè)提示,謝謝!

單片機(jī)源程序如下:
#include <reg52.h>      
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uint i,j,k ;
uint N=128;    //角度范圍調(diào)節(jié),對(duì)應(yīng)于45度,512對(duì)應(yīng)于360度

uchar code single_pos[4]={0x04,0x08,0x10,0x20};  /*單四拍正轉(zhuǎn)表*/                                                           
//uchar code single_rev[4]={0x20,0x10,0x08,0x04};  /*單四拍反轉(zhuǎn)表*/

void delay(uint z);
void m_single_pos();
//void m_single_rev();

void main()
{
    while(1)
        {
          m_single_pos(); //單四拍正轉(zhuǎn)45度
          delay(100000);
          m_single_pos(); //單四拍正轉(zhuǎn)45度
          delay(100000);                  
        }
}

void delay(uint z) /*延時(shí)z毫秒*/
{
    uint x,y;
        for(x=z;x>0;x--)
           for(y=300;y>0;y--) ;         //轉(zhuǎn)速調(diào)節(jié),數(shù)值越大馬達(dá)越慢
}
            
/******************************單四拍驅(qū)動(dòng)正轉(zhuǎn)(N*45/16)*************************************/  
void m_single_pos()
{
   for(k=0;k<N;k++)            
    {  
         for(i=0;i<4;i++)                                //單四拍一個(gè)脈沖轉(zhuǎn)子轉(zhuǎn)動(dòng)5.625*2=11.625度,四拍共45度
            {
                  P3=single_pos[ i] ;
                  delay(10) ;                //適當(dāng)延時(shí),保證轉(zhuǎn)子轉(zhuǎn)動(dòng)時(shí)間,延時(shí)過(guò)短會(huì)丟拍
                 }
         }
}

回復(fù)

使用道具 舉報(bào)

ID:353115 發(fā)表于 2023-3-5 15:56 | 顯示全部樓層
uint在這里最大也只能是65535,超過(guò)這個(gè)值反而會(huì)變小
回復(fù)

使用道具 舉報(bào)

ID:384109 發(fā)表于 2023-3-5 16:21 | 顯示全部樓層
使用定時(shí)器吧
回復(fù)

使用道具 舉報(bào)

ID:161164 發(fā)表于 2023-3-5 23:11 | 顯示全部樓層
  1. void main()
  2. {
  3.         uchar delay_Cnt = 0;
  4.         while(1)
  5.         {
  6.                 if(delay_Cnt==0)
  7.                 {
  8.                         m_single_pos(); //單四拍正轉(zhuǎn)45度
  9.                 }
  10.                 delay_Cnt++;
  11.                 if(delay_Cnt>=60)delay_Cnt=0;
  12.                 delay(1000);
  13.         }
  14. }
復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

ID:123289 發(fā)表于 2023-3-6 14:13 | 顯示全部樓層
很易想到的,將10秒延時(shí),做6次。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表