2013-12-18 13:59:42 [显示全部楼层]
6045浏览
查看: 6045|回复: 1

[转载] [Arduino模块]MD03 驱动24V 20A电机

[复制链接]
Arduino模块]
MD03 驱动24V 20A电机

电路示意:



代码示例:
  1. /*******************************************************
  2. *             MD03 example for Arduino                 *
  3. *   MD03 is in I2C mode LCD03 controlled by serial     *
  4. *                                                      *
  5. *             By James Henderson 2012                  *
  6. *******************************************************/
  7. #include <Wire.h>
  8. #include <SoftwareSerial.h>
  9. #define ADDRESS             0x58                    // Address of MD03
  10. #define SOFTREG             0x07                    // Byte to read software
  11. #define CMDBYTE             0x00                    // Command byte
  12. #define SPEEDBYTE           0x02                    // Byte to write to speed register
  13. #define TEMPREG             0x04                    // Byte to read temprature
  14. #define CURRENTREG          0x05                    // Byte to read motor current
  15. #define LCD_RX              0x02                    // Pin for rx
  16. #define LCD_TX              0x03                    // Pin for tx
  17. #define LCD03_HIDE_CUR      0x04
  18. #define LCD03_CLEAR         0x0C
  19. #define LCD03_SET_CUR       0x02
  20. SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX);  // Sets up serial for LCD03
  21. byte direct = 1;                                          // Stores what direction the motor should run in
  22. void setup(){
  23.   lcd_03.begin(9600);                                    // Begin serial for LCD03
  24.   
  25.   Wire.begin();
  26.   delay(100);
  27.   
  28.   lcd_03.write(LCD03_HIDE_CUR);                          // Hides LCD03 cursor
  29.   lcd_03.write(LCD03_CLEAR);                             // Clears LCD03 screen
  30.   
  31.   int software = getData(SOFTREG);                       // Gets software version and prints it to LCD03
  32.   lcd_03.print("MD03 Example  V:");
  33.   lcd_03.print(software);
  34. }
  35. void loop(){
  36.   for(int i = 0; i < 250; i=i+10){
  37.     sendData(SPEEDBYTE, i);             // Sets speed to i
  38.     sendData(CMDBYTE, direct);          // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
  39.     int temp = getData(TEMPREG);        // Gets temperature
  40.     lcd_03.write(LCD03_SET_CUR);
  41.     lcd_03.write(21);
  42.     lcd_03.print("temprature: ");
  43.     lcd_03.print(temp);                 // Prints temperature to LCD03
  44.     lcd_03.print("  ");                 // Prints spaces to clear space after data
  45.     delay(50);                          // Wait to make sure all data sent
  46.     int current = getData(CURRENTREG);  // Gets motor current
  47.     lcd_03.write(LCD03_SET_CUR);
  48.     lcd_03.write(41);
  49.     lcd_03.print("Motor current: ");
  50.     lcd_03.print(current);
  51.     lcd_03.print("   ");
  52.     delay(50);                          // Wait to make sure all data sent  
  53.   }
  54.   if(direct == 1)                       // If loop that swaps value of direct between 1 and 2 each time through loop
  55.     direct = 2;                        
  56.   else
  57.     direct = 1;                        
  58. }
  59. byte getData(byte reg){                   // function for getting data from MD03
  60.   Wire.beginTransmission(ADDRESS);
  61.     Wire.write(reg);
  62.   Wire.endTransmission();
  63.   
  64.   Wire.requestFrom(ADDRESS, 1);         // Requests byte from MD03
  65.   while(Wire.available() < 1);          // Waits for byte to become availble
  66.   byte data = Wire.read();
  67.   return(data);
  68. }
  69. void sendData(byte reg, byte val){         // Function for sending data to MD03
  70.   Wire.beginTransmission(ADDRESS);         // Send data to MD03
  71.     Wire.write(reg);
  72.     Wire.write(val);
  73.   Wire.endTransmission();
  74. }
复制代码




20060606  高级技匠

发表于 2020-8-25 05:34:00

图好像一直加载不出来
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail