PIC16F877 assembly program problem on a LED cube

Thread Starter

stuffy

Joined May 12, 2009
12
For some time now I keep trying to program my cube. But no success. I think is something that I'm declaring wrong. :confused: I have attached a sample of my program to light up a led made by me and my main program (the final one that will go on the cube; it should show on the cube the letters VALHI and numbers 209 and some animations). I have the npn tranzistors that comand the catods on ports A0 A1 A2 A3 A4 and pnp tranzistors that comand the anods on ports B0 all the way to E0 (25 in total). To light up a led I have to activate, let's say layer 1 (A0) by giving 1 and column 1 (B0) by giving 0. It is not a difficult program but I can't just get it to work. I use MPLAB to make the program.

Main program(final one)(it is to long so I'ved upload it on a server): http://www17.zippyshare.com/v/63284413/file.html
Pic of my cube: http://www1.zippyshare.com/v/49857516/file.html
Please help. For any other info just ask me ;)

Program to light up a led:
Rich (BB code):
 		processor       16F877
           list            f=INHX32
	
	list      p=16F877        	; list directive to define processor
        
	include         p16f877.inc
       __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
          radix           dec
        __config        h'3F32' ;OSC is HS
                                ;RB3,RB6,RB7 are I/O
                                ;Brown-out detection OFF
                                ;Power-up timer ON
                                ;Code protection OFF
                                ;Data code protection OFF
                                ;Watchdog timer OFF

        errorlevel      -302    ;Eliminate bank warning
        errorlevel      -307    ;Eliminate Setting page bits warning
;----------------------------------------------------------------------

        cblock  h'20'
        cnt100u                         ;100us counter
        endc


;************************  Program Start  ***************************
        org     0               ;Reset Vector
        goto    init

;************************  Initial Process  *************************
        org     6
init

;*** Port initialization
        bsf     status,rp0      ;Change to Bank1
        clrf    trisa           ;Set RA5-0 to OUTPUT
        clrf    trisb           ;Set RB7-0 to OUTPUT
        clrf    trisc           ;Set RC7-0 to OUTPUT
        clrf    trisd           ;Set RD7-0 to OUTPUT
        clrf    trise           ;Set RE2-0 to OUTPUT
        bcf     status,rp0      ;Change to Bank0
        clrf    porta           ;Clear PORTA
        clrf    portb           ;Clear PORTB
        clrf    portc           ;Clear PORTC
        clrf    portd           ;Clear PORTD
        clrf    porte           ;Clear PORTE


 ;***********************************************************************

START:	    movlw b'00000001'
		movwf PORTA
               movlw b'11111110'
		movwf PORTB
		movlw b'11111111'
		movwf PORTC
		movlw b'11111111'
		movwf PORTD
		movlw b'00000111'
		movwf PORTE

	

GOTO	START




;********************************************************************
;                 END of Clock display check program
;********************************************************************

       end


Thanks

Sorry for my english:p
 
Last edited:

Thread Starter

stuffy

Joined May 12, 2009
12
Is there anyone that could help me?

Please. I don't know what to try anymore with my program. I just have to give 1 and 0 signals to the ports. 1 for A0,A1,A2,A3,A4 and 0 for the rest of the ports. As You cand can see I have allready come up with a program to light a led and a big one with text and animations. I just don't know how to put them togheter in the big program, how to call the declarations.

I'm really desperate. Please help.

Thanks
 

AlexR

Joined Jan 16, 2008
732
I have not looked at your full listing but one thing that does strike me is that you are using porta and porte as digital ports but I don't see any statements in your configuration to set them to digital. Be aware that in PICs ports that can be analog or digital default to the analog state.
 

Thread Starter

stuffy

Joined May 12, 2009
12
First let me explain more the working principle of my cube. It has 5 layers (5 led matrix 5x5) witch are the ground units controlet by 5 tranzistors npn witch are set ON by giving from the MCU "1" signals trough ports A0,A1,A2,A3,A4; and 25 columns wich are the anods + that are controled by pnp tranzistors wich are activated by giving from MCU "0" signals trough ports B0-B7,C0-C7,D0-D7 and E0.

