Since wiring_digital.c turns off PWM on pins that support it prior to digitalRead and digitalWrite, I'm sure its necessary but I would like to know why. I've looked at the ATmega328P datasheet and searched here and the web, but can't find the answer.
Here's some details of my sketch - I'm using an Uno and of the 6 digital pins that support PWM, two are used with analogWrite. Of the remaining 4 , two are input only (from switches) and two are output only (control signals to other chips). The two pins using PWM are connected to analog meters and I use analogWrite to move the needle and I use digitalWrite to zero the needle.
I'm converting the sketch to direct port manipulation to speed things up but don't want to brake anything in the process. Do I need to turn off PWM on the pins not using it? If so, could someone please tell me why? Or to put it another way, what will/could happen if I don't?
You have to turn off PWM or the pin will keep pulsing up and down. Your digitalWrite command will have a very very temporary effect. The output from the timer compare register that drives the PWM signal over rules whatever you write to the port register.
Okay that makes sense but if I never use analogWrite on a PWM supported pin, do I still have to turn it off for that pin?
I'm just trying to avoid unnecessary steps but if the state of the timer compare register is unpredictable say, after power on, then I should turn off pwm just to be sure. I would think the default would be pwm off but what do I know.
If you've never used pwm on the pin since power on, it's off. It initializes to not being on. But for digitalWrite to work in general case, it has to do that. The pin and port lookup is more expensive than the turning off pwm.
I would recommend doing direct port manipulation when you need the speed, instead of hacking up digitalWrite