Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

scrivere su LCD

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] scrivere su LCD

Messaggioda Foto Utentestepsan78 » 8 gen 2009, 17:39

Ho un display LCD 16x2 e un PIC 16F684.
il seguente programma che mi permette di scrivere caratteri alfanumerici su LCD, ma in realtà mi piacerebbe scrivere sul display il valore numerico di W (per esempio ciò che leggo da un'ingresso analogico). Come devo modificare il mio programma?
Nel seguente esempio visualizzo sul display la scritta "hello world" ma io vorrei scrivere il contenuto numerico del W register..

Grazie


    PROCESSOR 16F684
    RADIX DEC
    INCLUDE "P16F684.INC"

    ;Suppress MPASM warning message 302:
    ;"Register in operand not in bank 0. Ensure that bank bits are correct"

    __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF)
    ;LCD Control lines

    LCD_RS equ 0 ;Register Select
    LCD_E equ 1 ;Enable

    ;LCD data line bus

    LCD_DB4 equ 2 ;LCD data line DB4
    LCD_DB5 equ 3 ;LCD data line DB5
    LCD_DB6 equ 4 ;LCD data line DB6
    LCD_DB7 equ 5 ;LCD data line DB7

    ORG 020H

    tmpLcdRegister res 2
    msDelayCounter res 2

    ;Reset Vector

    ORG 00H
    Start
    ; bsf STATUS,RP0 ;Swap to register bank 1
    ;
    ; movlw 00011111B ;Set PORTA lines
    ; movwf TRISA
    ; movlw 00000000B ;Set PORTC lines
    ; movwf TRISC

    ; Codice preso dal DATASHEET
    ; Disattivazione del comparatore
    BCF STATUS,RP0 ;Bank 0
    CLRF PORTA ;Init PORTA
    MOVLW 07h ;Set RA<2:0> to
    MOVWF CMCON0 ;digital I/O
    BSF STATUS,RP0 ;Bank 1
    CLRF ANSEL ;digital I/O
    bsf STATUS,RP0 ;Swap to register bank 1

    movlw 00011111B ;Set PORTA lines
    movwf TRISA

    ; Disattivazione degli ingressi analogici
    BANKSEL PORTC ;
    CLRF PORTC ;Init PORTC
    ;MOVLW 07h ;Set RC<4,1:0> to
    ;MOVWF CMCON0 ;digital I/O
    BANKSEL ANSEL ;
    CLRF ANSEL ;digital I/O
    MOVLW 00h ;IMPOSTAZIONE DI PORTC COME OUTPUT
    MOVWF TRISC ;

    BCF STATUS,RP0 ;Bank 0

    ; bcf PORTC,LCD_DB4 ;Set as output just the LCD's lines
    ; bcf PORTC,LCD_DB5
    ; bcf PORTC,LCD_DB6
    ; bcf PORTC,LCD_DB7
    ; bcf PORTC,LCD_E
    ;bcf PORTC,LCD_RS

    bcf STATUS,RP0 ;Swap to register bank 0

    ;LCD inizialization

    call LcdInit

    ;Locate LCD cursor on row 0, col 0

    movlw 10H
    call LcdLocate

    ;Shows "HELLO WORLD" string on LCD

    movlw 'H'
    call LcdSendData

    movlw 'E'
    call LcdSendData

    movlw 'L'
    call LcdSendData

    movlw 'L'
    call LcdSendData

    movlw 'O'
    call LcdSendData

    movlw ' '
    call LcdSendData

    movlw 'W'
    call LcdSendData

    movlw 'O'
    call LcdSendData

    movlw 'R'
    call LcdSendData

    movlw 'L'
    call LcdSendData

    movlw 'D'
    call LcdSendData

    movlw ' '
    call LcdSendData

    movlw '!'
    call LcdSendData

    foreverLoop
    goto foreverLoop

    ;**********************************************************************
    ; Delay subroutine
    ;
    ; W = Requested delay time in ms (clock = 4MHz)
    ;**********************************************************************

    msDelay
    movwf msDelayCounter+1
    clrf msDelayCounter+0

    ; 1 ms (about) internal loop
    msDelayLoop
    nop
    decfsz msDelayCounter+0,F
    goto msDelayLoop
    nop

    decfsz msDelayCounter+1,F
    goto msDelayLoop

    return

    ;**********************************************************************
    ; Init LCD
    ; This subroutine must be called before each other lcd subroutine
    ;**********************************************************************

    LcdInit
    movlw 30 ;Wait 30 ms
    call msDelay

    ;****************
    ; Reset sequence
    ;****************

    bcf PORTC,LCD_RS ;Set LCD command mode

    ;Send a reset sequence to LCD

    bsf PORTC,LCD_DB4
    bsf PORTC,LCD_DB5
    bcf PORTC,LCD_DB6
    bcf PORTC,LCD_DB7

    bsf PORTC,LCD_E ;Enables LCD
    movlw 5 ;Wait 5 ms
    call msDelay
    bcf PORTC,LCD_E ;Disables LCD
    movlw 1 ;Wait 1ms
    call msDelay

    bsf PORTC,LCD_E ;Enables LCD
    movlw 1 ;Wait 1ms
    call msDelay
    bcf PORTC,LCD_E ;Disables LCD
    movlw 1 ;Wait 1ms
    call msDelay

    bsf PORTC,LCD_E ;Enables E
    movlw 1 ;Wait 1ms
    call msDelay
    bcf PORTC,LCD_E ;Disables E
    movlw 1 ;Wait 1ms
    call msDelay

    bcf PORTC,LCD_DB4
    bsf PORTC,LCD_DB5
    bcf PORTC,LCD_DB6
    bcf PORTC,LCD_DB7

    bsf PORTC,LCD_E ;Enables LCD
    movlw 1 ;Wait 1ms
    call msDelay
    bcf PORTC,LCD_E ;Disabled LCD
    movlw 1 ;Wait 1ms
    call msDelay

    ;Set 4 bit data bus length

    movlw 28H;
    call LcdSendCommand

    ;Entry mode set, increment, no shift

    movlw 06H;
    call LcdSendCommand

    ;Display ON, Curson ON, Blink OFF

    movlw 0EH
    call LcdSendCommand

    ;Clear display

    call LcdClear

    return


    ;**********************************************************************
    ; Clear LCD
    ;**********************************************************************

    LcdClear
    ;Clear display

    movlw 01H
    call LcdSendCommand

    movlw 2 ;Wait 2 ms
    call msDelay

    ;DD RAM address set 1st digit

    movlw 80H;
    call LcdSendCommand

    return

    ;**********************************************************************
    ; Locate cursor on LCD
    ; W = D7-D4 row, D3-D0 col
    ;**********************************************************************

    LcdLocate
    movwf tmpLcdRegister+0

    movlw 80H
    movwf tmpLcdRegister+1

    movf tmpLcdRegister+0,W
    andlw 0FH
    iorwf tmpLcdRegister+1,F

    btfsc tmpLcdRegister+0,4
    bsf tmpLcdRegister+1,6

    movf tmpLcdRegister+1,W
    call LcdSendCommand

    return


    ;**********************************************************************
    ; Send a data to LCD
    ;**********************************************************************

    LcdSendData

    bsf PORTC,LCD_RS
    call LcdSendByte
    return

    ;**********************************************************************
    ; Send a command to LCD
    ;**********************************************************************

    LcdSendCommand
    bcf PORTC,LCD_RS
    call LcdSendByte
    return

    ;**********************************************************************
    ; Send a byte to LCD by 4 bit data bus
    ;**********************************************************************

    LcdSendByte
    ;Save value to send

    movwf tmpLcdRegister

    ;Send highter four bits

    bcf PORTC,LCD_DB4
    bcf PORTC,LCD_DB5
    bcf PORTC,LCD_DB6
    bcf PORTC,LCD_DB7

    btfsc tmpLcdRegister,4
    bsf PORTC,LCD_DB4
    btfsc tmpLcdRegister,5
    bsf PORTC,LCD_DB5
    btfsc tmpLcdRegister,6
    bsf PORTC,LCD_DB6
    btfsc tmpLcdRegister,7
    bsf PORTC,LCD_DB7

    bsf PORTC,LCD_E ;Enables LCD
    movlw 1 ;Wait 1ms
    call msDelay
    bcf PORTC,LCD_E ;Disabled LCD
    movlw 1 ;Wait 1ms
    call msDelay

    ;Send lower four bits

    bcf PORTC,LCD_DB4
    bcf PORTC,LCD_DB5
    bcf PORTC,LCD_DB6
    bcf PORTC,LCD_DB7

    btfsc tmpLcdRegister,0
    bsf PORTC,LCD_DB4
    btfsc tmpLcdRegister,1
    bsf PORTC,LCD_DB5
    btfsc tmpLcdRegister,2
    bsf PORTC,LCD_DB6
    btfsc tmpLcdRegister,3
    bsf PORTC,LCD_DB7

    bsf PORTC,LCD_E ;Enables LCD
    movlw 1 ;Wait 1ms
    call msDelay
    bcf PORTC,LCD_E ;Disabled LCD
    movlw 1 ;Wait 1ms
    call msDelay

    return

    END
Avatar utente
Foto Utentestepsan78
0 3
 
Messaggi: 37
Iscritto il: 23 dic 2007, 13:05

0
voti

[2] Re: scrivere su LCD

Messaggioda Foto UtenteGrinn » 3 set 2009, 12:51

Ciao, MOVF PORTA,W (dipende dalla porta che vuoi leggere A,B, ecc..) cancella tutta la parte movlw 'h', ecc..

quindi sarebbe così:

clrw
movf porta,w
call LcdSendData

;Shows "HELLO WORLD" string on LCD

>>INSERISCI QUI

CANCELLA SOTTO

movlw 'H'
call LcdSendData

movlw 'E'
call LcdSendData

movlw 'L'
call LcdSendData

movlw 'L'
call LcdSendData

movlw 'O'
call LcdSendData

movlw ' '
call LcdSendData

movlw 'W'
call LcdSendData

movlw 'O'
call LcdSendData

movlw 'R'
call LcdSendData

movlw 'L'
call LcdSendData

movlw 'D'
call LcdSendData

movlw ' '
call LcdSendData

movlw '!'
call LcdSendData
Avatar utente
Foto UtenteGrinn
0 2
 
Messaggi: 27
Iscritto il: 5 dic 2007, 21:11

0
voti

[3] Re: scrivere su LCD

Messaggioda Foto UtenteDonJ » 3 set 2009, 14:11

beh la visualizzazione del contenuto di W non è una cosa così tragica.

se lo vuoi fare in binario
controlli ogni bit di W partendo da quello più significativo
se è zero mando all'LCD il carattere "0"
se è uno mando "1"

se lo vuoi fare in decimale è un po' più complicato perché devi fare la conversione da binario.
pensandoci bene il problema non è affatto semplice.. bisognerebbe trasformare il binario in decimale e poi il valore numerico in stringa, per mandare al display LCD ogni singolo carattere.
non so, dovrei ragionarci parecchio e non è detto che trovi una soluzione.. non puoi fare in altro modo?
"Computers, operating systems, networks are a hot mess. They're barely manageable, even if you know a decent amount about what you're doing. Nine out of ten software engineers agree: it's a miracle anything works at all."
@fasterthanlime
Avatar utente
Foto UtenteDonJ
4.611 6 10 13
Master EY
Master EY
 
Messaggi: 2559
Iscritto il: 19 lug 2009, 22:13
Località: Croccamauria


Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 2 ospiti