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

QQ登錄

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

搜索
查看: 3783|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

AVR單片機(jī)驅(qū)動(dòng)二相步進(jìn)電機(jī)(單極驅(qū)動(dòng))簡(jiǎn)易程序,帶串口控制

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:76127 發(fā)表于 2015-4-8 21:15 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

  1. AVR單片機(jī)驅(qū)動(dòng)二相步進(jìn)電機(jī)(單極驅(qū)動(dòng))簡(jiǎn)易程序,帶串口控制。
  2. 編譯器為ICC6.31A;
  3. #include"iom16v.h"
  4. #include"macros.h"
  5. #define uint unsigned int
  6. #define uchar unsigned char
  7. #pragma interrupt_handler usart:14
  8. uchar i;
  9. uchar  const *p;
  10. uchar zf=1; //電機(jī)正反轉(zhuǎn)標(biāo)志;
  11. uchar  k1=5;
  12. //uchar const timer2dath[10]={};
  13. //uchar const timer2datl[10]={};
  14. unsigned char const suzhu1[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
  15.                       0x77,0x7c,0x39,0x5e,0x79,0x71};
  16. uchar const bjdjdat[48]={0x80,0xa0,0x20,0x60,0x40,0x50,0x10,0x90,
  17.                            0x80,0x90,0x10,0x50,0x40,0x60,0x20,0xa0,
  18.          0x80,0x20,0x40,0x10,0x80,0x20,0x40,0x10,
  19.          0x80,0x10,0x40,0x20,0x80,0x10,0x40,0x20,
  20.          0xa0,0x60,0x50,0x90,0xa0,0x60,0x50,0x90,
  21.          0xa0,0x90,0x50,0x60,0xa0,0x90,0x50,0x60
  22.           };//設(shè)置單片機(jī)IO口高低電平數(shù)據(jù)組,用以驅(qū)動(dòng)步進(jìn)電機(jī)的各相按需通電;
  23. uint const timer1dat[11]={36000,23000,13000,7000,4300,3000,2500,2100,1800,1700,0};//定時(shí)器1定時(shí)數(shù)據(jù),用于控制電機(jī)的轉(zhuǎn)速,定時(shí)越短轉(zhuǎn)速越快;            
  24. uchar uartdat[4]={0,0,0,0}; //劃出串行通信時(shí)所需的寄存器;
  25. uchar add,open,jslopen;
  26. void siansi(unsigned int dat,unsigned char bit,unsigned int time)//數(shù)碼管顯示函數(shù)
  27. {
  28. //PORTA&=~BIT(4);
  29. PORTC&=~BIT(7);
  30. PORTA&=~0x17;
  31. PORTB=suzhu1[dat];
  32. PORTA|=bit ;
  33. PORTA|=BIT(4);
  34. PORTC|=BIT(7);
  35. while(time--);

  36. }        
  37. void ioinit()//IO口初始化;
  38. {
  39. DDRB=0XFF;
  40. DDRD=0XF3;
  41. DDRA|=0X17;
  42. DDRC|=0X80;
  43. PORTD|=0X0f;
  44. }
  45. void usart()//串行接收中斷處理;
  46. {
  47. uchar a;
  48. a=UDR;
  49. if(a==168)
  50. {
  51.   open=1;
  52.   add=0;
  53. }
  54. if(a==138&&open)
  55. {
  56.   open=0;
  57.   uartdat[add]=a;
  58.   jslopen=1;
  59. }
  60. if(open)
  61. {
  62. uartdat[add]=a;
  63. add++;
  64. }
  65. }
  66. void jsl()//一幀數(shù)據(jù)處理;
  67. {
  68. uchar j;
  69. jslopen=0;
  70. switch(uartdat[1])
  71. {
  72.   case 1:
  73.   if(uartdat[2])TIMSK|=0X04;
  74.   else TIMSK&=~0X04;
  75.   break;
  76.   case 2:
  77.   if(uartdat[2]>0)
  78.   k1=uartdat[2]-1;
  79.   else k1=10;
  80.   break;
  81.   case 3:
  82.   if(uartdat[2]&&!zf)p=p-8,zf=1;
  83.   if(!uartdat[2]&&zf)p=p+8,zf=0;
  84.   break;
  85.   case 4:
  86. if(uartdat[2]==1&&zf)p=bjdjdat;
  87. if(uartdat[2]==1&&!zf)p=bjdjdat+8;
  88. if(uartdat[2]==2&&zf)p=bjdjdat+16;
  89. if(uartdat[2]==2&&!zf)p=bjdjdat+24;
  90. if(uartdat[2]==3&&zf)p=bjdjdat+32;
  91. if(uartdat[2]==3&&!zf)p=bjdjdat+40;
  92.   break;
  93. }
  94. for(j=0;j<4;j++)
  95. {
  96.   /* 等待發(fā)送緩沖器為空 */
  97.   while ( !( UCSRA & (1<<UDRE)) )
  98. ;
  99. /* 將數(shù)據(jù)放入緩沖器,發(fā)送數(shù)據(jù) */
  100. UDR=uartdat[j];
  101. }
  102. }
  103. void uartinit()//串行口初始化;
  104. {
  105. UCSRA=0X00;
  106. UCSRB=0X98;
  107. UCSRC=0X86;
  108. UBRRH=0X00;
  109. UBRRL=71;
  110. SREG|=0X80;
  111. }
  112. #pragma interrupt_handler timer1:9
  113. void timer1() //定時(shí)器1中斷處理;
  114. {
  115. TCNT1=65536-timer1dat[k1];
  116. i++;
  117. if(i>7)i=0;
  118. PORTD&=0X0F;
  119. if(k1==10)return;
  120. PORTD|=p[i];
  121. }
  122. void timer1init()
  123. {
  124. TCCR1A=0X00;
  125. TCCR1B=0X02;
  126. TIMSK|=0X04;
  127. }
  128. void main(void)//主程序;
  129. {
  130. p=bjdjdat;
  131. uartinit();
  132. ioinit();
  133. timer1init();
  134. while(1)
  135. {
  136.   siansi(timer1dat[k1]%10,7,1000);
  137.   siansi(timer1dat[k1]%100/10,6,1000);
  138.   siansi(timer1dat[k1]%1000/100,5,1000);
  139.   siansi(timer1dat[k1]%10000/1000,4,1000);
  140.   siansi(timer1dat[k1]/10000,3,1000);
  141.   if(zf)siansi(14,2,1000);
  142.   else siansi(15,2,1000);
  143.   if(jslopen)jsl();

  144. }
  145. }
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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