Advertisement
sombruxo

Untitled

Apr 21st, 2025 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 2.27 KB | Source Code | 0 0
  1. ;
  2. ; pwm_v1.asm
  3. ;
  4. ; Created: 21/04/2025 13:31:25
  5. ; Author : Bemposta
  6. ;
  7.  
  8.  .EQU Clock = 16000000 ;processor’s clock frequency, Hz
  9.  .EQU Baud = 9600 ;desired serial port baud rate (bits per second)
  10.  .EQU UBRRvalue = Clock/(Baud*16) -1 ;calculates value to be put in UBRR0H:L
  11.  
  12.  .DSEG
  13.     valorPWM: .byte 1
  14.  
  15.  .CSEG
  16.  .ORG 0x00000 ;reset interrupt vector
  17.       jmp Start
  18.  .ORG URXCaddr ; 0x00024  -> interrupt vectors for USART0
  19.       jmp USART0_reception_completed
  20.  
  21. .ORG INT_VECTORS_SIZE ;leave room for IRQ vectors
  22. Start:
  23.     ldi r16, 0xFF
  24.     out ddrd, r16
  25.  
  26.     ldi r18, (2<<COM0A0) | (1<<WGM00)
  27.     out TCCR0A, r18
  28.     ldi r18, (0<<WGM02) | (3<<CS00)
  29.     out TCCR0B, r18
  30.  
  31.     ldi r20, 0      ;registro resevado para la PWM. No se debe hacer asi!!!!!!
  32.     sts valorPWM, r20
  33.     out ocr0a, r20
  34.  
  35.     ;configure USART0
  36.     RCALL init_USART0
  37.     SEI ;enable interrupts globally
  38.  
  39. Loop:
  40.     ;nothing to do here, just be alive
  41.     NOP
  42.     rjmp Loop
  43.  
  44.  
  45. init_USART0:
  46.     PUSH R16
  47.     LDI R16, LOW(UBRRvalue)
  48.     STS UBRR0L, R16   ;load the low byte
  49.     LDI R16, HIGH(UBRRvalue)
  50.     STS UBRR0H, R16   ;load the low byte
  51.     ; enable receive and transmit, enable USART0 interrupts (UDR empty, Tx finished, Rx finished)
  52.     LDI R16, (1<<RXEN0)|(0<<TXEN0)|(0<<UDRIE0)|(0<< TXCIE0)|(1<< RXCIE0)
  53.     STS UCSR0B, R16 ;set control register UCSR0B with the corresponding bits
  54.     ; configure USART 0 as asynchronous, set frame format: 8 data bits, 1 stop bit, no parity
  55.     LDI R16, (0<<UMSEL00) |(1<<UCSZ01)|(1<< UCSZ00) |(0<< USBS0)|(0<<UPM01)|(0<< UPM00)
  56.     STS UCSR0C, R16 ;set control register UCSR0C with the corresponding bits
  57.     POP R16
  58.     RET
  59.  
  60. USART0_reception_completed:
  61.     PUSH R16 ;this handler routine will be automatically called every 61msec (in this example)
  62.     IN R16, SREG ;Backup SREG. MANDATORY in interrupt handler routines
  63.     PUSH R16
  64.     push r20
  65.    
  66.     ;do the desired periodic task here
  67.     LDS R16, UDR0  ;pick up the byte received and do anything with it
  68.     cpi r16, 'a'
  69.     brne sigue
  70.     lds r20, valorPWM
  71.     inc r20
  72.     out ocr0a, r20
  73.     sts valorPWM, r20
  74. sigue:
  75.     cpi r16, 's'
  76.     brne termina
  77.     lds r20, valorPWM
  78.     dec r20
  79.     out ocr0a, r20
  80.     sts valorPWM, r20
  81.     out ocr0a, r20
  82.  
  83. termina:
  84.     pop r20
  85.     POP R16
  86.     OUT SREG, R16  ;Recover SREG from the previous backup
  87.     POP R16
  88.     RETI ;RETI is MANDATORY when returning from an interrupt handling routine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement