baldengineer

Yur a DAC

Mar 5th, 2021
772
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "dac.h"
  3.  
  4. void init_555() {
  5.     pinMode(clock_595, OUTPUT);
  6.     pinMode(latch_595, OUTPUT);
  7.     pinMode(data_595, OUTPUT);
  8.  
  9.     digitalWrite(clock_595, LOW);
  10.     digitalWrite(data_595, LOW);
  11.     digitalWrite(latch_595, HIGH);
  12.  
  13.  
  14.     uint32_t data_out=0x0;
  15.     data_out = set_relay(data_out, RELAY_ON, 0);
  16.     data_out = set_relay(data_out, RELAY_ON, 1);
  17.     data_out = set_relay(data_out, RELAY_ON, 2);
  18.     data_out = set_relay(data_out, RELAY_ON, 3);
  19.  
  20.     send_data_to_bits(data_out);
  21. }
  22.  
  23. uint32_t set_relay(uint32_t data_stream, bool state, int relay_number) {
  24.     int shift_by = (relay_number * 8) + 6;
  25.     bitWrite(data_stream, shift_by, !state);
  26.     Serial.print(relay_number); Serial.print(": "); Serial.println(data_stream, HEX);
  27.     return data_stream;
  28. }
  29.  
  30. void send_data_to_bits(uint32_t data_out) {
  31.     // X R X X  X X X 1  X R X X  X X X 1   X R X X  X X X 1   X R X X  X X X 1
  32.     digitalWrite(latch_595, HIGH);
  33.      shiftOut(data_595, clock_595, MSBFIRST, (data_out >> 24));  // bit 4
  34.      shiftOut(data_595, clock_595, MSBFIRST, (data_out >> 16));  // bit 3
  35.      shiftOut(data_595, clock_595, MSBFIRST, (data_out >> 8));  // bit 2
  36.      shiftOut(data_595, clock_595, MSBFIRST, (data_out));       // bit 0
  37.     digitalWrite(latch_595, LOW);
  38. }
  39.  
  40.  
  41. uint32_t convert_to_bit(byte counter) {
  42.     uint32_t data_out = 0x0;
  43.    
  44.     switch(counter) {
  45.         case 0:
  46.           data_out = 0x40404040;
  47.           break;
  48.         case 1:
  49.           data_out = 0x404040BF;  // 1011 111
  50.           break;
  51.         case 2:
  52.           data_out = 0x4040BF40;
  53.           break;
  54.         case 3:
  55.           data_out = 0x4040BFBF;
  56.           break;
  57.         case 4:
  58.           data_out = 0x40BF4040;
  59.           break;
  60.         case 5:
  61.           data_out = 0x40BF40BF;
  62.           break;
  63.         case 6:
  64.           data_out = 0x40BFBF40;
  65.           break;
  66.         case 7:
  67.           data_out = 0x40BFBFBF;
  68.           break;
  69.         case 8:
  70.           data_out = 0xBF404040;
  71.           break;
  72.         case 9:
  73.           data_out = 0xBF4040BF;
  74.           break;
  75.         case 10:
  76.           data_out = 0xBF40BF40;
  77.           break;  
  78.         case 11:
  79.           data_out = 0xBF40BFBF;
  80.           break;  
  81.         case 12:
  82.           data_out = 0xBFBF4040;
  83.           break;
  84.         case 13:
  85.           data_out = 0xBFBF40BF;
  86.           break;  
  87.         case 14:
  88.           data_out = 0xBFBFBF40;
  89.           break;  
  90.         case 15:
  91.           data_out = 0xBFBFBFBF;
  92.           break;  
  93.       }
  94.     return data_out;
  95. }
  96.  
  97.  
  98. void setup() {
  99.     Serial.begin(9600);
  100.     Serial.println(F("Visualized ADC - DAC"));
  101.     init_555();
  102.  
  103.     pinMode(sh_enable, OUTPUT);
  104.     digitalWrite(sh_enable, LOW);
  105.     pinMode(comparator_result, INPUT);
  106.  
  107.     pinMode(button, INPUT_PULLUP);
  108. }
  109.  
  110. unsigned long previous_print_millis = 0;
  111. unsigned long print_interval = 1000;
  112.  
  113. int comparator_value = 0;
  114.  
  115. void loop() {
  116.  
  117.     if (digitalRead(button) == PRESSED) {
  118.         start_SAR = true;
  119.         send_data_to_bits(convert_to_bit(0));
  120.     }
  121.  
  122.     // start conversion
  123.     if (start_SAR) {
  124.         start_SAR = false;
  125.  
  126.         // sample Vin
  127.         digitalWrite(sh_enable, HIGH);
  128.         delay(500);
  129.         digitalWrite(sh_enable, LOW);
  130.  
  131.         final_countdown =0;
  132.  
  133.         Serial.print("3: "), Serial.print(final_countdown);
  134.         bitWrite(final_countdown, 3, 1); // 1000
  135.         send_data_to_bits(convert_to_bit(final_countdown));
  136.         delay(500);
  137.         comparator_value = digitalRead(comparator_result);
  138.         if (comparator_value==0)
  139.             bitWrite(final_countdown, 3, 0);
  140.         Serial.print(","); Serial.println(final_countdown);
  141.    
  142.         Serial.print("2: "), Serial.print(final_countdown);
  143.         bitWrite(final_countdown, 2, 1); // 1100
  144.         send_data_to_bits(convert_to_bit(final_countdown));
  145.         delay(500);
  146.         comparator_value = digitalRead(comparator_result);
  147.         if (comparator_value==0)
  148.             bitWrite(final_countdown, 2, 0);
  149.         Serial.print(","); Serial.println(final_countdown);
  150.    
  151.         Serial.print("1: "), Serial.print(final_countdown);
  152.         bitWrite(final_countdown, 1, 1); // 1110
  153.         send_data_to_bits(convert_to_bit(final_countdown));
  154.         delay(500);
  155.         comparator_value = digitalRead(comparator_result);
  156.         if (comparator_value==0)
  157.             bitWrite(final_countdown, 1, 0);  // 1100
  158.         Serial.print(","); Serial.println(final_countdown);
  159.        
  160.         Serial.print("0: "), Serial.print(final_countdown);
  161.         bitWrite(final_countdown, 0, 1); // 1111  -- 1101
  162.         send_data_to_bits(convert_to_bit(final_countdown));
  163.         delay(500);
  164.         comparator_value = digitalRead(comparator_result);
  165.         if (comparator_value==0)
  166.             bitWrite(final_countdown, 0, 0);
  167.         Serial.print(","); Serial.println(final_countdown);
  168.        
  169.         send_data_to_bits(convert_to_bit(final_countdown));
  170.         Serial.print("Conversion: "); Serial.println(final_countdown);
  171.  
  172.     }
  173.  
  174.     unsigned long current_millis = millis();
  175.  
  176.     // print status of comparator
  177.     if (current_millis - previous_print_millis >= print_interval) {
  178.         //Serial.print("Comparator: ");
  179.         //Serial.println(digitalRead(comparator_result));  
  180.  
  181.         previous_print_millis = current_millis;
  182.     }
  183.  
  184.    
  185.  
  186.     // determine direction
  187.     // loop until comparator flips or run out of bits
  188.  
  189.     // *** when IN < DAC
  190.     // set DAC to 1.25 V
  191.  
  192.     // ***
  193.  
  194.  
  195.     // 4-bit DAC
  196.     // 0x0 to 0xF
  197.     //     0x7
  198.     //     0x8
  199.  
  200.  
  201.     if (Serial.available()) {
  202.         if (Serial.peek() != '!') {
  203.             int dac_value = Serial.parseInt();
  204.  
  205.             if (dac_value < 0)
  206.                 dac_value = 0;
  207.             if (dac_value > 0xF)
  208.                 dac_value = 0xF;
  209.             Serial.print("Got: "); Serial.println(dac_value);
  210.  
  211.             send_data_to_bits(convert_to_bit(dac_value));  
  212.  
  213.         } else {
  214.             Serial.read();
  215.             digitalWrite(sh_enable, HIGH);
  216.             delay(5);
  217.             digitalWrite(sh_enable, LOW);
  218.             Serial.println("Sampled");
  219.         }
  220.  
  221.     }
  222.  
  223. /*  uint32_t data_out = convert_to_bit(counter);
  224.     send_data_to_bits(data_out);
  225.     delay(wait);
  226.     counter++;
  227.     if (counter > 15)
  228.         counter=0; */
  229. //  send_data_to_bits(convert_to_bit(0));
  230. //  delay(2000);
  231. //  send_data_to_bits(convert_to_bit(15));
  232. //  delay(2000);
  233.    
  234. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×