Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;-------------------------------------------------------------------------------
- ;
- ; Archivo: main.s
- ; Fecha de creación/modificación: 22 Febrero 2021
- ; Author original: Jose Morales
- ; Modificado por: Kurt Kellner
- ; Dispositivo: PIC16F887
- ; Descripción: Ejemplo de multiplexado, macros y el simulador de MPLAB X
- ; Hardware: (Explicar qué está conectado en dónde)
- ;
- ;-------------------------------------------------------------------------------
- ;-------------------------------------------------------------------------------
- ; Librerías incluidas
- ;-------------------------------------------------------------------------------
- PROCESSOR 16F887
- #include <xc.inc>
- ;-------------------------------------------------------------------------------
- ; Palabras de configuración
- ;-------------------------------------------------------------------------------
- ; CONFIG1
- CONFIG FOSC = XT ; Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
- CONFIG WDTE = OFF ; Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
- CONFIG PWRTE = OFF ; Power-up Timer Enable bit (PWRT disabled)
- CONFIG MCLRE = OFF ; RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
- CONFIG CP = OFF ; Code Protection bit (Program memory code protection is disabled)
- CONFIG CPD = OFF ; Data Code Protection bit (Data memory code protection is disabled)
- CONFIG BOREN = OFF ; Brown Out Reset Selection bits (BOR disabled)
- CONFIG IESO = OFF ; Internal External Switchover bit (Internal/External Switchover mode is disabled)
- CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
- CONFIG LVP = OFF ; Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
- ; CONFIG2
- CONFIG BOR4V = BOR40V ; Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
- CONFIG WRT = OFF ; Flash Program Memory Self Write Enable bits (Write protection off)
- ;-------------------------------------------------------------------------------
- ; Macros
- ;-------------------------------------------------------------------------------
- movlf macro arg1, arg2
- movlw arg1
- movwf arg2
- endm
- restartTMR0 macro
- movlf 61, TMR0
- bcf T0IF
- endm
- ;pinMode(pin, INPUT/OUTPUT);
- pinMode macro argPin, argPort, argIO
- banksel TRISA
- IF argIO
- bsf argPort, argPin
- ELSE
- bcf argPort, argPin
- ENDIF
- ENDM
- down MACRO count
- LOCAL more
- more: decfsz count
- goto more
- ENDM
- ;digitalWrite(pin, HIGH/LOW);
- ; 2 + 1 + 2 + 1 = 6 líneas de código
- digitalWrite macro argPin, argPort, argHL
- banksel TRISA
- bsf argPort, argPin
- banksel PORTA
- IF argIO
- bsf argPort, argPin
- ELSE
- bcf argPort, argPin
- ENDIF
- ENDM
- ;-------------------------------------------------------------------------------
- ; Variables
- ;-------------------------------------------------------------------------------
- ; Para hacer que todas las variables sean globales (se puedan usar en más
- ; módulos)
- GLOBAL W_TEMP, STATUS_TEMP, banderas, display, nibble, count, test
- PSECT vars, space=1, class=udata
- count: DS 1
- test: DS 1
- PSECT udata_shr ;common memory, 16 espacios
- W_TEMP: DS 1
- STATUS_TEMP: DS 1
- banderas: DS 1
- ; Los bits que representan nuestra variable "banderas"
- displayValue EQU 0
- DP0 EQU 1
- display: DS 2
- nibble: DS 2
- ;-------------------------------------------------------------------------------
- ; Vector de Reset
- ;-------------------------------------------------------------------------------
- PSECT resetVector, delta=2
- ;linker: -PresetVector=0x0000
- goto main
- ;-------------------------------------------------------------------------------
- ; Vector de Interrupción
- ;-------------------------------------------------------------------------------
- PSECT interruptVector, delta=2
- ;linker: -PinterruptVector=0x0004
- push:
- movwf W_TEMP
- swapf STATUS, W
- movwf STATUS_TEMP
- isr:
- btfss TMR0IF
- goto pop
- restartTMR0
- bsf banderas, displayValue
- ;call display_var
- pop:
- swapf STATUS_TEMP, W
- movwf STATUS
- swapf W_TEMP, F
- swapf W_TEMP, W
- retfie
- ;-------------------------------------------------------------------------------
- ; Loop principal
- ;-------------------------------------------------------------------------------
- PSECT code, delta=2
- ;linker: -PloopPrincipal=0x0100
- ;PSECT loopPrincipal, class=code, delta=2, abs
- ;ORG 0x0100
- tabla_7seg:
- andlw 00001111B ; no saltar más del tamaño de la tabla
- addwf PCL
- retlw 00111111B ;0
- retlw 00000110B ;1
- retlw 01011011B ;2
- retlw 01001111B ;3
- retlw 01100110B ;4
- retlw 01101101B ;5
- retlw 01111101B ;6
- retlw 00000111B ;7
- retlw 01111111B ;8
- retlw 01101111B ;9
- retlw 01110111B ;A
- retlw 01111100B ;b
- retlw 00111001B ;C
- retlw 01011110B ;d
- retlw 01111001B ;E
- retlw 01110001B ;F
- retlw 0
- main:
- ; movlw 0xAA
- ; movwf test
- ; bsf flags, displayValue
- ; bsf flags, flag2
- ; bsf flags, flag4
- ; bsf flags2, flag20
- ; bsf flags2, flag27
- ; bsf flags2, flag28
- call config_io
- call config_tmr0
- call config_interrupt
- call config_reloj
- banksel PORTA
- loop:
- btfsc PORTB, 0
- call inc_count
- btfsc PORTB, 1
- call dec_count
- call separar_nibbles
- call preparar_displays
- btfsc banderas, displayValue
- call display_var
- goto loop
- ;-------------------------------------------------------------------------------
- ; Subrutinas de configuración
- ;-------------------------------------------------------------------------------
- config_io:
- banksel TRISC
- clrf TRISA
- bsf TRISB, 0
- bsf TRISB, 1
- clrf TRISC
- bcf TRISD, 0
- bcf TRISD, 1
- banksel ANSEL
- clrf ANSEL
- clrf ANSELH
- banksel PORTC
- clrf PORTC
- clrf PORTD
- return
- config_reloj:
- banksel OSCCON
- bsf IRCF2
- bsf IRCF1
- bcf IRCF0 ; IRCF = 110 = 4MHz
- bsf SCS ; Internal oscilator for system clock
- return
- config_tmr0:
- banksel OPTION_REG
- bcf T0CS
- bcf PSA
- bsf PS2
- bsf PS1
- bsf PS0
- banksel PORTA
- restartTMR0
- return
- config_interrupt:
- bsf T0IE ;INTCON, 101B
- bcf TMR0IF
- bsf GIE
- return
- /*
- restart_tmr0:
- movlw 61
- movwf TMR0
- bcf T0IF
- return
- */
- ;-------------------------------------------------------------------------------
- ; Subrutinas contador
- ;-------------------------------------------------------------------------------
- inc_count:
- btfsc PORTB, 0
- goto $-1
- incf count, F
- return
- dec_count:
- btfsc PORTB, 1
- goto $-1
- decf count, F
- return
- ;-------------------------------------------------------------------------------
- ; Subrutinas display
- ;-------------------------------------------------------------------------------
- separar_nibbles:
- movf count, W
- andlw 00001111B
- movwf nibble
- swapf count, W
- andlw 00001111B
- movwf nibble+1
- return
- preparar_displays:
- movf nibble, W
- call tabla_7seg
- movwf display
- movf nibble+1, W
- call tabla_7seg
- movwf display+1
- return
- display_var:
- clrf PORTD
- btfsc banderas, DP0
- goto display_1
- goto display_0
- display_0:
- movf display, W
- movwf PORTC
- bsf PORTD, 0
- goto toggle_display
- display_1:
- movf display+1, W
- movwf PORTC
- bsf PORTD, 1
- goto toggle_display
- toggle_display:
- movlw 0x02
- xorwf banderas, f
- bcf banderas, displayValue
- return
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement