Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Phase Control**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-21 20:17:07
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* is the signal generate 40Khz and 200HZ */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <QuadratureEncoder.h> //https://github.com/Saeterncj/QuadratureEncoder
- #include <Arduino.h> // Required for digitalWrite, pinMode, etc.
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // USER CODE
- int latchPin = 6;
- int clockPin = 4;
- int dataPin = 5;
- const int numTransducers = 64;
- const int numSignalDivisions = 10;
- float phi[numTransducers] = {
- 1.164926349,4.230464127,1.960340454,0.782127899,0.782127899,1.960340454,4.230464127,1.164926349,
- 4.230464127,0.782127899,4.610572037,3.328161403,3.328161403,4.610572037,0.782127899,4.230464127,
- 1.960340454,4.610572037,2.004350852,0.634851558,0.634851558,2.004350852,4.610572037,1.960340454,
- 0.782127899,3.328161403,0.634851558,5.497764014,5.497764014,0.634851558,3.328161403,0.782127899,
- 0.782127899,3.328161403,0.634851558,5.497764014,5.497764014,0.634851558,3.328161403,0.782127899,
- 1.960340454,4.610572037,2.004350852,0.634851558,0.634851558,2.004350852,4.610572037,1.960340454,
- 4.230464127,0.782127899,4.610572037,3.328161403,3.328161403,4.610572037,0.782127899,4.230464127,
- 1.164926349,4.230464127,1.960340454,0.782127899,0.782127899,1.960340454,4.230464127,1.164926349
- };
- uint8_t phaseDelayBitShiftRegister[numSignalDivisions][8] = {0};
- uint16_t phaseDelayBit[numTransducers] = {0};
- const float carrierFrequency = 40000.0; // 40 kHz
- const float modulationFrequency = 200.0; // 200 Hz
- volatile bool modulationSignal = true;
- void PreparePhaseBits() {
- for (int i = 0; i < numTransducers; i++) {
- float phase_value = phi[i];
- float phase_increment = (2 * M_PI / numSignalDivisions);
- int bit_shift = round(phase_value / phase_increment) % numSignalDivisions;
- uint16_t bit_mask_left = (1 << (numSignalDivisions - bit_shift - 1));
- uint16_t bit_mask_right = (1 << bit_shift);
- phaseDelayBit[i] = bit_mask_left | bit_mask_right;
- }
- for (int d = 0; d < numSignalDivisions; d++) {
- memset(phaseDelayBitShiftRegister[d], 0, 8); // Clear all bytes
- for (int t = 0; t < numTransducers; t++) {
- int byte_index = 7 - (t / 8); // Reverse byte order
- int bit_position = t % 8; // MSB-first bit position
- int selectedBit = (phaseDelayBit[t] >> (numSignalDivisions - d - 1)) & 0x01;
- phaseDelayBitShiftRegister[d][byte_index] |= (selectedBit << (7 - bit_position));
- }
- }
- }
- // Timer1 Interrupt - 40 kHz Carrier
- ISR(TIMER1_COMPA_vect) {
- static int currentDivision = 0;
- // Apply modulation
- if(modulationSignal) {
- digitalWrite(latchPin, LOW);
- for(int j = 0; j < 8; j++) {
- shiftOut(dataPin, clockPin, MSBFIRST, phaseDelayBitShiftRegister[currentDivision][j]);
- }
- digitalWrite(latchPin, HIGH);
- }
- currentDivision = (currentDivision + 1) % numSignalDivisions;
- }
- // Timer2 Interrupt - PROPER 200 Hz Modulation
- ISR(TIMER2_COMPA_vect) {
- static bool toggle = false;
- toggle = !toggle;
- modulationSignal = toggle; // Exact 50% duty cycle
- }
- void setup() {
- Serial.begin(115200);
- PreparePhaseBits();
- pinMode(dataPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(latchPin, OUTPUT);
- noInterrupts();
- // Timer1 - CORRECT 40 kHz (16MHz/(199+1)/2 = 40000Hz)
- TCCR1A = 0;
- TCCR1B = (1 << WGM12) | (1 << CS10); // CTC mode, no prescale
- OCR1A = 199;
- TIMSK1 = (1 << OCIE1A);
- // Timer2 - CORRECT 200 Hz (16MHz/(7812/200) = 200.08Hz)
- TCCR2A = (1 << WGM21); // CTC mode
- TCCR2B = (1 << CS22) | (1 << CS21); // 256 prescaler
- OCR2A = 124; // (16e6)/(256*200) -1 = 311.5 -> 311
- TIMSK2 = (1 << OCIE2A);
- interrupts();
- }
- void loop() {
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement