Skip to content

Instantly share code, notes, and snippets.

@didasy
Last active August 9, 2017 15:12
Show Gist options
  • Select an option

  • Save didasy/c3d9e97b167324a1574dd24eaf043826 to your computer and use it in GitHub Desktop.

Select an option

Save didasy/c3d9e97b167324a1574dd24eaf043826 to your computer and use it in GitHub Desktop.
1Hz - 1MHz Frequency Generator Using Arduino
// Taken from https://forum.arduino.cc/index.php?topic=116094.15
// "Here's my code to set up timer 1 "
// dynamic frequency generator
// for timer2
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned long frequency = 1000000 ; // in hertz
void setup(){
pinMode(9,1);
pinMode(10,1);
DFG(frequency);
Serial.begin(57600);
}
void loop(){
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment