找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2415|回复: 4
收起左侧

怎么修改单片机程序可以使步进电机在满足条件后正转和反转180°

[复制链接]
回帖奖励 10 黑币 回复本帖可获得 10 黑币奖励! 每人限 1 次
ID:757095 发表于 2020-5-22 00:47 | 显示全部楼层 |阅读模式
  1. #include "include.h"

  2. #define   uchar unsigned char
  3. #define   uint unsigned int      
  4. #define   DataPort P2  
  5. #define MotorData P0                    
  6. #define GapValue 305

  7. uchar phasecw[4] ={0x08,0x04,0x02,0x01};
  8. uchar phaseccw[4]={0x01,0x02,0x04,0x08};


  9. sbit LCM_RS=P0^7;
  10. sbit LCM_RW=P0^6;
  11. sbit LCM_EN=P0^5;

  12. sbit IN1 = P3^4;
  13. sbit IN2 = P3^5;
  14. sbit IN3 = P3^6;
  15. sbit IN4 = P3^7;



  16. sbit HX711_DOUT=P3^3;
  17. sbit HX711_SCK=P3^2;


  18. unsigned long Time_Cont = 0;
  19. unsigned char PollCount = 0;

  20. unsigned int qupi=0;
  21. long weight_wuti = 0;
  22. long weight_wutinew = 0;
  23. unsigned long weight_maopi = 0;
  24. unsigned long weight_maopi_0 = 0;

  25. unsigned char tx_buf[5];

  26. void Get_maopi();
  27. void Get_weight();
  28. void Delay__hx711_us(void);
  29. unsigned long HX711_Read(void);





  30. void Delay_ms(unsigned int n)
  31. {
  32.         unsigned int  i,j;
  33.         for(i=0;i<n;i++)
  34.                 for(j=0;j<121;j++);
  35. }





  36. void Delay__hx711_us(void)
  37. {
  38.         _nop_();
  39.         _nop_();
  40. }




  41. unsigned long HX711_Read(void)        
  42. {
  43.         unsigned long count;
  44.         unsigned char i;
  45.           HX711_DOUT=1;
  46.                 Delay__hx711_us();
  47.           HX711_SCK=0;
  48.           count=0;
  49.           while(HX711_DOUT);
  50.           for(i=0;i<24;i++)                              
  51.         {
  52.                   HX711_SCK=1;
  53.                   count=count<<1;
  54.                         HX711_SCK=0;
  55.                   if(HX711_DOUT)
  56.                         count++;
  57.         }
  58.         HX711_SCK=1;
  59.         count=count^0x800000;
  60.         Delay__hx711_us();
  61.         HX711_SCK=0;  
  62.         return(count);
  63. }



  64. void Get_Weight()
  65. {
  66.         weight_wuti = HX711_Read();
  67.         weight_wuti = weight_wuti - weight_maopi;               
  68.         
  69.         
  70.         
  71.         weight_wuti = (unsigned int)((weight_wuti)/GapValue);         
  72. if(weight_wuti>5000)weight_wuti=0;      

  73.                 tx_buf[0]= weight_wuti/1000;
  74.                 tx_buf[1]= weight_wuti/100%10;
  75.                 tx_buf[2]= weight_wuti%100/10;
  76.                 tx_buf[3]= weight_wuti%10;


  77. }




  78. void Get_maopi()
  79. {
  80.         unsigned char clear;
  81. mm:        weight_maopi_0 = HX711_Read();
  82.         for(clear=0;clear<2;clear++)
  83.         {
  84.                 Delay_ms(200);
  85.         }
  86.         weight_maopi = HX711_Read();
  87.         if(weight_maopi/GapValue!=weight_maopi_0/GapValue)
  88.         goto mm;
  89. }

  90. void Timer0Interrupt(void)  interrupt 1  
  91. {
  92.         TH0 = (65536 - 20000)/256;
  93.         TL0 = (65536 - 20000)%256;
  94.         PollCount++;
  95.         if(PollCount==50)
  96.         {
  97.           PollCount=0;

  98.                
  99.         }
  100. }

  101. void positive(unsigned int time)
  102. {
  103.         IN1 = 1;IN2 = 0; IN3 = 0;IN4 = 0;
  104.         Delay_ms(time);
  105.         IN1 = 0;IN2 = 1; IN3 = 0;IN4 = 0;
  106.         Delay_ms(time);
  107.         IN1 = 0;IN2 = 0; IN3 = 1;IN4 = 0;
  108.         Delay_ms(time);
  109.         IN1 = 0;IN2 = 0; IN3 = 0;IN4 = 1;
  110.         Delay_ms(time);
  111. }

  112. unsigned int count;
  113. void run(unsigned int time,unsigned int k)
  114. {
  115.         for(count=0;count<time;count++)
  116.                 positive(k);
  117. }

  118. void MotorCW(void)
  119. {
  120. uchar i;
  121. for(i=0;i<4;i++)
  122.   {
  123.    MotorData=phasecw[i];
  124.    Delay_ms(4);
  125.   }
  126. }

  127. void MotorCCW(void)
  128. {
  129. uchar i;
  130. for(i=0;i<4;i++)
  131.   {
  132.    MotorData=phaseccw[i];
  133.    Delay_ms(4);
  134.   }
  135. }

  136. void MotorStop(void)
  137. {
  138. MotorData=0x00;
  139. }



  140. void main()
  141. {
  142.   InitLcd();
  143.         Get_maopi();
  144.         DisplayOneChar(0,0,'W');
  145.         DisplayOneChar(1,0,'E');
  146.         DisplayOneChar(2,0,'I');
  147.         DisplayOneChar(3,0,'G');
  148.         DisplayOneChar(4,0,':');
  149.       
  150.         DisplayOneChar(0,1,'N');
  151.         DisplayOneChar(1,1,'U');
  152.         DisplayOneChar(2,1,'M');
  153.         DisplayOneChar(3,1,'B');
  154.         DisplayOneChar(4,1,':');
  155.       






  156.         while(1)
  157.         {      

  158.                 Get_weight();
  159.                 DisplayOneChar(5,0,'0'+tx_buf[0]);
  160.                 DisplayOneChar(6,0,'0'+tx_buf[1]);
  161.                 DisplayOneChar(7,0,'0'+tx_buf[2]);
  162.                 DisplayOneChar(8,0,'0'+tx_buf[3]);
  163.                 DisplayOneChar(9,0,'G');
  164.                 if((weight_wutinew-weight_wuti>10)||(weight_wuti-weight_wutinew>10))
  165.                 {
  166.                                 weight_wutinew        = weight_wuti;               
  167.                                 
  168.                                 
  169.                                 
  170.                                         if((weight_wuti>0)&&(weight_wuti<100)){DisplayOneChar(5,1,'1');MotorCW();   
  171.                         
  172.                                   MotorStop();  
  173.       
  174.                           else if(weight_wuti>100){DisplayOneChar(5,1,'2'); MotorCCW(); }
  175.                                   MotorStop();  
  176.       
  177.                                 
  178.                                 
  179.                 }                       
  180.                 Delay_ms(2);
  181.         }
  182. }


  183. /*******************************/
  184. void WaitForEnable(void)      
  185. {                                       
  186.                 DataPort=0xff;               
  187.                 LCM_RS=0;LCM_RW=1;_nop_();
  188.                 LCM_EN=1;_nop_();_nop_();
  189.                 while(DataPort&0x80);      
  190.                 LCM_EN=0;                              
  191. }                                       
  192. /*******************************/
  193. void WriteCommandLCM(uchar CMD,uchar Attribc)
  194. {                                       
  195.                 if(Attribc)WaitForEnable();      
  196.                 LCM_RS=0;LCM_RW=0;_nop_();
  197.                 DataPort=CMD;_nop_();      
  198.                 LCM_EN=1;_nop_();_nop_();LCM_EN=0;
  199. }                                       
  200. /*******************************/
  201. void WriteDataLCM(uchar dataW)
  202. {                                       
  203.                 WaitForEnable();               
  204.                 LCM_RS=1;LCM_RW=0;_nop_();
  205.                 DataPort=dataW;_nop_();      
  206.                 LCM_EN=1;_nop_();_nop_();LCM_EN=0;
  207. }               
  208. /***********************************/
  209. void InitLcd()                              
  210. {                       
  211.                 WriteCommandLCM(0x38,1);      
  212.                 WriteCommandLCM(0x08,1);      
  213.                 WriteCommandLCM(0x01,1);      
  214.                 WriteCommandLCM(0x06,1);      
  215.                 WriteCommandLCM(0x0c,1);
  216. }                       
  217. /***********************************/
  218. void DisplayOneChar(uchar X,uchar Y,uchar DData)
  219. {                                               
  220.                 Y&=1;                                               
  221.                 X&=15;                                               
  222.                 if(Y)X|=0x40;                                       
  223.                 X|=0x80;                       
  224.                 WriteCommandLCM(X,0);               
  225.                 WriteDataLCM(DData);               
  226. }  
复制代码
回复

使用道具 举报

ID:440403 发表于 2020-5-22 08:13 | 显示全部楼层
具体要看步进电机上面的驱动器,一步是多少度,一个脉冲是半步还是一步,单片机只是负责发脉冲
回复

使用道具 举报

ID:757095 发表于 2020-5-22 10:49 | 显示全部楼层
两仪式 发表于 2020-5-22 08:13
具体要看步进电机上面的驱动器,一步是多少度,一个脉冲是半步还是一步,单片机只是负责发脉冲

看了一下,只需解决正反转就行,度数可以随时调整
回复

使用道具 举报

ID:94031 发表于 2020-5-22 13:13 | 显示全部楼层
把你现在的现象描述一下。这一句MotorStop(); 要屏蔽掉,否则每次都要停一下。
回复

使用道具 举报

ID:758414 发表于 2020-5-23 12:08 | 显示全部楼层
MotorStop(); 要屏蔽掉
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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