2 Replies Latest reply on Aug 19, 2014 5:26 AM by kurjak

    variable frequency and duty cycle

    kurjak

      Hello to everybody

      I need to get the possibility to modify frequency and duty cycle from 1Hz to 1MHz using the console of the IDE on my Mac.

      Using AT Mega ... i succeed thanks to a sketch that a guy of yours made some year ago, but obviously it doesn't work on Intel Galileo.

      Someone of you can please help me?

      thank you

       

      following the prig i used through AT Mega (in italian .... )

       

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

      // di Alessio F. Alessiof76@gmail.it

      // licenza creative commons

      //----------------- PROGRAMMA PWM CON TimerOne-------------------------------------

      //-parte con softstart e i valori di frequenza e dutycicle si inviano dal PC------

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

      // genera segnali da meno 0Hz a 1 MHz con duty clicle impostabile

       

       

      #include "TimerOne.h"  //carico la libreria Timer1

       

       

      String inputString = "";         // variabile stringa per ricevere dati dal pc

      boolean stringComplete = false;  // variabile per capire se la stringa è completa

      long periodo = 100 ;

      long frequenza=10000;            //frequenza di funzionamento

      int dt=512;                      // variabile del duty cicle da 0 a 1024

       

       

      char Data[8];                    // stringa per leggere dal PC

      int i;

      unsigned long Tempo;

       

       

      void setup()

      {

        pinMode(13, OUTPUT);          // abilito il pin13 con led interno per segnalare la fine del soft start

       

       

        periodo=1000000/frequenza ;

        Timer1.initialize(periodo);         // initializzo timer1, per un perido di 100microsecondi = 10KHz

        Timer1.pwm(9, dt);             // attivo il pwm on pin 9, 50% duty cycle per un istante

      //  ------------------------------------------SOFT START------------------------------------------

        for(int i=1; i<512;i++) {

               Timer1.setPwmDuty(9, i);

               delay(50);          //-----------------------se soft start troppo corto aumenta questo valore----------------

        }

        digitalWrite(13,HIGH) ; // accendo il led per visualizzare la fine del soft start

      // ------------------------------------------FINE SOFTSTART----------------------------------------

        Serial.begin(9600);

        Serial.print("IL SOFT START E' TERMINATO ");

              Serial.println(" ");

              Serial.println(" PREMI 'F' PER IMPOSTARE FREQUENZA IN Hz ");

              Serial.println(" PREMI 'D' PER IMPOSTARE DUDY CICLE DA 1 A 1023 ");

              Serial.println(" PREMI 'a' PER AUMENTARE LA FREQUENZA DI UNO STEP");

              Serial.println(" PREMI 'A' PER AUMENTARE LA FREQUENZA DI 10 STEP");

              Serial.println(" PREMI 's' PER DIMINUIRE LA FREQUENZA DI UNO STEP");

              Serial.println(" PREMI 'S' PER DIMINUIRE LA FREQUENZA DI 10 STEP");

              Serial.println(" PREMI 'p' PER AUMENTARE IL DUTY CICLE DI 1%");

              Serial.println(" PREMI 'P' PER AUMENTARE IL DUTY CICLE DI 10%");

              Serial.println(" PREMI 'm' PER DIMINUIRE IL DUTY CICLE DI 1%");

              Serial.println(" PREMI 'M' PER DIMINUIRE IL DUTY CICLE DI 10%");

              Serial.println(" ");

      }

       

       

      void loop() {

       

        if (Serial.available() > 0) {

          int inByte = Serial.read();

        

          switch (inByte) {

          case 'F':         // ---------------------------------------- premere f per cambiare frequenza-----------------------------------------

              Serial.print("la frequenza attuale e': ");

              Serial.print(frequenza);

              Serial.print(" Hz ");

              Serial.print(" duty cicle: ");

              Serial.println(dt);

              Serial.println(" ");

              Serial.println("inserisci la nuova frequenza e premi Invio");

              do {

                 if (Serial.available()) {

              Data[i] = Serial.read();

              i++;

              }

                 if(i<1)Tempo = millis();

                 } while (i<7&&(millis()-Tempo) < 500);

       

       

             Data[i] = 0;

             frequenza = atof(Data);

             i=0;

           

             if(frequenza<0) frequenza=1;

             if(frequenza>999999) frequenza=999999;

           

             periodo=1000000/frequenza;

             frequenza=1000000/periodo;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

          case 'D':  //------------------------------------------------------premi d per cambiare duti cicle--------------------------------------

              Serial.print("la frequenza attuale e': ");

              Serial.print(frequenza);

              Serial.print(" Hz ");

              Serial.print(" duty cicle: ");

              Serial.println(dt);

              Serial.println(" ");

              Serial.println("inserisci il nuovo duty cicle e premi Invio");

              do {

                 if (Serial.available()) {

              Data[i] = Serial.read();

              i++;

              }

                 if(i<1)Tempo = millis();

                 } while (i<7&&(millis()-Tempo) < 500);

       

       

             Data[i] = 0;

             dt = atof(Data);

             i=0;

           

             if(dt<1) dt=1;

             if(dt>1024) dt=1023;

           

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

            break;

          case 'a':  //----------------------------------------------a AUMENTA LA FREQUENZA DI UNO STEP----------------------------------------

            

             periodo=periodo-1;

             if(periodo<0) periodo=1;

             frequenza=1000000/periodo;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

          case 'A':  //----------------------------------------------a AUMENTA LA FREQUENZA DI 10 STEP----------------------------------------

            

             periodo=periodo-10;

             if(periodo<0) periodo=1;

             frequenza=1000000/periodo;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

       

            case 's':  //----------------------------------------------s DIMINUISCE LA FREQUENZA DI UNO STEP----------------------------------------

            

             periodo=periodo+1;

             if(periodo<0) periodo=1;

             frequenza=1000000/periodo;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

          case 'S':  //---------------------------------------------- S AUMENTA LA FREQUENZA DI 10 STEP----------------------------------------

            

             periodo=periodo+10;

             if(periodo<0) periodo=1;

             frequenza=1000000/periodo;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

       

       

          case 'p':  //----------------------------------------------p AUMENTA IL DUTY CICLE DI 1%----------------------------------------

            

             dt=dt+10;

             if(dt>1023) dt=1023;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

          case 'P':  //----------------------------------------------P AUMENTA IL DUTY CILE DI 10%----------------------------------------

            

             dt=dt+100;

             if(dt>1023) dt=1023;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

       

      case 'm':  //----------------------------------------------m DIMINUISCE IL DUTY CICLE DI 1% ----------------------------------------

            

             dt=dt-10;

             if(dt<1) dt=1;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

          case 'M':  //----------------------------------------------M DIMINUISCE IL DUTY CILE DI 10%----------------------------------------

            

             dt=dt-100;

             if(dt<1) dt=1;

             Timer1.setPeriod(periodo) ;

             Timer1.pwm(9, dt);

             Serial.println(" ");

             Serial.print("la frequenza generata e':");

             Serial.print(1000000/periodo);

             Serial.print(" Hz ");

             Serial.print(" duty cicle ");

             Serial.println(dt);

             Serial.println(" ");

           

             break;

       

       

          case 'z':  

            digitalWrite(5, HIGH);

            break;

          case 'e':  

            digitalWrite(6, HIGH);

            break;

          default:

            // turn all the LEDs off:

            for (int thisPin = 6; thisPin < 7; thisPin++) {

            //  digitalWrite(thisPin, LOW);

              Serial.println(" ");

              Serial.println(" PREMI 'F' PER IMPOSTARE FREQUENZA IN Hz ");

              Serial.println(" PREMI 'D' PER IMPOSTARE DUDY CICLE DA 1 A 1023 ");

              Serial.println(" PREMI 'a' PER AUMENTARE LA FREQUENZA DI UNO STEP");

              Serial.println(" PREMI 'A' PER AUMENTARE LA FREQUENZA DI 10 STEP");  // A COME "ALSA" S COME "SBASSA"

              Serial.println(" PREMI 's' PER DIMINUIRE LA FREQUENZA DI UNO STEP");

              Serial.println(" PREMI 'S' PER DIMINUIRE LA FREQUENZA DI 10 STEP");

              Serial.println(" PREMI 'p' PER AUMENTARE IL DUTY CICLE DI 1%"); //P COME PIU' FORTE

              Serial.println(" PREMI 'P' PER AUMENTARE IL DUTY CICLE DI 10%");

              Serial.println(" PREMI 'm' PER DIMINUIRE IL DUTY CICLE DI 1%"); //M COME MENO FORTE

              Serial.println(" PREMI 'M' PER DIMINUIRE IL DUTY CICLE DI 10%");

              Serial.println(" ");

       

       

            }

          }

        }

      }

        • 1. Re: variable frequency and duty cycle
          JPMontero_Intel

          Hi

          Could you be more specific on what is not working? The PWM are not getting the right values, the period is not changing, or is simply not doing anything. If you are having problems with the PWM I recommend you to configure them in the linux side by using the command system(); In case you are not familiar with this you can check this blog http://www.malinov.com/Home/sergey-s-blog/intelgalileo-programminggpiofromlinux . For example if you want to use PWM in pin 3, you would have to use this lines in your sketch:

           

          system(“echo -n 3 > /sys/class/pwm/pwmchip0/export”);  
          system(“echo -n 1 > /sys/class/pwm/pwmchip0/pwm3/enable”);
          system(“echo -n 1000000 > /sys/class/pwm/pwmchip0/pwm3/period”);
          system(“echo -n 500000 > /sys/class/pwm/pwmchip0/pwm3/duty_cycle”);

           

          Regards,
          JPMontero_Intel

          • 2. Re: variable frequency and duty cycle
            kurjak

            Hi Mr.

            The program i posted doesn't work at all.

            I reduced it into a smaller prog and cutting many useless part, finally it's work.

            Apparently!!

            Unfortunately i have not an oscilloscope so i cannot verify, but I think that after a certain frequency, the state of pin is locked to ON....forever.

            I suppose it depends on the configuration of the "pins". I mean if the clock is set by default at 800 Hz, of course I cannot reach 50 KHz.

            So I was wondering which pin are set by default at higher frequency or how to set them manually considering my 0% experience with Linux.

            I'm using a Mac book pro and I have only IDE.

            Following i put my new program using tone. It seams working.

             

             

            String inputString = "";      

                boolean stringComplete = false;

                char Data[8];                  

                unsigned long Tempo;

                unsigned int frequenza;

                int i;

                int led = 13;

                int out = 2;

                int d;

                int sec;

              

                  void setup() {

                

                     pinMode(led, OUTPUT);

                  //   pinMode(out, OUTPUT_FAST);

                     pinMode(out, OUTPUT);

                     digitalWrite(led, HIGH);

                     delay(1000);

                     sec = 2;

                    

                  Serial.begin(9600);

                   

                    Serial.println("IL SOFT START E' TERMINATO ");

                    Serial.println("___________________________");

                    Serial.println(" ");

                    Serial.println("PREMI 'F' PER IMPOSTARE FREQUENZA IN Hz ");

                    Serial.println("PREMI 'D' PER IMPOSTARE LA DURATA IN Sec ");

                    Serial.println(" ");        

                                  }

                 

                  void loop() {

                   

                      if (Serial.available() > 0) {

                      int inByte = Serial.read();

                     

                    switch (inByte) {

                   

                 case 'F':

                   

                        Serial.println(" ");

                        Serial.println("NUOVA FREQUENZA E INVIO");

                        do {

                            if (Serial.available()) {

                     Data[i] = Serial.read();

                     i++;

                    }

                        if(i<1)Tempo = millis();

                       } while (i<7&&(millis()-Tempo) < 500);

             

                   Data[i] = 0;

                   frequenza = atof(Data);

                   i=0;

                 

                   if(frequenza<33) frequenza=33;

                   if(frequenza>100000) frequenza=100000;

                    

                   Serial.println(" ");

                   Serial.print("FREQUENZA ATTUALE: ");

                   Serial.print(frequenza);

                   Serial.print(" Hz ");

                   Serial.print(" ");

                   Serial.println(" ");

                   Serial.print("DURATA ATTUALE: ");

                   Serial.print(sec);

                   Serial.print(" Sec");

                    

                   d = sec * 1000;

                   tone(out, frequenza);

                   delay(d);

                   noTone(out);

                  

                   Serial.println(" ");

                   Serial.println("-----FINE TONO-----");

                   Serial.println(" ");

                   break;

                  

                 case 'D':

                

                    Serial.println(" ");

                    Serial.println("NUOVA DURATA E INVIO");

                    do {

                       if (Serial.available()) {

                    Data[i] = Serial.read();

                    i++;

                    }

                       if(i<1)Tempo = millis();

                       } while (i<7&&(millis()-Tempo) < 500);

             

                   Data[i] = 0;

                   sec = atof(Data);

                   i=0;

                 

                   if(sec<1) sec=1;

                   if(sec>10) sec=10;

                  

                   Serial.println(" ");

                   Serial.print("FREQUENZA ATTUALE: ");

                   Serial.print(frequenza);

                   Serial.print(" Hz ");

                   Serial.print(" ");

                   Serial.println(" ");

                   Serial.print("DURATA ATTUALE: ");

                   Serial.print(sec);

                   Serial.print(" Sec ");

                 

                   d = sec * 1000;

                   tone(out, frequenza);

                   delay(d);

                   noTone(out);

             

                   Serial.println(" ");

                   Serial.println("-----FINE TONO-----");

                   Serial.println(" ");

                }

              }

            }

             

             

            this one is with PWM function but as i told you i think it doesn't work as the requested frequency reach 50 KHz and the internal clock is lower.

             

             

             

            #include "TimerOne.h"  //LOAD the Timer1 library

             

             

             

             

            String inputString = "";         // var string to get data from pc

            boolean stringComplete = false;  // var to assure string is complete

            long periodo = 100 ;

            long frequenza=10000;            // running frequency

            int dt=512;                      // var  duty cicle from to 1024

             

             

            char Data[8];                    // string for reading from PC

            int i;

            unsigned long Tempo;

             

             

            void setup()

            {

              pinMode(13, OUTPUT);          // enable pin13 with internal led to show the end of soft start

             

             

              periodo=1000000/frequenza ;

              Timer1.initialize(periodo);         // initialize  timer1, for a period of 100microseconds = 10KHz

              Timer1.pwm(9, dt);             // active pwm on pin 9, 50% duty cycle for a moment

              

            //  ------------------------------------------SOFT START------------------------------------------

             

             

              for(int i=1; i<512;i++) {

                     Timer1.setPwmDuty(9, i);

                     delay(50);        

              }

              digitalWrite(13,HIGH) ; // switch on the internal led to show the end of soft start

             

            // ------------------------------------------END SOFTSTART----------------------------------------

             

             

             

             

              Serial.begin(9600);

                    Serial.print("SOFT START ENDED ");

                    Serial.println(" ");

                    Serial.println(" 'F' to set FREQUENCY in Hz ");

                    Serial.println(" 'D' to set DUDY CICLE from 1 to 1023 ");

                    Serial.println(" ");

            }

             

             

            void loop() {

             

              if (Serial.available() > 0) {

                int inByte = Serial.read();

               

                switch (inByte) {

               

                case 'F':         // F to change frequency

                  

                    Serial.print("THE FREQUENCY IS: ");

                    Serial.print(frequenza);

                    Serial.print(" Hz ");

                    Serial.print(" DUTY CYCLE: ");

                    Serial.println(dt);

                    Serial.println(" ");

                    Serial.println("set new Frequency ");

                    do {

                       if (Serial.available()) {

                    Data[i] = Serial.read();

                    i++;

                    }

                       if(i<1)Tempo = millis();

                       } while (i<7&&(millis()-Tempo) < 500);

             

             

                   Data[i] = 0;

                   frequenza = atof(Data);

                   i=0;

                  

                   if(frequenza<0) frequenza=1;

                   if(frequenza>999999) frequenza=999999;

                  

                   periodo=1000000/frequenza;

                   frequenza=1000000/periodo;

                   Timer1.setPeriod(periodo) ;

                   Timer1.pwm(9, dt);

                   Serial.println(" ");

                   Serial.print("THE FREQUENCY IS: ");

                   Serial.print(1000000/periodo);

                   Serial.print(" Hz ");

                   Serial.print(" duty cicle ");

                   Serial.println(dt);

                   Serial.println(" ");

                  

                   break;

                case 'D':  //------------------------------------------------------premi d per cambiare duti cicle--------------------------------------

                    Serial.print("THE FREQUENCY IS: ");

                    Serial.print(frequenza);

                    Serial.print(" Hz ");

                    Serial.print(" duty cicle: ");

                    Serial.println(dt);

                    Serial.println(" ");

                    Serial.println("INSERT NEW DUTY CYCLE AND PRESS START");

                    do {

                       if (Serial.available()) {

                    Data[i] = Serial.read();

                    i++;

                    }

                       if(i<1)Tempo = millis();

                       } while (i<7&&(millis()-Tempo) < 500);

             

             

                   Data[i] = 0;

                   dt = atof(Data);

                   i=0;

                  

                   if(dt<1) dt=1;

                   if(dt>1024) dt=1023;

                  

                   Timer1.setPeriod(periodo) ;

                   Timer1.pwm(9, dt);

                   Serial.println(" ");

                   Serial.print("THE FREQUENCY IS:");

                   Serial.print(1000000/periodo);

                   Serial.print(" Hz ");

                   Serial.print(" duty cicle ");

                   Serial.println(dt);

                   Serial.println(" ");

                  break;

              

              

                    Serial.println(" ");

                    Serial.println(" PRESS 'F' TO SET FREQUENCY IN Hz ");

                    Serial.println(" PRESS 'D' TO SET DUDY CICLE FROM 1 TO 1023 ");

                    Serial.println(" ");

             

             

                  }

                }

              }

             

             

             

             

            Thank you for your support..... I hope to get news.

            best regards