Problems with 16F877A code


Closed Thread
Results 1 to 2 of 2
  1. #1
    NightHawk2's Avatar
    NightHawk2 Guest

    Default Problems with 16F877A code

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  2. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 19:51
  3. PicBasic code problems with a 16F84A
    By Lauren Barta in forum mel PIC BASIC
    Replies: 3
    Last Post: - 30th May 2006, 22:50
  4. Timer PIC16F57
    By leonel in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 1st July 2005, 08:14
  5. Line following robot... problems with some code
    By nepstar in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 24th March 2005, 01:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts