Pls help me.Need advice

Thread Starter

ridge84

Joined Aug 18, 2008
1
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
 

SgtWookie

Joined Jul 17, 2007
22,230
Just "prettying up" your code a bit.

You need to use the CODE tokens when posting source code, otherwise your original formatting is lost.
Rich (BB code):
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
 
Last edited:

lihle

Joined Apr 12, 2009
83
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.
 

eng1

Joined Oct 25, 2009
13
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??
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:
Top