Pls help me.Need advice

Discussion in 'Embedded Systems and Microcontrollers' started by ridge84, Aug 27, 2008.

  1. ridge84

    Thread Starter New Member

    Aug 18, 2008
    1
    0
    Hi all,I tried running this program below and program succesfully but I could not get the LED to light up on my hardware. Apparently there are no output voltage from the Pic to LED when I check with a multimeter,but there is input though and everything seems to work fine except the LED.Is there something wrong with my software??

    List p=p16f873a ; Set target Processor to P16F873
    Include p16f873a.inc ; Include additional source file
    ERRORLEVEL -302
    ;__CONFIG _HS_OSC & _CP_OFF & _WDT_OFF & _PWRTE_ON
    ; ***** Main Program *****
    speed equ d'8' ;Reference speed ;(5x8/256=0.156V)
    change equ d'1' ;Change value (10mV/ms)
    led equ 0x20 ;LED control data address
    status equ 0x83
    trisa equ 0x85 ;Set TrisA register
    trisb equ 0x86 ;Set TrisB register
    trisc equ 0x87 ;Set TrisC register
    adcon0 equ 0x1f
    tmr2 equ 0x11
    ccpr1l equ 0x15
    pr2 equ 0x92
    t2con equ 0x12
    ccp1con equ 0x17
    tmr1h equ 0x0f
    tmr1l equ 0x0e
    ccpr2h equ 0x1c
    ccpr2l equ 0x1b
    t1con equ 0x10
    ccp2con equ 0x1d
    intcon equ 0x8b
    pir2 equ 0x0d
    t2con equ 0x12
    adresh equ 0x1e
    portb equ 0x06
    adcon1 equ 0x9f
    pie2 equ 0x8d
    z equ 0
    rp0 equ 0x03
    c equ 0
    go equ 0

    ; ***** Program Start *****
    org 0 ;Reset Vector
    goto init
    org 4 ;Interrupt Vector
    goto int
    ; ***** Initial Process *****
    init
    ; ***** Port initialization*****
    bsf status,rp0 ;Select to Bank1
    movlw b'00000001' ;Select Port A to input mode
    movwf trisa ;Set TRISA register
    clrf trisb ;Set Port B to output mode
    clrf trisc ;Set TRISC to output mode
    bcf status,rp0 ;Change to Bank0
    ; ***** A/D converter initialization*****
    movlw b'10000001' ;Setup ADC
    movwf adcon0 ;Set ADCON0 register
    bsf status,rp0 ; Select to Bank1
    movlw b'00001110' ;ADFM=0 PCFG=1110
    movwf adcon1 ;Set ADCON1 register
    bcf status,rp0 ;Change to Bank0
    ; ***** PWM initialization*****
    clrf tmr2 ;Clear TMR2 register
    movlw b'11111111' ;Max duty (low speed)
    movwf ccpr1l ;Set CCPR1L register
    bsf status,rp0 ;Change to Bank1
    movlw d'255' ;Period=1638.4usec(610Hz)
    movwf pr2 ;Set PR2 register
    bcf status,rp0 ;Change to Bank0
    movlw b'00000110' ; I/O pin selection
    movwf t2con ;Set T2CON register
    movlw b'00001100' ; Ser PWM mode
    movwf ccp1con ;Set CCP1CON register
    ; ***** Interruption control*****
    bsf status,rp0 ;Change to Bank1
    movlw b'00000001' ;CCP2IE=Enable
    movwf pie2 ;Set PIE2 register
    bcf status,rp0 ;Change to Bank0
    movlw b'11000000' ;GIE=ON PEIE=ON
    movwf intcon ;Set INTCON register
    wait
    goto $ ;Interruption wait
    ; ***** Interruption Process *****
    int
    clrf pir2 ;Clear interruption flag
    ad_check
    btfsc adcon0,go ;Check if completed A/D conversion?
    goto ad_check ;If no repeat ad_check else proceed.
    movfw adresh ;Read ADRESH register
    sublw speed ;Ref speed - Detect speed
    btfsc status,c ;Is speed lesser?
    goto check1 ;If no go to check1,if yes proceed.
    ; ***** control to low speed *****
    movfw ccpr1l ;Read CCPR1L register
    addlw change ;Change value + CCPR1L
    btfss status,c ;Overflow ?
    movwf ccpr1l ;Write CCPR1L
    goto led_cont ;Jump to LED control
    check1
    btfsc status,z ;Reference speed = Detect speed ?
    goto led_cont ;Yes. Jump to LED control

    ; ***** control to fast speed *****
    movlw change ;Set change value
    subwf ccpr1l,f ;CCPR1L - Change value
    btfsc status,c ;Underflow ?
    goto led_cont ;If no check again,else go to LED control
    clrf ccpr1l ;Clear CCPR1L
    ; ****** LED control Process *******
    led_cont
    comf ccpr1l,w ;Complement CCPR1L bit
    movwf led ;Save LED data
    movlw b'0001000' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ;Check status of LED
    goto led1 ;proceed to LED1
    movlw b'0000000' ;Set LED control data
    goto int_end ;Go to interrupt end
    led1 movlw b'0011000' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ;Check status of LED?
    goto led2 ;Proceed to LED2
    movlw b'0011000' ;Set LED control data
    goto int_end ; Go to interrupt end
    led2 movlw b'1101110' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ;Check status of LED?
    goto led3 ;Proceed to LED3
    movlw b'1101110' ;Set LED control data
    goto int_end ; Go to interrupt end
    led3 movlw b'0111110' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ; Check status of LED?
    goto led4 ; Proceed to LED4.
    movlw b'0111110' ;Set LED control data
    goto int_end ; Go to interrupt end
    led4 movlw b'0011011' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ;Check status of LED ?
    goto led5 ;Proceed to LED5.
    movlw b'0011011' ;Set LED control data
    goto int_end ;Go to interrupt end
    led5 movlw b'0110111' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ; Check status of LED ?
    goto led6 ; Proceed to LED6
    movlw b'0110111' ;Set LED control data
    goto int_end ; Go to interrupt end
    led6 movlw b'1110111' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ; Check status of LED ?
    goto led7 ; Proceed to LED7
    movlw b'1110111' ;Set LED control data
    goto int_end ; Go to interrupt end
    led7 movlw b'0011100' ;Set compare data
    subwf led,w ;LED - data
    btfsc status,c ; Check status of LED ?
    goto led8 ; Proceed to LED8
    movlw b'0011100' ;Set LED control data
    goto int_end ; Go to interrupt end
    led8 movlw b'1111111' ;Set LED control data

    ;************ END of Interruption Process **************
    int_end
    movwf portb ;Set PORTB
    retfie ;Comes out of the interrupt routine
    end
     
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Just "prettying up" your code a bit.

    You need to use the CODE tokens when posting source code, otherwise your original formatting is lost.
    Code ( (Unknown Language)):
    1.  
    2. List     p=p16f873a  ; Set target Processor to P16F873
    3. Include  p16f873a.inc  ; Include additional source file
    4.  ERRORLEVEL -302
    5.  ;__CONFIG _HS_OSC & _CP_OFF & _WDT_OFF & _PWRTE_ON
    6. ;    ***** Main Program *****
    7. speed       equ d'8'    ;Reference speed ;(5x8/256=0.156V)
    8. change   equ d'1'     ;Change value (10mV/ms)
    9. led   equ 0x20     ;LED control data address
    10. status  equ 0x83
    11. trisa  equ 0x85  ;Set TrisA register
    12. trisb  equ 0x86  ;Set TrisB register
    13. trisc  equ 0x87  ;Set TrisC register
    14. adcon0  equ 0x1f
    15. tmr2  equ 0x11
    16. ccpr1l  equ 0x15
    17. pr2   equ 0x92
    18. t2con  equ 0x12
    19. ccp1con  equ 0x17
    20. tmr1h  equ 0x0f
    21. tmr1l  equ 0x0e
    22. ccpr2h  equ 0x1c
    23. ccpr2l  equ 0x1b
    24. t1con  equ 0x10
    25. ccp2con  equ 0x1d
    26. intcon  equ 0x8b
    27. pir2  equ 0x0d
    28. t2con  equ 0x12
    29. adresh  equ 0x1e
    30. portb  equ 0x06
    31. adcon1  equ 0x9f
    32. pie2  equ 0x8d
    33. z  equ 0
    34. rp0  equ 0x03
    35. c  equ 0
    36. go  equ 0
    37.  
    38. ;     *****  Program Start  *****
    39.  org      0     ;Reset Vector
    40.  goto     init
    41.  org      4                ;Interrupt Vector
    42.  goto     int
    43. ;   ***** Initial Process *****
    44. init
    45. ;   ***** Port initialization*****
    46.  bsf      status,rp0       ;Select to Bank1
    47.  movlw    b'00000001'      ;Select Port A to input mode
    48.  movwf    trisa            ;Set TRISA register
    49.  clrf     trisb            ;Set Port B to output mode
    50.  clrf     trisc            ;Set TRISC to output mode
    51.  bcf      status,rp0       ;Change to Bank0
    52. ;   ***** A/D converter initialization*****
    53.  movlw    b'10000001'      ;Setup ADC
    54.  movwf    adcon0           ;Set ADCON0 register
    55.  bsf      status,rp0       ; Select to Bank1
    56.  movlw    b'00001110'      ;ADFM=0 PCFG=1110
    57.  movwf    adcon1           ;Set ADCON1 register
    58.  bcf      status,rp0       ;Change to Bank0
    59. ;   ***** PWM initialization*****
    60.  clrf     tmr2             ;Clear TMR2 register
    61.  movlw    b'11111111'      ;Max duty (low speed)
    62.  movwf    ccpr1l           ;Set CCPR1L register
    63.  bsf      status,rp0       ;Change to Bank1
    64.  movlw    d'255'           ;Period=1638.4usec(610Hz)
    65.  movwf    pr2              ;Set PR2 register
    66.  bcf      status,rp0       ;Change to Bank0
    67.  movlw    b'00000110'      ; I/O pin selection
    68.  movwf    t2con            ;Set T2CON register
    69.  movlw    b'00001100'      ; Ser PWM mode
    70.  movwf    ccp1con          ;Set CCP1CON register
    71. ;   ***** Interruption control*****
    72.  bsf      status,rp0       ;Change to Bank1
    73.  movlw    b'00000001'      ;CCP2IE=Enable
    74.  movwf    pie2             ;Set PIE2 register
    75.  bcf      status,rp0       ;Change to Bank0
    76.  movlw    b'11000000'      ;GIE=ON PEIE=ON
    77.  movwf    intcon           ;Set INTCON register
    78. wait
    79.  goto     $               ;Interruption wait
    80. ;   ***** Interruption Process *****
    81. int
    82.  clrf     pir2             ;Clear interruption flag
    83. ad_check
    84.  btfsc    adcon0,go        ;Check if completed A/D conversion?
    85.  goto     ad_check         ;If no repeat ad_check else proceed.
    86.  movfw    adresh            ;Read ADRESH register
    87.  sublw    speed            ;Ref speed - Detect speed
    88.  btfsc    status,c         ;Is speed lesser?
    89.  goto     check1           ;If no go to check1,if yes proceed.
    90. ;   ***** control to low speed *****
    91.  movfw    ccpr1l           ;Read CCPR1L register
    92.  addlw    change           ;Change value + CCPR1L
    93.  btfss    status,c         ;Overflow ?
    94.  movwf    ccpr1l           ;Write CCPR1L
    95.  goto     led_cont         ;Jump to LED control
    96. check1
    97.  btfsc    status,z         ;Reference speed = Detect speed ?
    98.  goto     led_cont         ;Yes. Jump to LED control
    99.  
    100. ;   ***** control to fast speed *****
    101.  movlw    change           ;Set change value
    102.  subwf    ccpr1l,f         ;CCPR1L - Change value
    103.  btfsc    status,c         ;Underflow ?
    104.  goto     led_cont         ;If no check again,else go to LED control
    105.  clrf     ccpr1l           ;Clear CCPR1L
    106. ;   ******  LED control Process *******
    107. led_cont
    108.  comf     ccpr1l,w         ;Complement CCPR1L bit
    109.  movwf    led              ;Save LED data
    110.  movlw    b'0001000'       ;Set compare data
    111.  subwf    led,w            ;LED - data
    112.  btfsc    status,c         ;Check status of LED
    113.  goto     led1             ;proceed to LED1
    114.  movlw    b'0000000'       ;Set LED control data
    115.  goto     int_end          ;Go to interrupt end
    116. led1    movlw   b'0011000'       ;Set compare data
    117.  subwf    led,w            ;LED - data
    118.  btfsc    status,c         ;Check status of LED?
    119.  goto     led2             ;Proceed to LED2
    120.  movlw    b'0011000'       ;Set LED control data
    121.  goto     int_end          ; Go to interrupt end
    122. led2    movlw   b'1101110'       ;Set compare data
    123.  subwf    led,w            ;LED - data
    124.  btfsc    status,c         ;Check status of LED?
    125.         goto    led3             ;Proceed to LED3
    126.         movlw   b'1101110'       ;Set LED control data
    127.         goto    int_end          ; Go to interrupt end
    128. led3    movlw   b'0111110'       ;Set compare data
    129.         subwf   led,w            ;LED - data
    130.         btfsc   status,c         ; Check status of LED?
    131.         goto    led4             ; Proceed to LED4.
    132.         movlw   b'0111110'       ;Set LED control data
    133.         goto    int_end          ; Go to interrupt end
    134. led4    movlw   b'0011011'      ;Set compare data
    135.         subwf   led,w            ;LED - data
    136.         btfsc   status,c         ;Check status of LED ?
    137.         goto    led5             ;Proceed to LED5.
    138.         movlw   b'0011011'       ;Set LED control data
    139.         goto    int_end          ;Go to interrupt end
    140. led5    movlw   b'0110111'      ;Set compare data
    141.         subwf   led,w            ;LED - data
    142.         btfsc   status,c         ; Check status of LED ?
    143.         goto    led6             ; Proceed to LED6
    144.         movlw   b'0110111'       ;Set LED control data
    145.         goto    int_end          ; Go to interrupt end
    146. led6    movlw   b'1110111'       ;Set compare data
    147.         subwf   led,w            ;LED - data
    148.         btfsc   status,c         ; Check status of LED ?
    149.         goto    led7             ; Proceed to LED7
    150.         movlw   b'1110111'       ;Set LED control data
    151.         goto    int_end          ; Go to interrupt end
    152. led7    movlw   b'0011100'       ;Set compare data
    153.         subwf   led,w            ;LED - data
    154.         btfsc   status,c         ; Check status of LED ?
    155.         goto    led8             ; Proceed to LED8
    156.         movlw   b'0011100'       ;Set LED control data
    157.         goto    int_end          ; Go to interrupt end
    158. led8    movlw   b'1111111'       ;Set LED control data
    159.  
    160. ;************ END of Interruption Process **************
    161. int_end
    162.         movwf   portb            ;Set PORTB
    163.         retfie    ;Comes out of the interrupt routine
    164.     end
    165.  
     
    Last edited: Aug 28, 2008
  3. lihle

    Active Member

    Apr 12, 2009
    83
    3
    wht you can do is to check if you have configured the ports correctly.then put a resistor before the LED to reduce the current into the LED. if not try to check how are your registers declared.
     
  4. eng1

    New Member

    Oct 25, 2009
    13
    0
    Please post a schematic or people can only guess what could be wrong.

    I had a quick look at your code; why is the __CONFIG statement commented out? It seems that you are willing to use a high speed crystal as oscillator, is that correct?
    You have to configure the PIC accordingly. Are you doing that in MPLAB? If this is not the case, you need to uncomment (and double-check) the __CONFIG statement.


    EDIT: I've just noticed the date of the first post of this thread, hopefully the OP has already solved his problem :)
     
    Last edited: Nov 29, 2009
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I was wondering why it had over 1000 views.
    Probably best to ignore this thread.
     
Loading...