sombruxo

miNombre

May 13th, 2026
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 2.74 KB | Software | 0 0
  1.  
  2.  .DSEG
  3.     ; Aqui las declaraciones de variables. tipo:
  4.     array: .byte 20 ;esto es ejemplo, en este codigo no lo uso.
  5.  
  6.  .CSEG
  7.  .ORG 0x00000       ;reset interrupt vector
  8.       jmp INICIO
  9.  .ORG URXCaddr      ; 0x00024  -> interrupt vectors for USART0
  10.       jmp USART0_reception_completed
  11.  
  12.  .ORG INT_VECTORS_SIZE ;leave room for IRQ vectors
  13.  
  14. INICIO:
  15.  
  16.     ldi zl, low(array)
  17.     ldi zh, high(array)
  18.     clr r16
  19.     st z, r16
  20.  
  21.  
  22.     ; Inicializar UART
  23.     rcall UART_INICIO
  24.  
  25.     ser r16
  26.     out ddrb, r16
  27.     SEI
  28.  
  29.     ldi r20, 0xFF
  30.  
  31. MAIN_LOOP:
  32.  
  33.     out portb, r20
  34.     call delay500ms
  35.     com r20
  36.  
  37.     jmp MAIN_LOOP
  38.  
  39. ; ---------------------------------------------------------------------------------------
  40. UART_INICIO:
  41.     push r16
  42.     ldi r16, 103                                                          
  43.     sts UBRR0L, r16
  44.     ldi r16, 0
  45.     sts UBRR0H, r16
  46.  
  47.     ldi r16, (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)                                
  48.     sts UCSR0B, r16
  49.  
  50.     ldi r16, (1<<UCSZ01)|(1<<UCSZ00)
  51.     sts UCSR0C, r16
  52.  
  53.     pop r16
  54.     ret
  55.  
  56. ;----USART0_reception_completed handler --------------------------------------------------
  57. USART0_reception_completed:
  58.     PUSH R16            ;this handler routine will be automatically called every 61msec (in this example)
  59.     IN R16, SREG        ;Backup SREG. MANDATORY in interrupt handler routines
  60.     PUSH R16
  61.    
  62.     ;do the desired periodic task here
  63.     LDS R17, UDR0       ;pick up the byte received and do anything with it
  64.  
  65.     ; Z apunta al inicio del array
  66.     ldi zl, low(array)
  67.     ldi zh, high(array)
  68.  
  69. buscarFinArray:
  70.     ld r16, z
  71.     cpi r16, 0
  72.     breq instertar_rx
  73.     ADIW zl, 1
  74.     rjmp buscarFinArray
  75.  
  76. instertar_rx:
  77.     st z+, R17
  78.     clr r17
  79.     st z, r17
  80.  
  81.     call UART_PRINT_STRING
  82.    
  83.     POP R16
  84.     OUT SREG, R16       ;Recover SREG from the previous backup
  85.     POP R16
  86.     RETI                ;RETI is MANDATORY when returning from an interrupt handling routine
  87.  
  88. ; ---------------------------------------------------------------------------------------
  89. UART_PRINT_STRING:
  90.     push r30
  91.     push r31
  92.  
  93.     ldi r16, 10
  94.     sts UDR0, r16
  95.     WAIT_TX2:
  96.     lds r18, UCSR0A
  97.     sbrs r18, UDRE0                                            
  98.     rjmp WAIT_TX2
  99.     ldi r16, 13
  100.     sts UDR0, r16
  101.  
  102.     ; Z apunta al inicio del array
  103.     ldi zl, low(array)
  104.     ldi zh, high(array)
  105.  
  106. PRINT_LOOP:
  107.     ld r16, Z+
  108.     cpi r16, 0
  109.     breq END_PRINT
  110.  
  111. WAIT_TX:
  112.     lds r18, UCSR0A
  113.     sbrs r18, UDRE0                                            
  114.     rjmp WAIT_TX
  115.  
  116.     sts UDR0, r16
  117.     rjmp PRINT_LOOP
  118.  
  119. END_PRINT:
  120.     pop r31
  121.     pop r30
  122.     ret
  123.  
  124. ; ---------------------------------------------------------------------------------------
  125. delay500ms:
  126.     ldi  r21, 41
  127.     ldi  r22, 150
  128.     ldi  r23, 128
  129. L1: dec  r23
  130.     brne L1
  131.     dec  r22
  132.     brne L1
  133.     dec  r21
  134.     brne L1
  135.     ret
Advertisement
Add Comment
Please, Sign In to add comment