Muhammad Jabbar Haziq
Muhammad Jabbar Haziq

Reputation: 11

bcd seven segement control from input in assembly language MPlab

I am trying to make a BCD seven segment display which takes input from 4 bits (port d) and gives output at port b for lsb and port c for msb what I am tryin to do is I take the input and I will subtract it with a value which I stored in w register if the result is zero than accordingly I will turn on or turn off those pins at port b and c. Every time I will change values every time in w register here is my code in below links the code is make for only showing 14 and 15. but its not working can any one help me.

{
RES_VECT CODE 0x0000 ; processor reset vector    
GOTO START ; go to beginning of program         

; TODO ADD INTERRUPTS HERE IF USED             
FIXED EQU 20h       
;INPUT EQU 21h          
;output_LSB EQU 22h         
;output_MSB EQU 23h       
MAIN_PROG CODE ; let linker place main program       
START        

BSF STATUS,RP0          
MOVLW b'00000000'         ;making all pins of port b as output         
MOVWF TRISB         
MOVLW b'00000000'         ;making all pins of port c as output        
MOVWF TRISC           
MOVLW b'00001111'         ;making first 4  pins of port d as input        
MOVWF TRISD       
BCF STATUS,5         
;MOVF PORTD,W        
;MOVWF PORTB        
;CLRF PORTD           
MAIN_LOOP           
BCF STATUS, 2                 ;CLEAR STATUS REGISTER PIN 2             
CLRF PORTD                     ;CLEARING ALL PORT D PINS            
MOVF PORTD, W               ;TAKING VALUES FROM PORTD AND STORING IN W REGISTER          
MOVWF FIXED                  ;MOVING VALUE FROM W REGISTER TO VARIABLE FIXED          
MOVF b'00001111',W        ;MOVING 15('F') TO W REGISTER               
SUBWF FIXED, 0               ;SUBTRACTING VALUE OF W REGISTER FROM FIXED AND         
                             ;STORING RESULT IN W REGISTER                          
BTFSS STATUS,2               ;BIT TEST IF ZERO REGISTER OF STATUS REGISSSTER IF 
                             ;IT IS SET. SKIP NEXT LINE              
MOVLW b'00000101'          ; TRANSFERING 5 ON PORT B "WHICH IS LSB OF OUTPUT"        
MOVWF PORTB           
MOVLW b'00000001'          ;TRANSFERING 1 ON PORT C "WHICH IS MSB OF OUTPUT"             
MOVWF PORTC          
GOTO LINEAR_1              
GOTO MAIN_LOOP             

LINEAR_1               
MOVF PORTD ,W             
MOVF b'00001110',W      ;MOVING 14('E') TO W REGISTER           
SUBWF FIXED, 0             ;SUBTRACTING VALUE OF W REGISTER FROM FIXED AND         
                           ;STORING RESULT IN W REGISTER                  
BTFSS STATUS, 2            ;BIT TEST IF ZERO REGISTER OF STATUS REGISSSTER IF IT 
                           ;IS SET. SKIP NEXT LINE                  
GOTO LINEAR_2                   
MOVLW b'00000100'       ;TRANSFERING 4 ON PORT B "WHICH IS LSB OF OUTPUT"          
MOVWF PORTB                 
MOVLW b'00000001'       ;TRANSFERING 1 ON PORT C "WHICH IS MSB OF OUTPUT"         
MOVWF PORTC              
BSF PORTD,7              
GOTO MAIN_LOOP             

END           

}

enter image description here enter image description here

Upvotes: 1

Views: 268

Answers (1)

Joe Thomas
Joe Thomas

Reputation: 335

ok, where to begin. 1) BCD, unless you are talking higher density encoding, only goes from 0b00000000 to 0b00001001 (decimal 0-9), what it appears you want is a hex to decimal converter. 2) You didn't include linear_2, so we don't have the full picture. 3) Making the assumption that you wanted a hex (4 bit) to decimial (2 digit) converter and assuming you are using some sort of BCD to 7-segment for each, all you needed to do is check port D for greater than 9, and if greater, set MSB to 1 and set the lsb to val-9, for instance:

MOVF  PORTD, W               ; move input to working reg
MOVWF FIXED                  ; move value to ram
SUBLW b'00001001'            ; subtract 9 from input
btfsc STATUS,C               ; check the carry bit, skip if no carry
CALL  ADJUST                 ; Adjust FIXED register    
MOVWF PORTC                  ; save msb to port C
MOVF  FIXED, W               ; reload port D, but from ram to avoid changes
MOVWF PORTB                  ; save LSB
RETURN
ADJUST
MOVF b'00001010',W           ; move dec 10 to W               
SUBWF FIXED, 0               ; subtract and store back to fixed
retlw b'00000001'            ; return with 1 in w 

The basic premise is to see if the value of D is less than or equal to 9, if it is, then clear the MSB and just store the the value of the input to the LSB. If the input is greater than 9 (greater than or equal to 10), then set the MSB to 1 and subtract 10 from the input-> store the result to LSB.

Upvotes: 1

Related Questions