Brightness; S'ware PWM (uses 3 GPRs) on a 3 bit (8 step) base, LEDdrive GPR bits 0,2 are the common anodes in this case. At about a 1.37ms prg loop speed (my app. prg) 100% DC period is 11mSec or 90Hz refresh.
incf LUXCTRL,w; 100% DC timebase. GPR register
andlw .7; lower 3 bits
movwf LUXCTRL; range 0-7.
subwf LUX_setpt,w; Brightness PWM setting, GPR register. Range = 0 (12.5%) thru 7 (100%) for dimmest to brightest.
movf LEDdrive,w; buffer GPR register for common anodes avoids Read Modify Write issues.
skpc
andlw b'1111010'; disable active common anodes if no carry.
movwf LEDport; mapped to actual pic PORT, eg #define LEDport PORTB
return
rrf LuxSet, w
rrf LuxSet, f
skpc
return
movlw 0x07 ; mask for the pins to set
xorwf LEDPort, f ; preferably LAT
return
00000000 - 100%
10000001 - 87.5 %
01000001 - 75%
00100001 - 62.5%
00010001 - 50%
00001001 - 37.5%
00000101 - 25%
00000011 - 12.5%
00000000 - 0%
Won't the Xorwf on the port become a candidate for the RMW issue. By using the LEDdrive as a GPR buffer of what is desired as the actual PORT output, the 6 other PORT pins are not corrupted, merely unchanged. Note that I am driving just two common anodes here, although up to 8 can be driven with the full PORT with little change except to the ANDLW statement.
movf LEDdrive,w
andlw b'1111010'
movwf LEDport
bsf LEDdrive, #5
movf LEDdrive
movwf LEDport
Well if driving PB0 and PB2 high is undesirable at the time you wish to set PB5, simply set PB0 and PB2 to what you need in the shadow register of LEDdrive before writing LEDdrive to the actual PORT
Imagine I'm doing 10kHz PWM on PB5. How do I know which values are desirable for B0 and B2 at the moment?
What does RB5 have to do with the required settings of RB0 and RB2? It is what you want it to be based on the prg requirements....I don't see the point of the question. Whatever you want ANY pin to be , simply set it before you write to the port.
As I understand, you're trying to deal with the RMW issue by maintaining a shadow register which you call LEDdrive. This would assume that the other parts of the code should use the same LEDdrive as a shadow register, and they should maintain it up to date. If they don't, you will overwrite all their actions with this:
Say, somewhere else in the code, when you want to set bit #5 on PORTB (both real and the shadow) you do something like:Code:movf LEDdrive,w andlw b'1111010' movwf LEDport
But, LEDdrive has bits #0 and #2 set to 1 (otherwise the code you have posted wouldn't work). So, this code which wants to set only bit #5 will unintentionally drive pins #0 and #2 high. How's that better than RMW?Code:bsf LEDdrive, #5 movf LEDdrive movwf LEDport
movf LEDdrive,w
andlw b'1111010'
movwf LEDport
bsf LEDdrive, #5
movf LEDdrive
movwf LEDport
movf LEDdrive,w
andlw b'1111010'
movwf LEDport
bsf LEDdrive, #5
movf LEDdrive
movwf LEDport
Your approach to doing a 10Khz with this shadow register is not the proper one. It breaks the function of the posted code.
TenK_PWM; test of a simple 10K PWM on bit 5 of PORTB vi a shadow register.
incf PWMCTRL,w
andlw .7; 3 bit, 8 step, range of control.
movwf PWMCTRL
subwf PWMDC,w
bsf LEDdrive,5; default PWM condition = ON.
skpc
bcf LEDdrive,5
movf LEDdrive,w; PORT buffer GPR
movwf LEDport; mapped to actual PIC PORT
return
incf LUXCTRL,w; 100% DC timebase. GPR register
andlw .7; lower 3 bits
movwf LUXCTRL; range 0-7.
subwf LUX_setpt,w; Brightness PWM setting, GPR register. Range = 0 (12.5%) thru 7 (100%) for dimmest to brightest.
movf LEDdrive,w; buffer GPR register for common anodes avoids Read Modify Write issues.
skpc
andlw b'1111010'; disable active common anodes if no carry.
movwf LEDport; mapped to actual pic PORT, eg #define LEDport PORTB
return
incf PWMCTRL,w
andlw .7; 3 bit, 8 step, range of control.
movwf PWMCTRL
subwf PWMDC,w
bsf LEDdrive,5; default PWM condition = ON.
skpc
bcf LEDdrive,5
movf LEDdrive,w; PORT buffer GPR
movwf LEDport; mapped to actual PIC PORT
return
incf LUXCTRL,w; 100% DC timebase. GPR register
andlw .7; lower 3 bits
movwf LUXCTRL; range 0-7.
subwf LUX_setpt,w; Brightness PWM setting, GPR register. Range = 0 (12.5%) thru 7 (100%) for dimmest to brightest.
movf LEDdrive,w; buffer GPR register for common anodes avoids Read Modify Write issues.
skpc
andlw b'1111010'; disable active common anodes if no carry.
movwf LEDport; mapped to actual pic PORT, eg #define LEDport PORTB
return
incf PWMCTRL,w
andlw .7; 3 bit, 8 step, range of control.
movwf PWMCTRL
subwf PWMDC,w
bsf LEDdrive,5; default PWM condition = ON.
skpc
bcf LEDdrive,5
movf LEDdrive,w; PORT buffer GPR
movwf LEDport; mapped to actual PIC PORT
return
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?