main.asm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. ;**************************************************************
  2. ;* This stationery serves as the framework for a *
  3. ;* user application. For a more comprehensive program that *
  4. ;* demonstrates the more advanced functionality of this *
  5. ;* processor, please see the demonstration applications *
  6. ;* located in the examples subdirectory of the *
  7. ;* Freescale CodeWarrior for the HC12 Program directory *
  8. ;**************************************************************
  9. ; Include derivative-specific definitions
  10. INCLUDE 'derivative.inc'
  11. XREF __SEG_END_SSTACK ; symbol defined by the linker for the end of the stack
  12. ; LCD References
  13. XREF init_LCD
  14. ; export symbols
  15. ; we use export 'Entry' as symbol. This allows us to
  16. ; reference 'Entry' either in the linker .prm file
  17. ; or from C/C++ later on
  18. XDEF Entry,_Startup,disp,CRGFLG
  19. ;==================== variables =================================
  20. ;password
  21. XDEF password,new_password,pw_prompt_shown,pw_check,pw_correct,pw_set,pw_incorrect,entered_password,pw_required
  22. ;keypad
  23. XDEF pressed_key,pressed_num,await_key_release,await_key_press
  24. ;menu
  25. XDEF menu_active,toast_timer,menu_option_num,menu_timer,display_timeout
  26. ;watering (menu option 1)
  27. XDEF is_watering,watering_timer,sprinkler_event,sprinkler_timer,sprinkler_arr_i
  28. ;temperature (menu option 2)
  29. XDEF show_temperature,fan_speed,circulation_count,dc_on
  30. ;wall clock (menu option 3)
  31. XDEF current_year,current_month,current_day,current_hour,current_minute,current_second
  32. XDEF display_time,wall_timer,clock_event,second_passed,ms_counter
  33. ;adjust clock (menu option 4)
  34. XDEF change_time
  35. ;reset password (menu option 5)
  36. XDEF pw_reset
  37. ;================== constants =================================
  38. ;ports and peripherals
  39. XDEF PSR_U,PDR_U,PORT_U,DDR_T,PORT_T,DDR_P,PORT_P,DDR_S,PORT_S
  40. ;keypad
  41. XDEF KVS,SEQUENCE
  42. ;stepper motor
  43. XDEF SPRINKLER_ARR
  44. ;===================== subroutines =============================
  45. ; Potentiometer References
  46. XREF display_string
  47. XREF pot_value
  48. ;subroutines
  49. XREF Password,Menu,Keypad,Water,Circulation,Wall,Growlights
  50. ;================= debugging ===================================
  51. XDEF word_out,byte_out
  52. ; ================= general notes =====================
  53. ; - routines should generally be asynchronous
  54. ; - need a way to marshall values into single characters
  55. ; like the pot. resistance display from lab
  56. ; - use two seperate routines, one for each song
  57. ; - set all the flags with the 1 ms RTI
  58. ; - reading the password asychronously will be a
  59. ; pain in the ass, maybe skip that
  60. ; - think about the last lab and skipping the await
  61. ;======================================================
  62. ;================ flow of the program =================
  63. ; 1. Prompt for the password. Nothing will happen until the password is set.
  64. ; 2. Acknowlege the password, wait, then show help, wait, then show menu
  65. ; 3. Show help, wait (press 0 to show help)
  66. ; 4. Show main menu, start air circulation, and start grow light control
  67. ;======================================================
  68. ;==================== main menu =======================
  69. ; 1. water the plants
  70. ; 2. show temperature (adjusts when the pot is turned)
  71. ; 3. show time
  72. ; 4. set time (passord protected)
  73. ; 5. change password (password protected)
  74. ;======================================================
  75. ; variable/data section
  76. my_variable: SECTION
  77. disp ds.b 33 ;the character string to output to the LCD (given)
  78. ;password variables
  79. password: ds.b 8 ;the byte array that stores the password
  80. entered_password ds.b 8 ;will be check against the password
  81. new_password ds.b 8 ;overwrite the old password with a new one
  82. pw_prompt_shown ds.b 1 ;only print the initial password prompt once
  83. pw_check ds.b 1 ;require the password before continueing
  84. pw_correct ds.b 1 ;check the correctness of the password
  85. pw_set ds.b 1 ;flag to fire after the password has been entered
  86. pw_incorrect ds.b 1 ;will determine if the password needs to be tried again
  87. pw_required ds.b 1 ;flag to set if a password is required to enter a screen
  88. ;keypad variables
  89. pressed_key ds.b 1 ;the keypad value in the LUT
  90. pressed_num ds.b 1 ;the numeral of the pressed key or the index of PRESSED in the LUT
  91. await_key_release ds.b 1 ;whether or not to await the keypad release
  92. await_key_press ds.b 1
  93. ;================== state variables =====================
  94. ; Primary UI
  95. menu_active ds.b 1 ;show the main menu
  96. toast_timer ds.w 1 ;(counter) used to delay the screen outputs so that the user can read them
  97. menu_option_num ds.b 1 ;which menu option is displayed
  98. menu_timer ds.w 1 ;timer to check the menu cycle
  99. display_timeout ds.b 1 ;(flag) to go back to the menu when an input is made
  100. ; menu item 1 (watering state)
  101. is_watering ds.b 1 ;flag that sets the state to watering (spin the motor, play the tune, and
  102. ;show "Watering in \nprogress..." on the LCD
  103. ;also one or more LED's should illuminate
  104. ;also the stepper motor should spin like a sprinkler
  105. ;also this should only run for a period of time
  106. watering_timer ds.w 1 ;(counter) to time the watering, stop after some time
  107. sprinkler_event ds.b 1 ;event to fire to spin stepper motor like a sprinkler
  108. sprinkler_timer ds.b 1 ;(counter) time delay for sprinkler
  109. sprinkler_arr_i ds.b 1 ;sprinkler array index
  110. ; menu item 2 (air circulation state, show temperature)
  111. show_temperature ds.b 1 ;display the current temperature
  112. fan_speed ds.b 1 ;reads the potentiometer
  113. circulation_count ds.b 1 ;counter to spin the DC motor
  114. dc_on ds.b 1 ;event pulse for DC motor
  115. ; menu item 3 (display time)
  116. display_time ds.b 1 ;show the time on the screen
  117. current_year ds.w 1 ;what year is it
  118. current_month ds.b 1 ;what month is it (a number)
  119. current_day ds.b 1 ;day of the month
  120. current_hour ds.b 1 ;hour of the day (military time)
  121. current_minute ds.b 1 ;...
  122. current_second ds.b 1 ;...
  123. wall_timer ds.w 1 ;timeout for the clock display
  124. clock_event ds.b 1 ;event that will fire every 4 milliseconds
  125. ms_counter ds.w 1 ;counter for the milliseconds
  126. second_passed ds.b 1 ;event that fires when a second has passed
  127. setting_wall ds.b 1 ;will flag when the clock is being set by the user
  128. ; menu item 4 (change time)
  129. change_time ds.b 1 ;the user has elected to change the date and the time
  130. ;all or nothing on this; everything must be entered
  131. ; menu item 5 (change password)
  132. pw_reset ds.b 1 ;flag to trigger that a password should be reset
  133. ;this should be done in an async manner when prompted by the user
  134. ;(in an emergency, this process should be synchronous)
  135. ;in order to not stop the air or watering or grow lights
  136. ; menu item 6 (plant seeds)
  137. ; menu option 7 (show plot)
  138. ; debugging
  139. word_out ds.w 1
  140. byte_out ds.b 1
  141. ;=============================== END state variables =====================================
  142. my_constant: SECTION
  143. ;ports
  144. DDR_U equ $26A
  145. PSR_U equ $26D
  146. PDR_U equ $26C
  147. PORT_U equ $268
  148. DDR_T equ $242 ;the DIP switches and DC motor
  149. PORT_T equ $240
  150. PORT_P equ $258 ;stepper motor
  151. DDR_P equ $25A
  152. DDR_S equ $24A ;LED's
  153. PORT_S equ $248
  154. ;keypad ;key value pairs from 4.3
  155. KVS dc.b $EB,$77,$7B,$7D,$B7,$BB,$BD,$D7,$DB,$DD,$E7,$ED,$7E,$BE,$DE,$EE
  156. ;given sequence from lab instructions (with trailing zero)
  157. SEQUENCE dc.b $70,$B0,$D0,$E0,$00
  158. ;stepper motor
  159. SPRINKLER_ARR dc.b $0A,$12,$14,$12,$14,$0C,$14,$0C
  160. MyCode: SECTION
  161. _Startup:
  162. Entry:
  163. lds #__SEG_END_SSTACK
  164. ;keypad stuff
  165. BSET DDR_U,#$F0 ;bits 1-3 in bits 4-7 out
  166. BSET PSR_U,#$F0 ;pins 1-3 pull up
  167. BSET PDR_U,#$0F ;pull up pins 1-3
  168. CLR await_key_release ;do not await by default
  169. MOVB #$FF,await_key_press;await presses by default
  170. ;LED stuff
  171. BSET DDR_S,#$FF ;all outputs on LED port
  172. MOVB #$00,PORT_S ;off initally
  173. ;switch and DC motor stuff
  174. MOVB #$08,DDR_T ;needs to be set for the motor to spin
  175. ;LCD stuff
  176. JSR init_LCD
  177. ;event loop stuff
  178. MOVB #$80,CRGINT ;enable real-time interrupts
  179. MOVB #$60,RTICTL ;set RTI interval to 4 ms; "#$40,RTICTL" would be 1 ms
  180. ;MOVB #$40,RTICTL
  181. CLI ;enable interrupts
  182. ;state variable stuff (password
  183. MOVB #$FF,password ;this password will never be correct, it needs to be changed
  184. MOVB #$FF,password+1
  185. MOVB #$FF,password+2
  186. MOVB #$FF,password+3
  187. MOVB #$FF,password+4
  188. MOVB #$FF,password+5
  189. MOVB #$FF,password+6
  190. MOVB #$FF,password+7
  191. CLR pw_set ;the password is not inititally set
  192. CLR pw_incorrect ;the password will not start out as incorrect because it hasn't been set
  193. CLR pw_correct
  194. ;menu stuff
  195. CLR menu_active ;menu is not inititally active
  196. MOVB #$01,menu_option_num;start on option '1'
  197. CLR menu_timer ;set the menu timer at '0' this will be incremented in the event loop
  198. ;watering and stepper motor stuff
  199. CLR is_watering ;watering
  200. CLR watering_timer ;duration of watering
  201. CLR sprinkler_event ;fired to the stepper motor
  202. CLR sprinkler_timer ;duration of delay among items of the stepper array sequence
  203. MOVB #$1E,DDR_P ;DDR setting for the stepper
  204. MOVB #$00,sprinkler_arr_i;start at the beginning of the array
  205. ;other menu items
  206. CLR show_temperature ;menu item
  207. CLR change_time ;menu item
  208. CLR pw_reset ;menu item
  209. ;circulation
  210. CLR fan_speed
  211. CLR circulation_count
  212. ;wall clock
  213. MOVW #1970,current_year ;UNIX time
  214. MOVB #1,current_month ;start on Jan. 1
  215. MOVB #1,current_day ;...
  216. CLR current_hour ;start at 00:00:00
  217. CLR current_minute ;...
  218. CLR current_second ;...
  219. CLR wall_timer ;the timer that sets whether or not the clock displays
  220. CLR clock_event ;event is not initially fired
  221. CLR second_passed ;event that will fire each time one second has passed
  222. CLR ms_counter ;count milliseconds in the event loop
  223. CLR display_time
  224. Init:
  225. ;run any password related things first
  226. JSR Password
  227. ;do nothing until the password is set
  228. LDAA pw_set
  229. CMPA #$FF
  230. BNE Init
  231. ;activate the menu after the password is set
  232. MOVB #$FF,menu_active
  233. ;hide evey other display element
  234. ;set the keypress to an impossible value
  235. ;this clears the value from the password
  236. ;before reading the keypress in the menu
  237. MOVB #$FF,pressed_num
  238. Main:
  239. ;go to the menu when the password is set
  240. JSR Menu
  241. ;call each subroutine
  242. ;subroutines are controlled by events fired in the RTI
  243. JSR Circulation
  244. JSR Water
  245. JSR Growlights; <- should be uncommented (using LED's for debugging)
  246. JSR Wall
  247. JSR Password
  248. ;read the keypad when the menu is showing
  249. ;make the keypad read not branch from the password leftover pressed value
  250. ;the keypad being the primary means of controlling the program,
  251. ;we have it running in main (for now)
  252. ;get user menu selection (do not await presses or releases)
  253. MOVB #$00,await_key_release
  254. MOVB #$00,await_key_press
  255. JSR Keypad
  256. ;return to the main loop
  257. BRA Main