พระราชพรหมยาน(หลวงพ่อฤาษีลิงดำ)การบรรลุมรรคผลขึ้นอยู่กับผู้ปฏิบัติ ช้า-...


//ATmega 168 ATmega 328 P
// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017

// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary



#include "arduino.h" //Store data in flash (program) memory instead of SRAM

#include "avr/pgmspace.h"

#include "avr/io.h"

#include "SevSeg.h"

SevSeg sevseg; //Instantiate a seven segment controller object



   const byte sine256[] PROGMEM  = {

  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,

  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,

  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,

  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,

  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124



};

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator

#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator

#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction





int PWM1=  9;  //PWM1 output, phase 1

int PWM2 = 10; //PWM2 output, phase 2

int PWM3 = 11; //PWM3 output, phase 3





//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls

//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls

int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls

int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls

//int program_exec_time = A3; //monitor how quickly the interrupt trigger

int ISR_exec_time = A4; //monitor how long the interrupt takes

int INVERTOR_ENABLE = A1; //INVERTOR ENABLE



double ad_cel; //Manat Add Motor Acceleration, Deceleration

double spd_ref;  //Manat Add

double spd_ref_max = 481;  //60Hz Manat Add

double spd_ref_min = 20;  //2.5Hz Manat Add

double speed;   // Manat Add

unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)

unsigned char run;  //Manat Add

int num = 0; // Manat Add for Hz Show 7-Segments



const double refclk=31376.6;      // measured output frequency

//----------------------------------------------------------------------------

const int ledPin =  A5;         // the number of the LED pin

int ledState = LOW;             // ledState used to set the LED

long previousMillis = 0;        // will store last time LED was updated

long interval = 50000;           // interval at which to blink (milliseconds)

//----------------------------------------------------------------------------



// variables used inside interrupt service declared as voilatile

volatile byte current_count;              // Keep track of where the current count is in sine 256 array

volatile byte ms4_delay;             //variable used to generate a 4ms delay

volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second

volatile unsigned long phase_accumulator;   // pahse accumulator

volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.



void setup()

{

  Serial.begin(9600);        // Manat Add

 

  pinMode(PWM1, OUTPUT);      //sets the digital pin as output

  pinMode(PWM2, OUTPUT);      //sets the digital pin as output

  pinMode(PWM3, OUTPUT);  //sets the digital pin as output

 

  pinMode(ledPin, OUTPUT);  //Manat Add

  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output

  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output

  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add



 

 

  //sbi(PORTB,program_exec_time); //Sets the pin

  //digitalWrite(program_exec_time, HIGH);

 

  Setup_timer0();

  Setup_timer1();

  Setup_timer2();

  //Disable Timer 1 interrupt to avoid any timing delays

  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available

  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt

 

 

  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word

 

  //-----------SevenSegment-------------

  byte numDigits = 2;

  byte digitPins[] = {13, 12};

  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};

  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins

  byte hardwareConfig = COMMON_CATHODE; // See README.md for options

  bool updateWithDelays = false; // Default. Recommended

  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros

 

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);

  sevseg.setBrightness(1);

  //------------------------------------

  digitalWrite(ledPin, HIGH); 

  WaitLoop(30000);

  digitalWrite(ledPin, LOW); 

}



void loop()

{

  while(1)

  {

     ReadAnalogs();

     unsigned long currentMillis = millis();    //  For ledState

   

   //---------Control Power IR2111----------------------------

     if (speed > spd_ref_min){

       offset_1 = 85;

       offset_2 = 170;

       run = 1;

       digitalWrite(INVERTOR_ENABLE, HIGH);

     }

     else {

       offset_1 = 0;

       offset_2 = 0;

       run = 0;

       digitalWrite(INVERTOR_ENABLE, LOW);

     }

   //---------7 Segments Show Hz------------------------------ 

     num = (speed/8);

     if(speed == spd_ref_min) num = 0;

     sevseg.setNumber(num, 2);

     sevseg.refreshDisplay();   

   //sbi(PORTC,program_exec_time); //Sets the pin

   //digitalWrite(program_exec_time, HIGH);

 

   //---------Monitor program---------------------------------

  if((currentMillis - previousMillis) > (interval/(num+1))) {

    // save the last time you blinked the LED

    previousMillis = currentMillis; 



    // if the LED is off turn it on and vice-versa:

    if (ledState == LOW)

      ledState = HIGH;

    else

      ledState = LOW;



    // set the LED with the ledState of the variable:

    digitalWrite(ledPin, ledState);

  }

 

   //---------------------------------------------------------------

   

      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay

       {               

        c4ms=0;                          //Reset c4ms

        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt

        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word

        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt



      }

     

  }

}



void WaitLoop(unsigned int time)

{

 unsigned int i,j;

 for (j=0;j<time;j++)

 {

  for (i=0;i<200;i++) //the ATmega is runs at 16MHz

   if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction

 }

}





void ReadAnalogs(void)

{

      spd_ref=map(analogRead(0),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz

      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add

     

       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz 

       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz

 



       if (INPUT_DIR)

        {

          if (direction==0) spd_ref=spd_ref_min;

          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed

         

        }

 

       else

       {

          if (direction==1) spd_ref=spd_ref_min;

          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed

         

        } 



     //if (spd_ref>speed) speed=speed+0.02;   // Hz step

     //if (spd_ref<speed) speed=speed-0.02;

     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step

     if (spd_ref<speed) speed=speed-(1/ad_cel);

     if (speed<spd_ref_min) speed=spd_ref_min;

 

}





void Setup_timer0(void)

{

 

 

  TCCR0B = (TCCR0B & 0b11111000) | 0x02;

  // Timer1 PWM Mode set to Phase Correct PWM

  cbi (TCCR0A, COM0A0);

  sbi (TCCR0A, COM0A1);

  cbi (TCCR0A, COM0B0);

  sbi (TCCR0A, COM0B1);



  // Mode 1 / Phase Correct PWM

  sbi (TCCR0A, WGM00);

  cbi (TCCR0A, WGM01);

 

}



void Setup_timer1(void)

{

 

 TCCR1B = (TCCR1B & 0b11111000) |0x02;

  // Timer1 Clock Prescaler to : 1

 

  cbi (TCCR1A, COM1A0);

  sbi (TCCR1A, COM1A1);

  cbi (TCCR1A, COM1B0);

  sbi (TCCR1A, COM1B1);





  sbi (TCCR1A, WGM10);

  cbi (TCCR1A, WGM11);

  cbi (TCCR1B, WGM12);

  cbi (TCCR1B, WGM13);

}





void Setup_timer2()

{

 

  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1



  cbi (TCCR2A, COM2A0);  // clear Compare Match

  sbi (TCCR2A, COM2A1);

  cbi (TCCR2A, COM2B0);

  sbi (TCCR2A, COM2B1);

 

  // Mode 1  / Phase Correct PWM

  sbi (TCCR2A, WGM20); 

  cbi (TCCR2A, WGM21);

  cbi (TCCR2B, WGM22);

}







ISR(TIMER2_OVF_vect)

{

  //cbi(PORTC,program_exec_time); //Clear the pin

  //sbi(PORTC,ISR_exec_time);          // Sets the pin

  //digitalWrite(program_exec_time, LOW);

  digitalWrite(ISR_exec_time, HIGH);

  if (direction==0)

     phase_accumulator=phase_accumulator+tword_m;

  else

     phase_accumulator=phase_accumulator-tword_m;

   

   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination.

 

  if (run==0)

    current_count=0;

  else

    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information

  //-------------------------------





  //------------------------------

 

  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM

  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1

  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2

 

  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS

  if(ms4_delay++ == 125)

 

  { 

    c4ms++;

    ms4_delay=0; //reset count

   } 



//cbi(PORTC,ISR_exec_time);            //Clear the pin

digitalWrite(ISR_exec_time, LOW);

}
???????? ??????? ????? ? ????? ??????? ?????? ?????????? ???????? ???????? ???????????????? ?????? ? ?????? ????????? ?????? ???????????? ??????? ??????????????? ???????????? ????????? ????????? ???? ?????? ?? ??? 01:48 ????????????????: 
?????????????????
BlogThis!
????????? Twitter
????????? Facebook
?????? Pinterest
ATmega 168 ATmega 328 P
// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017

// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary



#include "arduino.h" //Store data in flash (program) memory instead of SRAM

#include "avr/pgmspace.h"

#include "avr/io.h"

#include "SevSeg.h"

SevSeg sevseg; //Instantiate a seven segment controller object



   const byte sine256[] PROGMEM  = {

  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,

  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,

  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,

  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,

  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124



};

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator

#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator

#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction





int PWM1=  9;  //PWM1 output, phase 1

int PWM2 = 10; //PWM2 output, phase 2

int PWM3 = 11; //PWM3 output, phase 3





//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls

//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls

int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls

int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls

//int program_exec_time = A3; //monitor how quickly the interrupt trigger

int ISR_exec_time = A4; //monitor how long the interrupt takes

int INVERTOR_ENABLE = A1; //INVERTOR ENABLE



double ad_cel; //Manat Add Motor Acceleration, Deceleration

double spd_ref;  //Manat Add

double spd_ref_max = 481;  //60Hz Manat Add

double spd_ref_min = 20;  //2.5Hz Manat Add

double speed;   // Manat Add

unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)

unsigned char run;  //Manat Add

int num = 0; // Manat Add for Hz Show 7-Segments



const double refclk=31376.6;      // measured output frequency

//----------------------------------------------------------------------------

const int ledPin =  A5;         // the number of the LED pin

int ledState = LOW;             // ledState used to set the LED

long previousMillis = 0;        // will store last time LED was updated

long interval = 50000;           // interval at which to blink (milliseconds)

//----------------------------------------------------------------------------



// variables used inside interrupt service declared as voilatile

volatile byte current_count;              // Keep track of where the current count is in sine 256 array

volatile byte ms4_delay;             //variable used to generate a 4ms delay

volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second

volatile unsigned long phase_accumulator;   // pahse accumulator

volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.



void setup()

{

  Serial.begin(9600);        // Manat Add

 

  pinMode(PWM1, OUTPUT);      //sets the digital pin as output

  pinMode(PWM2, OUTPUT);      //sets the digital pin as output

  pinMode(PWM3, OUTPUT);  //sets the digital pin as output

 

  pinMode(ledPin, OUTPUT);  //Manat Add

  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output

  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output

  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add



 

 

  //sbi(PORTB,program_exec_time); //Sets the pin

  //digitalWrite(program_exec_time, HIGH);

 

  Setup_timer0();

  Setup_timer1();

  Setup_timer2();

  //Disable Timer 1 interrupt to avoid any timing delays

  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available

  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt

 

 

  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word

 

  //-----------SevenSegment-------------

  byte numDigits = 2;

  byte digitPins[] = {13, 12};

  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};

  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins

  byte hardwareConfig = COMMON_CATHODE; // See README.md for options

  bool updateWithDelays = false; // Default. Recommended

  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros

 

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);

  sevseg.setBrightness(1);

  //------------------------------------

  digitalWrite(ledPin, HIGH); 

  WaitLoop(30000);

  digitalWrite(ledPin, LOW); 

}



void loop()

{

  while(1)

  {

     ReadAnalogs();

     unsigned long currentMillis = millis();    //  For ledState

   

   //---------Control Power IR2111----------------------------

     if (speed > spd_ref_min){

       offset_1 = 85;

       offset_2 = 170;

       run = 1;

       digitalWrite(INVERTOR_ENABLE, HIGH);

     }

     else {

       offset_1 = 0;

       offset_2 = 0;

       run = 0;

       digitalWrite(INVERTOR_ENABLE, LOW);

     }

   //---------7 Segments Show Hz------------------------------ 

     num = (speed/8);

     if(speed == spd_ref_min) num = 0;

     sevseg.setNumber(num, 2);

     sevseg.refreshDisplay();   

   //sbi(PORTC,program_exec_time); //Sets the pin

   //digitalWrite(program_exec_time, HIGH);

 

   //---------Monitor program---------------------------------

  if((currentMillis - previousMillis) > (interval/(num+1))) {

    // save the last time you blinked the LED

    previousMillis = currentMillis; 



    // if the LED is off turn it on and vice-versa:

    if (ledState == LOW)

      ledState = HIGH;

    else

      ledState = LOW;



    // set the LED with the ledState of the variable:

    digitalWrite(ledPin, ledState);

  }

 

   //---------------------------------------------------------------

   

      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay

       {               

        c4ms=0;                          //Reset c4ms

        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt

        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word

        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt



      }

     

  }

}



void WaitLoop(unsigned int time)

{

 unsigned int i,j;

 for (j=0;j<time;j++)

 {

  for (i=0;i<200;i++) //the ATmega is runs at 16MHz

   if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction

 }

}





void ReadAnalogs(void)

{

      spd_ref=map(analogRead(0),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz

      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add

     

       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz 

       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz

 



       if (INPUT_DIR)

        {

          if (direction==0) spd_ref=spd_ref_min;

          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed

         

        }

 

       else

       {

          if (direction==1) spd_ref=spd_ref_min;

          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed

         

        } 



     //if (spd_ref>speed) speed=speed+0.02;   // Hz step

     //if (spd_ref<speed) speed=speed-0.02;

     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step

     if (spd_ref<speed) speed=speed-(1/ad_cel);

     if (speed<spd_ref_min) speed=spd_ref_min;

 

}





void Setup_timer0(void)

{

 

 

  TCCR0B = (TCCR0B & 0b11111000) | 0x02;

  // Timer1 PWM Mode set to Phase Correct PWM

  cbi (TCCR0A, COM0A0);

  sbi (TCCR0A, COM0A1);

  cbi (TCCR0A, COM0B0);

  sbi (TCCR0A, COM0B1);



  // Mode 1 / Phase Correct PWM

  sbi (TCCR0A, WGM00);

  cbi (TCCR0A, WGM01);

 

}



void Setup_timer1(void)

{

 

 TCCR1B = (TCCR1B & 0b11111000) |0x02;

  // Timer1 Clock Prescaler to : 1

 

  cbi (TCCR1A, COM1A0);

  sbi (TCCR1A, COM1A1);

  cbi (TCCR1A, COM1B0);

  sbi (TCCR1A, COM1B1);





  sbi (TCCR1A, WGM10);

  cbi (TCCR1A, WGM11);

  cbi (TCCR1B, WGM12);

  cbi (TCCR1B, WGM13);

}





void Setup_timer2()

{

 

  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1



  cbi (TCCR2A, COM2A0);  // clear Compare Match

  sbi (TCCR2A, COM2A1);

  cbi (TCCR2A, COM2B0);

  sbi (TCCR2A, COM2B1);

 

  // Mode 1  / Phase Correct PWM

  sbi (TCCR2A, WGM20); 

  cbi (TCCR2A, WGM21);

  cbi (TCCR2B, WGM22);

}







ISR(TIMER2_OVF_vect)

{

  //cbi(PORTC,program_exec_time); //Clear the pin

  //sbi(PORTC,ISR_exec_time);          // Sets the pin

  //digitalWrite(program_exec_time, LOW);

  digitalWrite(ISR_exec_time, HIGH);

  if (direction==0)

     phase_accumulator=phase_accumulator+tword_m;

  else

     phase_accumulator=phase_accumulator-tword_m;

   

   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination.

 

  if (run==0)

    current_count=0;

  else

    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information

  //-------------------------------





  //------------------------------

 

  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM

  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1

  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2

 

  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS

  if(ms4_delay++ == 125)

 

  { 

    c4ms++;

    ms4_delay=0; //reset count

   } 



//cbi(PORTC,ISR_exec_time);            //Clear the pin

digitalWrite(ISR_exec_time, LOW);

}
???????? ??????? ????? ? ????? ??????? ?????? ?????????? ???????? ???????? ???????????????? ?????? ? ?????? ????????? ?????? ???????????? ??????? ??????????????? ???????????? ????????? ????????? ???? ?????? ?? ??? 01:48 ????????????????: 
?????????????????
BlogThis!
????????? Twitter
????????? Facebook
?????? Pinterest
DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015
???????? ??????? ????? ? ????? ??????? ?????? ?????????? ???????? ???????? ???????????????? ?????? ? ?????? ????????? ?????? ???????????? ??????? ??????????????? ???????????? ????????? ????????? ???? ?????? ?? ??? 00:39 ????????????????: 
?????????????????
BlogThis!
????????? Twitter
????????? Facebook
?????? Pinterest
ATmega 168 ATmega 328 P

???????? ??????? ????? ? ????? ??????? ?????? ?????????? ???????? ???????? ???????????????? ?????? ? ?????? ????????? ?????? ???????????? ??????? ??????????????? ???????????? ????????? ????????? ???? ?????? ?? ??? 00:39 ????????????????: 
?????????????????
BlogThis!
????????? Twitter
????????? Facebook
?????? Pinterest
???????????????????????????????????
// DDS Sine Generator 3 phase ATMEGA 168 328P PWM 4KHZ + Danijel Gorupec, 2015

// Support 7 Segments Show Hz  15/07/2017
// Import SevSeg Library : https://playground.arduino.cc/Main/SevenSegmentLibrary

#include "arduino.h" //Store data in flash (program) memory instead of SRAM
#include "avr/pgmspace.h"
#include "avr/io.h"
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

   const byte sine256[] PROGMEM  = {
  127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
  242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
  221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
  76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
  33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124

};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) //define a bit to have the properties of a clear bit operator
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))//define a bit to have the properties of a set bit operator
#define INPUT_DIR ((PINC&0x04)==0)  // 00000010 = A2 //Control Direction


int PWM1=  9;  //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3


//int offset_1 = 85; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
//int offset_2 = 170; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
int offset_1; //offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls
//int program_exec_time = A3; //monitor how quickly the interrupt trigger
int ISR_exec_time = A4; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE

double ad_cel; //Manat Add Motor Acceleration, Deceleration
double spd_ref;  //Manat Add
double spd_ref_max = 481;  //60Hz Manat Add
double spd_ref_min = 20;  //2.5Hz Manat Add
double speed;   // Manat Add
unsigned char direction; // Manat Add rotation direction (0 forwared, 1 reverse)
unsigned char run;  //Manat Add
int num = 0; // Manat Add for Hz Show 7-Segments

const double refclk=31376.6;      // measured output frequency
//----------------------------------------------------------------------------
const int ledPin =  A5;         // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 50000;           // interval at which to blink (milliseconds)
//----------------------------------------------------------------------------

// variables used inside interrupt service declared as voilatile
volatile byte current_count;              // Keep track of where the current count is in sine 256 array
volatile byte ms4_delay;             //variable used to generate a 4ms delay
volatile byte c4ms;              // after every 4ms this variable is incremented, its used to create a delay of 1 second
volatile unsigned long phase_accumulator;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.

void setup()
{
  Serial.begin(9600);        // Manat Add
   
  pinMode(PWM1, OUTPUT);      //sets the digital pin as output
  pinMode(PWM2, OUTPUT);      //sets the digital pin as output
  pinMode(PWM3, OUTPUT);  //sets the digital pin as output
  
  pinMode(ledPin, OUTPUT);  //Manat Add
  pinMode(ISR_exec_time, OUTPUT);      //sets the digital pin as output
  pinMode(INVERTOR_ENABLE, OUTPUT);      //sets the digital pin as output
  digitalWrite(INVERTOR_ENABLE, LOW);  //Manat Add
 
  
  
  //sbi(PORTB,program_exec_time); //Sets the pin
  //digitalWrite(program_exec_time, HIGH);
  
  Setup_timer0();
  Setup_timer1();
  Setup_timer2();
  //Disable Timer 1 interrupt to avoid any timing delays
  //cbi (TIMSK0,TOIE0);              //disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              //enable Timer2 Interrupt
  
  
  tword_m=pow(2,32)*speed/refclk;  //calulate DDS new tuning word 
  
  //-----------SevenSegment-------------
  byte numDigits = 2;
  byte digitPins[] = {13, 12};
  byte segmentPins[] = {8, 7, 6, 5, 4, 3, 2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(1);
  //------------------------------------
  digitalWrite(ledPin, HIGH);  
  WaitLoop(30000);
  digitalWrite(ledPin, LOW);  
}

void loop()
{
  while(1) 
  { 
     ReadAnalogs();
     unsigned long currentMillis = millis();    //  For ledState
     
   //---------Control Power IR2111----------------------------
     if (speed > spd_ref_min){
       offset_1 = 85;
       offset_2 = 170;
       run = 1;
       digitalWrite(INVERTOR_ENABLE, HIGH);
     }
     else {
       offset_1 = 0;
       offset_2 = 0;
       run = 0;
       digitalWrite(INVERTOR_ENABLE, LOW);
     }
   //---------7 Segments Show Hz------------------------------  
     num = (speed/8);
     if(speed == spd_ref_min) num = 0;
     sevseg.setNumber(num, 2);
     sevseg.refreshDisplay();    
   //sbi(PORTC,program_exec_time); //Sets the pin 
   //digitalWrite(program_exec_time, HIGH);
   
   //---------Monitor program---------------------------------
  if((currentMillis - previousMillis) > (interval/(num+1))) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
   
   //---------------------------------------------------------------
     
      if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
       {                 
        c4ms=0;                          //Reset c4ms
        cbi (TIMSK2,TOIE2);              //Disable Timer2 Interrupt
        tword_m=pow(2,32)*speed/refclk;  //Calulate DDS new tuning word
        sbi (TIMSK2,TOIE2);              //Enable Timer2 Interrupt 

      }
      
  }
}

void WaitLoop(unsigned int time)
{
 unsigned int i,j;
 for (j=0;j<time;j++)
 {
  for (i=0;i<200;i++) //the ATmega is runs at 16MHz
   if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
 }
}


void ReadAnalogs(void)
{
      spd_ref=map(analogRead(0),0,1023,0,spd_ref_max);             //Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz
      ad_cel=map(analogRead(3),0,1023,1,200);    // Manat Add
      
       if(spd_ref > spd_ref_max) spd_ref = spd_ref_max;  // Manat add maximum 60Hz  
       if(spd_ref < spd_ref_min) spd_ref = 0;  // Manat Add minimum 2.5Hz
  

       if (INPUT_DIR)
        {
          if (direction==0) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=1; //only allow direction change at minimum speed
          
        }
  
       else
       {
          if (direction==1) spd_ref=spd_ref_min;
          if (speed==spd_ref_min) direction=0; //only alow direction change at minimum speed
          
        }  

     //if (spd_ref>speed) speed=speed+0.02;   // Hz step
     //if (spd_ref<speed) speed=speed-0.02;
     if (spd_ref>speed) speed=speed+(1/ad_cel);   // Hz step
     if (spd_ref<speed) speed=speed-(1/ad_cel);
     if (speed<spd_ref_min) speed=spd_ref_min;
  
}


void Setup_timer0(void)
{
  
  
  TCCR0B = (TCCR0B & 0b11111000) | 0x02;
  // Timer1 PWM Mode set to Phase Correct PWM
  cbi (TCCR0A, COM0A0);
  sbi (TCCR0A, COM0A1);
  cbi (TCCR0A, COM0B0); 
  sbi (TCCR0A, COM0B1);

  // Mode 1 / Phase Correct PWM
  sbi (TCCR0A, WGM00); 
  cbi (TCCR0A, WGM01);
  
}

void Setup_timer1(void)
{
  
 TCCR1B = (TCCR1B & 0b11111000) |0x02;
  // Timer1 Clock Prescaler to : 1
  
  cbi (TCCR1A, COM1A0);
  sbi (TCCR1A, COM1A1);
  cbi (TCCR1A, COM1B0); 
  sbi (TCCR1A, COM1B1);

 
  sbi (TCCR1A, WGM10); 
  cbi (TCCR1A, WGM11);
  cbi (TCCR1B, WGM12);
  cbi (TCCR1B, WGM13);
}


void Setup_timer2() 
{
  
  TCCR2B = (TCCR2B & 0b11111000) | 0x02;// Timer2 Clock Prescaler to : 1
 
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);
  cbi (TCCR2A, COM2B0); 
  sbi (TCCR2A, COM2B1);
  
  // Mode 1  / Phase Correct PWM
  sbi (TCCR2A, WGM20);  
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);
}



ISR(TIMER2_OVF_vect)
{
  //cbi(PORTC,program_exec_time); //Clear the pin
  //sbi(PORTC,ISR_exec_time);          // Sets the pin
  //digitalWrite(program_exec_time, LOW);
  digitalWrite(ISR_exec_time, HIGH);
  if (direction==0) 
     phase_accumulator=phase_accumulator+tword_m;
  else
     phase_accumulator=phase_accumulator-tword_m; 
     
   //phase_accumulator=phase_accumulator+tword_m; //Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination. 
   
  if (run==0)
    current_count=0; 
  else
    current_count=phase_accumulator >> 24;     // use upper 8 bits of phase_accumulator as frequency information 
  //-------------------------------

 
  //------------------------------
  
  OCR1A = pgm_read_byte_near(sine256 + current_count); // read value fron ROM sine table and send to PWM
  OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1
  OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2
  
  //increment variable ms4_delay every 4mS/125 =  milliseconds 32uS
  if(ms4_delay++ == 125)
  
  {  
    c4ms++;
    ms4_delay=0; //reset count
   }   

//cbi(PORTC,ISR_exec_time);            //Clear the pin
digitalWrite(ISR_exec_time, LOW);
}
บุคคลผู้ได้มรรคผล ๕ ประเภท

๑. ผู้ได้ข่าว ๒. ผู้เห็นรูป ๓. ผู้ได้ยินเสียง ๔. ผู้ได้เห็นและได้ฟังเสียง ๕. ผู้ลงมือปฏิบัติ

บุคคล ๕ ประเภทนี้ เป็นผู้ที่สามารถบรรลุมรรคผลได้

ผู้ได้ข่าว ได้แก่ นางกาฬี เพียงได้ยินข่าวว่าพระพุทธเจ้าทรงแสดงธรรมจักรฯ ก็ใช้ศรัทธาตัดวิจิกิจฉาแล้ว สักกายทิฐิก็ดับลงพร้อมกัน เกิดเป็นพระโสดาบันขึ้นมา พระราชาพระนามว่าภคุนสาติ เพียงได้ยินข่าวว่า "พระพุทธเจ้าทรงประกาศพระศาสนา อยู่ในเมืองราชคฤห์" ก็บรรลุเป็นพระโสดาบัน

ผู้เห็นรูป ได้แก่ พระราชาพระนามว่า "มหากัปปินะ" พร้อมข้าราชบริวารหนึ่งพัน และพระมเหสีพร้อมด้วยภรรยาของมหาอำมาตย์หนึ่งพันคน เพียงได้เห็นพระรูปพระพุทธเจ้า ก็ใช้ศรัทธาตัดวิจิกิจฉา อัตตาดับลงพร้อมกัน เข้าสู่การเสวยโสตาปัตติผล

ผู้ได้ยินเสียง ได้แก่ มิคารเศรษฐี พ่อสามีนางวิสาขา เป็นสาวกของนักบวชชีเปลือย ไปเฝ้าพระพุทธเจ้าพร้อมกับนางวิสาขาด้วยความจำใจ แต่นักบวชชีเปลือยบังคับให้มิคารเศรษฐีกั้นผ้าม่าน เพื่อมิให้เห็นพระพุทธองค์ เมื่อพระพุทธเจ้าทรงแสดงธรรม มิคารเศรษฐีฟังธรรมอยู่หลังม่าน ก็ได้บรรลุธรรมเป็นพระโสดาบัน โดยไม่เห็นพระพุทธองค์

ผู้ได้เห็นและได้ฟังเสียง ได้แก่ นางวิสาขา, อนาถปิณฑิกเศรษฐี เป็นต้น ได้เห็นและได้ฟัง พระธรรมแล้วใช้ศรัทธาตัดวิกิจฉา อัตตาก็ดับลงพร้อมกัน บรรลุเป็นพระโสดาบัน

ผู้ลงมือปฏิบัติ ได้แก่ พระมหาสีวะ ตลอดจนถึงพวกเราท่านทั้งหลาย ที่เป็นปกติสาวก ซึ่งได้ศึกษาเรียนรู้ ได้ฟังและได้สอบถามแล้วเกิดศรัทธาเลื่อมใส ใช้ปัญญาตัดอัตตทิฐิ วิจิกิจฉาก็ดับพร้อมกันเข้าสู่ความเป็นโสดาบัน บุคคลประเภทลงมือปฏิบัตินี้ที่สามารถเข้าสู่การบรรลุธรรมมีมากมายจนไม่อาจนับจำนวนได้ฯ

ธรรมสากัจฉา ช่วงเย็นครับ

ด้วยความเคารพอย่างสูง

//Arduino atmega 1280 2560 good version 101-106  3 sine   pwm   

#include"avr/io.h"

#define cbi(sfr,bit)SFR_BYTE(sfr)&=_BV(bit)

#define sbi(sfr,bit)SFR_BYTE(sfr)| =_BV(bit)

#define UN (400.0) // motor rated voltage

#define FN (50.0) // motor nominal frequency

#define P (UN/FN) // associate Determining the ratio of voltage to nominal frequency

#define T_PWM (0.000255) // PWM signal period - set by the prescaler in the counters

#define T_MAX (4.0) // Specify the maximum output voltage period

#define T_MIN (0.02) // minimum output voltage

#define K_MAX floor (T_MAX/T_PWM) // number of period values for T_MAX

#define K_MIN ceil (T_MIN/T_PWM) // number of period values for T_MIN

volatile static unsigned int length_tab_sin; // variable containing the number of values in the full // period of the output voltage

static unsigned int i = 0; // auxiliary variable

volatile static unsigned int main_counter = 0; // variable in the interrupt

// ^ every T_PWM period increasing its value by 1

static unsigned int next_value_sin = 0; // variable which value sin should be calculated

static double t_param = 50; // parameter specifying the period of the output voltage

static float t = T_PWM; // T_PWM

static float omega_t; // pulsation of the output voltage multiplied by T_PWM

static float t_out; // output voltage period

static float U_o_param; // parameter specifying the size of the output voltage

// ^ calculated on the basis of t_out and U_in

static unsigned int ocr0a, ocr0b, ocr1a; // auxiliary variables to store obl. fillings

static unsigned int ocr1b, ocr2a, ocr2b; // ^

static double sin_in; // variable containing the parameter of the function sin

static double error = 1; // variable used to stop generating voltage when overloaded

static unsigned int analog = 0; // variable containing the measured value

static double U_in = 0; // variable holding the voltage measurement of the intermediate system

static double U_rms_max; // maximum currently possible to generate the effective voltage value

static bool a = 0; // logical variable for the implementation of two alternating measurements


//void setup()

//{

//  Serial.begin(57600);

//}


int main()

{

  io_init(); // initiate entry and exit

  timers_init(); // initialization of PWM meters

  adc_init(); // initialization of the ADC transducer

  while (1) // infinite loop with the main program

  {

    if (i == 185) // condition specifying the entry to the change function

    { // parameter of the discharge voltage, calling approx. 100ms

      chang_para(); // function to change the parameters of the output voltage

      i = 0;

    }

    next_value_sin = main_counter % length_tab_sin; // the next sine value to be calculated

    sin_in = omega_t*next_value_sin;

    // calculating the value to registers specifying the completion of the output signal /

    ocr0a = round(error * (U_o_param * (sin(sin_in) + 1) * 254 / 2) + 1); // pin

    ocr0b = ocr0a - 1;

    ocr1a = round (error * (U_o_param * (sin(sin_in - 2.09) + 1) * 254 / 2) + 1); // pin

    ocr1b = ocr1a - 1;

    ocr2a = round(error * (U_o_param * (sin(sin_in + 2.09) + 1) * 254 / 2) + 1); // pin

    ocr2b = ocr2a - 1;

    // updating values in registers /

    cli(); // prohibition on obsloge interruptions in case if

    // an interrupt occurred during the upgrade

    OCR0A = ocr0a; // pin

    OCR0B = ocr0b; // pin

    OCR1AL = ocr1a; // pin

    OCR1BL = ocr1b; // pin

    OCR2A = ocr2a; // pin

    OCR2B = ocr2b; // pin

    sei(); // allow for interruption obsloge

    i++;

  }

}

