Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .include "m2560def.inc"
  2.  
  3. .def temp = r16
  4. .def counterP0 = r17
  5. .def counterP1 = r18
  6. .def leds = r19
  7. .def displayCount = r20
  8. .equ displayLength = 6
  9. .equ false = 0
  10. .equ true = 1
  11.  
  12.  
  13. .macro twoByteClear
  14.     ldi YL, low(@0)
  15.     ldi YH, high (@0)
  16.     clr temp
  17.     st Y+, temp
  18.     st Y, temp
  19. .endmacro
  20.  
  21. .dseg
  22. pattern0:       .byte 1
  23. pattern1:       .byte 1
  24. tempcounter:    .byte 2
  25. Timer1Counter:  .byte 1
  26. Timer3Counter:  .byte 1
  27. PB0Status:      .byte 1
  28. PB1Status:      .byte 1
  29.  
  30.  
  31. .cseg
  32. .org 0x0000
  33.     jmp RESET
  34. .org INT0addr
  35.     jmp PB0Press
  36. .org INT1addr
  37.     jmp PB1Press
  38.     jmp DEFAULT
  39. .org OVF1addr
  40.     jmp TIMER1OVF
  41.     jmp DEFAULT
  42. .org OVF0addr
  43.     jmp TIMER0OVF
  44. .org OVF3addr
  45.     jmp TIMER3OVF
  46.  
  47.  
  48. DEFAULT:
  49.     reti
  50.    
  51.  
  52. RESET:
  53.     ldi temp, high(RAMEND)
  54.     out sph, temp
  55.     ldi temp, low(ramend)
  56.     out spl, temp
  57.  
  58.     clr temp
  59.     out ddrd, temp                  //first two pins of port d ( RDx3 and RDx4 on the board) are mapped to input buttons; set port d as input
  60.     ser temp
  61.     out ddrc,temp                   //set port c as output
  62.  
  63.     clr temp
  64.     sts PB0Status, temp
  65.     sts PB1Status, temp
  66.  
  67.     clr temp
  68.     clr leds
  69.     clr counterP0
  70.     clr counterP1
  71.     clr displayCount
  72.  
  73.     sts pattern0, temp
  74.     sts pattern1, temp
  75.  
  76.     rjmp main
  77.  
  78. PB0Press:
  79.     push temp
  80.     in temp, sreg
  81.     push temp               //pushing sreg
  82.     push YL
  83.     push YH
  84.  
  85.     lds temp, PB0Status
  86.     cpi temp, 1
  87.     breq PB0Press_end
  88.  
  89.     ldi temp,1
  90.     sts PB0Status, temp
  91.  
  92.     cpi counterP0, 8        //check if pattern0 is full yet
  93.     breq PB0Press_end           //if it is, check pattern1
  94.     lds temp, pattern0      //otherwise, load pattern0
  95.     lsl temp                //left shift pattern to store most recent entry (0) as LSB
  96.     sts pattern0, temp
  97.     inc counterP0
  98.  
  99.     PB0Press_end:
  100.     pop YH
  101.     pop YL
  102.     pop temp                //popping sreg
  103.     out sreg, temp
  104.     pop temp   
  105.     reti
  106.  
  107. PB1Press:
  108.     push temp
  109.     in temp, sreg
  110.     push temp               //pushing sreg
  111.     push YL
  112.     push YH
  113.  
  114.     lds temp, PB1Status
  115.     cpi temp, 1
  116.     breq PB1Press_end
  117.  
  118.     ldi temp,1
  119.     sts PB1Status, temp
  120.  
  121.     cpi counterP0, 8        //check if pattern0 is full yet
  122.     breq PB1Press_end       //if it is, check pattern1
  123.     lds temp, pattern0      //otherwise, load pattern0
  124.     lsl temp                //left shift pattern to store most recent entry (0) as LSB
  125.     inc temp
  126.     sts pattern0, temp
  127.     inc counterP0
  128.  
  129.     PB1Press_end:
  130.     pop YH
  131.     pop YL
  132.     pop temp                //popping sreg
  133.     out sreg, temp
  134.     pop temp   
  135.     reti
  136.  
  137. Timer0OVF:
  138.     in temp, sreg
  139.     push temp
  140.     push yh
  141.     push yl
  142.     push XH
  143.     push XL
  144.  
  145.     cpi leds, 0x00                  //check if the leds are empty
  146.     brne doNothing                  //if they are not, do nothing
  147.    
  148.     cpi counterP0, 8                //check if the pattern0 is full
  149.     brne doNothing             
  150.     lds leds, pattern0              //if it is full, load it to leds
  151.     clr counterP0                   //clear counter P0, so it is ready for new input
  152.  
  153.     doNothing:
  154.  
  155.     lds XL,tempcounter
  156.     lds XH,tempcounter+1
  157.     adiw X,1
  158.  
  159.     cpi XL, low(7812)
  160.     ldi temp, high(7812)
  161.     cpc XH, temp
  162.     brne notASecond
  163.  
  164.     next:
  165.     cpi leds, 0x00                      //check there is a value loaded in the leds
  166.     breq noDisplay                      //if there isnt, then nothing will be displayed, keep counting
  167.     cpi displayCount, displayLength         //check if there have been previous displays
  168.     brge alreadyDisplayed               //if there is, clear display and continue to nodisplay
  169.     mov temp, displayCount                  //copy over to temp
  170.     andi temp, 0b00000001               //mask display to just the LSB
  171.     cpi temp, 0                         //check if the LSB is 0. if it is, number is even -> turn on
  172.     breq display                        //if it is , go to display to turn on leds
  173.     clr temp                            //otherwise, if the number is odd
  174.     out PORTC,temp                      //turn off leds
  175.     inc displayCount                    //incrments counter
  176.     rjmp NoDisplay
  177.  
  178.     display:                               
  179.     out PORTC, leds                     //turn on leds
  180.     inc displayCount                    //increment displayCOunt
  181.     rjmp noDisplay                      //proceed to clear tempcounter
  182.  
  183.     alreadyDisplayed:
  184.     clr displayCount
  185.     clr leds
  186.    
  187.     rjmp noDisplay
  188.  
  189.     noDisplay:
  190.  
  191.     twoByteClear tempcounter
  192.     rjmp epilogue
  193.  
  194.     notASecond:
  195.     sts tempcounter, XL
  196.     sts tempcounter+1,XH
  197.  
  198.     epilogue:
  199.     pop XL
  200.     pop XH
  201.     pop YL
  202.     pop YH
  203.     pop temp
  204.     out sreg,temp
  205.     reti
  206.  
  207. TIMER1OVF:                  //this timer will overfloew every ~30ms with a prescalar of 8 for 16-bit timer
  208.     push temp               //
  209.     in temp, sreg
  210.     push temp
  211.     push yh
  212.     push yl
  213.  
  214.     lds temp, PB0Status
  215.     cpi temp, 1
  216.     breq new501
  217.  
  218.     new501:
  219.         lds YL, Timer1Counter
  220.         ldi YH, 0
  221.         adiw Y,1
  222.         cpi YL, low(25)
  223.         ldi temp, high(25)
  224.         cpc temp, YH
  225.         brne NOt501
  226.  
  227.         clr temp
  228.         sts PB0Status, temp
  229.         sts Timer1Counter, temp
  230.         clr YL
  231.         clr YH
  232.         rjmp timer1end
  233.  
  234.     NOt501:
  235.         sts Timer1Counter, YL
  236.  
  237.     timer1end:
  238.     pop YL
  239.     pop YH
  240.     pop temp
  241.     out sreg, temp
  242.     pop temp
  243.     reti
  244.  
  245. TIMER3OVF:                  //this timer will overfloew every ~30ms with a prescalar of 8 for 16-bit timer
  246.     push temp               //
  247.     in temp, sreg
  248.     push temp
  249.     push yh
  250.     push yl
  251.  
  252.     lds temp, PB1Status
  253.     cpi temp, 1
  254.     breq new503
  255.  
  256.     new503:
  257.         lds YL, Timer3Counter
  258.         ldi YH, 0
  259.         adiw Y,1
  260.         cpi YL, low(25)
  261.         ldi temp, high(25)
  262.         cpc temp, YH
  263.         brne NOt503
  264.  
  265.         clr temp
  266.         sts PB1Status, temp
  267.         sts Timer3Counter, temp
  268.         clr YL
  269.         clr YH
  270.         rjmp timer3end
  271.  
  272.     NOt503:
  273.         sts Timer3Counter, YL
  274.  
  275.     timer3end:
  276.     pop YL
  277.     pop YH
  278.     pop temp
  279.     out sreg, temp
  280.     pop temp
  281.     reti
  282.  
  283. main:
  284.     twoByteClear tempCounter   
  285.  
  286.     ldi temp, 0b00000000
  287.     out tccr0a,temp
  288.     ldi temp,0b00000010
  289.     out tccr0b, temp
  290.     ldi temp, 1<< toie0
  291.     sts timsk0,temp
  292.  
  293.     ldi temp, 0b00000011
  294.     out eimsk, temp             //external interrupt mask register, 2 LSB are for int0 and int1. enabling this and global interrupts enables the interrupt pin
  295.     ldi temp, 0b00001010
  296.     sts eicra,temp              //enable falling edge, with ISCn1 = 1, ISCn0 = 0, for falling edge of INTn. eicra is for INT3:0. using sts as its a register
  297.    
  298.     ldi temp, 0b00000000    //counter in normal mode (UP), rolls over at
  299.     sts TCCR1A, temp        //maximum 16 bit value and restarts from bottom
  300.     ldi temp, 0b00000010    //overflow at 2^16 * 8/16 = 32768microseconds, with 8 prescalar
  301.     sts TCCR1B, temp
  302.     ldi temp, 1<<TOIE1      //enable timer1
  303.     sts timsk1, temp
  304.  
  305.     ldi temp, 0b00000000    //counter in normal mode (UP), rolls over at
  306.     sts TCCR3A, temp        //maximum 16 bit value and restarts from bottom
  307.     ldi temp, 0b00000010    //overflow at 2^16 * 8/16 = 32768microseconds, with 8 prescalar
  308.     sts TCCR3B, temp
  309.     ldi temp, 1<<TOIE3      //enable timer1
  310.     sts timsk3, temp
  311.  
  312.     ldi temp, true
  313.     sts PB0Status, temp
  314.     sei
  315.  
  316. end:
  317.     rjmp end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement