Advertisement
pleasedontcode

**Phase Control** rev_01

Feb 21st, 2025
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Phase Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-21 20:17:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* is the signal generate 40Khz and 200HZ */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <QuadratureEncoder.h>  //https://github.com/Saeterncj/QuadratureEncoder
  27. #include <Arduino.h> // Required for digitalWrite, pinMode, etc.
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  34.  
  35. // USER CODE
  36. int latchPin = 6;  
  37. int clockPin = 4;  
  38. int dataPin = 5;  
  39.  
  40. const int numTransducers = 64;
  41. const int numSignalDivisions = 10;
  42.  
  43. float phi[numTransducers] = {
  44.   1.164926349,4.230464127,1.960340454,0.782127899,0.782127899,1.960340454,4.230464127,1.164926349,
  45.   4.230464127,0.782127899,4.610572037,3.328161403,3.328161403,4.610572037,0.782127899,4.230464127,
  46.   1.960340454,4.610572037,2.004350852,0.634851558,0.634851558,2.004350852,4.610572037,1.960340454,
  47.   0.782127899,3.328161403,0.634851558,5.497764014,5.497764014,0.634851558,3.328161403,0.782127899,
  48.   0.782127899,3.328161403,0.634851558,5.497764014,5.497764014,0.634851558,3.328161403,0.782127899,
  49.   1.960340454,4.610572037,2.004350852,0.634851558,0.634851558,2.004350852,4.610572037,1.960340454,
  50.   4.230464127,0.782127899,4.610572037,3.328161403,3.328161403,4.610572037,0.782127899,4.230464127,
  51.   1.164926349,4.230464127,1.960340454,0.782127899,0.782127899,1.960340454,4.230464127,1.164926349
  52. };
  53.  
  54. uint8_t phaseDelayBitShiftRegister[numSignalDivisions][8] = {0};
  55. uint16_t phaseDelayBit[numTransducers] = {0};
  56.  
  57. const float carrierFrequency = 40000.0; // 40 kHz
  58. const float modulationFrequency = 200.0; // 200 Hz
  59.  
  60. volatile bool modulationSignal = true;
  61.  
  62. void PreparePhaseBits() {
  63.   for (int i = 0; i < numTransducers; i++) {
  64.     float phase_value = phi[i];
  65.     float phase_increment = (2 * M_PI / numSignalDivisions);
  66.     int bit_shift = round(phase_value / phase_increment) % numSignalDivisions;
  67.  
  68.     uint16_t bit_mask_left = (1 << (numSignalDivisions - bit_shift - 1));
  69.     uint16_t bit_mask_right = (1 << bit_shift);
  70.     phaseDelayBit[i] = bit_mask_left | bit_mask_right;
  71.   }
  72.  
  73.   for (int d = 0; d < numSignalDivisions; d++) {
  74.     memset(phaseDelayBitShiftRegister[d], 0, 8); // Clear all bytes
  75.    
  76.     for (int t = 0; t < numTransducers; t++) {
  77.       int byte_index = 7 - (t / 8);         // Reverse byte order
  78.       int bit_position = t % 8;             // MSB-first bit position
  79.       int selectedBit = (phaseDelayBit[t] >> (numSignalDivisions - d - 1)) & 0x01;
  80.      
  81.       phaseDelayBitShiftRegister[d][byte_index] |= (selectedBit << (7 - bit_position));
  82.     }
  83.   }
  84. }
  85.  
  86. // Timer1 Interrupt - 40 kHz Carrier
  87. ISR(TIMER1_COMPA_vect) {
  88.   static int currentDivision = 0;
  89.  
  90.   // Apply modulation
  91.   if(modulationSignal) {
  92.     digitalWrite(latchPin, LOW);
  93.     for(int j = 0; j < 8; j++) {
  94.       shiftOut(dataPin, clockPin, MSBFIRST, phaseDelayBitShiftRegister[currentDivision][j]);
  95.     }
  96.     digitalWrite(latchPin, HIGH);
  97.   }
  98.  
  99.   currentDivision = (currentDivision + 1) % numSignalDivisions;
  100. }
  101.  
  102. // Timer2 Interrupt - PROPER 200 Hz Modulation
  103. ISR(TIMER2_COMPA_vect) {
  104.   static bool toggle = false;
  105.   toggle = !toggle;
  106.   modulationSignal = toggle; // Exact 50% duty cycle
  107. }
  108.  
  109. void setup() {
  110.   Serial.begin(115200);
  111.  
  112.   PreparePhaseBits();
  113.  
  114.   pinMode(dataPin, OUTPUT);
  115.   pinMode(clockPin, OUTPUT);
  116.   pinMode(latchPin, OUTPUT);
  117.  
  118.   noInterrupts();
  119.  
  120.   // Timer1 - CORRECT 40 kHz (16MHz/(199+1)/2 = 40000Hz)
  121.   TCCR1A = 0;
  122.   TCCR1B = (1 << WGM12) | (1 << CS10); // CTC mode, no prescale
  123.   OCR1A = 199;
  124.   TIMSK1 = (1 << OCIE1A);
  125.  
  126.   // Timer2 - CORRECT 200 Hz (16MHz/(7812/200) = 200.08Hz)
  127.   TCCR2A = (1 << WGM21);               // CTC mode
  128.   TCCR2B = (1 << CS22) | (1 << CS21);  // 256 prescaler
  129.   OCR2A = 124;                         // (16e6)/(256*200) -1 = 311.5 -> 311
  130.   TIMSK2 = (1 << OCIE2A);
  131.  
  132.   interrupts();
  133. }
  134.  
  135. void loop() {
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement