The problem was basically me not knowing how to use PWM. There are a lot of ways it can be complicated, and the data sheet explains every single one all at once. If you want to be confused try to figure out PWM from the datasheet alone.
I got lucky when it was on C6 and grabbed an example that worked with basically no modification. I had to noodle through setting it up on B6 all on my own.
- TC4H = 0x03;
- OCR3C = 0xFF;
- TCCR3A = 0b10000010;
- TCCR3B = 0b00000001;
+ ICR1 = 0xFFFF;
+ TCCR1A = 0b00100010; // COM1B1 and WGM11, datasheet page 131
+ TCCR1B = 0b00011001; // WGM13, WGM12, and CS10, datasheet page 133
+
+ OCR1B = 0x0000;
This diff shows the changes I needed to make between C6 and B6. The biggest change is that I couldn't get 8-bit PWM working, so I eventually gave up and used 16-bit. That's where the ICR1 comes from, I had to change to 16 bit by writing to that set of registers. Then I not only had to change to a new set of timers, but also build a new bit-mask, which meant a lot of reading to determine what each bit meant. (Seriously,
grab the datasheet, head to page 131, and try to figure out what those mean.
)
switch (level)
{
case 0:
- pwm_level = 0x00;
+ // Turn off underlighting
+ PORTB &= ~(_BV(PORTB7));
+ OCR1B = 0x0000;
break;
case 1:
- pwm_level = 0x0F;
+ // Turn underlighting back on at low level
+ PORTB |= (_BV(PORTB7));
+ OCR1B = 0x00FF;
break;
case 2:
- pwm_level = 0x80;
+ OCR1B = 0x0FFF;
break;
case 3:
- pwm_level = 0xFF;
+ OCR1B = 0xFFFF;
break;
default:
xprintf("Unknown level: %d\n", level);
}
-
- TC4H = pwm_level >> 8;
- OCR3A = 0xFF & pwm_level;
}
This was the next fun change. In order to write the pwm level to C6, you have to use this weird 10 bit scheme where you write the top 2 bits to TC4H (this is the same register used for anything that needs 10 bits) and then you write the bottom 8 bits to the actual register, in C6's case OCR3A. The MCU knows to grab the full 10 bit value... somehow. This actually meant that writing PWM values to B6 is EASIER, but since C6 was complicated it took me a while to accept that I was overcomplicating it.
And so here we are, 6 hours later. It doesn't really seem like that big of a change now that it's working.