Facebook
From Idiotic Moth, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 76
  1. .equ Digits_P = PORTB
  2. .equ Segments_P = PORTD
  3.  
  4. .def Digit_0 = R5
  5. .def Digit_1 = R4
  6. .def Digit_2 = R3
  7. .def Digit_3 = R2
  8.  
  9. .def PulseEdgeCtrL = R0
  10. .def PulseEdgeCtrH = R1
  11.  
  12. ; INPUTS
  13. .def XL=R16 ; divident
  14. .def XH=R17
  15.  
  16. .def YL=R18 ; divisor
  17. .def YH=R19
  18.  
  19. ; OUTPUTS
  20. .def RL=R16 ; remainder
  21. .def RH=R17
  22.  
  23. .def QL=R18 ; quotient
  24. .def QH=R19
  25.  
  26. ; INTERNAL
  27. .def QCtrL=R24
  28. .def QCtrH=R25
  29.  
  30.  
  31.  
  32. .macro LOAD_CONST
  33.         ldi @0, low(@2)
  34.         ldi @1, high(@2)
  35. .endmacro
  36.  
  37. .macro SET_DIGIT
  38.         mov R16, Digit_@0
  39.         rcall DigitTo7segCode
  40.         ldi R24, 2<<@0
  41.         out Digits_P, R24
  42.         out Segments_P, R16
  43.         rcall DelayInMs
  44. .endmacro
  45.  
  46.  
  47. ; TIMER
  48. .cseg
  49.  
  50. .org 0
  51. rjmp Program_init
  52. .org OC1Aaddr  
  53. rjmp Timer_isr
  54. .org PCIBaddr
  55. rjmp ExtInt_isr
  56.  
  57. Timer_isr:
  58.  
  59.         ; COUNTER
  60.         push R21
  61.         push R22
  62.  
  63.         LOAD_CONST R21, R22, 10000
  64.  
  65.         cp PulseEdgeCtrL, R21
  66.         brne Counter_End
  67.         cp PulseEdgeCtrH, R22
  68.         brne Counter_End
  69.  
  70.         clr PulseEdgeCtrL
  71.         clr PulseEdgeCtrH
  72.  
  73.         Counter_End:
  74.  
  75.         pop R22
  76.         pop R21
  77.  
  78.         lsr PulseEdgeCtrL
  79.         lsr PulseEdgeCtrH
  80.  
  81.         rcall NumberToDigits
  82.  
  83.         clr PulseEdgeCtrL
  84.         clr PulseEdgeCtrH
  85.  
  86. reti
  87.  
  88.  
  89.  
  90. ExtInt_isr:
  91.  
  92.         push R20
  93.  
  94.         ldi R20, 1
  95.         add PulseEdgeCtrL, R20
  96.         clr R20
  97.         adc PulseEdgeCtrH, R20
  98.  
  99.         pop R20
  100.  
  101.         reti
  102.  
  103.  
  104.  
  105.         Divide:
  106. ; X/Y = Q * Y + R
  107.  
  108.         push R24
  109.         push R25
  110.  
  111.         LOAD_CONST QCtrL, QCtrH, 0
  112.  
  113.         Divide_2:
  114.                 cp XH, YH    ;cp porównuje rejestry odejmując je.
  115.                 brcs Divide_2_End ; XH < YH
  116.                 brne Divide_2_Sub ; XH > YH
  117.  
  118.                 cp XL, YL ; XH = YH
  119.                 brcs Divide_2_End ; XL < YL
  120.  
  121.                 Divide_2_Sub:
  122.                         sub XL, YL
  123.                         sbc XH, YH
  124.                         adiw QCtrL:QCtrH, 1
  125.                         rjmp Divide_2
  126.  
  127.                 Divide_2_End:
  128.                         mov QH, QCtrH
  129.                         mov QL, QCtrL
  130.                         mov RH, XH
  131.                         mov RL, XL
  132.  
  133.         pop R25
  134.         pop R24
  135.  
  136.         ret
  137.  
  138.  
  139.  
  140. DelayInMs:
  141.         push R24
  142.         push R25
  143.         LOAD_CONST R24, R25, 1
  144.         Loop_1:
  145.                 rcall DelayOneMs
  146.                 sbiw R24, 1
  147.         brne Loop_1
  148.         pop R25
  149.         pop R24
  150.         ret
  151.  
  152. DelayOneMs:
  153.         push R24
  154.         push R25
  155.  
  156.         LOAD_CONST R24, R25, 2000
  157.         Loop_2:
  158.                 sbiw R24, 1
  159.                 brne Loop_2
  160.  
  161.         pop R25
  162.         pop R24
  163.         ret
  164.  
  165.  
  166.  
  167.         DigitTo7segCode:
  168.  
  169.         push R30
  170.         push R31
  171.  
  172.         ldi R30, low(Table<<1)
  173.         ldi R31, high(Table<<1)
  174.  
  175.         add R30,R16
  176.         clr R16
  177.         adc R31,R16
  178.         lpm R16, Z
  179.  
  180.         pop R31
  181.         pop R30
  182.  
  183.         ret
  184.  
  185. Table: .db 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0xFF, 0x6F
  186.  
  187.  
  188.  
  189. NumberToDigits:
  190.         mov XL, PulseEdgeCtrL
  191.         mov XH, PulseEdgeCtrH
  192.         LOAD_CONST YL, YH, 1000
  193.         rcall Divide
  194.         mov Digit_0, QL
  195.  
  196.         LOAD_CONST YL, YH, 100
  197.         mov XH, RH
  198.         mov XL, RL
  199.         rcall Divide
  200.         mov Digit_1, QL
  201.  
  202.         LOAD_CONST YL, YH, 10
  203.         mov XH, RH
  204.         mov XL, RL
  205.         rcall Divide
  206.         mov Digit_2, QL
  207.  
  208.         mov Digit_3, RL
  209.         ret
  210.  
  211.  
  212.  
  213. Program_Init:
  214.  
  215.         push R16
  216.  
  217.         .equ Timer_Period = 32150
  218.  
  219.         ldi R16, (1<<CS12)|(1<<WGM12)
  220.         out TCCR1B, R16
  221.         ldi R16, high(Timer_Period)
  222.         out OCR1AH, R16
  223.         ldi R16, low(Timer_Period)
  224.         out OCR1AL, R16
  225.         ldi R16, 0b1000000
  226.         out TIMSK, R16
  227.  
  228.         ldi R16, 1
  229.         out PCMSK0, R16
  230.         ldi R16, 0b100000
  231.         out GIMSK, R16
  232.  
  233.         pop R16
  234.  
  235.         sei
  236.  
  237.  
  238.  
  239. ;### MAIN ###
  240. Main_Loop:
  241.  
  242.  
  243.         SET_DIGIT 0
  244.         SET_DIGIT 1
  245.         SET_DIGIT 2
  246.         SET_DIGIT 3
  247.  
  248. rjmp Main_Loop