first post here, so sorry for any bad styling.
I'm working on a group project and using an Atmega328, without going into specifics, I need to drive two fans, two servos, and 1 CTC based function from 3 timers. To my question then; there's registers OCRx A and B that I'm using to drive the fans at variable speeds, and using a similar method for the servos. Setting up a very basic PWM script, I've noticed that both registers will drive a fan, however when I attempt to use either register independently (fan 1 on, 2 off, then change), OCRxB will not produce an output.
Here's a cut-out of my setup code:
void setup_timers () {
// Set Timer0 to Fast PWM mode
TCCR0A |= (1 << WGM01) | (1 << WGM00);
// Set Timer0 to clear OCR0 A/B on match
TCCR0A |= (1 << COM0A1) | (1 << COM0A0) | (1 << COM0B1) | (1 << COM0B0);
TCCR0B |= (1 << CS01);
TCNT0 = 0;
// Set OCR0 A/B
DDRD = 0xFF;}
Now, ideally, this will set up both registers in Timer 0 for outputs. I'm sending this signal to a transistor, which then powers the fans as determined by this next bit:
int main () {
setup_timers();
while (1) {
/* Timer 0; OCR0A (i = 0, L), OCR0B (i = 1, R), i = 2 denotes both fans */
for (uint8_t i = 0; i <= 3; i++) {
// Cycle Fans Left/Right/Both
for (uint8_t j = 1; j <= 4; j++) {
// Cycle Speed
if (i == 2) {
// Check motor; run timer 0 A/B as required
OCR0A = 0xFF * j / 4;
OCR0B = 0xFF * j / 4;
_delay_ms(2000);
}
else if (i == 1) {
OCR0A = 0xFF * j / 4;
OCR0B = 0;
_delay_ms(2000);
}
else if (i == 0) {
OCR0A = 0;
OCR0B = 0xFF * j / 4;
_delay_ms(2000);
}
}
}
}
return 1; }
Now I know this could be done better, this segment has a heap of other tests cut out. The main issue here is that Fan 1 will run (directed by OCR0A), but Fan 2 is completely ignored. I have read through the data sheet a few times, but it was a bit unclear as to whether this is actually possible, it did say something about OCRxB not being stored, and I have seen other posts asking after that. From what I understand they are independent registers, but is it possible that OCRxA is retained, but after writing OCRxB it is impossible to execute?
Or maybe I'm completely wrong, certainly wouldn't be the first time.