Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.08 KB | None | 0 0
  1. EX2 - MAIN
  2. #include <avr/io.h>
  3. #ifdef _SIMULATE_
  4. #include "simAVRHeader.h"
  5. #endif
  6.  
  7. enum States {Initial, Wait, Inc, Dec, Reset} next_state;
  8.  
  9. void Tick() {
  10. switch (next_state) { //Transitions
  11. case Initial:
  12. next_state = Initial;
  13. break;
  14. case Reset:
  15. case Inc:
  16. case Dec:
  17. case Wait:
  18. if (PINA == 0x01) {
  19. next_state = Inc;
  20. break;
  21. }
  22. else if (PINA == 0x02) {
  23. next_state = Dec;
  24. break;
  25. }
  26. else if (PINA == 0x03) {
  27. next_state = Reset;
  28. break;
  29. }
  30. else {
  31. next_state = Wait;
  32. break;
  33. }
  34.  
  35. }
  36. switch (next_state) { //Actions
  37. case Initial:
  38. PORTC = 0x07;
  39. next_state = Wait;
  40. break;
  41. case Wait:
  42. next_state = Wait;
  43. break;
  44. case Inc:
  45. if (PORTC >= 9) {
  46. PORTC = 0x09;
  47. next_state = Wait;
  48. break;
  49. }
  50. else {
  51. PORTC = PORTC + 0x01;
  52. next_state = Wait;
  53. break;
  54. }
  55. case Dec:
  56. if (PORTC <= 0) {
  57. PORTC = 0x00;
  58. next_state = Wait;
  59. break;
  60. }
  61. else {
  62. PORTC = PORTC - 0x01;
  63. next_state = Wait;
  64. break;
  65. }
  66. case Reset:
  67. PORTC = 0x00;
  68. next_state = Wait;
  69. }
  70. }
  71.  
  72. int main(void) {
  73. /* Insert DDR and PORT initializations */
  74. DDRA = 0x00; PORTA = 0xFF;
  75. DDRC = 0xFF; PORTC = 0x00;
  76. next_state = Initial;
  77.  
  78. /* Insert your solution below */
  79. while (1) {
  80. Tick();
  81. }
  82. return 1;
  83. }
  84.  
  85. EX2 - TEST
  86. echo ======================================================\n
  87. echo Running all tests..."\n\n
  88.  
  89. test "inc once => PORTC: 0x08"
  90. set next_state = Initial
  91. setPINA 0x01
  92. continue 2
  93. expectPORTC 0x08
  94. checkResult
  95.  
  96. test "hold inc => PORTC: 0x09"
  97. set next_state = Initial
  98. setPINA 0x01
  99. continue 5
  100. expectPORTC 0x09
  101. checkResult
  102.  
  103. test "dec once => PORTC: 0x06"
  104. set next_state = Initial
  105. setPINA 0x02
  106. continue 2
  107. expectPORTC 0x06
  108. checkResult
  109.  
  110. test "hold dec => PORTC: 0x00"
  111. set next_state = Initial
  112. setPINA 0x02
  113. continue 10
  114. expectPORTC 0x00
  115. checkResult
  116.  
  117. test "inc, hold => PORTC: 0x08, state: Wait"
  118. set next_state = Initial
  119. setPINA 0x01
  120. continue 2
  121. setPINA 0x00
  122. continue 10
  123. expectPORTC 0x08
  124. expect next_state Wait
  125. checkResult
  126.  
  127. test "dec, hold => PORTC: 0x06, state: Wait"
  128. set next_state = Initial
  129. setPINA 0x02
  130. continue 2
  131. setPINA 0x00
  132. continue 10
  133. expectPORTC 0x06
  134. expect next_state Wait
  135. checkResult
  136.  
  137. test "press both => PORTC: 0x00"
  138. set next_state = Initial
  139. setPINA 0x03
  140. continue 3
  141. expectPORTC 0x00
  142. expect next_state Wait
  143. checkResult
  144.  
  145. # Report on how many tests passed/tests ran
  146. set $passed=$tests-$failed
  147. eval "shell echo Passed %d/%d tests.\n",$passed,$tests
  148. echo ======================================================\n
  149.  
  150. EX3 - MAIN
  151. #include <avr/io.h>
  152. #ifdef _SIMULATE_
  153. #include "simAVRHeader.h"
  154. #endif
  155.  
  156. enum Door_States {Initial, Wait, PPress, PRelease, YPress, YRelease, LockPress, LockRelease} next_state;
  157.  
  158. void Door_Tick() {
  159. switch (next_state) { //Transitions
  160. case Initial:
  161. next_state = Initial;
  162. break;
  163. case Wait:
  164. if (PINA == 0x04) {
  165. next_state = PPress;
  166. break;
  167. }
  168. else if (PINA == 0x80) {
  169. next_state = LockPress;
  170. break;
  171. }
  172. else {
  173. next_state = Wait;
  174. break;
  175. }
  176. case PPress:
  177. if (PINA == 0x04) {
  178. next_state = PPress;
  179. break;
  180. }
  181. else if (PINA == 0x00) {
  182. next_state = PRelease;
  183. break;
  184. }
  185. else {
  186. next_state = Wait;
  187. break;
  188. }
  189. case PRelease:
  190. if (PINA == 0x02) {
  191. next_state = YPress;
  192. break;
  193. }
  194. else if (PINA == 0x00) {
  195. next_state = PRelease;
  196. break;
  197. }
  198. else {
  199. next_state = Wait;
  200. break;
  201. }
  202. case YPress:
  203. if (PINA == 0x02) {
  204. next_state = YPress;
  205. break;
  206. }
  207. else if (PINA == 0x00) {
  208. next_state = YRelease;
  209. break;
  210. }
  211. else {
  212. next_state = Wait;
  213. break;
  214. }
  215. case YRelease:
  216. next_state = YRelease;
  217. break;
  218. case LockPress:
  219. if (PINA == 0x80) {
  220. next_state = LockPress;
  221. break;
  222. }
  223. else {
  224. next_state = LockRelease;
  225. break;
  226. }
  227. case LockRelease:
  228. next_state = Wait;
  229. break;
  230. }
  231. switch (next_state) { //Actions
  232. case Initial:
  233. PORTB = 0x01;
  234. next_state = Wait;
  235. break;
  236. case Wait:
  237. next_state = Wait;
  238. break;
  239. case PPress:
  240. next_state = PPress;
  241. break;
  242. case PRelease:
  243. next_state = PRelease;
  244. break;
  245. case YPress:
  246. next_state = YPress;
  247. break;
  248. case YRelease:
  249. PORTB = 0x01;
  250. next_state = Wait;
  251. break;
  252. case LockPress:
  253. PORTB = 0x00;
  254. next_state = LockPress;
  255. break;
  256. case LockRelease:
  257. PORTB = 0x00;
  258. next_state = LockRelease;
  259. break;
  260.  
  261. }
  262. }
  263.  
  264. int main(void) {
  265. /* Insert DDR and PORT initializations */
  266. DDRA = 0x00; PORTA = 0xFF;
  267. DDRB = 0xFF; PORTB = 0x00;
  268. DDRC = 0xFF; PORTC = 0x00;
  269. next_state = Initial;
  270.  
  271. /* Insert your solution below */
  272. while (1) {
  273. Door_Tick();
  274. PORTC = next_state;
  275. }
  276. return 1;
  277. }
  278.  
  279. EX3 - TEST
  280. echo ======================================================\n
  281. echo Running all tests..."\n\n
  282.  
  283. test "press nothing => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  284. set next_state = Initial
  285. setPINA 0x00
  286. continue 10
  287. expectPORTB 0x01
  288. expectPORTC 0x01
  289. expect next_state Wait
  290. checkResult
  291.  
  292. test "lock => PORTB: 0x00 Locked, PORTC: 1. Wait"
  293. set next_state = Initial
  294. setPINA 0x80
  295. continue 2
  296. setPINA 0x00
  297. continue 2
  298. expectPORTB 0x00
  299. expectPORTC 0x01
  300. checkResult
  301.  
  302. test "lock, #, Y => PORTB: 0x01 Unlocked , PORTC: 1. Wait"
  303. set next_state = Initial
  304. setPINA 0x80
  305. continue 2
  306. setPINA 0x00
  307. continue 2
  308. #locked
  309. setPINA 0x04
  310. continue 2
  311. setPINA 0x00
  312. continue 2
  313. #pound
  314. setPINA 0x02
  315. continue 2
  316. setPINA 0x00
  317. continue 2
  318. #y
  319. #unlocked
  320. expectPORTB 0x01
  321. expectPORTC 0x01
  322. expect next_state Wait
  323. checkResult
  324.  
  325. test "lock, #, X => PORTB: 0x00 Locked, PORTC: 1. Wait"
  326. set next_state = Initial
  327. setPINA 0x80
  328. continue 2
  329. setPINA 0x00
  330. continue 2
  331. #locked
  332. setPINA 0x04
  333. continue 2
  334. setPINA 0x00
  335. continue 2
  336. #pound
  337. setPINA 0x01
  338. continue 2
  339. #x
  340. expectPORTB 0x00
  341. expectPORTC 0x01
  342. expect next_state Wait
  343. checkResult
  344.  
  345. test "lock, #, # => PORTB: 0x00 Locked, PORTC: 1. Wait"
  346. set next_state = Initial
  347. setPINA 0x80
  348. continue 2
  349. setPINA 0x00
  350. continue 2
  351. #locked
  352. setPINA 0x04
  353. continue 2
  354. setPINA 0x00
  355. continue 2
  356. #pound
  357. setPINA 0x04
  358. continue 1
  359. #pound
  360. expectPORTB 0x00
  361. expectPORTC 0x01
  362. expect next_state Wait
  363. checkResult
  364.  
  365. test "lock, Y => PORTB: 0x00 Locked, PORTC: 1. Wait"
  366. set next_state = Initial
  367. setPINA 0x80
  368. continue 2
  369. setPINA 0x00
  370. continue 2
  371. #locked
  372. setPINA 0x20
  373. continue 2
  374. #y
  375. expectPORTB 0x00
  376. expectPORTC 0x01
  377. expect next_state Wait
  378. checkResult
  379.  
  380. test "lock, X => PORTB: 0x00 Locked, PORTC: 1. Wait"
  381. set next_state = Initial
  382. setPINA 0x80
  383. continue 2
  384. setPINA 0x00
  385. continue 2
  386. #locked
  387. setPINA 0x01
  388. continue 2
  389. #x
  390. expectPORTB 0x00
  391. expectPORTC 0x01
  392. expect next_state Wait
  393. checkResult
  394.  
  395. test "#YX => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  396. set next_state = Initial
  397. setPINA 0x07
  398. continue 2
  399. expectPORTB 0x01
  400. expectPORTC 0X01
  401. expect next_state Wait
  402. checkResult
  403.  
  404. test "#Y => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  405. set next_state = Initial
  406. setPINA 0x06
  407. continue 2
  408. expectPORTB 0x01
  409. expectPORTC 0X01
  410. expect next_state Wait
  411. checkResult
  412.  
  413. test "#X => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  414. set next_state = Initial
  415. setPINA 0x05
  416. continue 2
  417. expectPORTB 0x01
  418. expectPORTC 0X01
  419. expect next_state Wait
  420. checkResult
  421.  
  422. test "YX => PORTB: 0x00 Locked, PORTC: 1. Wait"
  423. set next_state = Initial
  424. setPINA 0x03
  425. continue 2
  426. expectPORTB 0x01
  427. expectPORTC 0X01
  428. expect next_state Wait
  429. checkResult
  430.  
  431. # Report on how many tests passed/tests ran
  432. set $passed=$tests-$failed
  433. eval "shell echo Passed %d/%d tests.\n",$passed,$tests
  434. echo ======================================================\n
  435.  
  436. EX4 - MAIN
  437. #include <avr/io.h>
  438. #ifdef _SIMULATE_
  439. #include "simAVRHeader.h"
  440. #endif
  441.  
  442. enum Door_States {Initial, Wait, PPress, PRelease, YPress, YRelease, LockPress, LockRelease} next_state;
  443.  
  444. void Door_Tick() {
  445. switch (next_state) { //Transitions
  446. case Initial:
  447. next_state = Initial;
  448. break;
  449. case Wait:
  450. if (PINA == 0x04) {
  451. next_state = PPress;
  452. break;
  453. }
  454. else if (PINA == 0x80) {
  455. next_state = LockPress;
  456. break;
  457. }
  458. else {
  459. next_state = Wait;
  460. break;
  461. }
  462. case PPress:
  463. if (PINA == 0x04) {
  464. next_state = PPress;
  465. break;
  466. }
  467. else if (PINA == 0x00) {
  468. next_state = PRelease;
  469. break;
  470. }
  471. else {
  472. next_state = Wait;
  473. break;
  474. }
  475. case PRelease:
  476. if (PINA == 0x02) {
  477. next_state = YPress;
  478. break;
  479. }
  480. else if (PINA == 0x00) {
  481. next_state = PRelease;
  482. break;
  483. }
  484. else {
  485. next_state = Wait;
  486. break;
  487. }
  488. case YPress:
  489. if (PINA == 0x02) {
  490. next_state = YPress;
  491. break;
  492. }
  493. else if (PINA == 0x00) {
  494. next_state = YRelease;
  495. break;
  496. }
  497. else {
  498. next_state = Wait;
  499. break;
  500. }
  501. case YRelease:
  502. next_state = YRelease;
  503. break;
  504. case LockPress:
  505. if (PINA == 0x80) {
  506. next_state = LockPress;
  507. break;
  508. }
  509. else {
  510. next_state = LockRelease;
  511. break;
  512. }
  513. case LockRelease:
  514. next_state = Wait;
  515. break;
  516. }
  517. switch (next_state) { //Actions
  518. case Initial:
  519. PORTB = 0x01;
  520. next_state = Wait;
  521. break;
  522. case Wait:
  523. next_state = Wait;
  524. break;
  525. case PPress:
  526. next_state = PPress;
  527. break;
  528. case PRelease:
  529. next_state = PRelease;
  530. break;
  531. case YPress:
  532. next_state = YPress;
  533. break;
  534. case YRelease:
  535. PORTB = (!PORTB & 0x03);
  536. next_state = Wait;
  537. break;
  538. case LockPress:
  539. PORTB = 0x00;
  540. next_state = LockPress;
  541. break;
  542. case LockRelease:
  543. PORTB = 0x00;
  544. next_state = LockRelease;
  545. break;
  546.  
  547. }
  548. }
  549.  
  550. int main(void) {
  551. /* Insert DDR and PORT initializations */
  552. DDRA = 0x00; PORTA = 0xFF;
  553. DDRB = 0xFF; PORTB = 0x00;
  554. DDRC = 0xFF; PORTC = 0x00;
  555. next_state = Initial;
  556.  
  557. /* Insert your solution below */
  558. while (1) {
  559. Door_Tick();
  560. PORTC = next_state;
  561. }
  562. return 1;
  563. }
  564.  
  565. EX4 - TEST
  566. echo ======================================================\n
  567. echo Running all tests..."\n\n
  568.  
  569. test "press nothing => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  570. set next_state = Initial
  571. setPINA 0x00
  572. continue 10
  573. expectPORTB 0x01
  574. expectPORTC 0x01
  575. expect next_state Wait
  576. checkResult
  577.  
  578. test "lock => PORTB: 0x00 Locked, PORTC: 1. Wait"
  579. set next_state = Initial
  580. setPINA 0x80
  581. continue 2
  582. setPINA 0x00
  583. continue 2
  584. expectPORTB 0x00
  585. expectPORTC 0x01
  586. checkResult
  587.  
  588. test "#, Y => PORTB: 0x00 Locked, PORTC 1. Wait"
  589. set next_state = Initial
  590. setPINA 0x04
  591. continue 2
  592. setPINA 0x00
  593. continue 2
  594. #pound
  595. setPINA 0x02
  596. continue 2
  597. setPINA 0x00
  598. continue 2
  599. #y
  600. expectPORTB 0x00
  601. expectPORTC 0x01
  602. expect next_state Wait
  603. checkResult
  604.  
  605. test "#, Y, #, Y => PORTB: 0x01 Unlocked, PORTC 1. Wait"
  606. set next_state = Initial
  607. setPINA 0x04
  608. continue 2
  609. setPINA 0x00
  610. continue 2
  611. #pound
  612. setPINA 0x02
  613. continue 2
  614. setPINA 0x00
  615. continue 2
  616. #y
  617. setPINA 0x04
  618. continue 2
  619. setPINA 0x00
  620. continue 2
  621. #pound
  622. setPINA 0x02
  623. continue 2
  624. setPINA 0x00
  625. continue 2
  626. #y
  627. expectPORTB 0x01
  628. expectPORTC 0x01
  629. expect next_state Wait
  630. checkResult
  631.  
  632. test "lock, #, Y => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  633. set next_state = Initial
  634. setPINA 0x80
  635. continue 2
  636. setPINA 0x00
  637. continue 2
  638. #locked
  639. setPINA 0x04
  640. continue 2
  641. setPINA 0x00
  642. continue 2
  643. #pound
  644. setPINA 0x02
  645. continue 2
  646. setPINA 0x00
  647. continue 2
  648. #y
  649. #unlocked
  650. expectPORTB 0x01
  651. expectPORTC 0x01
  652. expect next_state Wait
  653. checkResult
  654.  
  655. test "lock, #, X => PORTB: 0x00 Locked, PORTC: 1. Wait"
  656. set next_state = Initial
  657. setPINA 0x80
  658. continue 2
  659. setPINA 0x00
  660. continue 2
  661. #locked
  662. setPINA 0x04
  663. continue 2
  664. setPINA 0x00
  665. continue 2
  666. #pound
  667. setPINA 0x01
  668. continue 2
  669. #x
  670. expectPORTB 0x00
  671. expectPORTC 0x01
  672. expect next_state Wait
  673. checkResult
  674.  
  675. test "lock, #, # => PORTB: 0x00 Locked, PORTC: 1. Wait"
  676. set next_state = Initial
  677. setPINA 0x80
  678. continue 2
  679. setPINA 0x00
  680. continue 2
  681. #locked
  682. setPINA 0x04
  683. continue 2
  684. setPINA 0x00
  685. continue 2
  686. #pound
  687. setPINA 0x04
  688. continue 1
  689. #pound
  690. expectPORTB 0x00
  691. expectPORTC 0x01
  692. expect next_state Wait
  693. checkResult
  694.  
  695. test "lock, Y => PORTB: 0x00 Locked, PORTC: 1. Wait"
  696. set next_state = Initial
  697. setPINA 0x80
  698. continue 2
  699. setPINA 0x00
  700. continue 2
  701. #locked
  702. setPINA 0x20
  703. continue 2
  704. #y
  705. expectPORTB 0x00
  706. expectPORTC 0x01
  707. expect next_state Wait
  708. checkResult
  709.  
  710. test "lock, X => PORTB: 0x00 Locked, PORTC: 1. Wait"
  711. set next_state = Initial
  712. setPINA 0x80
  713. continue 2
  714. setPINA 0x00
  715. continue 2
  716. #locked
  717. setPINA 0x01
  718. continue 2
  719. #x
  720. expectPORTB 0x00
  721. expectPORTC 0x01
  722. expect next_state Wait
  723. checkResult
  724.  
  725. test "#YX => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  726. set next_state = Initial
  727. setPINA 0x07
  728. continue 2
  729. expectPORTB 0x01
  730. expectPORTC 0X01
  731. expect next_state Wait
  732. checkResult
  733.  
  734. test "#Y => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  735. set next_state = Initial
  736. setPINA 0x06
  737. continue 2
  738. expectPORTB 0x01
  739. expectPORTC 0X01
  740. expect next_state Wait
  741. checkResult
  742.  
  743. test "#X => PORTB: 0x01 Unlocked, PORTC: 1. Wait"
  744. set next_state = Initial
  745. setPINA 0x05
  746. continue 2
  747. expectPORTB 0x01
  748. expectPORTC 0X01
  749. expect next_state Wait
  750. checkResult
  751.  
  752. test "YX => PORTB: 0x00 Locked, PORTC: 1. Wait"
  753. set next_state = Initial
  754. setPINA 0x03
  755. continue 2
  756. expectPORTB 0x01
  757. expectPORTC 0X01
  758. expect next_state Wait
  759. checkResult
  760.  
  761. # Report on how many tests passed/tests ran
  762. set $passed=$tests-$failed
  763. eval "shell echo Passed %d/%d tests.\n",$passed,$tests
  764. echo ======================================================\n
  765.  
  766. EX5 - MAIN
  767. #include <avr/io.h>
  768. #ifdef _SIMULATE_
  769. #include "simAVRHeader.h"
  770. #endif
  771.  
  772. enum Door_States {Initial, Wait, Get, Check} next_state;
  773.  
  774. void Door_Tick() {
  775. switch (next_state) { //Transitions
  776. case Initial:
  777. next_state = Initial;
  778. break;
  779. case Get:
  780. scanf("%c", comb);
  781. scanf("%c", comb);
  782. scanf("%c", comb);
  783. scanf("%c", comb);
  784. next_state = Check;
  785. break;
  786. case Check:
  787. }
  788.  
  789. switch (next_state) { //Actions
  790. case Initial:
  791. PORTB = 0x01;
  792. next_state = Wait;
  793. break;
  794. case Get:
  795. case Check:
  796. }
  797.  
  798. int main(void) {
  799. /* Insert DDR and PORT initializations */
  800. DDRA = 0x00; PORTA = 0xFF;
  801. DDRB = 0xFF; PORTB = 0x00;
  802. DDRC = 0xFF; PORTC = 0x00;
  803.  
  804. int comb[4];
  805. unsigned char i = 0;
  806.  
  807.  
  808.  
  809. /* Insert your solution below */
  810. while (1) {
  811.  
  812. }
  813. return 1;
  814. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement