Advertisement
Guest User

8051 labs

a guest
May 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. MY_SECOND_TIMER SEGMENT CODE
  2.  
  3. CSEG AT 0
  4. JMP startC
  5.  
  6. RSEG MY_SECOND_TIMER
  7. startA:
  8. MOV R3, #0h
  9. MOV R0, #20
  10. DELAY:
  11. MOV TMOD,#00000001B // Sets Timer 0 to MODE1 (16 bit timer)
  12. MOV TH0,#76 // Loads TH0 register with 76
  13. MOV TL0,#01 // LOads TL0 register with 01
  14. SETB TR0 // Starts the Timer 0
  15. HERE:
  16. JNB TF0,HERE // Loops here until TF0 is set (ie;until roll over)
  17. CLR TR0 // Stops Timer 0
  18. CLR TF0 // Clears TF0 flag
  19. DJNZ R0,DELAY
  20. INC R3
  21. MOV P2,R3
  22. SJMP startA
  23.  
  24.  
  25. //Frequency 11.0592MHz=12 pules
  26. //1 clock pulse =11.0592MHz/12
  27. //F =0.921 MHz
  28. //Time delay=1/F
  29. //T=1/0.92MHz
  30. //T=1.080506 us (for ‘1’ cycle)
  31. //1000us=1MS
  32. //1000ms=1sec
  33.  
  34. // 1 second delay = 1000000/1.080806 ~= 925235 cycles
  35. // but timer can count 65535 cycles max :(
  36. // so grab 50ms and iterate it 20 times
  37. //
  38.  
  39.  
  40. startB:
  41. MOV R3, #0
  42. MOV R0, #2
  43. MOV P2, #00h
  44. wait:
  45. MOV A,P1
  46. CJNE A,#11111110B,wait
  47. //CJNE - Compare and Jump if not Equal
  48. //JNZ wait // Loops here until P1.0 is set
  49. //JZ - Jump if Accumulator Zero
  50. //JNZ - Jump if Accumulator not Zero
  51.  
  52. DELAYB:
  53. MOV TMOD,#00000001B // Sets Timer 0 to MODE1 (16 bit timer)
  54. MOV TH0,#76 // Loads TH0 register with 76
  55. MOV TL0,#01 // LOads TL0 register with 01
  56. SETB TR0 // Starts the Timer 0
  57. HEREB:
  58. JNB TF0,HEREB // Loops here until TF0 is set (ie;until roll over)
  59. CLR TR0 // Stops Timer 0
  60. CLR TF0 // Clears TF0 flag
  61. DJNZ R0,DELAYB
  62. INC R3
  63.  
  64.  
  65. MOV A,P1
  66. CJNE A,#11111110B,startB
  67. // JNZ startB // if button is clicked continue set diod if not, start again
  68.  
  69. MOV P2,R3
  70. SJMP DELAYB
  71.  
  72.  
  73. startC:
  74. MOV R3, #0
  75. MOV R0, #2
  76. MOV P2, #00h
  77.  
  78. checkButtonBeforeStart:
  79. MOV A,P1
  80. CJNE A,#11111110B,checkButtonBeforeStart
  81.  
  82. //CJNE - Compare and Jump if not Equal
  83. //JNZ wait // Loops here until P1.0 is set
  84. //JZ - Jump if Accumulator Zero
  85. //JNZ - Jump if Accumulator not Zero
  86.  
  87. DELAYC:
  88. MOV TMOD,#00001001B // Sets Timer 0 to MODE1 (16 bit timer)
  89. MOV TH0,#76 // Loads TH0 register with 76
  90. MOV TL0,#01 // LOads TL0 register with 01
  91. SETB TR0 // Starts the Timer 0
  92.  
  93. HEREC:
  94. MOV A,P1
  95. CJNE A,#11111111B,settimeron
  96. JMP settimeroff
  97. settimeron:
  98. MOV P3, #00000100B
  99. JMP go
  100. settimeroff:
  101. MOV P3, #00000000B
  102. go:
  103. JNB TF0,HEREC // Loops here until TF0 is set (ie;until roll over)
  104. CLR TR0 // Stops Timer 0
  105. CLR TF0 // Clears TF0 flag
  106. DJNZ R0,DELAYC
  107.  
  108. INC R3
  109. MOV P2, R3
  110. JMP checkButtonBeforeStart
  111. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement