-
Pwm 16f876a
I recently moved a project from a 16F88 to the 16F876A to gain more I/O. Previously the contrast of the LCD was controlled via PWM on CCP1, which worked perfectly. Since moving to the larger PIC I added PWM control of the backlight and can't get either to work.
I have set everything up the same way and have all the defines correct. I have reread Melanie's post about backlight and contrast control. I have no clue where I'm going wrong. Help?
; -----[ Fuses ]----------------------------------------------------------
@__config _HS_OSC & _WDT_ON & _PWRTE_ON & _LVP_OFF & _CP_OFF
; -----[ Setup ]----------------------------------------------------------
ADCON1=%10000110 ; Turn on A/D AN0 - Digital for the rest (16F876A specific)
CMCON=%00000111 ; Disable Comparators
TRISA=%11100001 ; PORTA Output/Input settings (1= Input 0=Output)
TRISB=%00000001 ; PORTB Output/Input settings (1= Input 0=Output)
TRISC=%10001000 ; PORTC Output/Input settings (1= Input 0=Output)
OPTION_REG.6=1 ; INTEDG rising edge=1 falling edge=0
T1CON.4=1
T1CON.5=1
; -----[ Defines ]----------------------------------------------------------
DEFINE OSC 20 ; 20 mhz XTAL (change notes above)
DEFINE I2C_SLOW 1
DEFINE HSER_BAUD 9600 ; 9600 Baud USART
DEFINE HSER_RCSTA 90h ; Enable USART RX
DEFINE HSER_TXSTA 24h ; Enable USART TX
DEFINE HSER_SPBRG 51 ; 9600 Bauds
DEFINE HSER_CLROERR 1 ; Clear all USART errors as they happen
DEFINE LCD_DREG PORTB ; LCD 20x2
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
DEFINE CCP1_REG PORTC ; HPWM 1 pin port
DEFINE CCP1_BIT 2 ; HPWM 1 pin bit
DEFINE CCP2_REG PORTC ; HPWM 2 pin port
DEFINE CCP2_BIT 1 ; HPWM 2 pin bit
-
what happen if you remove
Code:
DEFINE CCP1_REG PORTC ; HPWM 1 pin port
DEFINE CCP1_BIT 2 ; HPWM 1 pin bit
DEFINE CCP2_REG PORTC ; HPWM 2 pin port
DEFINE CCP2_BIT 1 ; HPWM 2 pin bit
Since this PIC don't have alternate pin for any CCPx, i don't know how it's handled.
And the obvious, if i can... did you really selected the right PIC in the editor?
Also, you should have a space between @ and __Config. But it could be a forum copy/paste problem so..
Also, even if not PWM related
Code:
DEFINE HSER_BAUD 9600 ; 9600 Baud USART
DEFINE HSER_RCSTA 90h ; Enable USART RX
DEFINE HSER_TXSTA 24h ; Enable USART TX
DEFINE HSER_SPBRG 51 ; 9600 Bauds
DEFINE HSER_CLROERR 1 ; Clear all USART errors as they happen
You have two definition of the Baudrate... one is false. Or you keep HSER_BAUD 9600 or you correct the HSER_SPBRG 51 to HSER_SPBRG 129. 51 is the right value for 8MHZ.
-
If I remove the defines I get the same results, which is nothing.
And yes, 16F876A for CodeDesigner Lite & ICProg.
-
mmm how about the frequency? @20MHZ the minimum should be 'round 1.2KHZ if you're using HPWM.
-
Here are the subs I'm using.
; -----[ LCD Contrast PWM ]-----------------------------------------
PWM_Contrast:
value=Contrast*40
HPwm 1,value,1000
Return
; -----[ LCD Backlight PWM ]----------------------------------------
PWM_BackLight:
value=Backlight*51
HPwm 2,value,1000
Return
-
As i said, the minimum frequency should be 1.2Khz. 1.3KHZ work fine here. So change it to...
Code:
-----[ LCD Contrast PWM ]-----------------------------------------
PWM_Contrast:
value=Contrast*40
HPwm 1,value,1300
Return
; -----[ LCD Backlight PWM ]----------------------------------------
PWM_BackLight:
value=Backlight*51
HPwm 2,value,1300
Return
About now?
-
Of course, you are correct changing to 1300 worked. Which, now that I think about it, makes sense because I was using the internal 8mHz clock on the 16F88.
Thanks for the help, its my first time with PWM. Needless to say I will remember this in the future.
I may ask Melanie to make a note about this in here FAQ.
-
So my guess in the previous post #2 about SPBRG value was correct. You should modify it NOW!
And... the minimum frequency range is stated in the PBP manual... OUPS!
Glad to know it's working now.
Have fun!
-
I did the RTFM thing and saw the chart. I just didn't make the link mentally between the chart and my code. I will in the future. ;)
Thanks!