Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define potnPinDC A0 // Port potenciômetro duty
- #define potnPinFreq A1 // Port potenciômetro freq
- #include <SoftPWM.h>
- int value = 0; // Leitura ADC dutycycle
- int hertz = 250; // Frequencia de PWM
- unsigned int freq; // Varialvel para correcao da frequencia
- /*SOFTPWM_DEFINE_CHANNEL(0, DDRD, PORTD, PORTD0); //Arduino pin 0
- SOFTPWM_DEFINE_CHANNEL(1, DDRD, PORTD, PORTD1); //Arduino pin 1
- SOFTPWM_DEFINE_CHANNEL(2, DDRD, PORTD, PORTD2); //Arduino pin 2
- SOFTPWM_DEFINE_CHANNEL(3, DDRD, PORTD, PORTD3); //Arduino pin 3
- SOFTPWM_DEFINE_CHANNEL(4, DDRD, PORTD, PORTD4); //Arduino pin 4
- SOFTPWM_DEFINE_CHANNEL(5, DDRD, PORTD, PORTD5); //Arduino pin 5
- SOFTPWM_DEFINE_CHANNEL(6, DDRD, PORTD, PORTD6); //Arduino pin 6*/
- SOFTPWM_DEFINE_CHANNEL(7, DDRD, PORTD, PORTD7); //Arduino pin 7 // Port saida PWM
- /*SOFTPWM_DEFINE_CHANNEL(8, DDRB, PORTB, PORTB0); //Arduino pin 8
- SOFTPWM_DEFINE_CHANNEL(9, DDRB, PORTB, PORTB1); //Arduino pin 9
- SOFTPWM_DEFINE_CHANNEL(10, DDRB, PORTB, PORTB2); //Arduino pin 10
- SOFTPWM_DEFINE_CHANNEL(11, DDRB, PORTB, PORTB3); //Arduino pin 11
- SOFTPWM_DEFINE_CHANNEL(12, DDRB, PORTB, PORTB4); //Arduino pin 12
- SOFTPWM_DEFINE_CHANNEL(13, DDRB, PORTB, PORTB5); //Arduino pin 13
- SOFTPWM_DEFINE_CHANNEL(14, DDRC, PORTC, PORTC0); //Arduino pin A0
- SOFTPWM_DEFINE_CHANNEL(15, DDRC, PORTC, PORTC1); //Arduino pin A1
- SOFTPWM_DEFINE_CHANNEL(16, DDRC, PORTC, PORTC2); //Arduino pin A2
- SOFTPWM_DEFINE_CHANNEL(17, DDRC, PORTC, PORTC3); //Arduino pin A3
- SOFTPWM_DEFINE_CHANNEL(18, DDRC, PORTC, PORTC4); //Arduino pin A4
- SOFTPWM_DEFINE_CHANNEL(19, DDRC, PORTC, PORTC5); //Arduino pin A5*/
- SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(20, 100); // Qtde ports e PWM Level Deixar 20 e 100
- //-------------------------------
- void setup()
- {
- Serial.begin(115200);
- Palatis::SoftPWM.begin(10); // begin with 60hz pwm frequency
- Palatis::SoftPWM.printInterruptLoad(); // print interrupt load for diagnostic purposes
- pinMode(potnPinDC, INPUT); // Port entrada
- pinMode(potnPinFreq, INPUT); // Port entrada
- }
- //-------------------------------
- void loop()
- {
- hertz = map(analogRead(potnPinFreq), 0, 1023, 10 , 200); // Valor de ajuste da frequencia
- Serial.println(hertz);
- freq = hertz * 100 / 125; // Calculo da correcao da frequencia
- OCR1A = (F_CPU - freq * 128 / 2) / (freq * 128); // REG de frequencia de PWM
- value = map(analogRead(potnPinDC), 0, 1023, 1 , 99); // Valor de ajuste do duty cycle
- unsigned long const WAIT = 1000000 / Palatis::SoftPWM.PWMlevels() / 2;
- unsigned long nextMicros = 0;
- while (micros() < nextMicros);
- nextMicros = micros() + WAIT;
- Palatis::SoftPWM.set(7, value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement