Advertisement
RuiViana

PT_Teste_PWM

Oct 10th, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #define potnPinDC  A0                                         // Port potenciômetro duty
  2. #define potnPinFreq  A1                                       // Port potenciômetro freq
  3. #include <SoftPWM.h>
  4. int value = 0;                                              // Leitura ADC dutycycle
  5. int hertz = 250;                                               // Frequencia de PWM
  6. unsigned int freq;                                            // Varialvel para correcao da frequencia
  7.  
  8. /*SOFTPWM_DEFINE_CHANNEL(0, DDRD, PORTD, PORTD0);  //Arduino pin 0
  9.   SOFTPWM_DEFINE_CHANNEL(1, DDRD, PORTD, PORTD1);  //Arduino pin 1
  10.   SOFTPWM_DEFINE_CHANNEL(2, DDRD, PORTD, PORTD2);  //Arduino pin 2
  11.   SOFTPWM_DEFINE_CHANNEL(3, DDRD, PORTD, PORTD3);  //Arduino pin 3
  12.   SOFTPWM_DEFINE_CHANNEL(4, DDRD, PORTD, PORTD4);  //Arduino pin 4
  13.   SOFTPWM_DEFINE_CHANNEL(5, DDRD, PORTD, PORTD5);  //Arduino pin 5
  14.   SOFTPWM_DEFINE_CHANNEL(6, DDRD, PORTD, PORTD6);  //Arduino pin 6*/
  15. SOFTPWM_DEFINE_CHANNEL(7, DDRD, PORTD, PORTD7);  //Arduino pin 7          // Port saida PWM
  16. /*SOFTPWM_DEFINE_CHANNEL(8, DDRB, PORTB, PORTB0);  //Arduino pin 8
  17.   SOFTPWM_DEFINE_CHANNEL(9, DDRB, PORTB, PORTB1);  //Arduino pin 9
  18.   SOFTPWM_DEFINE_CHANNEL(10, DDRB, PORTB, PORTB2);  //Arduino pin 10
  19.   SOFTPWM_DEFINE_CHANNEL(11, DDRB, PORTB, PORTB3);  //Arduino pin 11
  20.   SOFTPWM_DEFINE_CHANNEL(12, DDRB, PORTB, PORTB4);  //Arduino pin 12
  21.   SOFTPWM_DEFINE_CHANNEL(13, DDRB, PORTB, PORTB5);  //Arduino pin 13
  22.   SOFTPWM_DEFINE_CHANNEL(14, DDRC, PORTC, PORTC0);  //Arduino pin A0
  23.   SOFTPWM_DEFINE_CHANNEL(15, DDRC, PORTC, PORTC1);  //Arduino pin A1
  24.   SOFTPWM_DEFINE_CHANNEL(16, DDRC, PORTC, PORTC2);  //Arduino pin A2
  25.   SOFTPWM_DEFINE_CHANNEL(17, DDRC, PORTC, PORTC3);  //Arduino pin A3
  26.   SOFTPWM_DEFINE_CHANNEL(18, DDRC, PORTC, PORTC4);  //Arduino pin A4
  27.   SOFTPWM_DEFINE_CHANNEL(19, DDRC, PORTC, PORTC5);  //Arduino pin A5*/
  28.  
  29. SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(20, 100);      // Qtde ports e PWM Level Deixar 20 e 100
  30.  
  31. //-------------------------------
  32. void setup()
  33. {
  34.   Serial.begin(115200);
  35.   Palatis::SoftPWM.begin(10);                       // begin with 60hz pwm frequency
  36.   Palatis::SoftPWM.printInterruptLoad();            // print interrupt load for diagnostic purposes
  37.   pinMode(potnPinDC, INPUT);                        // Port entrada
  38.   pinMode(potnPinFreq, INPUT);                        // Port entrada
  39. }
  40. //-------------------------------
  41. void loop()
  42. {
  43.   hertz = map(analogRead(potnPinFreq), 0, 1023, 10 , 200);  // Valor de ajuste da frequencia
  44.   Serial.println(hertz);
  45.   freq = hertz * 100 / 125;                                 // Calculo da correcao da frequencia
  46.   OCR1A = (F_CPU - freq * 128 / 2) / (freq * 128);          // REG de frequencia de PWM
  47.  
  48.   value = map(analogRead(potnPinDC), 0, 1023, 1 , 99);      // Valor de ajuste do duty cycle
  49.  
  50.   unsigned long const WAIT = 1000000 / Palatis::SoftPWM.PWMlevels() / 2;
  51.   unsigned long nextMicros = 0;
  52.   while (micros() < nextMicros);
  53.   nextMicros = micros() + WAIT;
  54.   Palatis::SoftPWM.set(7, value);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement