Advertisement
Guest User

Untitled

a guest
May 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. .include "m16def.inc"
  2. .def led=r16
  3. .def intr_flag=r22
  4. .def potIndicator=r23
  5. .def potL=r18
  6. .def potH=r19
  7. .def temp=r20
  8. .def sreg_handler=r21
  9. .def input=r8
  10. .def seconds=r9
  11. .def compL=r24
  12. .def compH=r25
  13.  
  14. .def TEMP1=R26
  15. .def TEMP2=r27
  16. .def TEMP3=r28
  17.  
  18. .cseg
  19.  
  20. .org $00
  21. jmp RESET
  22.  
  23.  
  24. .org $00C
  25. jmp TIM_COMPA
  26.  
  27. .org $010
  28. jmp TIM_OVF1
  29.  
  30. .org $01C
  31. jmp ADC_CONV_INTR
  32.  
  33. .org $029
  34.  
  35. TIM_COMPA:
  36. in sreg_handler, SREG
  37. ;7 seconds are finally completed
  38. ;stop timer
  39. ori intr_flag, $02 ;set bit 1 to 1
  40. out SREG, sreg_handler
  41. reti
  42.  
  43. TIM_OVF1:
  44. in sreg_handler, SREG
  45. ;set TCCR1B register to 00001100
  46. ;make it equal to zero after compare OCR1A match-> NOW DO SO
  47. ldi temp, $0C ;normally $0C
  48. out TCCR1B, temp
  49.  
  50. ;fix TIFR manually, FLAGS ARE RESET BY BEING SET TO ONE!
  51. ldi temp, $10 ;00010000
  52. out TIFR, temp
  53.  
  54. ;now enable OCR1A interrupt timer
  55. ldi temp, $10 ;00010000
  56. out TIMSK, temp
  57. out SREG, sreg_handler
  58. reti
  59.  
  60.  
  61.  
  62.  
  63. ADC_CONV_INTR:
  64. in sreg_handler, SREG
  65. in potH, ADCH
  66. com potH
  67. out PORTB, potH
  68. com potH
  69. call DELAY_FOR_BUTTONS
  70. in potL, ADCL
  71. com potL
  72. out PORTB, potL
  73. com potL
  74. call DELAY_FOR_BUTTONS
  75. in temp, ADMUX
  76. com temp
  77. out PORTB, temp
  78. call DELAY_FOR_BUTTONS
  79. ori intr_flag, $01
  80. out SREG, sreg_handler
  81. reti
  82.  
  83.  
  84. RESET:
  85. ;initialize stack pointer
  86. ldi R16, low(RAMEND)
  87. out SPL, R16
  88. ldi R16, high(RAMEND)
  89. out SPH, R16
  90. ;set PORTB output
  91. ori r20, $FF
  92. out DDRB, r20
  93. out PORTB, r20
  94. ;set PORTD input
  95. andi r20, $00
  96. out DDRD, r20
  97. com r20
  98. out PORTD, r20
  99. ;set PORTA input
  100. andi r20, $00
  101. out DDRA, r20
  102. com r20
  103. out PORTA, r20
  104.  
  105. ;clear timer
  106. clr temp
  107. out TCNT1H, temp
  108. out TCNT1L, temp
  109.  
  110. ;enable interrupts
  111. sei
  112.  
  113. ldi temp, 0x88
  114. out ADCSRA,temp ;10101111=88
  115.  
  116. ldi temp, $c1 ;11000001 for iner ref voltage 2.56 and pot2
  117. out ADMUX, temp
  118.  
  119. clr seconds
  120. inc seconds
  121. inc seconds
  122. inc seconds
  123. inc seconds
  124.  
  125. clr intr_flag
  126.  
  127. remain2:
  128. sbi ADCSRA, 6
  129.  
  130.  
  131. remain1:
  132. sbrs intr_flag,0
  133. rjmp remain1
  134.  
  135. clr intr_flag
  136. in led, ADCSRA
  137. com led
  138. mini:
  139. in input, PIND
  140. sbrc input, 0 ;START switch
  141. rjmp mini
  142. dec seconds
  143. brne remain2
  144.  
  145.  
  146.  
  147. ;wait for START switch
  148. START:
  149. in input, PIND
  150. sbrc input, 0 ;START switch
  151. rjmp START
  152. clr intr_flag
  153.  
  154.  
  155.  
  156. ;rjmp Y1_Stage_preparation
  157. ser led
  158.  
  159.  
  160. ;Operation On LED-OPEN LED7
  161. clr temp
  162. bst temp,0
  163. bld led,7
  164. out PORTB, led
  165.  
  166.  
  167. B1_Prep:
  168. ;intializations
  169. clr potIndicator
  170. ;enable adc
  171.  
  172. ;clear flag
  173. clr intr_flag
  174.  
  175.  
  176.  
  177.  
  178. ;******************
  179. rjmp A1_Prep
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. ;enable adc for B1-pot2
  189. ldi temp, $c1 ;11000001 for iner ref voltage 2.56 and pot2
  190. out ADMUX, temp
  191. sbi ADCSRA,6 ;start conversion
  192.  
  193. B1_Stage:
  194.  
  195. ;checking STOP_HANDLER
  196. in input, PIND
  197. sbrs input,7 ;if stop not pressed=1 skip else go back
  198. rjmp STOP_HANDLER
  199.  
  200. sbrs intr_flag,0
  201. rjmp B1_Stage
  202. call StageB1B3Handler
  203.  
  204. tst potIndicator ;code 1 means it is not low yet
  205. brne B1_Stage
  206.  
  207. ;TEMPORAL CHANGE
  208. clr temp
  209. bst temp,0
  210. bld led,6
  211. out PORTB, led
  212.  
  213. mini_wait:
  214. in input, PIND
  215. sbrc input, 0 ;START switch
  216. rjmp mini_wait
  217.  
  218. B3_prep:
  219. ;clear flag
  220. clr intr_flag
  221. clr potIndicator
  222. ;enable adc for B3-pot4
  223. ldi temp, $c3 ;11000100 for iner ref voltage 2.56 and pot2
  224. out ADMUX, temp
  225. sbi ADCSRA,6 ;start conversion
  226.  
  227. B3_Stage:
  228.  
  229. ;checking STOP_HANDLER
  230. in input, PIND
  231. sbrs input,7 ;if stop not pressed=1 skip else go back
  232. rjmp STOP_HANDLER
  233.  
  234. sbrs intr_flag,0
  235. rjmp B3_Stage
  236. call StageB1B3Handler
  237.  
  238. tst potIndicator ;code 1 means there is error
  239. brne B3_Stage
  240.  
  241. ;TEMPORAL CHANGE
  242. ser temp
  243. bst temp,0
  244. bld led,6
  245. out PORTB, led
  246.  
  247.  
  248.  
  249. A1_Prep:
  250. ;clear flag
  251. clr intr_flag
  252. clr potIndicator
  253. ;enable adc for B3-pot4
  254. ldi temp, $c0 ;11000001 for iner ref voltage 2.56 and pot2
  255. out ADMUX, temp
  256. sbi ADCSRA,6 ;start conversion
  257.  
  258. A1_Stage:
  259.  
  260. ;checking STOP_HANDLER
  261. in input, PIND
  262. sbrs input,7 ;if stop not pressed=1 skip else go back
  263. rjmp STOP_HANDLER
  264.  
  265. sbrs intr_flag,0
  266. rjmp A1_Stage
  267. call StageA1Handler
  268.  
  269. tst potIndicator ;code 1 means there is error
  270. brne A1_Stage
  271.  
  272.  
  273.  
  274. ;A1-OPEN LED1
  275. clr temp
  276. bst temp,0
  277. bld led,1
  278. out PORTB, led
  279.  
  280. clr intr_flag
  281. clr potIndicator
  282.  
  283. Y1_Press_Stage:
  284. ;Check sw1-Y1
  285. in input, PIND
  286. sbrc input, 1
  287. rjmp Y1_Press_Stage
  288. ;Y1-OPEN LED5
  289. clr temp
  290. bst temp,0
  291. bld led,5
  292. out PORTB, led
  293.  
  294. ;open M2
  295. call M2On
  296.  
  297.  
  298. Acceleration_stage_preparation:
  299.  
  300. ;LED 7,5,4,1
  301. ldi led, $4D
  302. out PORTB, led
  303.  
  304. call DELAY_FOR_BUTTONS
  305.  
  306. clr potIndicator
  307. clr intr_flag
  308.  
  309.  
  310. ;prepare OCR1A for later
  311. ldi temp, $AB ;(decimal) 43,839
  312. out OCR1AH, temp
  313. ldi temp, $3F
  314. out OCR1AL, temp
  315.  
  316. ;first enable only overflow interrupt
  317. ldi temp, $04 ;00000100
  318. out TIMSK, temp
  319.  
  320. ;begin timer,
  321. ;(make it equal to zero after compare OCR1A match)->
  322. ;NOT HERE, IT MAKES IT ZERO
  323. ;IT NEVER MAKES IT TO THE OVF INTERRUPT
  324. ldi temp, $04 ;00000100 normally $04
  325. out TCCR1B, temp
  326.  
  327. Acceleration_stage: ;acceleration stage
  328.  
  329. sbrc intr_flag, 1
  330. rjmp Y1_Stage_preparation
  331.  
  332. in input, PIND
  333.  
  334. sbrs input, 7 ;STOP switch
  335. rjmp STOP_HANDLER1
  336.  
  337. sbrs input, 5 ;Q2 switch
  338. rjmp Error_Handler1
  339.  
  340. rjmp Acceleration_stage
  341.  
  342. Y1_Stage_preparation:
  343.  
  344. ;LED 7,6,5,4,2,1 00001001
  345. ldi led, $09
  346. out PORTB, led
  347.  
  348. call DELAY_FOR_BUTTONS
  349.  
  350. call Reset_Timer_configuration
  351.  
  352.  
  353. clr potIndicator
  354.  
  355. ;important to be done AFTER STOPPING ANY CONVERSION THAT IS CURRENTLY TAKING PLACE
  356. clr intr_flag
  357.  
  358. ;start first conversion
  359. ldi temp, $C0 ;choose A1
  360. out ADMUX, temp
  361. sbi ADCSRA, 6
  362.  
  363. Y1_Stage:
  364.  
  365. sbrc intr_flag, 0
  366. call ADC_HANDLER_Y1
  367.  
  368. ;it is important to check potIndicator
  369. ;for code 2 right after return.
  370. ;This way, we are certain that right after
  371. ;the B2 is high, the Y2 is immediately activated
  372. ;and the conversion for pot A1 is done at the same time.
  373. ;The B2 becoming full must be immediately noticed
  374. sbrc potIndicator, 1 ;code 2 means move to the next stage
  375. rjmp Y2_Stage_preparation
  376.  
  377.  
  378. sbrc potIndicator, 0 ;code 1 means there is error
  379. rjmp Error_Handler
  380.  
  381. in input, PIND
  382.  
  383. sbrs input, 7 ;STOP switch
  384. rjmp STOP_HANDLER
  385.  
  386. sbrs input, 5 ;Q2 switch
  387. rjmp Error_Handler
  388.  
  389. sbrs input, 4 ;Q1 switch
  390. rjmp Error_Handler
  391.  
  392. ;**************************
  393. ;CAN I ACTUALLY SWITCH?
  394. ;sbrs input, 2 ; Y2 switch
  395. ;rjmp Y2_sudden_switch
  396.  
  397. rjmp Y1_Stage
  398.  
  399. Y2_sudden_switch:
  400. ;configuration for AD-CONVERTER
  401.  
  402.  
  403. ;important to be done AFTER STOPPING ANY CONVERSION THAT IS CURRENTLY TAKING PLACE
  404. clr intr_flag ;only the timer may set a different intr_flag, so the only possible bit open is the 0
  405. ;therefore i can clear it safely
  406.  
  407.  
  408. ;start first conversion
  409. ldi temp, $C0 ;choose A1
  410. out ADMUX, temp
  411. sbi ADCSRA, 6
  412.  
  413.  
  414. Y2_Stage_preparation:
  415.  
  416.  
  417. ;LED 7,6,4,3,2,1 00100001
  418. ldi led, $21
  419. out PORTB, led
  420.  
  421. clr potIndicator
  422.  
  423. ;*********do not keep, only now
  424. ;start first conversion
  425. ldi temp, $C0 ;choose A1
  426. out ADMUX, temp
  427. sbi ADCSRA, 6
  428. ;sbrs input, 2 ; Y2 switch
  429. ;rjmp Y2_Stage_preparation
  430.  
  431.  
  432. Y2_Stage:
  433.  
  434. sbrc intr_flag, 0
  435. call ADC_HANDLER_Y2
  436.  
  437. ;it is important to check potIndicator
  438. ;for code 2 right after return.
  439. ;This way, we are certain that right after
  440. ;the B2 is high, the Y2 is immediately activated
  441. ;and the conversion for pot A1 is done at the same time.
  442. ;The B2 becoming full must be immediately noticed
  443. sbrc potIndicator, 1 ;code 2 means move to the next stage
  444. rjmp STOP_HANDLER
  445.  
  446.  
  447. sbrc potIndicator, 0 ;code 1 means there is error
  448. rjmp Error_Handler
  449.  
  450. in input, PIND
  451.  
  452. sbrs input, 7 ;STOP switch
  453. rjmp STOP_HANDLER
  454.  
  455. sbrs input, 5 ;Q2 switch
  456. rjmp Error_Handler
  457.  
  458. sbrs input, 4 ;Q1 switch
  459. rjmp Error_Handler
  460.  
  461. rjmp Y2_Stage
  462.  
  463.  
  464. Error_Handler1:
  465. call Reset_Timer_configuration
  466. Error_Handler:
  467. ldi led, $FE ;11111110 led 0 on
  468. out PORTB, led
  469.  
  470. ;set PORTC output
  471. ;ori temp, $FF
  472. ;out DDRB, temp
  473. ;out PORTC, led
  474.  
  475. ErrorAck:
  476. in input, PIND
  477. sbrc input,6
  478. rjmp ErrorAck
  479.  
  480. StartErrorTimer:
  481. ;prepare OCR1A=4*10^6/64=62500=$3D09
  482. ldi temp, $F4
  483. out OCR1AH,temp
  484. ldi temp, $24
  485. out OCR1AL,temp
  486.  
  487. ;now enable OCR1A interrupt timer
  488. ldi temp, $10 ;00010000
  489. out TIMSK, temp
  490. ;start the counter and set prescaler
  491. ldi temp, $0B ;for now, for speed, normally it is $0B ;00001011
  492. out TCCR1B, temp
  493.  
  494. clr seconds
  495. ;wait for ack
  496. loopBlink:
  497.  
  498. sbrc intr_flag, 1
  499. call BLINK
  500.  
  501. ;check STOP_HANDLER
  502. in input, PIND
  503. sbrc input,7 ;if not pressed=1 skip wait for stop switch
  504. rjmp loopBlink
  505.  
  506. call Reset_Timer_configuration
  507. ser temp
  508. out PORTB, temp
  509. jmp START
  510.  
  511. STOP_HANDLER1:
  512. call Reset_Timer_configuration
  513. STOP_HANDLER:
  514. ser temp
  515. out PORTB, temp
  516. jmp START
  517.  
  518.  
  519. StageB1B3Handler:
  520. clr potIndicator
  521. tst potH ;chcek if potH=0
  522. breq ContB ;yes go to cont
  523. rjmp ErrorB ;no->go to ErrorB
  524. ContB:
  525. cpi potL, $1F ;16 ;check if lower tahn min
  526. brlo ContB_2 ;yes go to cont2
  527. ErrorB:
  528. ldi potIndicator, $01
  529. clr intr_flag
  530. sbi ADCSRA,6 ;start conversion
  531. ContB_2:
  532. ret
  533.  
  534. StageA1Handler:
  535. clr potIndicator
  536. cpi potH, $02
  537. brsh ContA1
  538. ldi potIndicator, $01
  539. clr intr_flag
  540. sbi ADCSRA,6 ;start conversion
  541. ContA1:
  542. ret
  543.  
  544. M2On:
  545. in input, PIND
  546. sbrs input,5 ; if its pressed go somewhere- if not pressed clear to go
  547. rjmp Error_Handler
  548. ;OPEN LED4
  549. clr temp
  550. bst temp,0
  551. bld led,4 ;this way previous values are not lost
  552. out PORTB, led ;open LED4
  553. ret
  554.  
  555. ADC_HANDLER_A1:
  556. cbr intr_flag, $01 ;bit 1
  557. tst potH
  558. brne exit_ADC_HANDLER_A1
  559. cpi potL, 0x10 ;16 A1 is the silo that has been measured. If its is low, error!
  560. brsh exit_ADC_HANDLER_A1
  561. ori potIndicator, $01 ;bit 0 of register, indicates problem at A1
  562. exit_ADC_HANDLER_A1:
  563. sbi ADCSRA, 6 ;begin conversion once more
  564. ret
  565.  
  566. ADC_HANDLER_Y1:
  567. in temp, ADMUX
  568. cpi temp, $C0
  569. brne potB2_check
  570. ;load B2 to ADMUX
  571. ldi temp, $C2
  572. out ADMUX, temp
  573. ;the conversion will be initialized at stage4 handler
  574. call ADC_HANDLER_A1
  575. ret
  576. potB2_check:
  577. cbr intr_flag, $01 ;bit 1
  578. cpi potH, $02
  579. brlo exit_adc_handler_stage_5 ;B2 is not full yet
  580. ori potIndicator, $02 ;bit 1 of register, indicates B2 is full
  581. exit_adc_handler_stage_5:
  582. ;load A1 to ADMUX
  583. ldi temp, $C0
  584. out ADMUX, temp
  585. sbi ADCSRA, 6 ;begin conversion once more
  586. ret
  587.  
  588. ADC_HANDLER_Y2:
  589. in temp, ADMUX
  590. cpi temp, $C0
  591. brne potB4_check
  592. ;load B4 to ADMUX
  593. ldi temp, $C4
  594. out ADMUX, temp
  595. ;the conversion will be initialized at stage4 handler
  596. call ADC_HANDLER_A1
  597. ret
  598. potB4_check:
  599. cbr intr_flag, $01 ;bit 1
  600. cpi potH, $02
  601. brlo exit_adc_handler_stage_6 ;B4 is not full yet
  602. ori potIndicator, $02 ;bit 1 of register, indicates B2 is full
  603. exit_adc_handler_stage_6:
  604. ;load A1 to ADMUX
  605. ldi temp, $C0
  606. out ADMUX, temp
  607. sbi ADCSRA, 6 ;begin conversion once more
  608. ret
  609.  
  610. Reset_Timer_configuration:
  611. cli
  612. clr temp
  613. out TCCR1B, temp
  614. out TIMSK, temp
  615. out TCNT1L, temp
  616. out TCNT1H, temp
  617. ;include the clearing of any flags
  618. ldi temp, $14;00010100 clear OCR1A & TIMOVF1 flag
  619. out TIFR, temp
  620. cbr intr_flag, $02 ;clear bit 1
  621. sei
  622. ret
  623.  
  624. Reset_ADC_configuration:
  625. cli
  626. clr temp
  627. out ADMUX, temp
  628. ;include the clearing of flags
  629. sbi ADCSRA, 4 ;clear the interrupt flag in case it has risen
  630. cbr intr_flag, $01 ;clear bit 0, that corresponds to the ADC interrupt
  631. clr potIndicator
  632. sei
  633. ret
  634.  
  635.  
  636. BLINK:
  637. cbr intr_flag, $02 ;clear bit 1
  638. ;1 second is completed
  639. inc seconds
  640. sbrc seconds,0 ;an peritos sbise
  641. call blink0Off
  642. sbrs seconds, 0 ;an artios anapse
  643. call blink0On
  644. ret
  645.  
  646. blink0On:
  647. clr temp
  648. bst temp,0
  649. bld led,0
  650. out PORTB, led
  651. ret
  652.  
  653. blink0Off:
  654. clr temp
  655. com temp
  656. bst temp,0
  657. bld led,0
  658. out PORTB, led
  659. ret
  660.  
  661.  
  662.  
  663. DELAY_FOR_BUTTONS:
  664. clr TEMP3
  665. clr TEMP2
  666. clr TEMP1
  667. ;1953/256=7
  668. ;outside here 256 ;1 counter
  669. ;outside here 7;+ 1 counter
  670. Start_mini_delay1:
  671. Start_mini_delay2:
  672. Start_mini_delay3:;ALL THIS I WANT TO HAPPEN 1953 TIMES
  673. nop
  674. nop
  675. nop
  676. nop
  677. inc TEMP3
  678. cpi TEMP3, $FF
  679. brne Start_mini_delay3
  680. clr TEMP3
  681. inc TEMP2
  682. cpi TEMP2, $FF
  683. brne Start_mini_delay2
  684. clr TEMP2
  685. inc TEMP1
  686. cpi TEMP1, $14
  687. brne Start_mini_delay1
  688. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement