I'm trying to run TLC5940s. I've tried the library but it doesn't work properly, and I thought it would be interesting to write the code myself.
I've started by setting up the grayscale clock. I want it as high frequency as possible, so I'm using timer 2 to control pin 3 (B) in fast pwm mode and a maximum register value in OCR2A. I've set OCR2A to 1 and OCR2B to 0.
void setup() {
// Assign pin modes:
DDRD |= B00011000;
DDRB |= B10111000;
// Clock settings, using timer 2
// This should give a square wave, duty cycle 50%, frequency 16MHz
// Achieved is actually ~14MHz, but significant uncertainty in this measurement.
// Turn on pin B (3) | CTC mode | fast PWM
TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
// OCR2B controlling top limit | set prescaler to 1, i.e. no prescaling
TCCR2B = _BV(WGM22) | _BV(CS20);
// Compare registers. Counter resets and pin goes HIGH on reaching value in A, then goes LOW on reaching value in B.
OCR2A = 1;
OCR2B = 0;
}
The picture shows what I'm getting on the oscilloscope (image). Voltage scale is 2V/div and the time base is <0.2us (I can't be sure exactly because it's an old oscilloscope and the calibration knob seems to vary the time base). I'm not too concerned about the frequency anyway so long as it's more than around 4MHz.
What I am concerned about is that the voltage swing is 10V! (That's all I can say with any certainty - again it's an old oscilloscope so I'm not 100% where ground is, though I would assume that ground is centre screen).
So what's going on here? I measured the signal with a multimeter, hoping to get an average voltage, and that came out at 2.2V but I don't know if that's a meaningful measurement. Do I have a dodgy oscilloscope? Or is the arduino really producing a peak to peak of 10V?
JizzaDaMan:
I can't be sure exactly because it's an old oscilloscope and the calibration knob seems to vary the time base
Have you turned the timebase cal knob past the "click" detent? At one end, it should have a detent position that engages a switch. You should normally leave it there.
Also, check the x10 (times ten) setting. It's for x10 probes. If your probe is the 1x type (or the switch on it is set to 1x) and your oscope setting is 10x, you will read 10x too high.
Also, when you get into this kind of situation, don't get all caught up in the high tech thinking right away. First do a simple sanity check, like connecting it to an AA battery or to the DC supplies on the Arduino.
You can see the overshoot upon changing from high to low and vice versa in both, and you can see that the signal had just about stabilised after 5 clock cycles. At the frequency I was going at, it was overshooting in both directions with no time to restabilise!
Do I need to worry about this? If I do, what should I do?
What I am concerned about is that the voltage swing is 10V! (That's all I can say with any certainty - again it's an old oscilloscope so I'm not 100% where ground is, though I would assume that ground is centre screen).
With an analogue oscilloscope, ground will not be be on the center of the screen, unless you have specifically set it there.
It is easy to find whereabouts it is. You simply disconnect your probe from the signal, and connect it to ground.