Arduino模块]
MD03 驱动24V 20A电机
电路示意:
代码示例:
- /*******************************************************
- * MD03 example for Arduino *
- * MD03 is in I2C mode LCD03 controlled by serial *
- * *
- * By James Henderson 2012 *
- *******************************************************/
-
- #include <Wire.h>
- #include <SoftwareSerial.h>
-
- #define ADDRESS 0x58 // Address of MD03
- #define SOFTREG 0x07 // Byte to read software
- #define CMDBYTE 0x00 // Command byte
- #define SPEEDBYTE 0x02 // Byte to write to speed register
- #define TEMPREG 0x04 // Byte to read temprature
- #define CURRENTREG 0x05 // Byte to read motor current
-
- #define LCD_RX 0x02 // Pin for rx
- #define LCD_TX 0x03 // Pin for tx
- #define LCD03_HIDE_CUR 0x04
- #define LCD03_CLEAR 0x0C
- #define LCD03_SET_CUR 0x02
-
- SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX); // Sets up serial for LCD03
-
- byte direct = 1; // Stores what direction the motor should run in
-
- void setup(){
- lcd_03.begin(9600); // Begin serial for LCD03
-
- Wire.begin();
- delay(100);
-
- lcd_03.write(LCD03_HIDE_CUR); // Hides LCD03 cursor
- lcd_03.write(LCD03_CLEAR); // Clears LCD03 screen
-
- int software = getData(SOFTREG); // Gets software version and prints it to LCD03
- lcd_03.print("MD03 Example V:");
- lcd_03.print(software);
- }
-
- void loop(){
- for(int i = 0; i < 250; i=i+10){
- sendData(SPEEDBYTE, i); // Sets speed to i
- sendData(CMDBYTE, direct); // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
- int temp = getData(TEMPREG); // Gets temperature
- lcd_03.write(LCD03_SET_CUR);
- lcd_03.write(21);
- lcd_03.print("temprature: ");
- lcd_03.print(temp); // Prints temperature to LCD03
- lcd_03.print(" "); // Prints spaces to clear space after data
- delay(50); // Wait to make sure all data sent
- int current = getData(CURRENTREG); // Gets motor current
- lcd_03.write(LCD03_SET_CUR);
- lcd_03.write(41);
- lcd_03.print("Motor current: ");
- lcd_03.print(current);
- lcd_03.print(" ");
- delay(50); // Wait to make sure all data sent
- }
- if(direct == 1) // If loop that swaps value of direct between 1 and 2 each time through loop
- direct = 2;
- else
- direct = 1;
- }
-
- byte getData(byte reg){ // function for getting data from MD03
- Wire.beginTransmission(ADDRESS);
- Wire.write(reg);
- Wire.endTransmission();
-
- Wire.requestFrom(ADDRESS, 1); // Requests byte from MD03
- while(Wire.available() < 1); // Waits for byte to become availble
- byte data = Wire.read();
-
- return(data);
- }
-
- void sendData(byte reg, byte val){ // Function for sending data to MD03
- Wire.beginTransmission(ADDRESS); // Send data to MD03
- Wire.write(reg);
- Wire.write(val);
- Wire.endTransmission();
- }
复制代码
|
|
|
|
|
|