void adc_init()

{

  ADCSRA |= _BV(ADEN); // start the transmitter

  ADCSRA |= _BV(ADPS2); // setting the prescaler

  ADCSRA |= _BV(ADPS1); // ^

  ADCSRA |= _BV(ADPS0); // ^

  ADMUX |= _BV(REFS0); // reference voltage set as power supply

  ADMUX |= ADMUX &= 0b11110000; // choose the ADC0 input for measurement

}

void timers_init()

{

  cli(); // interrupt handler is forbidden

  // timer0 init

  TCCR0A |= _BV(COM0A1) | _BV(COM0B0) | _BV(COM0B1) | _BV(WGM00);

  TCCR0B |= _BV(CS01); // preskaler 8  | _BV(WGM02)

  TIMSK0 |= _BV (TOIE0); // flag from value 0 enabled //

  // timer1 init

  TCCR1A |= _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(WGM10);

  TCCR1B |= _BV (CS11); // preskaler 8 //  | _BV(WGM02)

  // timer2 init

  TCCR2A |= _BV(COM2A1) | _BV(COM2B0) | _BV(COM2B1) | _BV(WGM20);

  TCCR2B |= _BV(CS21); // preskaler 8  | _BV(WGM02)

  // resetting counter values

  TCNT0 = 0;

  TCNT1L = 0;

  TCNT2 = 0; /* the counter counts in g3re to 255, then in d3 ณ: / \ / \ / \

            at the value of 255 there is an interruption at which it occurs

            voltage and current measurements

*/

  sei(); // allow for interruption obsloge

}

void io_init()

{

  pinMode(13, OUTPUT); // OC0A Arduino Mega2560 pin 13

  pinMode(4, OUTPUT); // OC0B Arduino Mega2560 pin 4

  pinMode(11, OUTPUT); // OC1A Arduino Mega2560 pin 11

  pinMode(12, OUTPUT); // OC1B Arduino Mega2560 pin 12

  pinMode(10, OUTPUT); // OC2A Arduino Mega2560 pin 10

  pinMode(9, OUTPUT); // OC2B Arduino Mega2560 pin 9

  pinMode(14, INPUT_PULLUP); // up Arduino Mega2560 14

  pinMode(15, INPUT_PULLUP); // dn Arduino Mega2560 15

  pinMode(16, OUTPUT); // Arduino Mega2560 16

}

ISR(TIMER0_OVF_vect) // interrupt at 0 counter 0

{

  analog = ADC;

  if (a)

  {

    U_in = 0.0709 * analog;

    ADMUX |= _BV(MUX0); // choose the ADC1 input to measure the current

  }

  else

  {

    ADMUX |= ADMUX &= 0b11110000; // select the ADC0 input to measure the voltage

    if (analog > 579)

    {

      error = 0; // if the overload turn off the voltage generation

      digitalWrite(16, HIGH); // lighting the diode

    }

  }

  ADCSRA |= _BV(ADSC); // start reading the measurement

  a = a ^ 1; // the XOR gate negates the logical value a

  main_counter++;

  if (main_counter >= length_tab_sin) main_counter = 0;

}

void chang_para()

{

  t_param = map(analogRead(3), 0, 102, 0, 100);

  U_rms_max = U_in * 0.62; // value 0.62 experimentally limited

  bool up; // logical variable, informs you that the button has been pressed to increase the frequency

  bool down; // logical variable, informs you that the button has been pressed to decrease the frequency

  up = digitalRead(14); // read: if you press the button, increase the frequency

  down = digitalRead(15); // read: if you press the button you will decrease the frequency

  if (!up) t_param--; // if you press the button to increase the frequency then you will decrease the period

  if (!down) t_param ++; // if the button you press decreases the frequency, it will increase the period

  if (t_param < 0) t_param = 0; // protection of exceeding the extreme values

  if (t_param > 100) t_param = 100; // ^

  length_tab_sin = ceil((K_MAX - K_MIN) * t_param / 500 + K_MIN); // amount of values filled in one period

  t_out = T_PWM * length_tab_sin; // calculate the period of the output voltage

  omega_t = t * 2 * PI / t_out; // calculate the output voltage pulsation

  U_o_param = (P / t_out) / U_rms_max; // calculate the parameter determining the value of the output voltage

  if (t_out > 1) U_o_param = 0.5 * (18.5 / U_rms_max); // voltage at the output at low frequencies of 10v

  if (U_o_param > 1) U_o_param = 1; //protection of exceeding the extreme values

  error = 1; //if the overload turn off the voltage generation

  //digitalWrite(13, HIGH); //diode lighting

  //if the overload turn off the voltage generation

  digitalWrite(16, LOW); //diode lighting

  //              Serial.println(t_param);

}

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

การสำเร็จมรรคผล#ธรรมทั้งหลายมีใจเป็นหัวหน้ามีใจเป็นใหญ่สําเร็จด้วยใจ สังสารวัฏนี้น่ากลัว เพราะมันปิดบังตัวเองได้ กระทู้คำถาม ศาสนาพุทธ ความจริงเกี่ยวกับความจำที่อาจไม่สนุกนัก และนับเป็นเรื่องน่ากลัวของสังสารวัฏก็คือ ธรรมชาติเองก็เล่นเกม "ลบความทรงจำ" ปล่อยให้เราทำดีทำชั่ว และรับผลดีผลชั่วอย่างไม่รู้อีโหน่อีเหน่อยู่ทุกเมื่อเชื่อวันแบบนี้มานับชาติไม่ถ้วนอยู่แล้ว ไม่มีใครรู้ ไม่มีใครจำได้ ว่าตัวเองเกิดมาเป็นอย่างนี้ได้อย่างไร ทำไมถึงเป็นอย่างที่เป็น และทำไมต้องเจอกับอะไรที่เจอ ความทรงจำในอดีตชาติของเราถูกลบเลือนไปสิ้นในช่วงแห่งภวังค์ ๙ เดือนในท้องแม่ ถ้าเราจำได้ว่าเคยทำอะไรไว้ เคยถูกลงโทษไว้สาหัสสากรรจ์จนทุกข์ทรมานเพียงไหน เราคงไม่กล้าอีก... ที่จะทุจริตฉ้อโกง ทำร้ายหรือทำลายชีวิตใคร ฯลฯ อย่างน้อยย่อมมีความเข็ดขยาดอย่างแรงกล้า แม้สิ่งยั่วยุตรงหน้าจะล่อใจเพียงใดก็ตาม เหมือนเด็กที่รู้แล้วว่าการเอานิ้วแหย่ปลั๊กไฟมันเจ็บปวดขนาดไหน แล้วเข็ดที่จะทำอีก แต่เพราะเราจำไม่ได้ และเราต่างก็ยังดำเนินชีวิตไปเรื่อย ๆ ด้วยความไม่รู้ และไม่สนใจกระทั่งที่จะศึกษาเรียนรู้ เราก็ได้แต่เสี่ยงผิดเสี่ยงถูก ทำอะไรกันไปตามสัญชาตญาณเพื่อสนองกิเลสเฉพาะหน้าเป็นคราว ๆ ไปเท่านั้น หลวงพ่อปราโมทย์ท่านเคยเปรยขึ้นมาครั้งหนึ่งนะว่า "ถ้าเราความจำดีนะ... จะหนาว..." ท่านพูดขึ้นท่ามกลางความเงียบ แล้วก็ทิ้งท้ายไว้ด้วยความเงียบครู่หนึ่งอย่างนั้น จนทำให้รู้สึกว่าคำว่า "จะหนาว" คำนั้น น่ากลัวและเย็นยะเยือกเกินจินตนาการยิ่งนัก "ถ้ามองย้อนลงไป... มองย้อนลงไป...ได้นะ จะหนาวจริง ๆ สังสารวัฏนี้น่ากลัว เพราะมันปิดบังตัวเองได้" "นรกผ่านมาแล้วทุกคนนะ เดรัจฉานเป็นมาแล้วนะ เป็นกันมาหมดแล้ว..." หลวงพ่อท่านต่อท้ายไว้เสียน่าหนาวสันหลัง เคยคิดไหมคะว่า เราเคยทำอะไรไว้บ้าง แล้วที่คิด ๆ ทำ ๆ อยู่ทุกวันนี้ จะซัดเราไปสู่อัตภาพแบบไหนหลังความตายได้อีก เดรัจฉาน หรือนรกนั้น ไม่ใช่เรื่องเกินวิสัยเลย เราเดิน ๆ กันอยู่แค่ปากเหวนี่เอง ถ้าเป็นมนุษย์กิเลสหนาอย่างเรา ๆ ที่ไม่มีความสามารถระลึกถึงอดีตชาติเช่นนั้นได้ ก็คงรับผลไปอย่างงง ๆ โดยที่ไม่รู้เลยว่า ตัวเองนั่นแหละที่สร้างเหตุแห่งความเป็นเช่นนี้ไว้ "ความจำ" มีศัพท์เทคนิคในทางพุทธศาสนาอยู่คำหนึ่ง คือคำว่า "สัญญา" ซึ่งไม่ได้หมายถึงคำมั่นสัญญานะ แต่มีความหมายถึง ความจำได้หมายรู้ พระพุทธเจ้าท่านพูดถึง สัญญา หรือความจำนี้ ว่าไม่เที่ยง ไม่มีอะไรเป็นประกันได้เลยว่า เกิดใหม่เราจะเป็นอย่างไร จะคิดอย่างไร ไม่ว่าชาตินี้เราจะเป็นคนแสนดี หรือรู้จักจดจำข้อธรรมได้มากมายแค่ไหนก็ตาม พอตายแล้วเกิดใหม่ เราก็จำไม่ได้อยู่ดี และถ้าเราเป็นคนเลวในอนาคต ก็ต้องรับกรรมด้วยการไปเกิดในอบาย

การบรรลุมรรคผลนิพพาน#ธรรมไม่เนิ่นช้า#วิมุตติความหลุดพ้น#แนวทางบรรลุธรรม​...