elvito
Newbie level 5
- Joined
- Feb 18, 2010
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- VENEZUELA
- Activity points
- 1,408
GOOD EVENING GUYS, I'M USING THE P18F1330 AND I'M HAVING SOME PROBLEMS IN ORDER TO GET THE PWM PERIOD I NEED. I'VE ALREADY READ THE DATASHEET AND THIS IS WHAT I HAVE:
LIST P=18F1330
INCLUDE "P18F1330.INC"
ORG 0X0000
GOTO START
ORG 0X0008
GOTO INTER
ORG 0X0018
BTFSS INTCON3,INT3IF
RETFIE
BSF PORTA,4
CALL T_2S
BCF PORTA,4
BCF INTCON3,INT3IF
RETFIE
INTER BTFSC PIR3,PTIF
GOTO PWM
BTFSS INTCON3,INT2IF
RETFIE
BSF PORTA,3
CALL T_2S
BCF PORTA,3
BCF INTCON3,INT2IF
RETFIE
PWM BCF PIR3,PTIF
RETFIE
START MOVLW B'01110011'
MOVWF OSCCON
; BSF OSCTUNE,PLLEN
MOVLW .7
MOVWF ADCON1
MOVLW .1
MOVWF TRISA
CLRF TRISB
BSF PIE3,PTIE
BSF IPR3,PTIP
CLRF PTMRL
CLRF PTMRH
BSF INTCON,GIE
BSF INTCON,PEIE
CLRF PORTA
CLRF PORTB
MOVLW B'00010111'
MOVWF PWMCON0
MOVLW B'00001100'
MOVWF PTCON0
MOVLW 0X08
MOVWF PTPERH
MOVLW 0X22
MOVWF PTPERL
MOVLW B'00001001'
MOVWF PWMCON1
MOVLW 0X02
MOVWF PDC0H
MOVLW 0X00
MOVWF PDC0L
MOVLW B'10000000'
MOVWF PTCON1
ESP1 BTFSC PORTB,1
GOTO ESP1
ESP2 BTFSS PORTB,1
GOTO ESP2
GOTO ESP1
END
I'M WORKING WITH A 8MHZ INTERNAL OSCILATOR, PORTB CONFIGURED AS OUTPUTS.
I ENABLED PWM BASE TIME INTERRUPT WITH A HIGH PRIORITY, PLEASE DO NOT PAY ATTENTION TO THE LOW PRIORITY INTERRUPT BECAUSE I WAS TESTING OTHER MODULES.
THEN, I ACTIVATED THE GIE AND ARRIVED WHERE MY DOUBT IS.
FOR EXAMPLE: IF I'D LIKE TO HAVE A 60HZ PERIOD WITH A 8MHZ OSCILATOR(IF I CAN'T HAVE THIS PERIOD WOULD YOU MIND GIVING ME AN EXAMPLE WITH A DIFFERENT ONE)
T(PWM)=((PTPER+1)*PTMRPS)/(8MHZ/4)
PTMRPS=256/4=64 THE 4 THAT DIVIDES THE 8MHZ, I SIMPLIFIED IT WITH THE 256 PREESCALER GETTING THE NEW ONE 64 RIGHT?
T(PWM)=16.66mS SO I'LL GET THAT THE PTPER=2082=0X0822
PLEASE CORRECT ME IF I'M WRONG WITH THE LAST FORMULA, AS YOU COULD SEE IN THE PROGRAM I PASTED I WASN'T SURE HOW TO LOAD THE DUTY CYCLE, BECAUSE THE A NOTE IN THE DATASHEET THAT SAYS:"To get the correct PWM duty cycle, always multiply the calculated PWM duty cycle value by four before writing it to the PWM Duty Cycle registers. This is due to the two additional LSBs in the PWM Duty Cycle registers which are compared against the internal Q clock for the PWM duty cycle match" BEFORE THIS QUOTE, THEY EXPLAINED SOMETHING LIKE I HAVE TWO CONSIDER THE 12 UPPER BITS OF THE PCD0 IN ORDER TO ESTABLISH THE DUTY CYCLE I NEED. ?
I'M REALLY SORRY IF I TALKED TO MUCH BUT I TRIED TO EXPLAIN MY PROBLEM THE BEST I COULD. PLEASE HELP ME WITH THIS IT'S IMPORTANT.
PS. THE BTFSS AT THE END OF THE PROGRAM IT'S A WAY OF MEASURING THE PWM DUTY CYCLE AND PERIOD USING THE DEBUGGER.
THANK YOU IN ADVANCED
LIST P=18F1330
INCLUDE "P18F1330.INC"
ORG 0X0000
GOTO START
ORG 0X0008
GOTO INTER
ORG 0X0018
BTFSS INTCON3,INT3IF
RETFIE
BSF PORTA,4
CALL T_2S
BCF PORTA,4
BCF INTCON3,INT3IF
RETFIE
INTER BTFSC PIR3,PTIF
GOTO PWM
BTFSS INTCON3,INT2IF
RETFIE
BSF PORTA,3
CALL T_2S
BCF PORTA,3
BCF INTCON3,INT2IF
RETFIE
PWM BCF PIR3,PTIF
RETFIE
START MOVLW B'01110011'
MOVWF OSCCON
; BSF OSCTUNE,PLLEN
MOVLW .7
MOVWF ADCON1
MOVLW .1
MOVWF TRISA
CLRF TRISB
BSF PIE3,PTIE
BSF IPR3,PTIP
CLRF PTMRL
CLRF PTMRH
BSF INTCON,GIE
BSF INTCON,PEIE
CLRF PORTA
CLRF PORTB
MOVLW B'00010111'
MOVWF PWMCON0
MOVLW B'00001100'
MOVWF PTCON0
MOVLW 0X08
MOVWF PTPERH
MOVLW 0X22
MOVWF PTPERL
MOVLW B'00001001'
MOVWF PWMCON1
MOVLW 0X02
MOVWF PDC0H
MOVLW 0X00
MOVWF PDC0L
MOVLW B'10000000'
MOVWF PTCON1
ESP1 BTFSC PORTB,1
GOTO ESP1
ESP2 BTFSS PORTB,1
GOTO ESP2
GOTO ESP1
END
I'M WORKING WITH A 8MHZ INTERNAL OSCILATOR, PORTB CONFIGURED AS OUTPUTS.
I ENABLED PWM BASE TIME INTERRUPT WITH A HIGH PRIORITY, PLEASE DO NOT PAY ATTENTION TO THE LOW PRIORITY INTERRUPT BECAUSE I WAS TESTING OTHER MODULES.
THEN, I ACTIVATED THE GIE AND ARRIVED WHERE MY DOUBT IS.
FOR EXAMPLE: IF I'D LIKE TO HAVE A 60HZ PERIOD WITH A 8MHZ OSCILATOR(IF I CAN'T HAVE THIS PERIOD WOULD YOU MIND GIVING ME AN EXAMPLE WITH A DIFFERENT ONE)
T(PWM)=((PTPER+1)*PTMRPS)/(8MHZ/4)
PTMRPS=256/4=64 THE 4 THAT DIVIDES THE 8MHZ, I SIMPLIFIED IT WITH THE 256 PREESCALER GETTING THE NEW ONE 64 RIGHT?
T(PWM)=16.66mS SO I'LL GET THAT THE PTPER=2082=0X0822
PLEASE CORRECT ME IF I'M WRONG WITH THE LAST FORMULA, AS YOU COULD SEE IN THE PROGRAM I PASTED I WASN'T SURE HOW TO LOAD THE DUTY CYCLE, BECAUSE THE A NOTE IN THE DATASHEET THAT SAYS:"To get the correct PWM duty cycle, always multiply the calculated PWM duty cycle value by four before writing it to the PWM Duty Cycle registers. This is due to the two additional LSBs in the PWM Duty Cycle registers which are compared against the internal Q clock for the PWM duty cycle match" BEFORE THIS QUOTE, THEY EXPLAINED SOMETHING LIKE I HAVE TWO CONSIDER THE 12 UPPER BITS OF THE PCD0 IN ORDER TO ESTABLISH THE DUTY CYCLE I NEED. ?
I'M REALLY SORRY IF I TALKED TO MUCH BUT I TRIED TO EXPLAIN MY PROBLEM THE BEST I COULD. PLEASE HELP ME WITH THIS IT'S IMPORTANT.
PS. THE BTFSS AT THE END OF THE PROGRAM IT'S A WAY OF MEASURING THE PWM DUTY CYCLE AND PERIOD USING THE DEBUGGER.
THANK YOU IN ADVANCED