So… a bit of head scratching this afternoon. I had working PWM on the LEDs… which was fantastic.
With the trade-off of LEDs being ⅛ brightness maximum, which is fine since the LED strings that I’ve been given for this project would make Manfred Mann proud…
I was a little stumped with my audio PWM. I could not get a peep out of it, not via the amplifier or anything else. Tracing back, I was getting nothing out of the PWM pin. Real strange. Looked up the data sheets, couldn’t see what I was doing wrong.
My code for initialising Timer1 looked like this:
OCR1B = 128;
OCR1D = 32;
OCR1C = 255;
TCCR1A = (2 << COM1B0)
| (1 << PWM1B);
TCCR1B = (1 << CS10);
TCCR1C = (3 << COM1D0)
| (1 << PWM1D);
TCCR1D = (0 << WGM10);
It looked fine… even checked the header files to see there wasn’t some cock up in the headers. Never had an issue with avr-libc, but you never know, and of course, no problem there.
In exasperation, I swapped setting up TCCR1A and TCCR1C. Bingo, I had PWM both channels. Why? Turns out there are some bits in TCCR1C that shadow TCCR1A, and I was inadvertently setting those back to 0 when I set up TCCR1C. So my code now:
OCR1C = 255;
TC1H = 0;
TCNT1 = 0;
OCR1A = 0;
OCR1B = 127;
OCR1D = 127;
TCCR1A = (1 << PWM1B);
TCCR1B = (1 << CS10);
TCCR1C = (2 << COM1B0S)
| (3 << COM1D0)
| (1 << PWM1D);
TCCR1D = (0 << WGM10);
TCCR1E = 0;
We still need to turn PWM1B on via TCCR1A… but we set the output control mode via TCCR1C to avoid clobbering our settings.
Having fixed that, I now have a second problem, in my dead-bugging of the amp, I got my left and right arse-about, which would explain why my amp is making no noise. That’ll be a tomorrow job methinks, at 22:30 UTC+10:00 it is too dark to work on PCBs. I’ll just keep myself amused looking at the waveform on the oscilloscope.
Recent Comments