Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. ;
  2. ; ToUpper_USART.asm
  3. ;
  4. ; Created: 24/04/2018 13:29:04
  5. ; Author : 1418
  6. ;
  7.  
  8. ;*********************************************************************************************
  9. ;******************** Const Definitions, Precompiler sentences ******************************
  10. ;*********************************************************************************************
  11. .EQU lowercase_a = 97 ;ASCII for 'a'
  12. .EQU lowercase_z = 122 ;ASCII for 'z'
  13. .EQU TOUPPER = 32 ;to be subtracted from the lowercase to get the uppercase
  14. .EQU minus = 45 ;Letra guion "-"
  15.  
  16.  
  17. .EQU Clock = 16000000 ;processor’s clock frequency, Hz
  18. .EQU Baud = 9600 ;desired serial port baud rate (bits per second)
  19. .EQU UBRRvalue = Clock/(Baud*16) -1 ;calculates value to be put in UBRR0H:L
  20.  
  21. ;*********************************************************************************************
  22. ;******************** Main VARs **************************************************************
  23. ;*********************************************************************************************
  24. .DSEG
  25.  
  26. ;*********************************************************************************************
  27. ;******************** Main Program ***********************************************************
  28. ;*********************************************************************************************
  29. .CSEG
  30. .ORG 0x00000 ;reset interrupt vector
  31. jmp Init
  32.  
  33. .ORG 0x00032 ;interrupt vectors for USART0
  34. jmp USART0_reception_completed
  35. reti
  36. reti
  37.  
  38. .ORG 0x00100 ;leave room for IRQ vectors
  39. Init:
  40. ;configure USART0
  41. RCALL init_USART0
  42. rcall InitBPort
  43.  
  44. SEI ;enable interrupts globally
  45.  
  46. clr r16
  47. Loop:
  48. out portb, r16
  49. rcall delay500ms
  50. com r16
  51. rjmp Loop
  52.  
  53. ;*********************************************************************************************
  54. ;**************** End Main Program ***********************************************************
  55. ;*********************************************************************************************
  56.  
  57. ;------- initialize USART0 as 9600baud, asynchronous, 8 data bits, 1 stop bit, no parity -----
  58. ;Especificaciones: https://appelsiini.net/2011/simple-usart-with-avr-libc/#registers
  59. ; http://microcontroladores-mrelberni.com/interrupcion-usart-avr/
  60. init_USART0:
  61. PUSH R16
  62. LDI R16, LOW(UBRRvalue)
  63. STS UBRR0L, R16 ;load the low byte
  64. LDI R16, HIGH(UBRRvalue)
  65. STS UBRR0H, R16 ;load the low byte
  66. ; enable receive and transmit, enable USART0 interrupts (UDR empty, Tx finished, Rx finished)
  67. ;ldi r16, (1<<RXEN0)| (1<<RXCIE0)
  68. LDI R16, (1<<RXEN0)|(1<< TXEN0)|(0<<UDRIE0)|(0<< TXCIE0)|(1<< RXCIE0)
  69. STS UCSR0B, R16 ;set control register UCSR0B with the corresponding bits
  70. ; configure USART 0 as asynchronous, set frame format: 8 data bits, 1 stop bit, no parity
  71. LDI R16, (0<<UMSEL00) |(1<<UCSZ01)|(1<< UCSZ00) |(0<< USBS0)|(0<<UPM01)|(0<< UPM00)
  72. STS UCSR0C, R16 ;set control register UCSR0C with the corresponding bits
  73. POP R16
  74. RET
  75.  
  76. ;----USART0_reception_completed handler --------------------------------------------------
  77. USART0_reception_completed:
  78. PUSH r16 ;this handler routine will be automatically called every 61msec (in this example)
  79. IN r16, SREG ;Backup SREG. MANDATORY in interrupt handler routines
  80. PUSH r16
  81.  
  82. ;do the desired periodic task here
  83. LDS r16, UDR0 ;pick up the byte received and do anything with it
  84. CPI r16,lowercase_a ;'a'
  85. BRLT NotConvert ;not a lowercase letter
  86. CPI r16,lowercase_z+1 ;'z'+1
  87. BRGE NotConvert ;not an lowercase letter
  88. SUBI r16, TOUPPER ;change to Uppercase
  89. STS UDR0, r16 ;transmits the [modified] byte
  90. JMP USART0_reception_cont
  91. ; Non conver. not lowercase letter. send "-" character
  92. NotConvert:
  93. ;LDI r16, minus
  94. STS UDR0, r16 ;transmits the [modified] byte
  95.  
  96. USART0_reception_cont:
  97. POP r16
  98. OUT SREG, r16 ;Recover SREG from the previous backup
  99. POP r16
  100. RETI ;RETI is MANDATORY when returning from an interrupt handling routine
  101.  
  102. ;Funcion inicializar puerto B como salida
  103. InitBPort:
  104. push r31
  105. LDI r31, 0xff
  106. OUT DDRB, r31
  107. pop r31
  108. ret
  109.  
  110. ; Delay 3 200 000 cycles
  111. ; 200ms at 16 MHz
  112. delay500ms:
  113. push r18
  114. push r19
  115. push r20
  116. ldi r18, 41
  117. ldi r19, 150
  118. ldi r20, 128
  119. L1: dec r20
  120. brne L1
  121. dec r19
  122. brne L1
  123. dec r18
  124. brne L1
  125. pop r20
  126. pop r19
  127. pop r18
  128. ret
  129.  
  130. .EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement