DDS Sine Generator [PDF]

  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

/* DDS Sine Generator / 3 phase PWM Inverter mit Atmega32u4 ver.0.1 Alfa Timer4 generates the 15625 Hz Clock Interrupt */ #include // table of 8 bit 256 sine values / 1/2 sine period / stored in flash memory /* const byte PROGMEM sine256[] = {0,3,6,9,12,15,18,21,25,28,31,34,37,40,43,46,49,52,55,59,62,65,68,71,74,77,80,83,86,89,91,94,97,10 0,103,106,109, 112,114,117,120,123,125,128,131,134,136,139,141,144,147,149,152,154,157,159,162,164,166,169, 171,173,176,178,180, 182,185,187,189,191,193,195,197,199,201,203,205,207,208,210,212,214,215,217,219,220,222,223, 225,226,228,229,230, 232,233,234,236,237,238,239,240,241,242,243,244,245,246,247,247,248,249,249,250,251,251,252, 252,253,253,253,254, 254,254,255,255,255,255,255,255,255,255,255,255,255,254,254,254,253,253,253,252,252,251,251, 250,249,249,248,247, 247,246,245,244,243,242,241,240,239,238,237,236,234,233,232,230,229,228,226,225,223,222,220, 219,217,215,214,212, 210,208,207,205,203,201,199,197,195,193,191,189,187,185,182,180,178,176,173,171,169,166,164, 162,159,157,154,152, 149,147,144,141,139,136,134,131,128,125,123,120,117,114,112,109,106,103,100,97,94,91,89,86,83 ,80,77,74,71,68,65, 62,59,55,52,49,46,43,40,37,34,31,28,25,21,18,15,12,9,6,3}; */ const byte PROGMEM sine256[] = { 0, 5, 11, 16,22,27,32,38,43,49,54,59,65,70,76,81,86,91,97,102,107, 113,118,123,128,133,139,144,149,154,159,164,169,174,179,184,189, 194,199,203,208,213,218,221,223,224,226,227,229,230,231,233,234, 235,236,238,239,240,241,242,243,244,245,245,246,247,248,249,249, 250,251,251,252,252,253,253,253,254,254,254,254,255,255,255,255, 255,255,255,255,255,255,254,254,254,254,253,253,252,252,251,251, 250,250,249,248,248,247,246,245,244,243,242,241,240,239,238,237, 236,235,234,232,231,230,228,227,225,224,222,221,222,224,225,227, 228,230,231,232,234,235,236,237,238,239,240,241,242,243,244,245, 246,247,248,248,249,250,250,251,251,252,252,253,253,254,254,254, 254,255,255,255,255,255,255,255,255,255,255,254,254,254,254,253, 253,253,252,252,251,251,250,249,249,248,247,246,245,245,244,243, 242,241,240,239,238,236,235,234,233,231,230,229,227,226,224,223, 221,218,213,208,203,199,194,189,184,179,174,169,164,159,154,149, 144,139,133,128,123,118,113,107,102,97,91,86,81,76,70,65,59,54, 49,43,38,32,27,22,16,11,5}; #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) volatile byte sine_table_indexA; volatile byte sine_table_indexA_Prev; volatile byte sine_table_indexB; volatile byte sine_table_indexB_Prev; volatile byte sine_table_indexC;

volatile byte sine_table_indexC_Prev; volatile word phase_accumulator; volatile int tuning_word; volatile long tuning_word_soft; volatile int tuning_word_ADC; int Temp_Monitor; float Temp_Monitor_Float; word LCD_Buttons_ADC; volatile word PWM_Soft_A; volatile word PWM_Soft_B; volatile word PWM_Soft_C; volatile word Amp; volatile boolean phaseA_sign; volatile boolean phaseB_sign; volatile boolean phaseC_sign; volatile byte phaseShiftA; volatile byte phaseShiftB; volatile byte phaseShiftC; volatile word PhaseCount; volatile byte StateVar; volatile int Revers; volatile byte LCD_Button; char myStr[10]; int SpeedIncrease = 1024; int nsamples = 10; float average = 0; void setup() { pinMode(13, OUTPUT); // pin13 / OC4A / PWM output phase A pinMode(5, OUTPUT); // pin5 / #OC4A / PWM output phase inv A pinMode(10, OUTPUT); // pin10 / OC4B / PWM output phase B pinMode(9, OUTPUT); // pin9 / #OC4B / PWM output phase inv B pinMode(6, OUTPUT); // pin6 / OC4D / PWM output phase C pinMode(12, OUTPUT); // pin12 / #OC4D / PWM output phase inv C //************************************************************************** cbi (TIMSK0, TOIE0); // disable Timer0 Interrupt cbi (TIMSK1, TOIE1); // disable Timer1 Interrupt cbi (TIMSK4, TOIE4); // disable Timer4 Interrupt cbi (PLLFRQ, PDIV0);

// PLL Lock Frequency 96MHz

sbi (PLLFRQ, PDIV1); cbi (PLLFRQ, PDIV2); sbi (PLLFRQ, PDIV3); cbi (PLLFRQ, PLLTM0); sbi (PLLFRQ, PLLTM1); sbi (PLLFRQ, PLLUSB); cbi (PLLFRQ, PINMUX); sbi (PLLCSR, PINDIV); sbi (PLLCSR, PLLE); cbi (CLKPR, CLKPS0); sbi (CLKPR, CLKPS1); cbi (CLKPR, CLKPS2); cbi (CLKPR, CLKPS3);

// PLL Postcaler for Timer4 / PLL/1,5 = 64MHz // PLL Postcaler for USB Peripheral / PLL/2 = 48MHz // Use Primary System Clock for PLL // Using a 16MHz clock source for PLL // PLL enable // Clock Prescaler Select to 4

TCCR4A = 0; TCCR4B = 0; TCCR4C = 0; TCCR4D = 0; // Timer4 Clock Prescaler to : 1 / Timer4 Phase and Frequency Correct PWM Mode, 9-bit / TOP OCR4C (0x01FF) sbi (TCCR4A, COM4A0); // Clear OC4A/OC4B on Compare Match when up-counting. Set on Compare Match when down-counting. cbi (TCCR4A, COM4A1); // Enable complementary Compare Output mode sbi (TCCR4A, COM4B0); cbi (TCCR4A, COM4B1); sbi (TCCR4A, PWM4A); // Pulse Width Modulator A Enable sbi (TCCR4A, PWM4B); // Pulse Width Modulator B Enable sbi (TCCR4B, CS40); // Timer/Counter4 Prescaler Select / CK/4 sbi (TCCR4B, CS41); cbi (TCCR4B, CS42); cbi (TCCR4B, CS43); cbi (TCCR4B, DTPS40); // Dead Time Prescaler Bits / no division cbi (TCCR4B, DTPS41); sbi (TCCR4C, COM4D0); // Clear OC4D on Compare Match when up-counting. Set on Compare Match when down-counting. cbi (TCCR4C, COM4D1); // Enable complementary Compare Output mode sbi (TCCR4C, PWM4D); // Pulse Width Modulator D Enable sbi (TCCR4D, WGM40); // Phase and Frequency Correct PWM / TOP - OCR4C cbi (TCCR4D, WGM41); DT4 = 0x00;

// Timer/Counter4 Dead Time Value = 0

// TIFR4 - TC4 Interrupt Flag Register cbi (TIFR4, TOV4); // TOV - Timer/Counter4, Overflow Flag

cbi (TIFR4, OCF4A); // Timer/Counter4, Output Compare A Match Flag cbi (TIFR4, OCF4B); // Timer/Counter4, Output Compare B Match Flag cbi (TIFR4, OCF4D); // Timer/Counter4, Output Compare C Match Flag TC4H = 0x01; OCR4C = 0xFF;

// Timer/Counter4 PWM TOP 0x01FF / 9-bits resolution

//************************************************************************** tuning_word = 1; // Частота Amp = 127; // Амплитуда sbi (TIMSK4, TOIE4); // Timer/Counter4, Overflow Interrupt Enable //************************************************************************** } void loop() {} ISR(TIMER4_OVF_vect) { PWM(0, 170, 85); }

// Timer4 Interrupt

void PWM(byte phaseShiftA, byte phaseShiftB, byte phaseShiftC) { phase_accumulator += tuning_word; and extract the sine table index from it sine_table_indexA_Prev = sine_table_indexA; sine_table_indexA = highByte(phase_accumulator) + phaseShiftA; phase accumulator as index phase A sine_table_indexB_Prev = sine_table_indexB; sine_table_indexB = highByte(phase_accumulator) + phaseShiftB; phase accumulator as index phase B sine_table_indexC_Prev = sine_table_indexC; sine_table_indexC = highByte(phase_accumulator) + phaseShiftC; phase accumulator as index phase C

// Update phase accumulator // Use upper 8 bits // Use upper 8 bits // Use upper 8 bits

if (sine_table_indexA_Prev > sine_table_indexA) {phaseA_sign = not phaseA_sign;} if (sine_table_indexB_Prev > sine_table_indexB) {phaseB_sign = not phaseB_sign;} if (sine_table_indexC_Prev > sine_table_indexC) {phaseC_sign = not phaseC_sign;} if (phaseA_sign == true) {PWM_Soft_A = 256 + (((pgm_read_byte_near(sine256 + sine_table_indexA))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase A being constructed. else {PWM_Soft_A = 255 - (((pgm_read_byte_near(sine256 + sine_table_indexA))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase A being constructed. if (phaseB_sign == true)

{PWM_Soft_B = 256 + (((pgm_read_byte_near(sine256 + sine_table_indexB))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase B being constructed. else {PWM_Soft_B = 255 - (((pgm_read_byte_near(sine256 + sine_table_indexB))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase B being constructed. if (phaseC_sign == false) {PWM_Soft_C = 256 + (((pgm_read_byte_near(sine256 + sine_table_indexC))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase C being constructed. else {PWM_Soft_C = 255 - (((pgm_read_byte_near(sine256 + sine_table_indexC))*Amp) >> 7);} // Set current amplitude value for the 1/2 sine wave phase C being constructed.

}

// Serial. println(PWM_Soft_A); sbi (TCCR4E, TLOCK4); // Synchronous update TC4H = highByte(PWM_Soft_A); OCR4A = lowByte(PWM_Soft_A); TC4H = highByte(PWM_Soft_B); OCR4B = lowByte(PWM_Soft_B); TC4H = highByte(PWM_Soft_C); OCR4D = lowByte(PWM_Soft_C); cbi (TCCR4E, TLOCK4);