PDA

View Full Version : Problems with 16F877A code



NightHawk2
- 19th August 2003, 18:17
Im useing Pic Basic Pro 2.42. I have the following code that suposedly should create a 13 Khz PWM with a 50% duty cicle, but what I get is closer to a 40 khz. Also, please review my code for an interrupt in the falling edge of RB0:

ADCON1=7 'ADC OFF
TRISB.0=1 'INTERRUPT EN 33
TRISC= %00000000 'HACE EL PUERTO C PURAS ENTRADAS.
PORTD.2=1

wsave var byte $20 system
ssave var byte $21 system
psave var byte $22 system
define INTHAND MYINT

OPTION_REG.6=0; 'EL INTERRUPT EMPIEZA EN EL FALLING EDGE
INTCON = %11000000 'PRENDE LOS INTERRUPTS;

GOTO AFTERINTERRUPT:
asm
; Save W, STATUS and PCLATH registers
MYINT
CLRWDT
MOVWF wsave ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
CLRF STATUS ;bank 0, regardless of current bank, Clears IRP,RP1,RP0
MOVWF ssave ;Save status to bank zero STATUS_TEMP register
MOVF PCLATH, W ;Only required if using pages 1, 2 and/or 3
MOVWF psave ;Save PCLATH into W
CLRF PCLATH ;Page zero, regardless of current page

BTFSS PORTB,0
MOVLW 7;

CALL NEXTPULSE
BCF _INBYTE,7;
BTFSC PORTB,0
BSF _INBYTE,7;
RRF _INBYTE;
DECFSZ W,0;
GOTO $-6;
CALL NEXTPULSE
NOP
CALL NEXTPULSE
;(ISR) ;(Insert user code here)
CLRWDT
INCF _EVENT,1
MOVF psave, W ;Restore PCLATH
MOVWF PCLATH ;Move W into PCLATH
SWAPF ssave,W ;Swap STATUS_TEMP register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF wsave,F ;Swap W_TEMP
SWAPF wsave,W ;Swap W_TEMP into W
RETFIE ;RETURN FROM INTERRUPT

NEXTPULSE:
BTFSS PORTB,0
GOTO $-1;
BTFSC PORTB,0
GOTO $-1;
RETURN


endasm
AFTERINTERRUPT:

PR2 = 18 ' Set PWM Period for approximately 13KHz
CCPR1L = %00001001 ' MOST SIGNIFIFICANT BSet PWM Duty-Cycle to 50%
CCP1CON.4 =0
CCP1CON.5 =1
TRISC.2 = 0 ' CCP1 (PortC.2 = Output)
T2CON = %00000101 'TIMER IS SET AT 4 PRESCALE
CCP1CON = %00101100 ' Select PWM Mode, <4:5> LEAST SIGNIFICANT

Melanie
- 20th August 2003, 02:36
Is there a reason you're actually using PBP instead of just sticking to assembler?

If you have PBP 2.42 have you tried a simple...

HPWM 1,127,13000

... to give you 50% duty @ 13kHz on CCP1...

Melanie