Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.68 KB | None | 0 0
  1. /*#include "address_mapping.h"
  2. #include "assemblyImplementations.h"
  3.  
  4. // Assignment 3.(6, 7, 8)
  5. int main(){
  6. *SWITCHES_CONTROL=0xFFFF;
  7. *LED_CONTROL=0;
  8. while (1==1)
  9. //process_switches();
  10. //process_switches_left_right_mirror();
  11. process_switches_count();
  12. }*/
  13.  
  14. /*#include "displays.h"
  15. #include "address_mapping.h"
  16. #include "buttons.h"
  17. #include "interrupt_controller.h"
  18. #include "timers.h"
  19. #include "microBlaze.h"
  20.  
  21. unsigned int currentButtonsState = 0;
  22. unsigned int prevButtonsState = 0;
  23. unsigned int blinker = 0b0001000;
  24. unsigned int blinkerOnOff = 0;
  25. unsigned int application_state = 0;
  26. unsigned char index_pos = 0;
  27.  
  28. // Assignment 4.1
  29. int main() {
  30. resetDisplays();
  31. initTimers();
  32. initController();
  33. enableMicroBlazeInterrupts();
  34. enableTimers();
  35.  
  36. while (1 == 1) {
  37. *LED_DATA = 1 << application_state;
  38.  
  39. switch (application_state) {
  40. case 0:
  41. if (currentButtonsState != prevButtonsState) {
  42. if (((currentButtonsState & MIDDLE)!= (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
  43. application_state++;
  44. if (((currentButtonsState & LEFT) != (prevButtonsState & LEFT)) && ((currentButtonsState & LEFT) != 0))
  45. if (index_pos< 7)
  46. index_pos++;
  47. if (((currentButtonsState & RIGHT) != (prevButtonsState & RIGHT)) && ((currentButtonsState & RIGHT) != 0))
  48. if (index_pos > 0)
  49. index_pos--;
  50. }
  51. break;
  52. case 1:
  53. if (currentButtonsState != prevButtonsState) {
  54. if (((currentButtonsState & MIDDLE)!= (prevButtonsState & MIDDLE))&& ((currentButtonsState & MIDDLE) != 0))
  55. application_state++;
  56. if (((currentButtonsState & RIGHT) != (prevButtonsState & RIGHT)) && ((currentButtonsState & RIGHT) != 0))
  57. if (index_pos< 7)
  58. index_pos++;
  59. if (((currentButtonsState & LEFT) != (prevButtonsState & LEFT)) && ((currentButtonsState & LEFT) != 0))
  60. if (index_pos > 0)
  61. index_pos--;
  62. }
  63. break;
  64. case 2:
  65. if (currentButtonsState != prevButtonsState) {
  66. if (((currentButtonsState & MIDDLE) != (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
  67. application_state++;
  68. if (((currentButtonsState & UP) != (prevButtonsState & UP)) && ((currentButtonsState & UP) != 0))
  69. if (index_pos< 7)
  70. index_pos++;
  71. if (((currentButtonsState & DOWN) != (prevButtonsState & DOWN)) && ((currentButtonsState & DOWN) != 0))
  72. if (index_pos> 0)
  73. index_pos--;
  74. }
  75. break;
  76. case 3:
  77. if (currentButtonsState != prevButtonsState) {
  78. if (((currentButtonsState & MIDDLE) != (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
  79. application_state=0;
  80. if (((currentButtonsState & DOWN) != (prevButtonsState & DOWN)) && ((currentButtonsState & DOWN) != 0))
  81. if (index_pos< 7)
  82. index_pos++;
  83. if (((currentButtonsState & UP) != (prevButtonsState & UP)) && ((currentButtonsState & UP) != 0))
  84. if (index_pos> 0)
  85. index_pos--;
  86. }
  87. break;
  88. default:
  89. break;
  90. }
  91.  
  92. prevButtonsState = currentButtonsState;
  93. resetDisplays();
  94. SEVEN_SEGMENT_DISPLAY_BASE[index_pos] = blinker & blinkerOnOff;
  95. }
  96. }*/
  97.  
  98. #include "displays.h"
  99. #include "address_mapping.h"
  100. #include "buttons.h"
  101. #include "interrupt_controller.h"
  102. #include "timers.h"
  103. #include "microBlaze.h"
  104. #include "alarm_clock.h"
  105.  
  106. /*
  107. * Keeps the current state of the push buttons. Modified only in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler()
  108. */
  109. unsigned int currentButtonsState = 0;
  110.  
  111. /*
  112. * Keeps the previous state of the push buttons
  113. */
  114. unsigned int prevButtonsState = 0;
  115.  
  116. /*
  117. * Keeps the binary pattern for the bottom-most horizontal segment
  118. */
  119. unsigned int blinker = 0b0001000;
  120.  
  121. /*
  122. * Changes from 0x00000000 to 0xFFFFFFFF and vice-versa. Modified only in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler()
  123. * Used to indicate if the blinker should be on or off
  124. */
  125. unsigned int blinkerOnOff = 0;
  126.  
  127. /*
  128. * Keeps track of the current state of the alarm-clock.
  129. * value 0 == RUN state
  130. * value 1 == SET_TIME state
  131. * value 2 == SET_ALARM state
  132. * value 3 == ALARM state
  133. */
  134. unsigned int alarmclock_state = RUN;
  135.  
  136. /*
  137. * Keeps track of the index of the seven-segment display whose value might be modified by pressing the up or the down button
  138. */
  139. unsigned char index_pos = 0;
  140.  
  141. /*
  142. * Keeps the time set by the user.
  143. * Element at index 0 refers to hours and should only accept values in the range 0-23
  144. * Element at index 1 refers to minutes and should only accept values in the range 0-59
  145. * Element at index 2 refers to seconds and should only accept values in the range 0-59
  146. * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
  147. */
  148. unsigned char newTime[4];
  149.  
  150. /*
  151. * Keeps the running time.
  152. * Modified in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler() or when the state changes from SET_TIME to RUN
  153. * Element at index 0 refers to hours and should only accept values in the range 0-23
  154. * Element at index 1 refers to minutes and should only accept values in the range 0-59
  155. * Element at index 2 refers to seconds and should only accept values in the range 0-59
  156. * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
  157. */
  158. unsigned char currentTime[4];
  159.  
  160. /*
  161. * Keeps the time that indicates when the alarm goes off.
  162. * Element at index 0 refers to hours and should only accept values in the range 0-23
  163. * Element at index 1 refers to minutes and should only accept values in the range 0-59
  164. * Element at index 2 refers to seconds and should only accept values in the range 0-59
  165. * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
  166. */
  167. unsigned char alarmTime[4];
  168.  
  169. #define CLAMP(value, lower, upper) ((value) <= (lower) ? (lower) : (value) >= (upper) ? (upper) : (value))
  170.  
  171. int is_button_active(int button) {
  172. return (((currentButtonsState & button) != (prevButtonsState & button)) && ((currentButtonsState & button) != 0));
  173. }
  174.  
  175. /*
  176. * Implement the code based on the description written as a comment
  177. */
  178.  
  179. int main() {
  180. resetDisplays();
  181. initTimers_alarmClock();
  182. initController_alarmClock();
  183. enableTimer1();
  184. enableMicroBlazeInterrupts();
  185.  
  186. while (1 == 1) {
  187. int buttonsStateDiffers = currentButtonsState != prevButtonsState;
  188.  
  189. switch (alarmclock_state) {
  190. case RUN:
  191. /*
  192. * Turn off the 16 LEDs.
  193. * Displays the current time on the eight seven-segment displays
  194. * Changes the state of the alarm-clock to ALARM and enables TIMER2 only if the current time is the same as the alarm time and the leftmost switch is turned on
  195. * Checks if the UP or DOWN button are pressed now but has not been pressed before
  196. * If the UP button is pressed now, but has not been pressed before
  197. * -the state of the alarm-clock changes to SET_TIME
  198. * -the newTime is set to the same value as the currentTime
  199. * -the newTime is displayed on the eight seven-segment displays
  200. * -the index_pos is initialized to zero
  201. * If the DOWN button is pressed now, but has not been pressed before
  202. * -the state of the alarm-clock changes to SET_ALARM
  203. * -the alarmTime is displayed on the eight seven-segment displays
  204. * -the index_pos is initialized to zero
  205. * -the 16 LEDs are turned on
  206. */
  207. *LED_DATA = 0;
  208.  
  209. resetDisplays();
  210. display(currentTime);
  211.  
  212. if(*SWITCHES_DATA & (1 << 15) && compareTimeArray(currentTime, alarmTime)) {
  213. alarmclock_state = ALARM;
  214. enableTimer2();
  215. }
  216.  
  217. if (buttonsStateDiffers) {
  218. if (is_button_active(UP)) {
  219. alarmclock_state = SET_TIME;
  220. setTime(newTime, currentTime);
  221. index_pos = 0;
  222. } else if (is_button_active(DOWN)) {
  223. alarmclock_state = SET_ALARM;
  224. display(alarmTime);
  225. index_pos = 0;
  226. *LED_DATA = 0xFFFF;
  227. }
  228. }
  229. break;
  230. case SET_TIME:
  231. case SET_ALARM: {
  232. /* SET_TIME
  233. * Only when the state of the buttons has changed, the newTime is displayed
  234. * If the UP or DOWN buttons are pressed now, but have not been pressed before
  235. * -based on the index_pos, the corresponding element in the newTime array is incremented or decremented
  236. * If the LEFT or RIGHT buttons are pressed now, but have not been pressed before
  237. * -the index_pos is incremented/decremented by 2
  238. * If the MIDDLE button is pressed now, but has not been pressed before,
  239. * -the currentTime is set to be the same as the newTime
  240. * -the state of the alarm-clock changes to RUN
  241. * Based on the blinkerOnOff variable, the bottom-most horizontal segments of the displays with indices "index_pos" and "index_pos+1" are turned-on or turned-off
  242. * NOTE: The data registers of the SEVEN_SEGMENT_DISPLAY device are read-write registers, i.e. when reading the data register controlling a particular seven-segment display, one obtains the same information that is used to drive (control) the seven-segment display
  243. */
  244.  
  245. /* SET_ALARM
  246. * Only when the state of the buttons has changed, the alarmTime is displayed
  247. * If the UP or DOWN buttons are pressed now, but have not been pressed before
  248. * -based on the index_pos, the corresponding element in the alarmTime is incremented or decremented
  249. * If the LEFT or RIGHT buttons are pressed now, but have not been pressed before
  250. * -the index_pos is incremented/decremented by 2
  251. * If the MIDDLE button is pressed now, but has not been pressed before,
  252. * -the 16 LEDs are turned off
  253. * - the state of the alarm-clock changes to RUN
  254. * Based on the blinkerOnOff variable, the bottom-most horizontal segments of the displays with indices "index_pos" and "index_pos+1" are turned-on or turned-off
  255. * NOTE: The data registers of the SEVEN_SEGMENT_DISPLAY device are read-write registers, i.e. when reading the data register controlling a particular seven-segment display, one obtains the same information that is used to drive (control) the seven-segment display
  256. */
  257. int isAlarm = (alarmclock_state == SET_ALARM);
  258. unsigned char* theTime = isAlarm ? alarmTime : newTime;
  259.  
  260. if (buttonsStateDiffers) {
  261. int isButtonUp = is_button_active(UP);
  262.  
  263. if (isButtonUp || is_button_active(DOWN)) {
  264. int index = 3 - index_pos / 2;
  265. int value = theTime[index] + (!isButtonUp ? -1 : 1);
  266.  
  267. switch(index) {
  268. case 0: value = CLAMP(value, 0, 23); break;
  269. case 3: value = CLAMP(value, 0, 99); break;
  270. default: value = CLAMP(value, 0, 59); break;
  271. }
  272.  
  273. theTime[index] = value;
  274. }
  275.  
  276. if (is_button_active(LEFT)) {
  277. index_pos = CLAMP(index_pos + 2, 0, 6);
  278. } else if(is_button_active(RIGHT)) {
  279. index_pos = CLAMP(index_pos - 2, 0, 6);
  280. }
  281.  
  282. if (is_button_active(MIDDLE)) {
  283. if (isAlarm) {
  284. *LED_DATA = 0x0000;
  285. } else {
  286. setTime(currentTime, newTime);
  287. }
  288.  
  289. alarmclock_state = RUN;
  290. }
  291.  
  292. display(theTime);
  293. }
  294.  
  295. for (int i = 0; i < 2; i++) {
  296. SEVEN_SEGMENT_DISPLAY_BASE[index_pos + i] &= ~blinker;
  297. SEVEN_SEGMENT_DISPLAY_BASE[index_pos + i] |= blinker & blinkerOnOff;
  298. }
  299.  
  300. break;
  301. }
  302. case ALARM:
  303. /*
  304. * Based on the state of blinkerOnOff, the 16 LEDs are turned on or off
  305. * Checks if the state of the buttons has changed
  306. * If the MIDDLE button is pressed, but has not been pressed before
  307. * -the alarmTime is incremented by 5s
  308. * -TIMER2 is stopped an the interrupts in the device are acknowledged
  309. * -the alarm-clock state is changed to RUN
  310. */
  311. *LED_DATA = (blinkerOnOff ? 0xFFFF : 0x0000);
  312.  
  313. if (buttonsStateDiffers && is_button_active(MIDDLE)) {
  314. modifyTime(alarmTime, 2, 5);
  315. *TIMER2_CTRL |= (1 << 8);
  316. alarmclock_state = RUN;
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322.  
  323. if (buttonsStateDiffers) {
  324. prevButtonsState = currentButtonsState;
  325. }
  326. }
  327. }
  328.  
  329. void initTimers_alarmClock() {
  330. // Execute once every 10ms
  331. *TIMER1_LOAD = 999999;
  332. *TIMER1_CTRL = (1<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1);
  333.  
  334. // Execute once after 5 seconds
  335. *TIMER2_LOAD = 0xFFFFFFFF-500000000;
  336. *TIMER2_CTRL = (1<<8)|(1<<6)|(1<<5);
  337. }
  338.  
  339. void initController_alarmClock() {
  340. *IER |= 0b110;
  341. *IVAR1 = (unsigned int) timer1AlarmClockInterruptHandler;
  342. *IVAR2 = (unsigned int) timer2AlarmClockInterruptHandler;
  343. *MER |= 0b11;
  344. }
  345.  
  346. void enableTimer1() {
  347. *TIMER1_CTRL=(*TIMER1_CTRL|(1<<7))&(~(1<<5));
  348. }
  349.  
  350. void enableTimer2() {
  351. *TIMER2_CTRL=(*TIMER2_CTRL|(1<<5));
  352. *TIMER2_CTRL=(*TIMER2_CTRL|(1<<7))&(~(1<<5));
  353. }
  354.  
  355. void display(unsigned char time_array[4]) {
  356. for(int i = 0; i < 4; i++) {
  357. int value = time_array[i];
  358. int pos = 6 - i * 2;
  359.  
  360. displayNumberAtIndex(time_array[i], pos);
  361.  
  362. if (value < 10) {
  363. displayNumberAtIndex(0, pos + 1);
  364. }
  365. }
  366. }
  367.  
  368. unsigned int compareTimeArray(unsigned char * time_array1, unsigned char *time_array2) {
  369. return *((int*)time_array1) == *((int*)time_array2);
  370. }
  371.  
  372. void setTime(unsigned char *newTime_array, unsigned char *oldTime_array) {
  373. *((int*)newTime_array) = *((int*)oldTime_array);
  374. }
  375.  
  376. void modifyTime(unsigned char *time_array, unsigned char index, char diff) {
  377. int limits[] = { 23, 59, 59, 99 };
  378. for(int i = (index + 1); i-- > 0;) {
  379. int result = CLAMP(currentTime[i] + diff, 0, 100);
  380.  
  381. if (result > limits[i]) {
  382. currentTime[i] = 0;
  383. continue;
  384. }
  385.  
  386. currentTime[i] = result;
  387. break;
  388. }
  389. }
  390.  
  391. void timer1AlarmClockInterruptHandler() {
  392. static int timeElapsed = 0;
  393. timeElapsed++;
  394.  
  395. modifyTime(currentTime, 3, 1);
  396.  
  397. if ((timeElapsed % 2) == 0) {
  398. currentButtonsState = *BUTTONS_DATA;
  399. }
  400.  
  401. if ((timeElapsed % 25) == 0) {
  402. blinkerOnOff =~ blinkerOnOff;
  403. }
  404.  
  405. *TIMER1_CTRL |= (1<<8);
  406. *IAR = 0b10;
  407. }
  408.  
  409. void timer2AlarmClockInterruptHandler() {
  410. *TIMER2_CTRL |= (1<<8);
  411. alarmclock_state = RUN;
  412. *IAR = 0b100;
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement