Guest User

Untitled

a guest
Mar 17th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;
  3. ;MAKEFILE EXCERPT
  4. ;
  5. ;#Output file in intel hex format with -fI
  6. ;
  7. ;serial:
  8. ; sudo putty /dev/ttyUSB0 -serial -sercfg 8,2,9600,n,N # "-cs ISO-8859-1" not necessary
  9. ;
  10. ;compile:
  11. ; wine avrasm2.exe -fI -l test.lst test.asm
  12. ;
  13. ;flash: compile
  14. ; avrdude -c usbtiny -p atmega1284 -U flash:w:test.hex
  15. ;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. ;INCLUDES
  19. .nolist
  20. .include "./m1284def.asm"
  21. .list
  22.  
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24.  
  25. ;MACROS
  26.  
  27. .macro initialiseZregister
  28. ;Load from Program Memory (flash) Low-byte
  29. ;Use the low byte since that is where our string starts in this program
  30. ;Actually storing anything in flash at a word address should
  31. ;first result in a lowbyte being populated, if high bytes are not supplied
  32. ;with data, then they are 'padded' as a single null byte (0x00)
  33. ldi ZH,high(2*@0)
  34. ldi ZL,low(2*@0 + 0)
  35. .endm
  36.  
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38.  
  39. ;CODE
  40.  
  41. .cseg ;Code (flash) segment
  42.  
  43. ;Events/Interrupt Vectors
  44.  
  45. ; Reset Vector, just go to init
  46. .org 0x0000
  47. rjmp Reset
  48.  
  49.  
  50. ;FLASH BYTE STRING
  51. ;must come after 0x0000 (first address of flash)
  52.  
  53. TEST_MESSAGE: .db "ted does assembly!",'\r','\n'
  54. TEST_MESSAGE_END:
  55.  
  56. ;INIT
  57. Reset:
  58.  
  59. ;16MHz crystal ;CKDIV8 not set
  60. .equ FOSC = 16000000
  61. .equ BAUD = 9600
  62. call uart_init
  63.  
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65.  
  66. ;PRINT TEST_MESSAGE
  67.  
  68. ;Initialize compound register Z (=R31:R30)to point to "TEST_MESSAGE" location
  69. initialiseZregister TEST_MESSAGE
  70.  
  71. ;Loop through each byte location and print out the character found to the uart connection
  72. printCharFromFlash:
  73. ;load the current char for uart transmission
  74. lpm outChar,Z
  75. ;Do not necessarily rely on null to quit printing string, but if exists because assembler inserts it, then treat it as a terminating char
  76. cpi outChar,0x00
  77. breq stopPrinting
  78. ;Print the current character
  79. call uart_transmit
  80. ;Increment the 16 bit "pointer"
  81. call incZ
  82. ;Check if we have reached the end of the "string"
  83. call testIfAtEnd
  84. ;I should have found a better way than using a specific register and specific pair of 'signal values' (false = 0x00, true = 0xff)
  85. cpi r20,0xff
  86. ;testIfAtEnd passed so stop printing
  87. breq stopPrinting
  88. ;If not done printing, print the next char
  89. jmp printCharFromFlash
  90. stopPrinting:
  91.  
  92. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  93.  
  94. /* MAIN */
  95. Main:
  96. ;do nothing, respond to events only
  97. rjmp Main
  98.  
  99. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  100.  
  101. ;SUBROUTINES
  102.  
  103. incZ:
  104. ;We need to be able to increment these compound registers (X=r27:r26;Y=r29:r28;Z=r31:r30)
  105. ;and to my knowledge 'inc' does not work on them, only on single byte registers
  106. ;so I break up the task into 2 parts, increment the low byte,
  107. ;then add 0 with any resulting carry to the high byte,
  108. push r11
  109. in r11,sreg
  110. push r12
  111. clr r12 ; should be 0 now, need below
  112. clc ; clear the carry bit in SREG
  113. inc ZL ; should generate carry, if needed
  114. adc ZH,r12 ; should add 0+C, yielding effectively "inc Z"
  115. pop r12
  116. out sreg,r11
  117. pop r11
  118. ret
  119.  
  120. testIfAtEnd:
  121. ;Since end address is address after last string address
  122. ;and since address increment goes low byte, high byte, next word
  123. ;compare incremented address with low byte of "end" address
  124. ;that is compare ZL to 2N+0 not 2N+1
  125. cpi ZH,high(TEST_MESSAGE_END*2)
  126. brne notEqual
  127. cpi ZL,low(TEST_MESSAGE_END*2+0)
  128. brne notEqual
  129. ldi r20,0xff
  130. jmp equal
  131. notEqual:
  132. ldi r20,0x00
  133. equal:
  134. ret
  135.  
  136. .macro stsi
  137. ;store direct to "dataspace" immediate
  138. push r30
  139. ldi r30,@1
  140. sts @0,r30
  141. pop r30
  142. .endm
  143.  
  144.  
  145. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  146.  
  147. ;UART STUFF
  148.  
  149. uart_init:
  150. ;USAGE:
  151. ;
  152. ; .equ FOSC = 16000000
  153. ; .equ BAUD = 9600
  154. ; call uart_init
  155. ;
  156.  
  157. ;Set baud rate
  158. ;NOTE: ubrr is a 12 bit register so
  159. ; take the 16bit clocks/baud-period value and (<< 2^4) aka (/16)
  160. ; but not sure what the -1 is for
  161. ; the -1 just comes from Atmel's documentation without explanation
  162. .equ ubrr = (((FOSC/BAUD)/16)-1)
  163. stsi UBRR0H, ubrr >> 8 ;High byte
  164. stsi UBRR0L, ubrr
  165.  
  166. ; Enable receiver and transmitter ;Enable the rx complete interrupt
  167. stsi UCSR0B, (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)
  168.  
  169. ; Set frame format: 8data, 2stop bit
  170. stsi UCSR0C, (1<<USBS0) |(1<<UCSZ01)|(1<<UCSZ00)
  171.  
  172. ret
  173.  
  174.  
  175. .def tmp = r17
  176. .def outChar = r16
  177. uart_transmit:
  178. ;USAGE:
  179. ; mov outChar, rXX
  180. ; call uart_transmit
  181. push tmp
  182. UART_Transmit_Check:
  183. ; Wait for empty transmit buffer
  184. lds tmp, UCSR0A
  185. sbrs tmp, UDRE0 ; SBRS = SkipifBitinRegisterSet
  186. ; UDRE0 not set, data not ready, check again
  187. rjmp UART_Transmit_Check
  188. ; Put data (r16) into buffer, sends the data
  189. sts UDR0,outChar
  190. pop tmp
  191. ret
  192.  
  193. ;END EXAMPLE
  194.  
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Add Comment
Please, Sign In to add comment