Advertisement
kekellner

Blink Without Delay en Assembler

Mar 31st, 2021
2,922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 6.64 KB | None | 0 0
  1. ;-------------------------------------------------------------------------------
  2. ;
  3. ;  Archivo:   main.s
  4. ;  Fecha de creación/modificación: 31 Marzo 2021
  5. ;  Author original: Kurt Kellner
  6. ;  Modificado por:
  7. ;  Dispositivo: PIC16F887
  8. ;  Descripción: Blink Without Delay
  9. ;  Hardware: (Explicar qué está conectado en dónde)
  10. ;
  11. ;-------------------------------------------------------------------------------
  12.  
  13. ;-------------------------------------------------------------------------------
  14. ; Librerías incluidas
  15. ;-------------------------------------------------------------------------------
  16.  
  17. PROCESSOR 16F887
  18. #include <xc.inc>
  19.  
  20. ;-------------------------------------------------------------------------------
  21. ; Palabras de configuración
  22. ;-------------------------------------------------------------------------------
  23.  
  24. ; CONFIG1
  25.   CONFIG  FOSC = INTRC_NOCLKOUT ; Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
  26.   CONFIG  WDTE = OFF            ; Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  27.   CONFIG  PWRTE = OFF           ; Power-up Timer Enable bit (PWRT disabled)
  28.   CONFIG  MCLRE = OFF           ; RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
  29.   CONFIG  CP = OFF              ; Code Protection bit (Program memory code protection is disabled)
  30.   CONFIG  CPD = OFF             ; Data Code Protection bit (Data memory code protection is disabled)
  31.   CONFIG  BOREN = OFF           ; Brown Out Reset Selection bits (BOR disabled)
  32.   CONFIG  IESO = OFF            ; Internal External Switchover bit (Internal/External Switchover mode is disabled)
  33.   CONFIG  FCMEN = OFF           ; Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
  34.   CONFIG  LVP = OFF             ; Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
  35.  
  36. ; CONFIG2
  37.   CONFIG  BOR4V = BOR40V        ; Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  38.   CONFIG  WRT = OFF             ; Flash Program Memory Self Write Enable bits (Write protection off)
  39.  
  40.  
  41. ;-------------------------------------------------------------------------------
  42. ; Macros
  43. ;-------------------------------------------------------------------------------
  44.  
  45. movlf macro arg1, arg2
  46.     movlw   arg1
  47.     movwf   arg2
  48.     endm
  49.  
  50. _movff macro arg1, arg2
  51.     movf    arg1, w
  52.     movwf   arg2
  53.     endm
  54.    
  55. restartTMR0 macro
  56.     movlf   61, TMR0
  57.     bcf    T0IF
  58.     endm
  59.  
  60. restartTMR1 macro
  61.     banksel TMR1H
  62.     movlf   0x0B, TMR1H
  63.     movlf   0xDC, TMR1L
  64.     bcf     TMR1IF
  65.     endm
  66.    
  67. ;-------------------------------------------------------------------------------
  68. ; Variables
  69. ;-------------------------------------------------------------------------------
  70.    
  71. _tmr2Value  EQU 25
  72. _count      EQU 12
  73. _countA     EQU 20
  74. _countB     EQU 1
  75.    
  76. ; Para hacer que todas las variables sean globales (se puedan usar en más
  77. ; módulos)
  78. GLOBAL W_TEMP, STATUS_TEMP, count, countA, countB, countC
  79.  
  80. PSECT vars, space=1, class=udata
  81. ;linker: -Pvars=0x020
  82. count:      DS 1
  83. countA:     DS 1
  84. countB:     DS 1
  85. countC:     DS 1
  86. countCValue:    DS 1
  87.  
  88.    
  89. PSECT udata_shr ;common memory, 16 espacios
  90. W_TEMP:     DS  1
  91. STATUS_TEMP:    DS  1
  92.  
  93.  
  94. ;-------------------------------------------------------------------------------
  95. ; Vector de Reset
  96. ;-------------------------------------------------------------------------------
  97.  
  98. PSECT resetVector, delta=2
  99. ;linker: -PresetVector=0x0000
  100.     goto main
  101.  
  102. ;-------------------------------------------------------------------------------
  103. ; Vector de Interrupción
  104. ;-------------------------------------------------------------------------------
  105.  
  106. PSECT interruptVector, delta=2
  107. ;linker: -PinterruptVector=0x0004
  108. push:
  109.     movwf   W_TEMP
  110.     swapf   STATUS, W
  111.     movwf   STATUS_TEMP
  112. isr:
  113.     banksel PIR1
  114.     btfss   TMR1IF
  115.     goto    pop
  116.     restartTMR1
  117.     decf    count
  118.     decf    countA
  119.     decf    countB
  120.     decf    countC
  121.     bcf     TMR1IF
  122.     goto    pop
  123.  
  124. pop:
  125.     swapf   STATUS_TEMP, W
  126.     movwf   STATUS
  127.     swapf   W_TEMP, F
  128.     swapf   W_TEMP, W
  129.     retfie
  130.  
  131. ;-------------------------------------------------------------------------------
  132. ; Loop principal
  133. ;-------------------------------------------------------------------------------
  134.  
  135. PSECT code, delta=2
  136.  
  137. main:
  138.     call    config_io
  139.     call    config_osc
  140.     call    config_interrupt
  141.     call    config_tmr1
  142.     BANKSEL count
  143.     movlf   _count, count
  144.     movlf   _countA, countA
  145.     movlf   _countB, countB
  146.     movlf   8, countCValue
  147.     _movff   countCValue, countC
  148.     banksel PORTA
  149.  
  150. loop:
  151.     movf    count, f
  152.     btfsc   ZERO
  153.     call    toggleCount
  154.    
  155.     movf    countA, f
  156.     btfsc   ZERO
  157.     call    toggleCountA
  158.    
  159.     movf    countB, f
  160.     btfsc   ZERO
  161.     call    toggleCountB
  162.    
  163.     movf    countC, f
  164.     btfsc   ZERO
  165.     call    toggleCountC
  166.    
  167.     goto    loop
  168.  
  169. ;-------------------------------------------------------------------------------
  170. ; Subrutinas de configuración
  171. ;-------------------------------------------------------------------------------
  172.  
  173. config_io:
  174.     banksel TRISA
  175.     clrf    TRISA
  176.     clrf    TRISB
  177.     clrf    TRISC
  178.     clrf    TRISD
  179.     clrf    TRISE
  180.    
  181.     banksel ANSEL
  182.     clrf    ANSEL
  183.     clrf    ANSELH
  184.     banksel PORTA
  185.     clrf    PORTA
  186.     clrf    PORTB
  187.     clrf    PORTC
  188.     clrf    PORTD
  189.     clrf    PORTE
  190.     return
  191.  
  192. config_osc:
  193.     banksel OSCCON
  194.     bsf     IRCF2  
  195.     bsf     IRCF1
  196.     bcf     IRCF0   ; IRCF = 110 = 4MHz
  197.     bsf     SCS     ; Internal oscilator for system clock
  198.     return
  199.    
  200. config_tmr1:
  201.     banksel T1CON
  202.     bsf     T1CKPS1
  203.     bcf     T1CKPS0
  204.     restartTMR1
  205.     bsf     TMR1ON
  206.     return
  207.    
  208. config_interrupt:
  209.     ; clear flags
  210.     banksel PIR1
  211.     bcf     T0IF
  212.     bcf     TMR1IF
  213.    
  214.     ; set Enabled bits
  215.     banksel PIE1
  216.     bsf     T0IE
  217.     bsf     PEIE
  218.     bsf     TMR1IE
  219.    
  220.     banksel INTCON
  221.     bsf     GIE
  222.     return
  223.  
  224. ;-------------------------------------------------------------------------------
  225. ; Subrutinas contador
  226. ;-------------------------------------------------------------------------------
  227.  
  228. toggleCount:
  229.     movlf   _count, count
  230.     movlw   00000001B
  231.     xorwf   PORTB, f
  232.     return
  233.    
  234. toggleCountA:
  235.     movlf   _countA, countA
  236.     movlw   00000010B
  237.     xorwf   PORTB, f
  238.     return
  239.    
  240. toggleCountB:
  241.     movlf   _countB, countB
  242.     movlw   00000100B
  243.     xorwf   PORTB, f
  244.     return
  245.    
  246. toggleCountC:
  247.     _movff   countCValue, countC
  248.     movlw   00001000B
  249.     xorwf   PORTB, f
  250.     return
  251.  
  252. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement