Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //dynamic frequency generator
- //50% Duty cycle square wave 1Hz to 1MHz
- //for timer2
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define TOLERANCE 10
- unsigned long frequency = 1000000;
- int potPin = A0;
- void setup(){
- pinMode(9,1); // Pins 9 & 10
- pinMode(10,1); // are output.
- //Serial.begin(57600);
- }
- int oldVal = 0;
- void loop(){
- int val = analogRead(potPin);
- int diff = abs(val - oldVal); // absolute value
- if (diff > TOLERANCE)
- {
- oldVal = val; // only save if the val has changed enough to avoid slowly drifting
- DFG(map(val, 0, 1023, 1, 1000000)); // map 10-bit analog read to freq
- }
- }
- void DFG(unsigned long tempfreq){
- cli();//disable interupts
- TCCR1A = 0;//registers for timer 1
- TCCR1B = 0;
- TCNT1=0;
- TCCR1A |= _BV(COM1A0) + _BV(COM1B0);
- TCCR1B |=_BV(WGM12);
- TCCR1C = _BV(FOC1A);
- if(tempfreq > 122 && tempfreq < 1000001){
- OCR1A = (8000000/tempfreq)-1;//#TIMER COUNTS
- TCCR1B |= _BV(CS10);
- }
- else if(tempfreq <= 122 && tempfreq > 15){
- OCR1A = (1000000/tempfreq)-1;
- TCCR1B |= _BV(CS11);
- }
- else if(tempfreq <= 15 && tempfreq > 4){
- OCR1A = (125000/tempfreq)-1;
- TCCR1B |= _BV(CS10) + _BV(CS11);
- }
- //TIMSK1 = _BV(OCIE1A);//TIMER1 COMPARE INTERUPT
- sei();//enable interupts
- }
Advertisement
Comments
-
- Hi, I can't understand where to connect the pins. In the Arduino code, it is only declared A0 for the Potentiometer and, pinMode(9, 1); // Pins 9 & 10. Is it possible an explanation? If it is easier, my email is: meuviolino@hotmail.com
- Thank you
Add Comment
Please, Sign In to add comment
Advertisement