martes, 17 de septiembre de 2013

Actividades de programación

En este caso, el siguiente código generado en lenguaje Assembler, permite crear un juego de luces al colocar un 1 en la entrada.

;------------------------------------------------------------------------------------
    List        p=PIC16F84A
    INCLUDE        <p16F84A.inc>
    __CONFIG     _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_ON
    ERRORLEVEL    -302
;------------------------------------------------------------------------------------
    ORG            0x000
    goto        Main
    ORG            0x004
      retfie
#DEFINE         bank1    bsf STATUS, RP0
#DEFINE         bank0    bcf STATUS, RP0
;------------------------------------------------------------------------------------
Main
        bank1
        clrf        TRISB
        movlw        0x1F
        movwf        TRISA
        bank0
        clrf        PORTB
;------------------------------------------------------------------------------------
Loop
        btfsc        PORTA, 0
        goto         Loop
        movlw        0xA0
        xorwf        PORTB, F
        movlw        0x05
        xorwf        PORTB, F
        goto        Loop
;-------------------------------------------------------------------------------------
  END   
 
  En esta actividad, podemos colocar un mensaje "Hola Mundo"  en un LCD de 16x2, en movimiento:

;--------------------------------------------------------------------------------------      
 ; ZONA DE DATOS

    LIST        P=16F84A
    INCLUDE     
    __CONFIG    _CP_OFF &  _WDT_OFF & _PWRTE_ON & _XT_OSC
    ERRORLEVEL      -302

    CBLOCK  0x0C
    ENDC

; ZONA DE CÓDIGOS

    ORG    0
Inicio
    call    LCD_Inicializa          
Principal
    movlw    Mensaje0              
    call    LCD_MensajeMovimiento
    goto    Principal              

; Mensajes ----------------------------------------------------------------------------

Mensajes
    addwf    PCL,F
Mensaje0                          
    DT                          
    DT HOLA MUNDO 
    DT                 , 0x0      
;---------------------------------------------------------------------------------------


   END
;---------------------------------------------------------------------------------------

 Estas son las librerías que se utilizaron (LCD y Retardos).



;-----------------------LIBRERÍAS--------------------------------------------------


    CBLOCK
    LCD_Dato
    LCD_GuardaDato
    LCD_GuardaTRISB
    LCD_Auxiliar1
    LCD_Auxiliar2
    ENDC

LCD_CaracteresPorLinea    EQU    .16          

#DEFINE  LCD_PinRS    PORTA,0
#DEFINE  LCD_PinRW    PORTA,1
#DEFINE  LCD_PinEnable    PORTA,2
#DEFINE  LCD_BusDatos    PORTB

; Subrutina LCD_Inicializa ------------------------------------------------------------

LCD_Inicializa
    bsf        STATUS,RP0              
    bcf        LCD_PinRS             
    bcf        LCD_PinEnable
    bcf        LCD_PinRW
    bcf        STATUS,RP0
    bcf        LCD_PinRW              
                                  
    bcf        LCD_PinEnable          
    bcf     LCD_PinRS              
    call    Retardo_20ms
    movlw    b'00110000' 
    call    LCD_EscribeLCD          
    call    Retardo_5ms 
    movlw    b'00110000' 
    call    LCD_EscribeLCD
    call    Retardo_200micros
    movlw    b'00110000' 
    call    LCD_EscribeLCD
    call    Retardo_20micros      
    movlw    b'00100000'              
    call    LCD_EscribeLCD
    call    Retardo_20micros      
 


    call    LCD_2Lineas4Bits5x7
    call    LCD_Borra            
    call    LCD_CursorOFF     
    call    LCD_CursorIncr         
    return

; Subrutina LCD_EscribeLCD -----------------------------------------------------------


LCD_EscribeLCD
    andlw    b'11110000'              
    movwf    LCD_Dato              
    movf    LCD_BusDatos,W          
    andlw    b'00001111'              
    iorwf    LCD_Dato,F             
                                 
    bsf        STATUS,RP0              
    movf    TRISB,W                  
    movlw    b'00001111'              
    andwf    PORTB,F                  
    bcf        STATUS,RP0             
;
    movf    LCD_Dato,W              
    movwf    LCD_BusDatos        
    bsf        LCD_PinEnable       
    bcf        LCD_PinEnable        
    bsf        STATUS,RP0            
    movf    LCD_GuardaTRISB,W     
    movwf    TRISB
    bcf        STATUS,RP0              
    return

; Subrutinas variadas para el control del módulo LCD -----------------------------------------

LCD_CursorIncr                      
    movlw    b'00000110'
    goto    LCD_EnviaComando

LCD_CursorOFF                     
    movlw    b'00001100'
    goto    LCD_EnviaComando

LCD_Borra                          
    movlw    b'00000001'              
    goto    LCD_EnviaComando
LCD_2Lineas4Bits5x7                  
    movlw    b'00101000'              
;    goto    LCD_EnviaComando      

; Subrutinas LCD_EnviaComando y LCD_Caracter ------------------------------------

LCD_EnviaComando
    bcf        LCD_PinRS             
    goto    LCD_Envia

LCD_Caracter
    bsf        LCD_PinRS              
    call    LCD_CodigoCGROM          

LCD_Envia
    movwf    LCD_GuardaDato          
    call    LCD_EscribeLCD          
    swapf    LCD_GuardaDato,W      
                                  
    call    LCD_EscribeLCD          
    btfss    LCD_PinRS              
    call    Retardo_2ms              
    call    Retardo_50micros
    return 

; Subrutina LCD_CodigoCGROM -----------------------------------------------------------


LCD_CodigoCGROM
    movwf    LCD_Dato              
LCD_EnheMinuscula                  
    sublw    'ñ'                   
    btfss    STATUS,Z
    goto    LCD_EnheMayuscula      
    movlw    b'11101110'             
    movwf    LCD_Dato
    goto    LCD_FinCGROM
LCD_EnheMayuscula
    movf    LCD_Dato,W              
    sublw    'Ñ'                  
    btfss    STATUS,Z
    goto    LCD_Grado             
    movlw    b'11101110'            
    movwf    LCD_Dato              
    goto    LCD_FinCGROM 
LCD_Grado
    movf    LCD_Dato,W             
    sublw    'º'                   
    btfss    STATUS,
    goto    LCD_FinCGROM         
    movlw    b'11011111'              
    movwf    LCD_Dato
LCD_FinCGROM
    movf    LCD_Dato,W              
    return





; Librería LCD_MENS.INC


    CBLOCK
    LCD_ApuntaCaracter              
                                  
                                 
    LCD_ValorCaracter             
    ENDC                         


; Subrutina LCD_MensajeMovimiento -----------------------------------------------------

    CBLOCK 
    LCD_CursorPosicion              
    ENDC                          

LCD_MensajeMovimiento
    movwf    LCD_ApuntaCaracter     
    movlw    Mensajes              
    subwf    LCD_ApuntaCaracter,F 
    decf    LCD_ApuntaCaracter,F  
LCD_PrimeraPosicion
    clrf    LCD_CursorPosicion   
    call    LCD_Borra             
LCD_VisualizaCaracter             
    movlw    LCD_CaracteresPorLinea 
    subwf    LCD_CursorPosicion,W
    btfss    STATUS,Z
    goto    LCD_NoEsFinalLinea
LCD_EsFinalLinea
    call    Retardo_200ms        
    call    Retardo_200ms
    movlw    LCD_CaracteresPorLinea-1
    subwf    LCD_ApuntaCaracter,F  
    goto    LCD_PrimeraPosicion       
LCD_NoEsFinalLinea                  
    movf    LCD_ApuntaCaracter,W
    call    Mensajes              
    movwf    LCD_ValorCaracter     
    movf    LCD_ValorCaracter,F      
    btfsc    STATUS,Z              
    goto    LCD_FinMovimiento      
LCD_NoUltimoCaracter2
    call    LCD_Caracter          
    incf    LCD_CursorPosicion,F  
                                  
    incf    LCD_ApuntaCaracter,F  
    goto    LCD_VisualizaCaracter  
LCD_FinMovimiento                  
    return                          
 

    CBLOCK
    R_ContA                      
    R_ContB
    R_ContC
    ENDC

; RETARDOS de 20 hasta 500 microsegundos ------------------------------------------------

Retardo_200micros              
    nop                          
    movlw    d'64'              
    goto    RetardoMicros      

Retardo_50micros              
    nop                          
    movlw    d'14'              
    goto    RetardoMicros      

Retardo_20micros              
    movlw    d'5'             

RetardoMicros
    movwf    R_ContA              
Rmicros_Bucle
    decfsz    R_ContA,F          
    goto    Rmicros_Bucle      
    return                      

; RETARDOS de 1 ms hasta 200 ms. --------------------------------------------------------
;
Retardo_200ms                 
    movlw    d'200'              
    goto    Retardos_ms          

Retardo_20ms                  
    movlw    d'20'              
    goto    Retardos_ms          

Retardo_5ms                      
    movlw    d'5'              
    goto    Retardos_ms          

Retardo_2ms                      
    movlw    d'2'              
    goto    Retardos_ms          

Retardos_ms
    movwf    R_ContB              
R1ms_BucleExterno
    movlw    d'249'              
    movwf    R_ContA             
R1ms_BucleInterno
    nop                          
    decfsz    R_ContA,F          
    goto    R1ms_BucleInterno  
    decfsz    R_ContB,F          
    goto    R1ms_BucleExterno   
    return                      


;---------------------------------------------------------------------------------

END         

 ;---------------------------------------------------------------------------------

En esta actividad, hicimos un temporizador de tiempo:


;--------------------------------------------------------

    List p=PIC16F84A
    Include <p16f84a.inc>
__Config    _XT_OSC    & _WDT_OFF & _PWRTE_ON & _CP_OFF       
    Errorlevel -302

;--------------------------------------------------------
#DEFINE bank0  bcf STATUS, RP0
#DEFINE bank1  bsf STATUS, RP0
    CBLOCK 0x0C
    contador
    ENDC

;--------------------------------------------------------

ORG     0x000
goto     Main
ORG      0x004
Retfie

;---------------------------------------------------------
Main
    bank1
    bsf    TRISA, RA4
    clrf    TRISB    ;TRISB---> Salida
    movlw    0x28    ;Dividiendo por 256 
    movwf    OPTION_REG    ;por RA4
    bcf    INTCON, T0IF
    bank0
    clrf    TMR0

;---------------------------------------------------------

Loop
    btfss    INTCON, T0IF
    goto    Loop

    bcf        INTCON, T0IF
    incf    contador, F
    movf     contador, F
    movwf    PORTB
    goto    Loop

;----------------------------------------------------------

END

;----------------------------------------------------------

En esta actividad, hicimos un timer:

;-----------------------------------------------------------------
;TIMER0_INT
;Mediante interrupciones contar cantidad de overfload del TMR0
;Mostrar esa cuenta en PORTB
;TMR0 como temporizador sin prescaler (frec_interna= fext = 4)
;==> fext / (4.64)
;-----------------------------------------------------------------

    List p=PIC16F84A
    Include <p16f84a.inc>
    __Config    _XT_OSC    & _WDT_OFF & _PWRTE_ON & _CP_OFF       
    Errorlevel -302

;-----------------------------------------------------------------
#DEFINE bank0  bcf STATUS, RP0
#DEFINE bank1  bsf STATUS, RP0
    CBLOCK 0x0C
    contador
    d1
    d2
    ENDC

;-----------------------------------------------------------------

    ORG     0x000
    goto     Main

    ORG      0x004
    bcf        INTCON,T0IF
    incf    contador,F
    movf    contador,W
    movwf    PORTB
    retfie

;-----------------------------------------------------------------
Main
    bank1
    clrf    TRISB    ;TRISB---> Salida
    movlw    b'00001000'   
    movwf    OPTION_REG
    movlw    0xA0
    movwf        INTCON
    bank0
    clrf    TMR0
    clrf    PORTA
    call    delay_200ms

;-----------------------------------------------------------------

Loop
    incf    PORTA,F
    movf    PORTA,W
    sublw    .10
    btfsc    STATUS,Z
    call    delay_200ms   
    goto    Loop


;-----------------------------------------------------------------

delay_200ms
    decfsz    d1,F
    goto    S-1
    decfsz    d2,F
    goto    S-3
    return
;-----------------------------------------------------------------

    END

;-----------------------------------------------------------------

No hay comentarios.:

Publicar un comentario