2013-12-18 14:17:51 [显示全部楼层]
4998浏览
查看: 4998|回复: 0

[转载] [Arduino模块]MD22驱动双24V 5A电机

[复制链接]

[Arduino模块]

MD22驱动双24V 5A电机

电路示意:



代码示例:
  1. /******************************************************
  2. *            MD22 example code for arduino            *
  3. *     MD22 is in I2C mode LCD03 is in serial mode     *
  4. *                                                     *
  5. *              By James Henderson 2012                *
  6. ******************************************************/
  7. #include <SoftwareSerial.h>
  8. #include <Wire.h>
  9. #define MD22ADDRESS         0x58                              // address of md 22 (all mode switches on)
  10. #define SOFTREG             0x07                              // Byte for reading software register
  11. #define MOTOR1              0x01                              // Byte for first motor
  12. #define MOTOR2              0x02                              // Byte for second motor
  13. #define ACCELLREG           0x03                              // Byte to set acelleration
  14. #define LCD_RX              0x02                              // RX and TX pins used for LCD0303 serial port
  15. #define LCD_TX              0x03
  16. #define LCD03_HIDE_CUR      0x04
  17. #define LCD03_CLEAR         0x0C
  18. SoftwareSerial lcd_03 =  SoftwareSerial(LCD_RX, LCD_TX);      // Defines software serial port for LCD03
  19. void setup(){
  20.   lcd_03.begin(9600);                                         // Begin serial with LCD03
  21.   Wire.begin();
  22.   delay(100);                                                 // Wait for everything to be powered up
  23.   
  24.   lcd_03.write(LCD03_CLEAR);                                  // Clear the LCD03 screen
  25.   lcd_03.write(LCD03_HIDE_CUR);
  26.   getSoftware();                                              // Function that gets and prints software revision to screen
  27.   setMode();                                                  // Function that sets mode to 2 and sets acceleration
  28. }
  29. void loop(){
  30.   delay(1390);
  31.   
  32.   Wire.beginTransmission(MD22ADDRESS);                // Set first motor to a speed of 255
  33.   Wire.write(MOTOR1);
  34.   Wire.write(255);
  35.   Wire.endTransmission();
  36.   
  37.   Wire.beginTransmission(MD22ADDRESS);                // Set second motor to stop
  38.   Wire.write(MOTOR2);
  39.   Wire.write(128);
  40.   Wire.endTransmission();
  41.   
  42.   delay(1390);
  43.   
  44.   Wire.beginTransmission(MD22ADDRESS);                // Set first motor to speed 0
  45.   Wire.write(MOTOR1);
  46.   Wire.write((byte)0);                                // Values of 0 being sent using write have to be cast as a byteto stop them being misinterperted as NULL this is bug with arduino 1
  47.   Wire.endTransmission();
  48.   
  49.   Wire.beginTransmission(MD22ADDRESS);                // Set second motor to stop
  50.   Wire.write(MOTOR2);
  51.   Wire.write(128);
  52.   Wire.endTransmission();
  53.   
  54.   delay(1390);
  55.   
  56.   Wire.beginTransmission(MD22ADDRESS);                // Set first motor to stop
  57.   Wire.write(MOTOR1);
  58.   Wire.write(128);
  59.   Wire.endTransmission();
  60.   
  61.   Wire.beginTransmission(MD22ADDRESS);                // Set second motor to speed 255
  62.   Wire.write(MOTOR2);  
  63.   Wire.write(255);
  64.   Wire.endTransmission();
  65.   
  66.   delay(1390);
  67.   
  68.   Wire.beginTransmission(MD22ADDRESS);                // set first motor to stop
  69.   Wire.write(MOTOR1);
  70.   Wire.write(128);
  71.   Wire.endTransmission();
  72.   
  73.   Wire.beginTransmission(MD22ADDRESS);                // Set second motor to speed 0
  74.   Wire.write(MOTOR2);
  75.   Wire.write((byte)0);
  76.   Wire.endTransmission();
  77. }
  78. void getSoftware(){                                   // Reads and displays the software version of MD22
  79.   Wire.beginTransmission(MD22ADDRESS);                // Calles software register
  80.   Wire.write(SOFTREG);
  81.   Wire.endTransmission();
  82.   
  83.   Wire.requestFrom(MD22ADDRESS, 1);                   // Requests one byte
  84.   while(Wire.available() < 1);                        // Wait for it to arrive
  85.   int software = Wire.read();                         // Get byte
  86.   lcd_03.print("MD22 Example   V:");
  87.   lcd_03.print(software, DEC);                        // Print byte to LCD03
  88. }
  89. void setMode(){
  90.   Wire.beginTransmission(MD22ADDRESS);                // Set a value of 255 to the acceleration register
  91.   Wire.write(ACCELLREG);
  92.   Wire.write(0xFF);
  93.   Wire.endTransmission();
  94. }
复制代码




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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail