10 bits to 8 bits

edmunds

Senior Member
Dear all,

I'm configuring PWM with pokesfr, since I need to route around timers and use different pins than the defaults for picaxe firmware. 8 MSBs of duty cycle are stored in CCPRxL register and the remaining 2 LSBs are kept in bits 5:4 of CCPxCON register. Since the bits around 5:4 in CCPRxL (7:6;3:0) are meaningful, it is quite a bit of a hassle to read and mask the rest of the bits on every change. That is to say, it eats aways milliseconds of my tight PID loops.

I have been browsing the Internet and staring at textbooks for a while now, but I'm yet to understand what would happen if I would just forget about the 2 LSBs. Pen and paper style, what is the math? How much resolution do I loose? Max decimal number one can 'make' with two binary bits is 3. Does this mean I get duty cycle 'step' of 3? Like 0,3,6,9...81,86... etc? I understand my new range would be 0...255, but that does not divide with 3, so the above must be wrong somehow :).

Can anyone help, please? :)


Merry holidays,

Edmunds
 

inglewoodpete

Senior Member
There are a lot of your questions and assertions regarding PWM where I would answer "it depends". It depends on what you are driving with the PWM: an LED or a motor or something else? What is your interface between the chip and the load? And, why not use 10-bit PWM, if you want small increments? If driving LED/s, be aware that the human eye has a logarithmic response to light levels: 100% barely appears to be brighter than 50%.
 

hippy

Technical Support
Staff member
The extra two bits being in a different register is a pain; and that's why the PWMDUTY command generates so much code because it has to manipulate so many registers, and it's a 'hidden macro' style command rather than entirely included in the firmware.

There should be no real problem with using 8-bits all round, keeping the other two bits static. You lose three quarters of the resolution, go from 0-1023 to 0-255, but that is usually good enough. It still allows a resolution of better than 0.5%.
 

edmunds

Senior Member
There are a lot of your questions and assertions regarding PWM where I would answer "it depends". It depends on what you are driving with the PWM: an LED or a motor or something else? What is your interface between the chip and the load? And, why not use 10-bit PWM, if you want small increments? If driving LED/s, be aware that the human eye has a logarithmic response to light levels: 100% barely appears to be brighter than 50%.
It was/is for running a model car, the main engine. Since I'm running half-bridge mode, there is about 100 speed 'steps' each direction with stop being 127+-20 as the current is just too low for the engine to run in either direction very close to 127. It works very well now, the code is in the file attached to the other thread (on pause issue). I'm using a macro like this:

Code:
#macro SetSpeed(spd)
      pokesfr CCPR3L, spd    '8 MSBs of duty cycle
#endmacro
To change the speed, I only need to call it like this:

Code:
Symbol Slow_fwd    = 150

      SetSpeed(Slow_fwd)
/Edmunds
 
Top