Hello! I'm going to provide some background before detailing the problem I am facing. If you don't care, skip to the TLDR.
I am in the process of making educational PCBs to study both analog-to-digital conversion and digital-to-analog conversion. The ICs I am working with naturally require clock signals. My goal is to eventually use the ATTiny10 for this purpose, but I wanted to start with getting the code running on an Uno. Unfortunately my physics is strong than my programming.
My goal is to produce two separate clocks, one in the low MHz range (precision not critical, but datasheets ask for 4MHz. I'm fairly certain 1MHz will do) to regulate the internal instructions of the SAR ADC, and one variable frequency to manipulate the sampling frequency, topping out at 150kHz (a "when should I read, when should I write" type thing).
TL;DR:
I would like to use a single timer to produce two square waves of different frequencies but am struggling to work with OCR1A/B. Instead of producing two different frequencies, I am phase-shifting one signal by what looks like 8 clock cycles, exactly corresponding to my single count increment. See waveforms in the image below, where I've labeled D10 and D9 for output B and A, respectively.
current code, in my head, should produce:
- OC1A on D9: ~62.5kHz square wave
- OC1B on D10: ~1MHz
Though the counters will produce a "true" match comparison at twice these frequencies, my understanding is that it will take two match compares to toggle on/off, per my TCCR1A manipulations. Is it that OCR1B only matches on a true "0" in the counting register, so it needs to wait until OCR1A resets the counter bits?
Questions:
- What am I not understanding
- Will timers interfere with standard cpu instructions if they are not used to raise an interrupt? Say I wanted to regulate OC1A's frequency with a potentiometer. I'd analogRead the divided voltage, map the value to some corresponding integer to update OCR1A, and hope that my frequency changes. Would this work?
As a last comments aside from my thanks, I'd like to mention that these are educational boards, so missed instructions that only cause glitches during state changes are not particularly critical.
Thanks for taking the time to read, and I hope you enjoy your day!
void setup() {
// put your setup code here, to run once:
DDRD |= B11011100; //sets pin 4,3,2,6,7 to output
DDRB |= B11000110; //sets pin 9,10 to output
cli(); //stops existing interrupts
//set timer0 to increment @ 2MHz
TCCR1A = 0;// set entire TCCR2A register to 0
TCCR1B = 0;// same for TCCR2B
TCNT1 = 0;//initialize counter value to 0
OCR1A = 15;// every 8*16 = 128 clocks, ctc mode should force timer reset in response to OCRnA
OCR1B = 0; //every 8 clocks this compare reg should raise high
//set output type to toggle (com1x0//com1x1 relation, bits 0//1 respectively)
TCCR1A |= (1 << COM1A0) | (1 << COM1B0);
TCCR1B |= (1 << WGM12); // enable ctc
TCCR1B |= (1 << CS11); // 8 cnt prescaler
// // enable timer compare interrupt
// TIMSK1 |= (1 << OCIE1A);
// TIMSK1 |= (1 << OCIE1B);
sei(); //reenable interrupts
}