Advertisement
ZelekWafelek

KASZA

Jan 25th, 2022
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Laboratory AVR Microcontrollers Part2
  3. // Program template for lab 9
  4. // Please fill in this information before starting coding
  5. // Authors:
  6. //
  7. // Group:
  8. // Section:
  9. //
  10. // Task:
  11. //
  12. // Todo:
  13. //
  14. //
  15. // Version: 5.0
  16. //////////////////////////////////////////////////////////////////////////////
  17. .nolist ;quartz assumption 4Mhz
  18. .include "m2560def.inc"
  19. ;//////////////////////////////////////////////////////////////////////////////
  20. .list
  21. .equ xlength =  100
  22. ;//////////////////////////////////////////////////////////////////////////////
  23. ; EEPROM - data non volatile memory segment
  24. .ESEG
  25.  
  26. ;//////////////////////////////////////////////////////////////////////////////
  27. ; StaticRAM - data memory.segment
  28. .DSEG
  29.  
  30. .ORG 0x200; may be omitted this is default value
  31. ; Destination table (xlengthx bytes).
  32. ; Replace "xlengthx" with correct value
  33. TAB_RAM: .BYTE xlength
  34. blinks: .byte 1
  35.  
  36. ;//////////////////////////////////////////////////////////////////////////////
  37. ; CODE - Program memory segment
  38. ; Please Remember that it is "word" address space
  39. ;
  40. .CSEG  
  41. .org 0x0000 ; may be omitted this is default value
  42. jmp RESET   ; Reset Handler
  43.  
  44. ; Interrupts vector table / change to your procedure only when needed
  45. jmp EXT_INT0    ; IRQ0 Handler
  46. jmp EXT_INT1    ; IRQ1 Handler
  47. jmp EXT_INT2    ; IRQ2 Handler
  48. jmp EXT_INT3    ; IRQ3 Handler
  49. jmp EXT_INT4    ; IRQ4 Handler
  50. jmp EXT_INT5    ; IRQ5 Handler
  51. jmp EXT_INT6    ; IRQ6 Handler
  52. jmp EXT_INT7    ; IRQ7 Handler
  53. jmp HPCINT0     ; PCINT0 Handler
  54. jmp HPCINT1     ; PCINT1 Handler
  55. jmp HPCINT2     ; PCINT2 Handler
  56. jmp WDT         ; WDT Handler
  57. jmp TIM2_COMPA  ; Timer2 CompareA Handler
  58. jmp TIM2_COMPB  ; Timer2 CompareB Handler
  59. jmp TIM2_OVF    ; Timer2 Overflow Handler
  60. jmp TIM1_CAPT   ; Timer1 Capture Handler
  61. jmp TIM1_C0MPA  ; Timer1 CompareA Handler
  62. jmp TIM1_C0MPB  ; Timer1 CompareB Handler
  63. jmp TIM1_COMPC  ; Timer1 CompareC Handler
  64. jmp TIM1_0VF    ; Timer1 Overflow Handler
  65. jmp TIM0_COMPA  ; Timer0 CompareA Handler
  66. jmp TIM0_COMPB  ; Timer0 CompareB Handler
  67. jmp TIM0_OVF    ; Timer0 Overflow Handler
  68. jmp SPI_STC     ; SPI Transfer Complete Handler
  69. jmp USART0_RXC  ; USART0 RX Complete Handler
  70. jmp USART0_UDRE ; USART0,UDR Empty Handler
  71. jmp USART0_TXC  ; USART0 TX Complete Handler
  72. jmp ANA_COMP    ; Analog COmparator Handler
  73. jmp HADC            ; ADC Conversion Complete Handler
  74. jmp EE_RDY      ; EEPROM Ready Handler
  75. jmp TIM3_CAPT   ; Timer3 Capture Handler
  76. jmp TIM3_COMPA  ; Timer3 CompareA Handler
  77. jmp TIM3_COMPB  ; Timer3 CompareB Handler
  78. jmp TIM3_COMPC  ; Timer3 CompareC Handler
  79. jmp TIM3_OVF    ; Timer3 Overflow Handler
  80. jmp USART1_RXC  ; USART1 RX Complete Handler
  81. jmp USART1_UDRE ; USART1,UDR Empty Handler
  82. jmp USART1_TXC  ; USART1 TX Complete Handler
  83. jmp TWI         ; Two-wire Serial Interface Interrupt Handler
  84. jmp SPM_RDY     ; SPM Ready Handler
  85. jmp TIM4_CAPT   ; Timer4 Capture Handler
  86. jmp TIM4_COMPA  ; Timer4 CompareA Handler
  87. jmp TIM4_COMPB  ; Timer4 CompareB Handler
  88. jmp TIM4_COMPC  ; Timer4 CompareC Handler
  89. jmp TIM4_OVF    ; Timer4 Overlflow Handler
  90. jmp TIM5_CAPT   ; Timer5 Capture Handler
  91. jmp TIM5_COMPA  ; Timer5 CompareA Handler
  92. jmp TIM5_COMPB  ; Timer5 CompareB Handler
  93. jmp TIM5_COMPC  ; Timer5 CompareC Handler
  94. jmp TIM5_OVF    ; Timer5 Overlflow Handler
  95. jmp USART2_RXC  ; USART2 RX Complete Handler
  96. jmp USART2_UDRE ; USART2,UDR Empty Handler
  97. jmp USART2_TXC  ; USART2 TX Complete Handler
  98. jmp USART3_RXC  ; USART3 RX Complete Handler
  99. jmp USART3_UDRE ; USART3,UDR Empty Handler
  100. jmp USART3_TXC  ; USART3 TX Complete Handler
  101.  
  102. //////////////////////////////////////////////////////////////////////////////
  103. EXT_INT0:   ; IRQ0 Handler
  104. EXT_INT1:   ; IRQ1 Handler
  105. EXT_INT2:   ; IRQ2 Handler
  106. EXT_INT3:   ; IRQ3 Handler
  107. EXT_INT4:   ; IRQ4 Handler
  108. EXT_INT5:   ; IRQ5 Handler
  109. EXT_INT6:   ; IRQ6 Handler
  110. EXT_INT7:   ; IRQ7 Handler
  111. HPCINT0:        ; PCINT0 Handler
  112. HPCINT1:        ; PCINT1 Handler
  113. HPCINT2:        ; PCINT2 Handler
  114. WDT:        ; WDT Handler
  115. TIM2_COMPA: ; Timer2 CompareA Handler
  116. TIM2_COMPB: ; Timer2 CompareB Handler
  117. TIM2_OVF:   ; Timer2 Overflow Handler
  118. TIM1_CAPT:  ; Timer1 Capture Handler
  119. TIM1_C0MPB: ; Timer1 CompareB Handler
  120. TIM1_COMPC: ; Timer1 CompareC Handler
  121. TIM1_0VF:   ; Timer1 Overflow Handler
  122. TIM0_COMPA: ; Timer0 CompareA Handler
  123. TIM0_COMPB: ; Timer0 CompareB Handler
  124. TIM0_OVF:   ; Timer0 Overflow Handler
  125. SPI_STC:    ; SPI Transfer Complete Handler
  126. USART0_RXC: ; USART0 RX Complete Handler
  127. USART0_UDRE:; USART0,UDR Empty Handler
  128. USART0_TXC: ; USART0 TX Complete Handler
  129. ANA_COMP:   ; Analog COmparator Handler
  130. HADC:       ; ADC Conversion Complete Handler
  131. EE_RDY:     ; EEPROM Ready Handler
  132. TIM3_CAPT:  ; Timer3 Capture Handler
  133. TIM3_COMPA: ; Timer3 CompareA Handler
  134. TIM3_COMPB: ; Timer3 CompareB Handler
  135. TIM3_COMPC: ; Timer3 CompareC Handler
  136. TIM3_OVF:   ; Timer3 Overflow Handler
  137. USART1_RXC: ; USART1 RX Complete Handler
  138. USART1_UDRE:; USART1,UDR Empty Handler
  139. USART1_TXC: ; USART1 TX Complete Handler
  140. TWI:        ; Two-wire Serial Interface Interrupt Handler
  141. SPM_RDY:    ; SPM Ready Handler
  142. TIM4_CAPT:  ; Timer4 Capture Handler
  143. TIM4_COMPA: ; Timer4 CompareA Handler
  144. TIM4_COMPB: ; Timer4 CompareB Handler
  145. TIM4_COMPC: ; Timer4 CompareC Handler
  146. TIM4_OVF:   ; Timer4 Overlflow Handler
  147. TIM5_CAPT:  ; Timer5 Capture Handler
  148. TIM5_COMPA: ; Timer5 CompareA Handler
  149. TIM5_COMPB: ; Timer5 CompareB Handler
  150. TIM5_COMPC: ; Timer5 CompareC Handler
  151. TIM5_OVF:   ; Timer5 Overlflow Handler
  152. USART2_RXC: ; USART2 RX Complete Handler
  153. USART2_UDRE:; USART2,UDR Empty Handler
  154. USART2_TXC: ; USART2 TX Complete Handler
  155. USART3_RXC: ; USART3 RX Complete Handler
  156. USART3_UDRE:; USART3,UDR Empty Handler
  157. USART3_TXC: ; USART3 TX Complete Handler
  158.     reti        ; return from all no used
  159.  
  160. TIM1_C0MPA: ; Timer1 CompareA Handler
  161.     lds r16, blinks
  162.     cpi r16, 0
  163.     brbs 1, FIRST ; If blinks is equal to 0 jump to FIRST
  164.     cpi r16, 1
  165.     brbs 1, SECOND ; IF blinks is equal to 1 jump to SECOND
  166.     cpi r16, 2
  167.     brbs 1, THIRD ; If blinks is equal to 2 jump to THIRD
  168.     jmp TIM1_END
  169. FIRST:
  170.     lds r16, PORTB
  171.     cbr r16, (1 << 5)
  172.     sts PORTB, r16
  173.     jmp TIM1_END ; Turn the LED off and jump to end of function
  174. SECOND:
  175.     lds r16, PORTB
  176.     sbr r16, (1 << 5)
  177.     sts PORTB, r16
  178.     jmp TIM1_END ; Turn the LED on and jump to end of function
  179.     NOP
  180. THIRD:
  181.     lds r16, PORTB
  182.     cbr r16, (1 << 5)
  183.     sts PORTB, r16
  184.     ldi r16, 0
  185.     sts TCCR1B, r16 ; Stop timer
  186.     ldi r16, 255 ; 255 in blinks will be needed because after incrementing in end of this function there will be 0 in that variable
  187.     sts blinks, r16 ; Turn the LED off and jump to end of function
  188.  
  189. TIM1_END:
  190.     ldi r16, 0
  191.     sts TCNT1L, r16
  192.     sts TCNT1H, r16 ; Clear timer counter
  193.     lds r16, blinks
  194.     inc r16
  195.     sts blinks, r16 ; Increment blinks
  196.     reti
  197.  
  198. ;//////////////////////////////////////////////////////////////////////////////
  199. ; Program start
  200. RESET:
  201.  
  202.     cli         ; disable all interrupts
  203. //  Set stack pointer to top of RAM
  204.     ldi R16, HIGH(RAMEND)
  205.     out SPH, R16
  206.     ldi R16, LOW(RAMEND)
  207.     out SPL, R16
  208.     ldi r16, 0
  209.     sts blinks, r16 ; blinks = 0
  210.     lds r16, DDRB
  211.     sbr r16, (1<<5)
  212.     sts DDRB, r16 ; Set 5th pin of B port as Output
  213.     call init_timer1
  214.     lds r16, PORTB
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.     // 1. Place here code related to initialization of ports and interrupts
  222.     // for instance:
  223.     //    port A as input and switching Pull-up resistors on
  224.     // DDRA=0x00
  225.     // PORTA=0xFF
  226.     //    port C as output and initial value FF
  227.     // DDRB=0xFF
  228.     // PORTA=0xFF
  229.     //
  230.     // Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn
  231.     // Note that the SBI instruction can be used to toggle one single bit in a port.
  232.  
  233.  
  234.  
  235. // 2. Enable interrupts if needed
  236.     sei
  237.  
  238.  
  239. //------------------------------------------------------------------------------
  240. // 3. Load initial values of index registers
  241. //  Z, X, Y
  242.  
  243.  
  244.  
  245.  
  246. //------------------------------------------------------------------------------
  247. // Program end - Ending loop
  248. //------------------------------------------------------------------------------
  249. End:
  250.     in r16, PINA
  251.     cpi r16, 0
  252.     breq end
  253.     ; Any button on PORT A was pressed
  254.     lds r16, PORTB
  255.     sbr r16, (1 << 5)
  256.     sts PORTB, r16 ; Turn b.5 to high
  257.     ldi r16, (1<<CS12)
  258.     sts TCCR1B, r16 ; Set the prescaler to 256 and start the timer
  259.     call copy
  260.     rjmp END
  261. init_timer1:
  262.     ldi r16, (1 << 1)
  263.     sts TIMSK1, r16 ; Enable Compare interrupt
  264.     ldi r16, 0x0 ; 0x7a
  265.     sts OCR1AH, r16
  266.     ldi r16, 0x12 ; 0x12 ; Set the Output Compare A Register to 0x7a12. This and 256 prescaler will give 500ms interval.
  267.     sts OCR1AL, r16
  268.     ret
  269.  
  270. copy:
  271.     ldi r17, 0
  272.     ldi ZL, LOW(2*TAB_ROM)
  273.     ldi ZH, HIGH(2*TAB_ROM) ; Set the Z register as a pointer of the TAB_ROM address
  274.     ldi YL, LOW(TAB_RAM)
  275.     ldi YH, HIGH(TAB_RAM) ; Set the Y register as a pointer of the TAB_RAM address
  276. loop:
  277.     lpm r15, Z+
  278.     st Y+, r15
  279.     inc r17
  280.     cpi r17, xLength
  281.     BRLO loop ; while r17 is lower than length of tab_rom jump to start of loop
  282.  
  283.    
  284.     ret
  285.  
  286.  
  287.  
  288.  
  289.  
  290. //------------------------------------------------------------------------------
  291. // Table Declaration -  place here test values
  292. // Test with different table values and different begin addresses of table (also above 0x8000)
  293. //
  294. //.org 0x8000
  295. TAB_ROM:    .db     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x1F
  296.             .db     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1F, 0x1F
  297.             .db     0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F
  298.             .db     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
  299.             .db     0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F
  300.             .db     0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  301.             .db     0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F
  302.             .db     0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F
  303.             .db     0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F
  304.             .db     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F
  305.             .db     0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaA, 0xaB, 0xaC, 0xaD, 0xAE, 0xAF
  306.             .db     0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF
  307.             .db     0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  308.             .db     0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF
  309.             .db     0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3, 0xF2, 0xF1, 0xF0
  310.             // please correct end of this table according to the guidelines given by the teacher
  311.             .db     0x00,0x00
  312. .EXIT
  313. //------------------------------------------------------------------------------
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement