久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4248|回復: 1
打印 上一主題 下一主題
收起左側

Arduino+L293D的原理庫,以及源碼等資料

[復制鏈接]
跳轉到指定樓層
樓主
這是我在找了很多資料后,親自調試好用的資料,

電路原理圖如下:


Arduino is a great starting point for electronics, and with a motor shield it can also be a nice tidy platform for robotics and mechatronics. Here is a design for a full-featured motor shield that will be able to power many simple to medium-complexity projects.
•    2 connections for 5V 'hobby' servos connected to the Arduino's high-resolution dedicated timer - no jitter!
•    Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
•    Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
•    4 H-Bridges: L293D chipset provides 0.6A per bridge (1.2A peak) with thermal shutdown protection, 4.5V to12V
•    Pull down resistors keep motors disabled during power-up
•    Big terminal block connectors to easily hook up wires (10-22AWG) and power
•    Arduino reset button brought up top
•    2-pin terminal block to connect external power, for seperate logic/motor supplies
•    Tested compatible with Mega, Diecimila, & Duemilanove
•    附件程序在arduino-022版本使用,需要在其它版本上運行,可能需要修改頭文件

Arduino源程序如下:
  1. // Adafruit Motor shield library
  2. // copyright Adafruit Industries LLC, 2009
  3. // this code is public domain, enjoy!


  4. #include <avr/io.h>
  5. #include "WProgram.h"
  6. #include "AFMotor.h"

  7. static uint8_t latch_state;

  8. #if (MICROSTEPS == 8)
  9. uint8_t microstepcurve[] = {0, 50, 98, 142, 180, 212, 236, 250, 255};
  10. #elif (MICROSTEPS == 16)
  11. uint8_t microstepcurve[] = {0, 25, 50, 74, 98, 120, 141, 162, 180, 197, 212, 225, 236, 244, 250, 253, 255};
  12. #endif

  13. AFMotorController::AFMotorController(void) {
  14. }

  15. void AFMotorController::enable(void) {
  16.   // setup the latch
  17.   /*
  18.   LATCH_DDR |= _BV(LATCH);
  19.   ENABLE_DDR |= _BV(ENABLE);
  20.   CLK_DDR |= _BV(CLK);
  21.   SER_DDR |= _BV(SER);
  22.   */
  23.   pinMode(MOTORLATCH, OUTPUT);
  24.   pinMode(MOTORENABLE, OUTPUT);
  25.   pinMode(MOTORDATA, OUTPUT);
  26.   pinMode(MOTORCLK, OUTPUT);

  27.   latch_state = 0;

  28.   latch_tx();  // "reset"

  29.   //ENABLE_PORT &= ~_BV(ENABLE); // enable the chip outputs!
  30.   digitalWrite(MOTORENABLE, LOW);
  31. }


  32. void AFMotorController::latch_tx(void) {
  33.   uint8_t i;

  34.   //LATCH_PORT &= ~_BV(LATCH);
  35.   digitalWrite(MOTORLATCH, LOW);

  36.   //SER_PORT &= ~_BV(SER);
  37.   digitalWrite(MOTORDATA, LOW);

  38.   for (i=0; i<8; i++) {
  39.     //CLK_PORT &= ~_BV(CLK);
  40.     digitalWrite(MOTORCLK, LOW);

  41.     if (latch_state & _BV(7-i)) {
  42.       //SER_PORT |= _BV(SER);
  43.       digitalWrite(MOTORDATA, HIGH);
  44.     } else {
  45.       //SER_PORT &= ~_BV(SER);
  46.       digitalWrite(MOTORDATA, LOW);
  47.     }
  48.     //CLK_PORT |= _BV(CLK);
  49.     digitalWrite(MOTORCLK, HIGH);
  50.   }
  51.   //LATCH_PORT |= _BV(LATCH);
  52.   digitalWrite(MOTORLATCH, HIGH);
  53. }

  54. static AFMotorController MC;


  55. /******************************************
  56.                MOTORS
  57. ******************************************/
  58. inline void initPWM1(uint8_t freq) {
  59. #if defined(__AVR_ATmega8__) || \
  60.     defined(__AVR_ATmega48__) || \
  61.     defined(__AVR_ATmega88__) || \
  62.     defined(__AVR_ATmega168__) || \
  63.     defined(__AVR_ATmega328P__)
  64.     // use PWM from timer2A on PB3 (Arduino pin #11)
  65.     TCCR2A |= _BV(COM2A1) | _BV(WGM20) | _BV(WGM21); // fast PWM, turn on oc2a
  66.     TCCR2B = freq & 0x7;
  67.     OCR2A = 0;
  68. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  69.     // on arduino mega, pin 11 is now PB5 (OC1A)
  70.     TCCR1A |= _BV(COM1A1) | _BV(WGM10); // fast PWM, turn on oc1a
  71.     TCCR1B = (freq & 0x7) | _BV(WGM12);
  72.     OCR1A = 0;
  73. #else
  74.    #error "This chip is not supported!"
  75. #endif
  76.     pinMode(11, OUTPUT);
  77. }

  78. inline void setPWM1(uint8_t s) {
  79. #if defined(__AVR_ATmega8__) || \
  80.     defined(__AVR_ATmega48__) || \
  81.     defined(__AVR_ATmega88__) || \
  82.     defined(__AVR_ATmega168__) || \
  83.     defined(__AVR_ATmega328P__)
  84.     // use PWM from timer2A on PB3 (Arduino pin #11)
  85.     OCR2A = s;
  86. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  87.     // on arduino mega, pin 11 is now PB5 (OC1A)
  88.     OCR1A = s;
  89. #else
  90.    #error "This chip is not supported!"
  91. #endif
  92. }

  93. inline void initPWM2(uint8_t freq) {
  94. #if defined(__AVR_ATmega8__) || \
  95.     defined(__AVR_ATmega48__) || \
  96.     defined(__AVR_ATmega88__) || \
  97.     defined(__AVR_ATmega168__) || \
  98.     defined(__AVR_ATmega328P__)
  99.     // use PWM from timer2B (pin 3)
  100.     TCCR2A |= _BV(COM2B1) | _BV(WGM20) | _BV(WGM21); // fast PWM, turn on oc2b
  101.     TCCR2B = freq & 0x7;
  102.     OCR2B = 0;
  103. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  104.     // on arduino mega, pin 3 is now PE5 (OC3C)
  105.     TCCR3A |= _BV(COM1C1) | _BV(WGM10); // fast PWM, turn on oc3c
  106.     TCCR3B = (freq & 0x7) | _BV(WGM12);
  107.     OCR3C = 0;
  108. #else
  109.    #error "This chip is not supported!"
  110. #endif

  111.     pinMode(3, OUTPUT);
  112. }

  113. inline void setPWM2(uint8_t s) {
  114. #if defined(__AVR_ATmega8__) || \
  115.     defined(__AVR_ATmega48__) || \
  116.     defined(__AVR_ATmega88__) || \
  117.     defined(__AVR_ATmega168__) || \
  118.     defined(__AVR_ATmega328P__)
  119.     // use PWM from timer2A on PB3 (Arduino pin #11)
  120.     OCR2B = s;
  121. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  122.     // on arduino mega, pin 11 is now PB5 (OC1A)
  123.     OCR3C = s;
  124. #else
  125.    #error "This chip is not supported!"
  126. #endif
  127. }

  128. inline void initPWM3(uint8_t freq) {
  129. #if defined(__AVR_ATmega8__) || \
  130.     defined(__AVR_ATmega48__) || \
  131.     defined(__AVR_ATmega88__) || \
  132.     defined(__AVR_ATmega168__) || \
  133.     defined(__AVR_ATmega328P__)
  134.     // use PWM from timer0A / PD6 (pin 6)
  135.     TCCR0A |= _BV(COM0A1) | _BV(WGM00) | _BV(WGM01); // fast PWM, turn on OC0A
  136.     //TCCR0B = freq & 0x7;
  137.     OCR0A = 0;
  138. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  139.     // on arduino mega, pin 6 is now PH3 (OC4A)
  140.     TCCR4A |= _BV(COM1A1) | _BV(WGM10); // fast PWM, turn on oc4a
  141.     TCCR4B = (freq & 0x7) | _BV(WGM12);
  142.     //TCCR4B = 1 | _BV(WGM12);
  143.     OCR4A = 0;
  144. #else
  145.    #error "This chip is not supported!"
  146. #endif
  147.     pinMode(6, OUTPUT);
  148. }

  149. inline void setPWM3(uint8_t s) {
  150. #if defined(__AVR_ATmega8__) || \
  151.     defined(__AVR_ATmega48__) || \
  152.     defined(__AVR_ATmega88__) || \
  153.     defined(__AVR_ATmega168__) || \
  154.     defined(__AVR_ATmega328P__)
  155.     // use PWM from timer0A on PB3 (Arduino pin #6)
  156.     OCR0A = s;
  157. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  158.     // on arduino mega, pin 6 is now PH3 (OC4A)
  159.     OCR4A = s;
  160. #else
  161.    #error "This chip is not supported!"
  162. #endif
  163. }



  164. inline void initPWM4(uint8_t freq) {
  165. #if defined(__AVR_ATmega8__) || \
  166.     defined(__AVR_ATmega48__) || \
  167.     defined(__AVR_ATmega88__) || \
  168.     defined(__AVR_ATmega168__) || \
  169.     defined(__AVR_ATmega328P__)
  170.     // use PWM from timer0B / PD5 (pin 5)
  171.     TCCR0A |= _BV(COM0B1) | _BV(WGM00) | _BV(WGM01); // fast PWM, turn on oc0a
  172.     //TCCR0B = freq & 0x7;
  173.     OCR0B = 0;
  174. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  175.     // on arduino mega, pin 5 is now PE3 (OC3A)
  176.     TCCR3A |= _BV(COM1A1) | _BV(WGM10); // fast PWM, turn on oc3a
  177.     TCCR3B = (freq & 0x7) | _BV(WGM12);
  178.     //TCCR4B = 1 | _BV(WGM12);
  179.     OCR3A = 0;
  180. #else
  181.    #error "This chip is not supported!"
  182. #endif
  183.     pinMode(5, OUTPUT);
  184. }

  185. inline void setPWM4(uint8_t s) {
  186. #if defined(__AVR_ATmega8__) || \
  187.     defined(__AVR_ATmega48__) || \
  188.     defined(__AVR_ATmega88__) || \
  189.     defined(__AVR_ATmega168__) || \
  190.     defined(__AVR_ATmega328P__)
  191.     // use PWM from timer0A on PB3 (Arduino pin #6)
  192.     OCR0B = s;
  193. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  194.     // on arduino mega, pin 6 is now PH3 (OC4A)
  195.     OCR3A = s;
  196. #else
  197.    #error "This chip is not supported!"
  198. #endif
  199. }

  200. AF_DCMotor::AF_DCMotor(uint8_t num, uint8_t freq) {
  201.   motornum = num;
  202.   pwmfreq = freq;

  203.   MC.enable();

  204.   switch (num) {
  205.   case 1:
  206.     latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B); // set both motor pins to 0
  207.     MC.latch_tx();
  208.     initPWM1(freq);
  209.     break;
  210.   case 2:
  211.     latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // set both motor pins to 0
  212.     MC.latch_tx();
  213.     initPWM2(freq);
  214.     break;
  215.   case 3:
  216.     latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B); // set both motor pins to 0
  217.     MC.latch_tx();
  218.     initPWM3(freq);
  219.     break;
  220.   case 4:
  221.     latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // set both motor pins to 0
  222.     MC.latch_tx();
  223.     initPWM4(freq);
  224.     break;
  225.   }
  226. }

  227. void AF_DCMotor::run(uint8_t cmd) {
  228.   uint8_t a, b;
  229.   switch (motornum) {
  230.   case 1:
  231.     a = MOTOR1_A; b = MOTOR1_B; break;
  232.   case 2:
  233.     a = MOTOR2_A; b = MOTOR2_B; break;
  234.   case 3:
  235.     a = MOTOR3_A; b = MOTOR3_B; break;
  236.   case 4:
  237.     a = MOTOR4_A; b = MOTOR4_B; break;
  238.   default:
  239.     return;
  240.   }
  241.   
  242.   switch (cmd) {
  243.   case FORWARD:
  244.     latch_state |= _BV(a);
  245.     latch_state &= ~_BV(b);
  246.     MC.latch_tx();
  247.     break;
  248.   case BACKWARD:
  249.     latch_state &= ~_BV(a);
  250.     latch_state |= _BV(b);
  251.     MC.latch_tx();
  252.     break;
  253.   case RELEASE:
  254.     latch_state &= ~_BV(a);
  255.     latch_state &= ~_BV(b);
  256.     MC.latch_tx();
  257.     break;
  258.   }
  259. }

  260. void AF_DCMotor::setSpeed(uint8_t speed) {
  261.   switch (motornum) {
  262.   case 1:
  263.     setPWM1(speed); break;
  264.   case 2:
  265.     setPWM2(speed); break;
  266.   case 3:
  267.     setPWM3(speed); break;
  268.   case 4:
  269.     setPWM4(speed); break;
  270.   }
  271. }

  272. /******************************************
  273.                STEPPERS
  274. ******************************************/

  275. AF_Stepper::AF_Stepper(uint16_t steps, uint8_t num) {
  276.   MC.enable();

  277.   revsteps = steps;
  278.   steppernum = num;
  279.   currentstep = 0;

  280.   if (steppernum == 1) {
  281.     latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B) &
  282.       ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // all motor pins to 0
  283.     MC.latch_tx();
  284.    
  285.     // enable both H bridges
  286.     pinMode(11, OUTPUT);
  287.     pinMode(3, OUTPUT);
  288.     digitalWrite(11, HIGH);
  289.     digitalWrite(3, HIGH);

  290.     // use PWM for microstepping support
  291.     initPWM1(MOTOR12_64KHZ);
  292.     initPWM2(MOTOR12_64KHZ);
  293.     setPWM1(255);
  294.     setPWM2(255);

  295.   } else if (steppernum == 2) {
  296.     latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B) &
  297.       ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // all motor pins to 0
  298.     MC.latch_tx();

  299.     // enable both H bridges
  300.     pinMode(5, OUTPUT);
  301.     pinMode(6, OUTPUT);
  302.     digitalWrite(5, HIGH);
  303.     digitalWrite(6, HIGH);

  304.     // use PWM for microstepping support
  305.     // use PWM for microstepping support
  306.     initPWM3(1);
  307.     initPWM4(1);
  308.     setPWM3(255);
  309.     setPWM4(255);
  310.   }
  311. }

  312. void AF_Stepper::setSpeed(uint16_t rpm) {
  313.   usperstep = 60000000 / ((uint32_t)revsteps * (uint32_t)rpm);
  314.   steppingcounter = 0;
  315. }

  316. void AF_Stepper::release(void) {
  317.   if (steppernum == 1) {
  318.     latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B) &
  319.       ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // all motor pins to 0
  320.     MC.latch_tx();
  321.   } else if (steppernum == 2) {
  322.     latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B) &
  323.       ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // all motor pins to 0
  324.     MC.latch_tx();
  325.   }
  326. }

  327. void AF_Stepper::step(uint16_t steps, uint8_t dir,  uint8_t style) {
  328.   uint32_t uspers = usperstep;
  329.   uint8_t ret = 0;

  330.   if (style == INTERLEAVE) {
  331.     uspers /= 2;
  332.   }
  333. else if (style == MICROSTEP) {
  334.     uspers /= MICROSTEPS;
  335.     steps *= MICROSTEPS;
  336. #ifdef MOTORDEBUG
  337.     Serial.print("steps = "); Serial.println(steps, DEC);
  338. #endif
  339.   }

  340.   while (steps--) {
  341.     ret = onestep(dir, style);
  342.     delay(uspers/1000); // in ms
  343.     steppingcounter += (uspers % 1000);
  344.     if (steppingcounter >= 1000) {
  345.       delay(1);
  346.       steppingcounter -= 1000;
  347.     }
  348.   }
  349.   if (style == MICROSTEP) {
  350.     while ((ret != 0) && (ret != MICROSTEPS)) {
  351.       ret = onestep(dir, style);
  352.       delay(uspers/1000); // in ms
  353.       steppingcounter += (uspers % 1000);
  354.       if (steppingcounter >= 1000) {
  355.         delay(1);
  356.         steppingcounter -= 1000;
  357.       }
  358.     }
  359.   }
  360. }

  361. uint8_t AF_Stepper::onestep(uint8_t dir, uint8_t style) {
  362.   uint8_t a, b, c, d;
  363.   uint8_t ocrb, ocra;

  364.   ocra = ocrb = 255;

  365.   if (steppernum == 1) {
  366.     a = _BV(MOTOR1_A);
  367.     b = _BV(MOTOR2_A);
  368.     c = _BV(MOTOR1_B);
  369.     d = _BV(MOTOR2_B);
  370.   } else if (steppernum == 2) {
  371.     a = _BV(MOTOR3_A);
  372.     b = _BV(MOTOR4_A);
  373.     c = _BV(MOTOR3_B);
  374.     d = _BV(MOTOR4_B);
  375.   } else {
  376.     return 0;
  377.   }

  378.   // next determine what sort of stepping procedure we're up to
  379.   if (style == SINGLE) {
  380.     if ((currentstep/(MICROSTEPS/2)) % 2) { // we're at an odd step, weird
  381.       if (dir == FORWARD) {
  382.         currentstep += MICROSTEPS/2;
  383.       }
  384.       else {
  385.         currentstep -= MICROSTEPS/2;
  386.       }
  387.     } else {           // go to the next even step
  388.       if (dir == FORWARD) {
  389.         currentstep += MICROSTEPS;
  390.       }
  391.       else {
  392.         currentstep -= MICROSTEPS;
  393.       }
  394.     }
  395.   } else if (style == DOUBLE) {
  396.     if (! (currentstep/(MICROSTEPS/2) % 2)) { // we're at an even step, weird
  397.       if (dir == FORWARD) {
  398.         currentstep += MICROSTEPS/2;
  399.       } else {
  400.         currentstep -= MICROSTEPS/2;
  401.       }
  402.     } else {           // go to the next odd step
  403.       if (dir == FORWARD) {
  404.         currentstep += MICROSTEPS;
  405.       } else {
  406.         currentstep -= MICROSTEPS;
  407.       }
  408.     }
  409.   } else if (style == INTERLEAVE) {
  410.     if (dir == FORWARD) {
  411.        currentstep += MICROSTEPS/2;
  412.     } else {
  413.        currentstep -= MICROSTEPS/2;
  414.     }
  415.   }

  416.   if (style == MICROSTEP) {
  417.     if (dir == FORWARD) {
  418.       currentstep++;
  419.     } else {
  420.       // BACKWARDS
  421.       currentstep--;
  422.     }

  423.     currentstep += MICROSTEPS*4;
  424.     currentstep %= MICROSTEPS*4;

  425.     ocra = ocrb = 0;
  426.     if ( (currentstep >= 0) && (currentstep < MICROSTEPS)) {
  427.       ocra = microstepcurve[MICROSTEPS - currentstep];
  428.       ocrb = microstepcurve[currentstep];
  429.     } else if  ( (currentstep >= MICROSTEPS) && (currentstep < MICROSTEPS*2)) {
  430.       ocra = microstepcurve[currentstep - MICROSTEPS];
  431.       ocrb = microstepcurve[MICROSTEPS*2 - currentstep];
  432.     } else if  ( (currentstep >= MICROSTEPS*2) && (currentstep < MICROSTEPS*3)) {
  433.       ocra = microstepcurve[MICROSTEPS*3 - currentstep];
  434.       ocrb = microstepcurve[currentstep - MICROSTEPS*2];
  435.     } else if  ( (currentstep >= MICROSTEPS*3) && (currentstep < MICROSTEPS*4)) {
  436.       ocra = microstepcurve[currentstep - MICROSTEPS*3];
  437.       ocrb = microstepcurve[MICROSTEPS*4 - currentstep];
  438.     }
  439.   }

  440.   currentstep += MICROSTEPS*4;
  441.   currentstep %= MICROSTEPS*4;

  442. #ifdef MOTORDEBUG
  443.   Serial.print("current step: "); Serial.println(currentstep, DEC);
  444.   Serial.print(" pwmA = "); Serial.print(ocra, DEC);
  445.   Serial.print(" pwmB = "); Serial.println(ocrb, DEC);
  446. ……………………

  447. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:

L293D資料.7z (58.5 KB, 下載次數: 33)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

沙發
ID:510096 發表于 2019-4-11 23:22 | 只看該作者
看看怎么做
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品久久一区 | 久久精品久久精品久久精品 | 国产精品高潮呻吟久久av野狼 | 国产美女一区二区 | 91资源在线 | 成人免费淫片aa视频免费 | 中国一级特黄真人毛片 | 亚洲电影免费 | 国产一区二区精品 | 91亚洲一区| 欧美日韩高清免费 | 欧美日韩精品一区二区 | 亚洲精品久久 | 91美女在线观看 | 在线看片国产精品 | 精品久久久av| 日韩一区二区成人 | 亚洲综合在 | 中文字幕在线视频精品 | 九色porny自拍视频 | 亚洲精品乱码久久久久久按摩观 | 精品国产欧美 | 欧美不卡一区二区三区 | 不卡一区二区在线观看 | 日韩中文字幕一区二区 | 国产一区二区久久久 | 国产福利在线 | 福利社午夜影院 | 精产国产伦理一二三区 | 一区视频| 91精品国产综合久久久密闭 | 欧美 日韩 国产 成人 | 国产 日韩 欧美 在线 | 欧美日韩一区在线播放 | av一级久久| 婷婷综合在线 | 2018天天干天天操 | 国产99免费视频 | 久久久久久天堂 | 国产福利在线看 | 午夜精品视频在线观看 |