I have tested this by giving 5V ("1" signal) on ports A0-A4 and by giving 0V ("0" signal) on column ports and it works so the circuit is OK.
5 minutes ago I have simulated my program with pic simulator IDE from http://www.oshonsoft.com/ and appears to be working.

I don't know what to do anymore.
 

Thread Starter

stuffy

Joined May 12, 2009
12
Do you mean something like this:

BCF STATUS,RP1
BCF STATUS,RP0
CLRF PORTA
CLRF PORTE
MOVLW 0x00
MOVWF TRISA
MOVLW 0x00
MOVWF TRISE
 

AlexR

Joined Jan 16, 2008
732
No I'm thinking more in terms of:

MOVLW 0X06
MOVWF ADCON1

Take a look at the data sheet description of porta and the registers that it uses.
 
Last edited:

Thread Starter

stuffy

Joined May 12, 2009
12
But shouldn't ports B C and D be digital outputs to? Because I'm using all the ports of the MCU as outputs
 

AlexR

Joined Jan 16, 2008
732
Ports b and c are digital by default, they have no analog capabilities so you don,t have to worry about them. Its just the ports which can be either digital or analog that must be configured to work as digital ports.

The fact that they default to analog and must be commanded to act as digital ports catches out every one who has ever learning PIC programming.
 

Thread Starter

stuffy

Joined May 12, 2009
12
So this is for PORTA:
MOVLW 0X06
MOVWF ADCON1 ;makes all the pins of PORTA as Digital I/O
MOVLW 0x07
MOVWF COMCON ;disables the comparators

But I can't get it for PORTE and D
 

AlexR

Joined Jan 16, 2008
732
Setting ADCON1 to 0X06 sets AN0 - AN7 to be digital ports, this range includes all porta pins and all porte pins. Everything else is digital by default so you don't need to worry about it.
 

Thread Starter

stuffy

Joined May 12, 2009
12
So the final code to light up a led should be like this:

Rich (BB code):
	processor       16F877
           list            f=INHX32
	
	list      p=16F877        	; list directive to define processor
        
	include         p16f877.inc
       __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
          radix           dec
        __config        h'3F32' ;OSC is HS
                                ;RB3,RB6,RB7 are I/O
                                ;Brown-out detection OFF
                                ;Power-up timer ON
                                ;Code protection OFF
                                ;Data code protection OFF
                                ;Watchdog timer OFF

        errorlevel      -302    ;Eliminate bank warning
        errorlevel      -307    ;Eliminate Setting page bits warning
;----------------------------------------------------------------------

        cblock  h'20'
        cnt100u                         ;100us counter
        endc


;************************  Program Start  ***************************
        org     0               ;Reset Vector
        goto    init

;************************  Initial Process  *************************
        org     6
init

;*** Port initialization
        bsf     status,rp0      ;Change to Bank1
        clrf    trisa           ;Set RA5-0 to OUTPUT
        clrf    trisb           ;Set RB7-0 to OUTPUT
        clrf    trisc           ;Set RC7-0 to OUTPUT
        clrf    trisd           ;Set RD7-0 to OUTPUT
        clrf    trise           ;Set RE2-0 to OUTPUT
        bcf     status,rp0      ;Change to Bank0
        clrf    porta           ;Clear PORTA
        clrf    portb           ;Clear PORTB
        clrf    portc           ;Clear PORTC
        clrf    portd           ;Clear PORTD
        clrf    porte           ;Clear PORTE

        MOVLW 0X06
        MOVWF ADCON1

 ;***********************************************************************

START:	    movlw b'00000001'
		movwf PORTA
               movlw b'11111110'
		movwf PORTB
		movlw b'11111111'
		movwf PORTC
		movlw b'11111111'
		movwf PORTD
		movlw b'00000111'
		movwf PORTE

	

GOTO	START




;********************************************************************
;                 END of Clock display check program
;********************************************************************

       end
 

Thread Starter

stuffy

Joined May 12, 2009
12
Thank you very much. I didn't know about this.

Tomorrow I will load the final program in the MCU and hopefully it will bring to life my led cube.

Thanks again.

Cheers
 

Thread Starter

stuffy

Joined May 12, 2009
12
So.. I managed to get it to work. Had some hardware problems (the 15pf capacitors had to be replaced with 22pf) but the program to light up a led works great. I made another one for letter "V" and it works to.

I have made declarations for letters V A L H I and numbers 2 0 9 and some animations for each vertical matrice of the cube. The final effect should be the letters and numbers comming from the back of the cube to the front by turning off the previous one.
I want to call them one after the other with a short delay between them. I have made a sample with letter V and L. But I don't know how to create the delay.

Sample V and L:

Rich (BB code):
 		processor       16F877
        list            f=INHX32
        include         p16f877.inc
;       __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
        radix           dec
        __config        h'3F32' ;OSC is HS
                                ;RB3,RB6,RB7 are I/O
                                ;Brown-out detection OFF
                                ;Power-up timer ON
                                ;Code protection OFF
                                ;Data code protection OFF
                                ;Watchdog timer OFF

        errorlevel      -302    ;Eliminate bank warning
        errorlevel      -307    ;Eliminate Setting page bits warning
;----------------------------------------------------------------------

        cblock  h'20'
		cnt100u                         ;100us counter
        endc


;************************  Program Start  ***************************
        org     0               ;Reset Vector
        goto    init

;************************  Initial Process  *************************
        org     6
init

;*** Port initialization
	
        bsf     status,rp0      ;Change to Bank1
        clrf    trisa           ;Set RA5-0 to OUTPUT
	clrf    trisb           ;Set RB7-0 to OUTPUT
        clrf    trisc           ;Set RC7-0 to OUTPUT
        clrf    trisd           ;Set RD7-0 to OUTPUT
        clrf    trise           ;Set RE2-0 to OUTPUT
        bcf     status,rp0      ;Change to Bank0
        clrf    porta           ;Clear PORTA
        clrf    portb           ;Clear PORTB
        clrf    portc           ;Clear PORTC
        clrf    portd           ;Clear PORTD
        clrf    porte           ;Clear PORTE

	MOVLW 0X06
	MOVWF ADCON1        ;makes all the pins of PORTA as Digital I/O
	

 ;***********************************************************************

START:	call PROCV1

	call PROCL1


	goto START


PROCV1: movlw b'00000111'
	movwf PORTA
        movlw b'11101110'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00001000'
	movwf PORTA
        movlw b'11110101'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00100000'
	movwf PORTA
        movlw b'11111011'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

return

PROCL1: movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00101111'
	movwf PORTA
        movlw b'11111110'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00100000'
	movwf PORTA
        movlw b'11100001'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

return


;********************************************************************
;                 END of Clock display check program
;********************************************************************

       end
Can You help please.

THANKS
 

Thread Starter

stuffy

Joined May 12, 2009
12
So... I got a delay routine from the generator. This is how my program looks now:

Rich (BB code):
 	processor       16F877
        list            f=INHX32
        include         p16f877.inc
;        __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
        radix           dec
        __config        h'3F32' ;OSC is HS
                                ;RB3,RB6,RB7 are I/O
                                ;Brown-out detection OFF
                                ;Power-up timer ON
                                ;Code protection OFF
                                ;Data code protection OFF
                                ;Watchdog timer OFF

        errorlevel      -302    ;Eliminate bank warning
        errorlevel      -307    ;Eliminate Setting page bits warning

;***********************************************************************

        cblock  h'20'
		cnt100u                         ;100us counter
        endc

;************************  Program Start  ***************************
        ;org     0               ;Reset Vector
        ;goto    init

;************************  Initial Process  *************************
        ;org     6
	;init

;************************  Port initialization  *************************
	
        bsf     status,rp0      ;Change to Bank1
        clrf    trisa           ;Set RA5-0 to OUTPUT
	clrf    trisb           ;Set RB7-0 to OUTPUT
        clrf    trisc           ;Set RC7-0 to OUTPUT
        clrf    trisd           ;Set RD7-0 to OUTPUT
        clrf    trise           ;Set RE2-0 to OUTPUT
        bcf     status,rp0      ;Change to Bank0
        clrf    porta           ;Clear PORTA
        clrf    portb           ;Clear PORTB
        clrf    portc           ;Clear PORTC
        clrf    portd           ;Clear PORTD
        clrf    porte           ;Clear PORTE

	MOVLW 0X06
	MOVWF ADCON1        ;makes all the pins of PORTA as Digital I/O
	


;************************  Delay routine  *************************

; Delay = 0.5 seconds
; Clock frequency = 20 MHz

; Actual delay = 0.5 seconds = 2500000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	endc

Delay
			;2499992 cycles
	movlw	0x15
	movwf	d1
	movlw	0x74
	movwf	d2
	movlw	0x06
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;4 cycles
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return


;************************  Main program  *************************



START:	call procv1
	call delay
	clrf porta
	clrf portb
	clrf portc
	clrf portd
	clrf porte
	goto start



PROCV1: movlw b'00000111'
	movwf PORTA
        movlw b'11101110'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00001000'
	movwf PORTA
        movlw b'11110101'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00100000'
	movwf PORTA
        movlw b'11111011'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE




return


;********************************************************************


       end
And I don't get it. Shouldn't this blink procv1 at a interval of 0.5s??? It doesn't light up not even a single LED.
 

AlexR

Joined Jan 16, 2008
732
One thing that does stand out is that the cblock statements in the delay routine should be combined with your cblock statement at the beginning of the program, otherwise you may get your cnt100u variable overwritten.
 

Thread Starter

stuffy

Joined May 12, 2009
12
Something like this:

Rich (BB code):
 	processor       16F877
        list            f=INHX32
        include         p16f877.inc
;        __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
        radix           dec
        __config        h'3F32' ;OSC is HS
                                ;RB3,RB6,RB7 are I/O
                                ;Brown-out detection OFF
                                ;Power-up timer ON
                                ;Code protection OFF
                                ;Data code protection OFF
                                ;Watchdog timer OFF

        errorlevel      -302    ;Eliminate bank warning
        errorlevel      -307    ;Eliminate Setting page bits warning

;***********************************************************************

;************************  Program Start  ***************************
        ;org     0               ;Reset Vector
        ;goto    init

;************************  Initial Process  *************************
        ;org     6
	;init

;************************  Port initialization  *************************
	
        bsf     status,rp0      ;Change to Bank1
        clrf    trisa           ;Set RA5-0 to OUTPUT
	clrf    trisb           ;Set RB7-0 to OUTPUT
        clrf    trisc           ;Set RC7-0 to OUTPUT
        clrf    trisd           ;Set RD7-0 to OUTPUT
        clrf    trise           ;Set RE2-0 to OUTPUT
        bcf     status,rp0      ;Change to Bank0
        clrf    porta           ;Clear PORTA
        clrf    portb           ;Clear PORTB
        clrf    portc           ;Clear PORTC
        clrf    portd           ;Clear PORTD
        clrf    porte           ;Clear PORTE

	MOVLW 0X06
	MOVWF ADCON1        ;makes all the pins of PORTA as Digital I/O
	


;************************  Delay routine  *************************

; Delay = 0.5 seconds
; Clock frequency = 20 MHz

; Actual delay = 0.5 seconds = 2500000 cycles
; Error = 0 %

	cblock
	cnt100u
	cnt100u_1
	cnt100u_2
	endc

Delay
			;2499992 cycles
	movlw	0x15
	movwf	cnt100u
	movlw	0x74
	movwf	cnt100u_1
	movlw	0x06
	movwf	cnt100u_2
Delay_0
	decfsz	cnt100u, f
	goto	$+2
	decfsz	cnt100u_1, f
	goto	$+2
	decfsz	cnt100u_2, f
	goto	Delay_0

			;4 cycles
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return

;************************  Main program  *************************



START:	call procv1
	call delay
	clrf porta
	clrf portb
	clrf portc
	clrf portd
	clrf porte
	goto start



PROCV1: movlw b'00000111'
	movwf PORTA
        movlw b'11101110'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00001000'
	movwf PORTA
        movlw b'11110101'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00100000'
	movwf PORTA
        movlw b'11111011'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE

	movlw b'00000000'
	movwf PORTA
        movlw b'11111111'
	movwf PORTB
	movlw b'11111111'
	movwf PORTC
	movlw b'11111111'
	movwf PORTD
	movlw b'00000111'
	movwf PORTE




return


;********************************************************************


       end
 
Top