返回列表 发帖

AT89C2051驱动步进电机的电路和源码

电路如图所示:



程序
  1. stepper.c
  2. stepper.hex

  3. /*
  4. * STEPPER.C
  5. * sweeping stepper's rotor cw and cww 400 steps
  6. * Copyright (c) 1999 by W.Sirichote
  7. */

  8. #include c:\mc51\8051io.h /* include i/o header file */
  9. #include c:\mc51\8051reg.h

  10. register unsigned char j,flag1,temp;
  11. register unsigned int cw_n,ccw_n;

  12. unsigned char step[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90}
  13. #define n 400

  14. /* flag1 mask byte
  15. 0x01 run cw()
  16. 0x02 run ccw()
  17. */

  18. main()

  19. {
  20. flag1=0;
  21. serinit(9600);
  22. disable(); /* no need timer interrupt */
  23. cw_n = n; /* initial step number for cw */
  24. flag1 |=0x01; /* initial enable cw() */

  25. while(1){
  26. {
  27. tick_wait(); /* wait for 10ms elapsed */

  28. energize(); /* round-robin execution the following tasks every 10ms */
  29. cw();
  30. ccw();
  31. }
  32. }

  33. }

  34. cw(){
  35. if((flag1&0x01)!=0)
  36. {
  37. cw_n--; /* decrement cw step number */
  38. if (cw_n !=0)
  39. j++; /* if not zero increment index j */
  40. else
  41. {flag1&=~0x01; /* disable cw() execution */
  42. ccw_n = n; /* reload step number to ccw counter */
  43. flag1 |=0x02; /* enable cww() execution */
  44. }
  45. }

  46. }

  47. ccw(){
  48. if((flag1&0x02)!=0)
  49. {
  50. ccw_n--; /* decremnent ccw step number */
  51. if (ccw_n !=0)
  52. j--; /* if not zero decrement index j */
  53. else
  54. {flag1&=~0x02; /* disable ccw() execution */
  55. cw_n = n; /* reload step number to cw counter */
  56. flag1 |=0x01; /* enable cw() execution */
  57. }
  58. }

  59. }

  60. tick_wait(){ /* cputick was replaced by simpler ASM code 10ms wait */

  61. asm" JNB TCON.5,*"; /* wait for TF0 set */
  62. asm" CLR TCON.5"; /* clear TF0 for further set */
  63. asm" ORL TH0,#$DC"; /* reload TH0 with $DC, TL0 = 0 */
  64. }

  65. energize(){

  66. P1 = step[(j&0x07)]; /* only step 0-7 needed */

  67. }
复制代码
一心一意做事,一心一意做人,做站如做人。关于网站上的事务你可以直接联系我:13320088060。

不错不错,顶一个!!
I CAN

TOP

返回列表

最新关注 关闭


关于论坛注册,最新修改,请网友们注意

由于最近大量垃圾信息出现在我们的论坛,为了营造一个良好的氛围,目前论坛只开发邀请注册,你可以点击以下链接自动邀请注册(如果有人使用了点击下一个试一试) ...


查看
珩源工控论坛热诚欢迎您联系我们进行合作!

Powered by Discuz! 7.2© 2001-2009 Comsenz Inc.

珩源工控论坛 ( 桂ICP备19004328号) |论坛统计|WAP| 客服中心-www.hymcu.com
  

GMT+8, 2024-11-28 13:43, Processed in 1.060743 second(s), 6 queries, Gzip enabled.