problema LCD4 bits

hola, tengo un problema, he echo un proyecto con el pic 16f84a, y un lcd.

con el LCD controlo el estado de un rele y una salida controlada por PWM.

el problema es que si dejo el programa tal i como lo tengo y funciona, no tengo salidas disponibles para el rele y PWM.

entonces he optado por dos salidas,
1. poner el LCD en modo 4bits, es mas logico porque asi ja tengo las rutinas para cualquier otro proyecto.
2. usar un 16f628a, el problema que no me va si cargo el programa. pero bueno

e optado por la primera i despues ya me apañare para hacero funcionar en un 16f628a.

a continuacion os adjunto el programa en basic con la configuracion del lcd en 8bits, que es la que funciona.

i despues la del lcd en modo 4bits que es la que no me funciona, porque me salen simbolos raros, he estado repasandolo pero no doy para mas nose que reconxolis hago mal.

si podeis hacerle un ojeada me hareis un favor muchas gracias a todos.

Código:
#include <basic\system.bas>
#pragma DATA 0x2007, _XT_OSC & _WDT_OFF   
'#pragma DATA _CONFIG, _INTOSC_OSC_NOCLKOUT & _PWRTE_ON & _WDT_OFF & _CP_OFF & _LVP_OFF & _MCLRE_ON

#pragma OPTIMIZE "a"

Extern Sub lcd()

dim dato as byte
dim cont as byte
dim cadena as string(38)
dim j as byte
dim cadena2 as string(15)
dim puls1 as bit
dim puls2 as byte
dim est as byte
dim est2 as byte
dim est3 as byte
dim est4 as byte
dim savep2 as byte
dim savep3 as byte




Sub main()

        'cmcon = 7
        
        
        trisa =0x18 'configuro porta 3 y 4 como entrada
        trisb =0x00
        
            call delay_s (1)
    
            call inicialitzaLCD ()
        
            call control(0x38)
            call control(0x80)
            
            cadena = "EME sound 40w   Sound Amplifier 1.0" '34
                
            for j=0 to 34 step 1    
                
                dato = cadena(j)
                
                call datoLCD (dato)
                
            next j
            
            puls1=0
            puls2=0

            
        call delay_ms (250)
        
            for j=0 to 21 step 1    'desplaçament a l'esquerra    
                
                call control(0x18)
                
                call delay_ms (255)
                
            next j
'...........................................................
            call control (0x01)    'BORRAR DISPLAY    
'...........................................................        
    ini:
if puls1=1 then    
    puls2=savep2
        call primera ()
    savep2=puls2        
else
                    'segunda linea display
    puls2=savep3                
        call segunda ()    
    savep3=puls2

end if                    
    goto ini
'            call control (0x01)    'BORRAR DISPLAY    
end sub    
            
sub primera()

        call control(0x38 )            'posiciono texto al principio
        call control(0x80 )            'posiciono texto en la primera linea
        
        call compro(puls1, puls2)    'compruebo si he pulsado

        '...........................................................    

if puls1=0 then        
        Select Case est4
            Case 0 
                cadena="AMPLIFICADOR OFF"

            Case 1
                cadena="AMPLIFICADOR  ON"

        End Select
        
else
        est=puls2&1
                
        Select Case est
            Case 0 
                cadena="AMPLIFICADOR<OFF"
                est4=0

            Case 1
                cadena="AMPLIFICADOR< ON"
                est4=1
        End Select
end if    
        '...........................................................
        'en la posicion 92 es donde puedo situar el cursor
        
            '...........................................................
            for j=0 to 15 step 1    
                
                dato = cadena(j)
                
                call datoLCD (dato)
                
            next j

end sub

sub segunda()

        call control(0x38 )            'posiciono texto al principio
        call control(0xC0 )            'posiciono texto en la segunda linea
        
        call compro(puls1,puls2)    'compruebo si he pulsado
            
        '...........................................................
        
if puls1=1 then
        Select Case est3
            Case 0 
                cadena2="VENTILACION  OFF"
    
            Case 1
                cadena2="VENTILACION  MIN"

            Case 2
                cadena2="VENTILACION  MED"

            Case 3
                cadena2="VENTILACION  MAX"
        End Select
else
        est2=puls2&3
        Select Case est2
            Case 0 
                cadena2="VENTILACION <OFF"
                est3=0
            Case 1
                cadena2="VENTILACION <MIN"
                est3=1
            Case 2
                cadena2="VENTILACION <MED"
                est3=2
            Case 3
                cadena2="VENTILACION <MAX"
                est3=3
        End Select
end if
        '...........................................................
        'paso los valores al LCD
        for j=0 to 15 step 1    
                
            dato = cadena2(j)
                
            call datoLCD (dato)
                
        next j
        '...........................................................
        


end sub

'--------------------------------------------------------------
'subrutina de comprobacion  por si se a pulsado el algun pulsador
sub compro( byref puls1 as byte, byref puls2 as byte)

        
if porta.4=0 then
    call delay_us(100)            'delay antirrebote
    if porta.4=0 then
            puls1=puls1+1
            call delay_us(100)    'delay antirrebote
    end if
end if
'----------------------
'espero que acabe de pulsar el boton
espera2:
if porta.4=0 then
goto espera2
end if
'--------------------------------------------------------------
if porta.3=0 then
    call delay_us(100)        'delay antirrebote
    if porta.3=0 then
        puls2=puls2+1
        call delay_us(100)    'delay antirrebote
    end if
end if
'---------------------
'espero que acabe de pulsar el boton
espera:
if porta.3=0 then
goto espera
end if

end sub

'--------------------------------------------------------------
'SUBRUTINA LCD INIALIZA

sub inicialitzaLCD ()
    dim cont as byte
    cont = 0x38            'MODO TRANSFERENCIA 
                        'DISPLAY 5X7, DISPLAY CON 2 LINEAS
                        'DISPLAY A 8 BITS
    call    control (cont)    
    cont = 0x06            'MODO FUNCIONAMIENTO 
                        'INC CONT DIRECCIONES, DISPLAY QUIETO
    call    control (cont)    
    cont = 0x01            'BORRAR DISPLAY
    call    control (cont)
    cont = 0x0c            'ON/OFF 
                        'DISPLAY ON, CURSOR ON, NO PARPADEO CURSOR
    call    control (cont)            
end sub

'--------------------------------------------------------------
'--------------------------------------------------------------
'SUBRUTINA CONTROL

sub control (cont as byte)
    portb = cont                
    porta.0 = 0                'RS
    porta.1 = 0                'R/W
    porta.2 = 0    
    porta.2 = 1                'impuls
    porta.2 = 0
    call delay_100us(17)    'ret 1.64mS        
end sub

'---------------------------------------------------------------
'---------------------------------------------------------------
'SUBRUTINA    LEER DATO
sub datoLCD (dato as byte)
    portb = dato    
    porta.0 = 1                'RS
    porta.1 = 0                'R/W
    porta.2 = 0    
    porta.2 = 1                'impuls
    porta.2 = 0
    call delay_10us(4)        'ret 40uS
end sub
 
Última edición por un moderador:
Código:
LCD 4BITS





dim dato as byte
dim cont as byte
dim cadena as string(38)
dim j as byte
dim cadena2 as string(15)
dim puls1 as bit
dim puls2 as byte
dim est as byte
dim est2 as byte
dim est3 as byte
dim est4 as byte
dim savep2 as byte
dim savep3 as byte




Sub main()

        'cmcon = 7
        
        
        trisa =0x18 'configuro porta 3 y 4 como entrada
        trisb =0x00
    

            call delay_s (1)
    
            call lcd_init()
            'porta.1=1

            call lcd_clear()    'BORRAR DISPLAY

            call lcd_putcmd(0x38)
            call lcd_line1()
            
            cadena = "EME sound 40w   Sound Amplifier 1.0" '34
                
            for j=0 to 34 step 1    
                
                dato = cadena(j)
                
                call lcd_putch(dato)
                
            next j
            
            puls1=0
            puls2=0

            
        call delay_s (1)
        
            for j=0 to 21 step 1    'desplaçament a l'esquerra    
                
                call lcd_putcmd(0x18)
                
                call delay_ms (255)
                
            next j
'...........................................................
            call lcd_clear()    'BORRAR DISPLAY    
'...........................................................        
    ini:
if puls1=1 then    
    puls2=savep2
        call primera ()
    savep2=puls2        
else
                    'segunda linea display
    puls2=savep3                
        call segunda ()    
    savep3=puls2

end if                    
    goto ini
end sub    
            
sub primera()

        call lcd_putcmd(0x38 )            'posiciono texto al principio
        call lcd_line1()            'posiciono texto en la primera linea
        
        call compro(puls1, puls2)    'compruebo si he pulsado

        '...........................................................    

if puls1=0 then        
        Select Case est4
            Case 0 
                cadena="AMPLIFICADOR OFF"

            Case 1
                cadena="AMPLIFICADOR  ON"

        End Select
        
else
        est=puls2&1
                
        Select Case est
            Case 0 
                cadena="AMPLIFICADOR<OFF"
                est4=0

            Case 1
                cadena="AMPLIFICADOR< ON"
                est4=1
        End Select
end if    
        '...........................................................
        'en la posicion 92 es donde puedo situar el cursor
        
            '...........................................................
            for j=0 to 15 step 1    
                
                dato = cadena(j)
                
                call lcd_putch (dato)
                
            next j

end sub

sub segunda()

        call lcd_putcmd(0x38 )            'posiciono texto al principio
        call lcd_line2()            'posiciono texto en la segunda linea
        
        call compro(puls1,puls2)    'compruebo si he pulsado
            
        '...........................................................
        
if puls1=1 then
        Select Case est3
            Case 0 
                cadena2="VENTILACION  OFF"
    
            Case 1
                cadena2="VENTILACION  MIN"

            Case 2
                cadena2="VENTILACION  MED"

            Case 3
                cadena2="VENTILACION  MAX"
        End Select
else
        est2=puls2&3
        Select Case est2
            Case 0 
                cadena2="VENTILACION <OFF"
                est3=0
            Case 1
                cadena2="VENTILACION <MIN"
                est3=1
            Case 2
                cadena2="VENTILACION <MED"
                est3=2
            Case 3
                cadena2="VENTILACION <MAX"
                est3=3
        End Select
end if
        '...........................................................
        'paso los valores al LCD
        for j=0 to 15 step 1    
                
            dato = cadena2(j)
                
            call lcd_putch (dato)
                
        next j
        '...........................................................
        


end sub

'******************************************************************
'subrutina de comprobacion  por si se a pulsado el algun pulsador
sub compro( byref puls1 as byte, byref puls2 as byte)

        
if porta.4=0 then
    call delay_us(100)            'delay antirrebote
    if porta.4=0 then
            puls1=puls1+1
            call delay_us(100)    'delay antirrebote
    end if
end if
'----------------------
'espero que acabe de pulsar el boton
espera2:
if porta.4=0 then
goto espera2
end if
'--------------------------------------------------------------
if porta.3=0 then
    call delay_us(100)        'delay antirrebote
    if porta.3=0 then
        puls2=puls2+1
        call delay_us(100)    'delay antirrebote
    end if
end if
'---------------------
'espero que acabe de pulsar el boton
espera:
if porta.3=0 then
goto espera
end if

end sub

'********************************************************
'*                                                      *
'*      LCD 16x2 LIBRARY FOR USE WITH BOOST BASIC       *
'*                                                      *
'*      WRITTEN BY      :  PUNERJOT SINGH MANGAT        *
'*      COUNTRY         :  INDIA                        *
'*      CITY            :  PATIALA                      *
'*      WEB PAGE        :  [url]www.rackeys.com/punerjot[/url]     *
'*      EMAIL           :  [email]punerjot@rackeys.com[/email]         *
'*      SOURCEBOOST ID  :  c58_4311                     *
'*      RELEASE DATE    :  March 03, 2006               *
'*      REVISED DATE    :  May 24, 2006                 *
'*                                                      *
'********************************************************
'
'
'
' commandas are
' lcd_putch(ASCII value of char to be printed)
' lcd_clear()
' lcd_line1()
' lcd_line2()
' lcd_init()
'
' any 16x2 alphanumeric lcd can be used as far as it uses hd44780 compatible chipset
' 
' Pin connections of pic16f877A to lcd
' PIC PINS              LCD PINS
' --------              --------
'    RB2                   6
'    RB3                   4
'    RB4                   11
'    RB5                   12
'    RB6                   13
'    RB7                   14
'                          1            gnd
'                          2            +5V
'                          3            contrast (typicaly connected to gnd via 2.2 k resistor)
'                          5            +5V
'
'
' RB0 and RB1 of this PORTB are free
'
'


'******************************* delay routines for the LCD **************************
sub wait_us()
DIM wait_count as byte
asm
{
MOVLW   d'45'
MOVWF   _wait_count
LOOP_LCD:
DECFSZ  _wait_count, F
GOTO    LOOP_LCD
return
}
end sub
'******************************* delay routines for the LCD ends **************************




sub lcd_strobe()
porta.2 = 1        'E
porta.2 = 0
end sub



sub lcd_byte(dato as byte)
portb = (portb & 0x0f)
portb = (portb | (dato & 0xf0))
call lcd_strobe()
portb = (portb & 0x0f)
portb = (portb | (dato << 4))
call lcd_strobe()

if porta.0=1 then
    call delay_10us(4)        'ret 40uS
    call wait_us()
else
    call delay_100us(19)    'ret 1.64mS
end if



end sub



'************************ Procedure to output char to lcd *****************************
sub lcd_putch(dato as byte)
porta.0 = 1               'pin 3 of prt b as high RS
call lcd_byte(dato)
end sub



'************************ Procedure to give commands to lcd *****************************
sub lcd_putcmd(c as byte)
porta.0 = 0
call lcd_byte(c)
end sub



'************************ LCD Initializing routines *****************************
sub lcd_init()
trisb = ( 00000000b )       'pin(0) and pin(1) of this PORTB are free so it can be used for RS232
porta.0 = 0
call delay_ms(50)
portb = (portb & 0x0f)
portb = (portb | 0x30)
call lcd_strobe()
call delay_ms(5)
call lcd_strobe()
call wait_us()
call lcd_strobe()
call delay_ms(5)
portb = (portb & 0x0f)
portb = (portb | 0x20)
call lcd_strobe()
call wait_us()
call lcd_putcmd(0x28)
call lcd_putcmd(0x08)
call lcd_putcmd(0x0C)
call lcd_putcmd(0x06)
end sub



'************************ Procedure to clear the lcd *****************************
sub lcd_clear()
call lcd_putcmd(0x01)
call delay_ms(2)
end sub



'******************** Procedure to goto the begning of line1 of the lcd *****************
sub lcd_line1()
call lcd_putcmd(0x80)          'start of first line
end sub



'******************** Procedure to goto the begning of line2 of the lcd *****************
sub lcd_line2()
call lcd_putcmd(0xc0)       'start of second line
end sub
 
Última edición por un moderador:
Por favor encierra tu codigo entre las etiquetas code para que la lectura no se haga tan compleja, y si tu codigo es demasiado grande entonces mejor adjuntalo como un archivo de texto para que las demas personas puedan descargarlo....
 
Atrás
Arriba