SHARE
TWEET

Untitled

a guest Oct 13th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "arduino.h"
  2. #include <Wire_slave.h>
  3. #include <EEPROM.h>
  4. #include <Servo.h>
  5. #include <HardwareTimer.h>
  6. #include <STM32ADC.h>
  7.  
  8. STM32ADC myADC(ADC1);
  9. Servo servo1;
  10. Servo servo2;
  11. Servo servo3;
  12. Servo servo4;
  13. Servo servo5;
  14. Servo servo6;
  15. Servo servo7;
  16. Servo servo8;
  17.  
  18. uint8_t dataone = 0xff;
  19. uint8_t datas[3] = {0xff};
  20. uint16_t datapinacd[10];
  21. int adcpin[10] = {PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PB0, PB1};
  22. int servopin[8] = {PA8, PA9, PA10, PA11, PB6, PB7, PB8, PB9};
  23. uint16_t servodata;
  24. void setuppin()
  25. {
  26.   for (int i = 0; i < 10; i++) {
  27.  
  28.     pinMode(adcpin[i], INPUT_ANALOG);
  29.   }
  30. }
  31. void i2c_slave_write_handler(int numBytes)
  32. {
  33.   digitalWrite(PC13, !digitalRead(PC13));
  34.   if (numBytes <= 1) {
  35.     for (int i = 0; i < numBytes; i++)
  36.       dataone =  Wire1.read();
  37.   } else {
  38.     for (int i = 0; i < numBytes; i++)
  39.       datas[i] =  Wire1.read();
  40.   }
  41.   switch (datas[0]) {
  42.     case 0x30:
  43.       servodata = word(datas[1], datas[2]);
  44.       servo1.writeMicroseconds(servodata);
  45.       break;
  46.     case 0x31:
  47.       servodata = word(datas[1], datas[2]);
  48.       servo2.writeMicroseconds(servodata);
  49.       break;
  50.     case 0x32:
  51.       servodata = word(datas[1], datas[2]);
  52.       servo3.writeMicroseconds(servodata);
  53.       break;
  54.     case 0x33:
  55.       servodata = word(datas[1], datas[2]);
  56.       servo4.writeMicroseconds(servodata);
  57.       break;
  58.     case 0x34:
  59.       servodata = word(datas[1], datas[2]);
  60.       servo5.writeMicroseconds(servodata);
  61.       break;
  62.     case 0x35:
  63.       servodata = word(datas[1], datas[2]);
  64.       servo6.writeMicroseconds(servodata);
  65.       break;
  66.     case 0x36:
  67.       servodata = word(datas[1], datas[2]);
  68.       servo7.writeMicroseconds(servodata);
  69.       break;
  70.     case 0x37:
  71.       servodata = word(datas[1], datas[2]);
  72.       servo8.writeMicroseconds(servodata);
  73.       break;
  74.     default:
  75.       ;
  76.       break;
  77.   }
  78.  
  79. }
  80.  
  81. void i2c_slave_read_handler()
  82. {
  83.   digitalWrite(PC13, !digitalRead(PC13));
  84.   uint8_t val[2];
  85.   switch (dataone) {
  86.     case 0x10:
  87.       val[0] = datapinacd[0] >> 8;
  88.       val[1] = datapinacd[0];
  89.       Wire1.write(val, 2);
  90.       break;
  91.     case 0x11:
  92.       val[0] = datapinacd[1] >> 8;
  93.       val[1] = datapinacd[1];
  94.       Wire1.write(val, 2);
  95.       break;
  96.     case 0x12:
  97.       val[0] = datapinacd[2] >> 8;
  98.       val[1] = datapinacd[2];
  99.       Wire1.write(val, 2);
  100.       break;
  101.     case 0x13:
  102.       val[0] = datapinacd[3] >> 8;
  103.       val[1] = datapinacd[3];
  104.       Wire1.write(val, 2);
  105.       break;
  106.     case 0x14:
  107.       val[0] = datapinacd[4] >> 8;
  108.       val[1] = datapinacd[4];
  109.       Wire1.write(val, 2);
  110.       break;
  111.     case 0x15:
  112.       val[0] = datapinacd[5] >> 8;
  113.       val[1] = datapinacd[5];
  114.       Wire1.write(val, 2);
  115.       break;
  116.     case 0x16:
  117.       val[0] = datapinacd[6] >> 8;
  118.       val[1] = datapinacd[6];
  119.       Wire1.write(val, 2);
  120.       break;
  121.     case 0x17:
  122.       val[0] = datapinacd[7] >> 8;
  123.       val[1] = datapinacd[7];
  124.       Wire1.write(val, 2);
  125.       break;
  126.     case 0x18:
  127.       val[0] = datapinacd[8] >> 8;
  128.       val[1] = datapinacd[8];
  129.       Wire1.write(val, 2);
  130.       break;
  131.     case 0x19:
  132.       val[0] = datapinacd[9] >> 8;
  133.       val[1] = datapinacd[9];
  134.       Wire1.write(val, 2);
  135.       break;
  136.     default:
  137.       val[0] = 0xFF;
  138.       val[1] = 0xFF;
  139.       Wire1.write(val, 2);
  140.       break;
  141.   }
  142.  
  143. }
  144.  
  145. void read_and_write()
  146. {
  147.   for (int i = 0; i < 10; i++) {
  148.     datapinacd[i] = analogRead(adcpin[i]);
  149.   }
  150. }
  151.  
  152. void setup()
  153. {
  154.   Serial.begin(115200);
  155.   pinMode(PC13, OUTPUT);
  156.   setuppin();
  157.  
  158.   Wire1.begin(0x43);
  159.   Wire1.onReceive(i2c_slave_write_handler);
  160.   Wire1.onRequest(i2c_slave_read_handler);
  161.  
  162.   delay(1000);
  163.  
  164.   myADC.setSampleRate(ADC_SMPR_1_5);
  165.   servo1.attach(servopin[0]);
  166.   servo2.attach(servopin[1]);
  167.   servo3.attach(servopin[2]);
  168.   servo4.attach(servopin[3]);
  169.   servo5.attach(servopin[4]);
  170.   servo6.attach(servopin[5]);
  171.   servo7.attach(servopin[6]);
  172.   servo8.attach(servopin[7]);
  173.   delay(1000);
  174. }
  175. uint64_t previousMillis = 0;
  176. void loop()
  177. {
  178.   if (millis() - previousMillis > 1) {
  179.     previousMillis = millis();
  180.     read_and_write();
  181.   }
  182. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top