Advertisement
CuriousScientist

MCP41100 digital potentiometer with Arduino/STM32

Jul 16th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to this tutorial video: https://youtu.be/_SHazOc5wSU
  3.  
  4.  
  5. //STM32-based digital potmeter
  6. #include <SPI.h> //SPI comunication
  7.  
  8.  
  9. int analogValue_1;
  10. int analogValue_2;
  11.  
  12. const byte analogInputPin_1 = PA0;
  13. const byte analogInputPin_2 = PA1;
  14.  
  15. const byte CS1_pin = PA4; //CS pin for potmeter 1
  16. const byte CS2_pin = PB9; //CS pin for potmeter 2
  17.  
  18. float rwa_1;
  19. float rwa_2;
  20. float rwb_1;
  21. float rwb_2;
  22.  
  23. int counter_1 = 0; //counter for the potmeter 1
  24. int counter_2 = 256; //counter for the potmeter 2
  25.  
  26. void setup()
  27. {
  28.  Serial.begin(9600);
  29.  Serial.println("MCP41100");
  30.  
  31.  pinMode(CS1_pin, OUTPUT); //Chip select is an output
  32.  pinMode(CS2_pin, OUTPUT); //Chip select is an output
  33.  
  34.  digitalWrite(CS1_pin, HIGH);
  35.  digitalWrite(CS2_pin, HIGH);
  36.  
  37.  SPI.begin();
  38. }
  39.  
  40. void loop()
  41. {
  42.  
  43.   writePotmeter_1();
  44.   writePotmeter_2();
  45.  
  46.   delay(300);
  47.  
  48.   readAnalogPin_1();
  49.   readAnalogPin_2();
  50.  
  51.   counter_1++;
  52.   counter_2--;
  53.  
  54.   if(counter_1 > 256) //this is for the increasing counter
  55.   {
  56.   counter_1 = 0;  
  57.   }
  58.  
  59.   if(counter_2 < 1)  //this is for the decreasing counter
  60.   {
  61.   counter_2 = 256;  
  62.   }
  63.  
  64. }
  65.  
  66. void writePotmeter_1()
  67. {  
  68.     //CS goes low
  69.     digitalWrite(CS1_pin, LOW);  
  70.    
  71.     SPI.transfer(0x11);  //command 00010001 [00][01][00][01]    
  72.     SPI.transfer(counter_1);  
  73.    
  74.     delayMicroseconds(100);
  75.     Serial.print("counter_1: ");    
  76.     Serial.println(counter_1);    
  77.    
  78.     //CS goes high
  79.     digitalWrite(CS1_pin, HIGH);
  80.  
  81. }
  82.  
  83. void writePotmeter_2()
  84. {
  85.    
  86.     digitalWrite(CS2_pin, LOW);  
  87.    
  88.     SPI.transfer(0x11);  //command 00010001 [00][01][00][11]    
  89.     SPI.transfer(counter_2);  
  90.    
  91.     delayMicroseconds(100);    
  92.     Serial.print("counter_2: ");    
  93.     Serial.println(counter_2);
  94.     //End of transaction
  95.  
  96.     //CS goes high
  97.     digitalWrite(CS2_pin, HIGH);
  98. }
  99.  
  100. void readAnalogPin_1()
  101. {
  102.   analogValue_1 = analogRead(analogInputPin_1);
  103.   Serial.print("Analog_1: ");
  104.   Serial.println(analogValue_1);  
  105.  
  106.   //Calculate resistance
  107.   rwa_1 = (100000 * (256 - counter_1)/256) + 125;
  108.   rwb_1 = (100000 * (counter_1)/256) + 125;
  109.  
  110.   Serial.print("rwa_1: ");
  111.   Serial.println(rwa_1);  
  112.   Serial.print("rwb_1: ");
  113.   Serial.println(rwb_1);  
  114.   Serial.print("sum: ");
  115.   Serial.println(rwa_1+rwb_1);
  116.  
  117. }
  118.  
  119. void readAnalogPin_2()
  120. {
  121.   analogValue_2 = analogRead(analogInputPin_2);
  122.   Serial.print("Analog_2: ");
  123.   Serial.println(analogValue_2);  
  124.  
  125.  
  126.   //Calculate resistance
  127.   rwa_2 = (100000 * (256 - counter_2)/256) + 125;
  128.   rwb_2 = (100000 * (counter_2)/256) + 125;
  129.  
  130.   Serial.print("rwa_2: ");
  131.   Serial.println(rwa_2);  
  132.   Serial.print("rwb_2: ");
  133.   Serial.println(rwb_2);  
  134.   Serial.print("sum: ");
  135.   Serial.println(rwa_2+rwb_2);
  136.  
  137. }
  138.  
  139.  
  140. /*
  141.  *  Resistance formula
  142.  *  R = 100 kOhm *(256-x)/256 + Rw
  143.  *  Rw = 125 for the 100 kOhm
  144.  *  Check datasheet, because Rw depends on the VDD  
  145.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement