Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Assembly Code for my Microcontrollers Class

incontrol

Newbie
Joined
Jul 29, 2021
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
I am trying to write an assembly code for my Microcontrollers Class.

Here is the assignment:
Make a program that converts a 4-bit binary number in one hexadecimal number (0-F).
The output value must be presented in a 7-segment display and an LCD at the same time.

Here is the Beginning of the code I already started

Code:

PROCESSOR 16F877A
Code:
#include <p16f877a.inc>    ; incluir arquivo

    __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
 
    contador EQU 0X20

ORG 0x00

    org  0x00            ;  reset vector
    goto Main          ;
          
    org  0x04            ; interruption vector
    goto Main          ; don't has interruption routine


Main               
;--------------------------------------------;
;                 7-segments                            ;
;--------------------------------------------;
    BSF STATUS,5
    MOVLW B'00001111'
    MOVWF ADCON1
    MOVLW B'00001111'
    MOVWF TRISA
    MOVLW B'00000000'
    MOVWF TRISB
    bcf STATUS,5
    clrf PORTB

;--------------------------------------------;
;                    LCD                     ;
;--------------------------------------------;
BCF STATUS,RP1 ; select bank 1
BSF STATUS,RP0
CLRF TRISB ; bank b as output (LCD)
CLRF TRISD ; port d as output (RD0-R/S, RD1-E)

MOVLW b'00000111'
MOVWF OPTION_REG ; Configuração do registrador OPTION_REG para TMR0  1:256

BCF STATUS,RP0 ; select bank 0
CLRF PORTB ;
CLRF PORTD ;

CALL InicializaLCD

LOOP2
CALL CONVERTEBINPHEX
GOTO LOOP2


;-------------------------------------------;
REINICIO
        clrf contador
        ;clrf PORTA
IMPRIME
        MOVF PORTA,w   ; read  in A 4 bits.
        movf contador,w
        CALL tabela
        movwf PORTB
 
        incf contador, f ; +1 on table
        movlw d'16' ;
        xorwf contador
      
        BTFSS STATUS,Z
        goto IMPRIME  ; z=0  a xor /=0, isso significa que o contador não chegou a 16 posições da tabela
        goto REINICIO ; z=1   a xor = 0, isso significa que o contador  chegou a 16 posições da tabela e inicia novamente a tabela.

tabela
    ADDWF PCL,f    ;
    RETLW B'11111100' ; imprime 0
    RETLW B'01100000' ; imprime 1
    RETLW B'11011010' ; imprime 2
    RETLW B'11110010' ; imprime 3
    RETLW B'01100110' ; imprime 4
    RETLW B'10110110' ; imprime 5
    RETLW B'11111010' ; imprime 6
    RETLW B'11100000' ; imprime 7
    RETLW B'11111110' ; imprime 8
    RETLW B'11100110' ; imprime 9
    RETLW B'11101110' ; imprime A
    RETLW B'11111111' ; imprime B       
    RETLW B'11110000' ; imprime C
    RETLW B'11111101' ; imprime D
    RETLW B'10011110' ; imprime E
    RETLW B'10001110' ; imprime F


;tempo1s



;////////////////////////////////////////////////////////////////////////////////////////////////////

InicializaLCD
BCF PORTD,RD0 ; Define RS como 0 para enviar comandos ao LCD
CALL DELAY_5ms

MOVLW b'00000001' ; clean display
MOVWF PORTB
CALL Enable
CALL DELAY_5ms

MOVLW b'00111000' ;
MOVWF PORTB
CALL Enable
CALL DELAY_5ms

MOVLW b'00001111' ; Display on off
MOVWF PORTB
CALL Enable
CALL DELAY_5ms

MOVLW b'00000110' ;
MOVWF PORTB
CALL Enable
CALL DELAY_5ms

RETURN

EscreveCaracter

BCF PORTD,RD0 ; Define RS as 0 to sent enviar data to LCD
MOVLW b'00000001' ; Set home cursor
CALL Escreve

MOVLW b'00000010' ; Set  home cursor
CALL Escreve

BSF PORTD,RD0 ; Define RS as 1 to sent data to LCD
CALL DELAY_5ms

    BCF STATUS,RP1   ;select bank 1
    BSF STATUS,RP0
    CLRF TRISB       ;port b as output (LCD)
    CLRF TRISD       ;port d as output (RD0-R/S, RD1-E)
     MOVLW b'00000111'
     MOVWF OPTION_REG    ;Configuração do registrador OPTION_REG para TMR0  1:256
    BCF STATUS,RP0               ;Select bank 0
    CLRF PORTB     
    CLRF PORTD     

CALL InicializaLCD

LOOP2
CALL CONVERTEBINPHEX
GOTO LOOP0
;-------------------------------------------------------------------;

CONVERTEBINPHEX

table
    ADDWF PCL,f    ;
    RETLW B'00000000' ; print 0
    RETLW B'00000001' ; print 1
    RETLW B'00000010' ; print 2
    RETLW B'00000011' ; print 3
    RETLW B'00000100' ; print 4
    RETLW B'00000101' ; print 5
    RETLW B'00000110' ; print 6
    RETLW B'00000111' ; print 7
    RETLW B'00001000' ; print 8
    RETLW B'00001001' ; print 9
    RETLW B'00001010' ; print A
    RETLW B'00001011' ; print B     
    RETLW B'00001100' ; print C
    RETLW B'00001101' ; print D
    RETLW B'00001110' ; print E
    RETLW B'00001111' ; print  F

END

; IDK if the order is correct, or something link that, but I know something is missing, can you help?
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,542
Helped
4,463
Reputation
8,935
Reaction score
4,497
Trophy points
1,393
Activity points
135,805
Hi,

Did you compile it? Is there any error?
Did you simulate it? Error?
Did you test it on real hardware? Error?

If not ... why not?

Klaus
 

Dan1138

Member level 3
Joined
Jan 30, 2018
Messages
60
Helped
9
Reputation
20
Reaction score
12
Trophy points
8
Activity points
597
Please post a complete description of exactly how the HD44780 based LCD character module is connected to the PIC16F877A.

From the code you posted the 7-segment LED is a common cathode type connected to PORTB like this:

RB7 - segment A
RB6 - segment B
RB5 - segment C
RB4 - segment D
RB3 - segment E
RB2 - segment F
RB1 - segment G
RB0 - segment Decimal Point

Is this correct?
 

Dan1138

Member level 3
Joined
Jan 30, 2018
Messages
60
Helped
9
Reputation
20
Reaction score
12
Trophy points
8
Activity points
597
As you have not provided any information about how your circuit is connected this is an implementation based on the Microchip PICDEM2+ (DM163022-1):
Code:
        list    n=0, c=250      ; No page breaks, support long lines in list file
        list    r=dec           ; Set default radix to decimal
        errorlevel -302         ; Suppress not in bank zero warning
;
; File: main.asm
; Target: PIC16F877A
; IDE: MPLABX v5.35
; Compiler: MPASMWIN v5.87
;
;                         PIC16F877A
;                 +----------:_:----------+
;   S1  VPP ->  1 : MCLR/VPP      PGD/RB7 : 40 <> PGD       /LED_seg_a
; R16(0-5V) ->  2 : RA0/AN0       PGC/RB6 : 39 <> PGC       /LED_seg_b
;           <>  3 : RA1               RB5 : 38 <>           /LED_seg_c
;           <>  4 : RA2               RB4 : 37 <>           /LED_seg_d
;           <>  5 : RA3           PGM/RB3 : 36 <>    LED_D5 /LED_seg_e
;   S2      ->  6 : RA4               RB2 : 35 <>    LED_D4 /LED_seg_f
;           <>  7 : RA5               RB1 : 34 <>    LED_D3 /LED_seg_g
;           <>  8 : RE0               RB0 : 33 <> S3/LED_D2 /LED_seg_dp
;           <>  9 : RE1               VDD : 32 <- PWR
;           <> 10 : RE2               VSS : 31 <- GND
;       PWR -> 11 : VDD               RD7 : 30 -> LCD_ON
;       GND -> 12 : VSS               RD6 : 29 -> LCD_E
;20MHz XTAL -> 13 : OSC1              RD5 : 28 -> LCD_RW
;20MHz XTAL <- 14 : OSC2              RD4 : 27 -> LCD_RS
;           <> 15 : RC0/T1CKI   RX/DT/RC7 : 26 <- RXD
;           <> 16 : RC1/CCP2    TX/CK/RC6 : 25 -> TXD
;    BUZZER <> 17 : RC2/CCP1          RC5 : 24 <>
;       SCL <> 18 : RC3/SCL       SDA/RC4 : 23 <> SDA
;    LCD_D4 <> 19 : RD0               RD3 : 22 <> LCD_D7
;    LCD_D5 <> 20 : RD1               RD2 : 21 <> LCD_D6
;                 +-----------------------:
;                          DIP-40
;
; Description:
    list    p=16F877A       ; Select PIC16F877A  as target device
    #include <p16f877a.inc>
;
;**********************************************************************
; Configuration words.
;
    __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
;
;**********************************************************************
; Define macros to help with bank selection
;
#define BANK0  (h'000')
#define BANK1  (h'080')
#define BANK2  (h'100')
#define BANK3  (h'180')
;
;**********************************************************************
; Define the system oscillator frequency and instruction cycle frequency
;
#define FSYS (d'8000000')
#define FCYC (FSYS/d'4')
;
;**********************************************************************
; All memory is declared here
;
    cblock 0x20
        LCD_pszRomStr:2         ; used by LCD when printing ASCII strings from the CODE space
        LCD_byte:1              ; used by LCD to save byte sent to or read from LCD
        LCD_BusyBit:1           ; used by LCD to store the mask of where the BUSY bit is located
        ValueRead:1             ; Value read from PORTA
    endc
;
;**********************************************************************
; Reset vector
;
RESET:
    org     0x000               ; processor reset vector
    nop                         ; ICD2 needs this
    goto    start               ; begin PIC initialization
;
;**********************************************************************
; Initialize the PIC to the Power On Reset state
;
start:
    clrf    INTCON              ; Disable all interrupt sources
    banksel BANK1
    clrf    PIE1
    clrf    PIE2

    movlw   b'11111111'         ;
    movwf   OPTION_REG

    movlw   b'11111111'         ;
    movwf   TRISA

    movlw   b'11111111'         ;
    movwf   TRISB

    movlw   b'11111111'         ;
    movwf   TRISC

    movlw   b'11111111'         ;
    movwf   TRISD

    movlw   b'00001111'         ;
    movwf   TRISE

#ifdef __16F877A
    ; disable comparators
    movlw   b'00000111'
    movwf   CMCON
    ; disable ADC analog inputs
    movlw   b'00000110'
    movwf   ADCON1
#endif
    banksel BANK0
    goto    main
;
;**********************************************************************
; Support for HD44780 LCD character module
;
    INCLUDE lcd.inc
;
;**********************************************************************
; Function: LCD_PutHexNibble
; Description:
;   Writes one ASCII character of the hexadecimal value
;   of the low 4-bits in the WREG register.
;
; Inputs:   WREG = 4-bit value to convert to ASCII hex and send to the LCD
;
; Outputs:  none
;
; Returns:  nothing
;
LCD_PutHexNibble:
        andlw   0x0F
        addlw   0x06
        btfsc   STATUS,DC
        addlw   'A'-'0'-d'10'
        addlw   '0'-d'6'
        goto    LCD_WriteData
;
;**********************************************************************
; Main application process loop
;
main:
    call    LCD_Init
;
    movlw   LINE_ONE
    call    LCD_SetPosition
    movlw   LOW(LCD_message1)
    movwf   LCD_pszRomStr
    movlw   HIGH(LCD_message1)
    movwf   LCD_pszRomStr+1
    call    LCD_Putrs
;
; This is the main application loop.
;
; What this does:
;
;   Read PORTA and display the low 4-bits as
;   a hexidecimal digit on the LCD module in
;   line two, position 0 and on a common
;   cathode 7-segment LED displsy connected to
;   all 8 bits of PORTB.
;
    banksel ValueRead
    movlw   0x10;
    movwf   ValueRead
ApplicationLoop:
    banksel PORTA
    movf    PORTA,W
    andlw   0x0F
    xorwf   ValueRead,W
    skpnz
    goto    ApplicationLoop
    xorwf   ValueRead,F
    movlw   LINE_TWO
    call    LCD_SetPosition
    movf    ValueRead,W
    call    LCD_PutHexNibble
    movf    ValueRead,W
    call    Lookup7segHEX
    movwf   PORTB
    banksel TRISB
    clrf    TRISB
    pagesel ApplicationLoop
    goto    ApplicationLoop
;
;**********************************************************************
; Function: Lookup7segHEX
; Description:
;
Lookup7segHEX:
    andlw   0x0F
    movwf   PCLATH
    xorlw   HIGH(Table7segHEX)
    xorwf   PCLATH,F
    xorwf   PCLATH,W
    addlw   LOW(Table7segHEX)
    skpnc
    incf    PCLATH,F
    movwf   PCL

Table7segHEX:
;           abcdefg         _   
    DT    B'11111100'   ;  | | 
                        ;  |_| 
                        ;       
    DT    B'01100000'   ;    | 
                        ;    | 
                        ;   _   
    DT    B'11011010'   ;   _| 
                        ;  |_   
                        ;   _   
    DT    B'11110010'   ;   _| 
                        ;   _| 
                        ;       
    DT    B'01100110'   ;  |_| 
                        ;    | 
                        ;   _   
    DT    B'10110110'   ;  |_   
                        ;   _| 
                        ;   _   
    DT    B'10111110'   ;  |_   
                        ;  |_| 
                        ;   _   
    DT    B'11100000'   ;    | 
                        ;    | 
                        ;   _   
    DT    B'11111110'   ;  |_| 
                        ;  |_| 
                        ;   _   
    DT    B'11100110'   ;  |_| 
                        ;    | 
                        ;   _   
    DT    B'11101110'   ;  |_| 
                        ;  | | 
                        ;       
    DT    B'00111110'   ;  |_   
                        ;  |_| 
                        ;   _   
    DT    B'10011100'   ;  |   
                        ;  |_   
                        ;       
    DT    B'01111010'   ;   _| 
                        ;  |_| 
                        ;   _   
    DT    B'10011110'   ;  |_   
                        ;  |_   
                        ;   _   
    DT    B'10001110'   ;  |_   
                        ;  |   
;
; Text messages
;
LCD_message1:
    dt  "HEX digit - v1.0",0

    end
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top