Hi all,
I am programmig a dimmer for a 12V lamp with a PIC12F683 with duty cycle controlled by 2 button switch connected as PULL UP to GP5 (to decrease the duty cycle) & GP4 (to increase it).
The PWM period is 312.5Hz, Internal clock oscillator is 4Mhz so Tosc is 0.25us
PWM Period = [(PR2 + 1) ] · 4 · Tosc · TMR2Prescaler
Taking a TMR2 prescaler of 16
0.0032s = [(x + 1) ] · 16u
PR2 = 0xC7
T2CON = 0x03
I started writing the program, which has to be written in Assembly language, but I have a problem.
The register CCPR1L can't be changed during the program (has I know) so I use a general purpose register which I call dcycle.
Normally, I just want to test if GP4 or GP5 are to logical 0 and than control dcycle and than store the variable in CCPR1L, but, if this last register cannot be changed, is obvious that maybe my idea will not works.. Or I am wrong?
However, I my program which I am simulating through Proteus which seems to be a good PIC simulator (it always worked later with real PIC simulation)
program works until the register settings, than PWM doesn't work no more..
I post the code where I marked the line until the program works.
I am thinking the way I am structuring the program is not correct..
How can I solve this problem? It's the first time I use PWM with PIC..
Thank you for your help
Regards,
Simo
I am programmig a dimmer for a 12V lamp with a PIC12F683 with duty cycle controlled by 2 button switch connected as PULL UP to GP5 (to decrease the duty cycle) & GP4 (to increase it).
The PWM period is 312.5Hz, Internal clock oscillator is 4Mhz so Tosc is 0.25us
PWM Period = [(PR2 + 1) ] · 4 · Tosc · TMR2Prescaler
Taking a TMR2 prescaler of 16
0.0032s = [(x + 1) ] · 16u
PR2 = 0xC7
T2CON = 0x03
I started writing the program, which has to be written in Assembly language, but I have a problem.
The register CCPR1L can't be changed during the program (has I know) so I use a general purpose register which I call dcycle.
Normally, I just want to test if GP4 or GP5 are to logical 0 and than control dcycle and than store the variable in CCPR1L, but, if this last register cannot be changed, is obvious that maybe my idea will not works.. Or I am wrong?
However, I my program which I am simulating through Proteus which seems to be a good PIC simulator (it always worked later with real PIC simulation)
program works until the register settings, than PWM doesn't work no more..
I post the code where I marked the line until the program works.
Rich (BB code):
INCLUDE P12F683.INC
LIST p = 12F683
__CONFIG _CP_OFF&_CPD_OFF&_BOD_ON&_PWRTE_ON&_WDT_OFF&_HS_OSC
CBLOCK 0x20
dcycle ;variable duty cycle register
ENDC
; configuration settings
org 0x0000
bsf STATUS, RP0 ; step to
bcf STATUS, RP1 ; bank 1
clrf OPTION_REG
movlw 0x30 ;enable pull up interrupt
movwf IOC ;for GP4 & GP5
movlw 0xc7 ;configure PR2
movwf PR2
clrf ANSEL ;set all pin as Digital Inputs
movlw 0x3b ;set GP4 as input
movwf TRISIO ;and GP2/CCP1 as output
bcf STATUS, RP0 ;step to bank 0
movlw 0x07
movwf CMCON0
movlw 0x03 ;set T2CON with prescaler 1:16
movwf T2CON ;and turn on T2
movlw 0x3c
movwf CCP1CON
start:
movwf dcycle
movwf CCPR1L
goto $
end
;program works just until here
main:
movwf CCPR1L
btfss GPIO, 4 ;test if GP4 is 0
incf dcycle, 1 ;if so increment dcycle
btfss GPIO, 5 ;test if GP5 is 0
decf dcycle, 1 ;if so decrement dcycle
movf dcycle, W ;move dcycle to W
goto start
end
How can I solve this problem? It's the first time I use PWM with PIC..
Thank you for your help
Regards,
Simo
Last edited: