Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*#include "address_mapping.h"
- #include "assemblyImplementations.h"
- // Assignment 3.(6, 7, 8)
- int main(){
- *SWITCHES_CONTROL=0xFFFF;
- *LED_CONTROL=0;
- while (1==1)
- //process_switches();
- //process_switches_left_right_mirror();
- process_switches_count();
- }*/
- /*#include "displays.h"
- #include "address_mapping.h"
- #include "buttons.h"
- #include "interrupt_controller.h"
- #include "timers.h"
- #include "microBlaze.h"
- unsigned int currentButtonsState = 0;
- unsigned int prevButtonsState = 0;
- unsigned int blinker = 0b0001000;
- unsigned int blinkerOnOff = 0;
- unsigned int application_state = 0;
- unsigned char index_pos = 0;
- // Assignment 4.1
- int main() {
- resetDisplays();
- initTimers();
- initController();
- enableMicroBlazeInterrupts();
- enableTimers();
- while (1 == 1) {
- *LED_DATA = 1 << application_state;
- switch (application_state) {
- case 0:
- if (currentButtonsState != prevButtonsState) {
- if (((currentButtonsState & MIDDLE)!= (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
- application_state++;
- if (((currentButtonsState & LEFT) != (prevButtonsState & LEFT)) && ((currentButtonsState & LEFT) != 0))
- if (index_pos< 7)
- index_pos++;
- if (((currentButtonsState & RIGHT) != (prevButtonsState & RIGHT)) && ((currentButtonsState & RIGHT) != 0))
- if (index_pos > 0)
- index_pos--;
- }
- break;
- case 1:
- if (currentButtonsState != prevButtonsState) {
- if (((currentButtonsState & MIDDLE)!= (prevButtonsState & MIDDLE))&& ((currentButtonsState & MIDDLE) != 0))
- application_state++;
- if (((currentButtonsState & RIGHT) != (prevButtonsState & RIGHT)) && ((currentButtonsState & RIGHT) != 0))
- if (index_pos< 7)
- index_pos++;
- if (((currentButtonsState & LEFT) != (prevButtonsState & LEFT)) && ((currentButtonsState & LEFT) != 0))
- if (index_pos > 0)
- index_pos--;
- }
- break;
- case 2:
- if (currentButtonsState != prevButtonsState) {
- if (((currentButtonsState & MIDDLE) != (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
- application_state++;
- if (((currentButtonsState & UP) != (prevButtonsState & UP)) && ((currentButtonsState & UP) != 0))
- if (index_pos< 7)
- index_pos++;
- if (((currentButtonsState & DOWN) != (prevButtonsState & DOWN)) && ((currentButtonsState & DOWN) != 0))
- if (index_pos> 0)
- index_pos--;
- }
- break;
- case 3:
- if (currentButtonsState != prevButtonsState) {
- if (((currentButtonsState & MIDDLE) != (prevButtonsState & MIDDLE)) && ((currentButtonsState & MIDDLE) != 0))
- application_state=0;
- if (((currentButtonsState & DOWN) != (prevButtonsState & DOWN)) && ((currentButtonsState & DOWN) != 0))
- if (index_pos< 7)
- index_pos++;
- if (((currentButtonsState & UP) != (prevButtonsState & UP)) && ((currentButtonsState & UP) != 0))
- if (index_pos> 0)
- index_pos--;
- }
- break;
- default:
- break;
- }
- prevButtonsState = currentButtonsState;
- resetDisplays();
- SEVEN_SEGMENT_DISPLAY_BASE[index_pos] = blinker & blinkerOnOff;
- }
- }*/
- #include "displays.h"
- #include "address_mapping.h"
- #include "buttons.h"
- #include "interrupt_controller.h"
- #include "timers.h"
- #include "microBlaze.h"
- #include "alarm_clock.h"
- /*
- * Keeps the current state of the push buttons. Modified only in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler()
- */
- unsigned int currentButtonsState = 0;
- /*
- * Keeps the previous state of the push buttons
- */
- unsigned int prevButtonsState = 0;
- /*
- * Keeps the binary pattern for the bottom-most horizontal segment
- */
- unsigned int blinker = 0b0001000;
- /*
- * Changes from 0x00000000 to 0xFFFFFFFF and vice-versa. Modified only in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler()
- * Used to indicate if the blinker should be on or off
- */
- unsigned int blinkerOnOff = 0;
- /*
- * Keeps track of the current state of the alarm-clock.
- * value 0 == RUN state
- * value 1 == SET_TIME state
- * value 2 == SET_ALARM state
- * value 3 == ALARM state
- */
- unsigned int alarmclock_state = RUN;
- /*
- * Keeps track of the index of the seven-segment display whose value might be modified by pressing the up or the down button
- */
- unsigned char index_pos = 0;
- /*
- * Keeps the time set by the user.
- * Element at index 0 refers to hours and should only accept values in the range 0-23
- * Element at index 1 refers to minutes and should only accept values in the range 0-59
- * Element at index 2 refers to seconds and should only accept values in the range 0-59
- * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
- */
- unsigned char newTime[4];
- /*
- * Keeps the running time.
- * Modified in the interrupt handler for TIMER1 timer1AlarmClockInterruptHandler() or when the state changes from SET_TIME to RUN
- * Element at index 0 refers to hours and should only accept values in the range 0-23
- * Element at index 1 refers to minutes and should only accept values in the range 0-59
- * Element at index 2 refers to seconds and should only accept values in the range 0-59
- * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
- */
- unsigned char currentTime[4];
- /*
- * Keeps the time that indicates when the alarm goes off.
- * Element at index 0 refers to hours and should only accept values in the range 0-23
- * Element at index 1 refers to minutes and should only accept values in the range 0-59
- * Element at index 2 refers to seconds and should only accept values in the range 0-59
- * Element at index 3 refers to hundreds of a second and should only accept values in the range 0-99
- */
- unsigned char alarmTime[4];
- #define CLAMP(value, lower, upper) ((value) <= (lower) ? (lower) : (value) >= (upper) ? (upper) : (value))
- int is_button_active(int button) {
- return (((currentButtonsState & button) != (prevButtonsState & button)) && ((currentButtonsState & button) != 0));
- }
- /*
- * Implement the code based on the description written as a comment
- */
- int main() {
- resetDisplays();
- initTimers_alarmClock();
- initController_alarmClock();
- enableTimer1();
- enableMicroBlazeInterrupts();
- while (1 == 1) {
- int buttonsStateDiffers = currentButtonsState != prevButtonsState;
- switch (alarmclock_state) {
- case RUN:
- /*
- * Turn off the 16 LEDs.
- * Displays the current time on the eight seven-segment displays
- * 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
- * Checks if the UP or DOWN button are pressed now but has not been pressed before
- * If the UP button is pressed now, but has not been pressed before
- * -the state of the alarm-clock changes to SET_TIME
- * -the newTime is set to the same value as the currentTime
- * -the newTime is displayed on the eight seven-segment displays
- * -the index_pos is initialized to zero
- * If the DOWN button is pressed now, but has not been pressed before
- * -the state of the alarm-clock changes to SET_ALARM
- * -the alarmTime is displayed on the eight seven-segment displays
- * -the index_pos is initialized to zero
- * -the 16 LEDs are turned on
- */
- *LED_DATA = 0;
- resetDisplays();
- display(currentTime);
- if(*SWITCHES_DATA & (1 << 15) && compareTimeArray(currentTime, alarmTime)) {
- alarmclock_state = ALARM;
- enableTimer2();
- }
- if (buttonsStateDiffers) {
- if (is_button_active(UP)) {
- alarmclock_state = SET_TIME;
- setTime(newTime, currentTime);
- index_pos = 0;
- } else if (is_button_active(DOWN)) {
- alarmclock_state = SET_ALARM;
- display(alarmTime);
- index_pos = 0;
- *LED_DATA = 0xFFFF;
- }
- }
- break;
- case SET_TIME:
- case SET_ALARM: {
- /* SET_TIME
- * Only when the state of the buttons has changed, the newTime is displayed
- * If the UP or DOWN buttons are pressed now, but have not been pressed before
- * -based on the index_pos, the corresponding element in the newTime array is incremented or decremented
- * If the LEFT or RIGHT buttons are pressed now, but have not been pressed before
- * -the index_pos is incremented/decremented by 2
- * If the MIDDLE button is pressed now, but has not been pressed before,
- * -the currentTime is set to be the same as the newTime
- * -the state of the alarm-clock changes to RUN
- * 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
- * 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
- */
- /* SET_ALARM
- * Only when the state of the buttons has changed, the alarmTime is displayed
- * If the UP or DOWN buttons are pressed now, but have not been pressed before
- * -based on the index_pos, the corresponding element in the alarmTime is incremented or decremented
- * If the LEFT or RIGHT buttons are pressed now, but have not been pressed before
- * -the index_pos is incremented/decremented by 2
- * If the MIDDLE button is pressed now, but has not been pressed before,
- * -the 16 LEDs are turned off
- * - the state of the alarm-clock changes to RUN
- * 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
- * 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
- */
- int isAlarm = (alarmclock_state == SET_ALARM);
- unsigned char* theTime = isAlarm ? alarmTime : newTime;
- if (buttonsStateDiffers) {
- int isButtonUp = is_button_active(UP);
- if (isButtonUp || is_button_active(DOWN)) {
- int index = 3 - index_pos / 2;
- int value = theTime[index] + (!isButtonUp ? -1 : 1);
- switch(index) {
- case 0: value = CLAMP(value, 0, 23); break;
- case 3: value = CLAMP(value, 0, 99); break;
- default: value = CLAMP(value, 0, 59); break;
- }
- theTime[index] = value;
- }
- if (is_button_active(LEFT)) {
- index_pos = CLAMP(index_pos + 2, 0, 6);
- } else if(is_button_active(RIGHT)) {
- index_pos = CLAMP(index_pos - 2, 0, 6);
- }
- if (is_button_active(MIDDLE)) {
- if (isAlarm) {
- *LED_DATA = 0x0000;
- } else {
- setTime(currentTime, newTime);
- }
- alarmclock_state = RUN;
- }
- display(theTime);
- }
- for (int i = 0; i < 2; i++) {
- SEVEN_SEGMENT_DISPLAY_BASE[index_pos + i] &= ~blinker;
- SEVEN_SEGMENT_DISPLAY_BASE[index_pos + i] |= blinker & blinkerOnOff;
- }
- break;
- }
- case ALARM:
- /*
- * Based on the state of blinkerOnOff, the 16 LEDs are turned on or off
- * Checks if the state of the buttons has changed
- * If the MIDDLE button is pressed, but has not been pressed before
- * -the alarmTime is incremented by 5s
- * -TIMER2 is stopped an the interrupts in the device are acknowledged
- * -the alarm-clock state is changed to RUN
- */
- *LED_DATA = (blinkerOnOff ? 0xFFFF : 0x0000);
- if (buttonsStateDiffers && is_button_active(MIDDLE)) {
- modifyTime(alarmTime, 2, 5);
- *TIMER2_CTRL |= (1 << 8);
- alarmclock_state = RUN;
- }
- break;
- default:
- break;
- }
- if (buttonsStateDiffers) {
- prevButtonsState = currentButtonsState;
- }
- }
- }
- void initTimers_alarmClock() {
- // Execute once every 10ms
- *TIMER1_LOAD = 999999;
- *TIMER1_CTRL = (1<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1);
- // Execute once after 5 seconds
- *TIMER2_LOAD = 0xFFFFFFFF-500000000;
- *TIMER2_CTRL = (1<<8)|(1<<6)|(1<<5);
- }
- void initController_alarmClock() {
- *IER |= 0b110;
- *IVAR1 = (unsigned int) timer1AlarmClockInterruptHandler;
- *IVAR2 = (unsigned int) timer2AlarmClockInterruptHandler;
- *MER |= 0b11;
- }
- void enableTimer1() {
- *TIMER1_CTRL=(*TIMER1_CTRL|(1<<7))&(~(1<<5));
- }
- void enableTimer2() {
- *TIMER2_CTRL=(*TIMER2_CTRL|(1<<5));
- *TIMER2_CTRL=(*TIMER2_CTRL|(1<<7))&(~(1<<5));
- }
- void display(unsigned char time_array[4]) {
- for(int i = 0; i < 4; i++) {
- int value = time_array[i];
- int pos = 6 - i * 2;
- displayNumberAtIndex(time_array[i], pos);
- if (value < 10) {
- displayNumberAtIndex(0, pos + 1);
- }
- }
- }
- unsigned int compareTimeArray(unsigned char * time_array1, unsigned char *time_array2) {
- return *((int*)time_array1) == *((int*)time_array2);
- }
- void setTime(unsigned char *newTime_array, unsigned char *oldTime_array) {
- *((int*)newTime_array) = *((int*)oldTime_array);
- }
- void modifyTime(unsigned char *time_array, unsigned char index, char diff) {
- int limits[] = { 23, 59, 59, 99 };
- for(int i = (index + 1); i-- > 0;) {
- int result = CLAMP(currentTime[i] + diff, 0, 100);
- if (result > limits[i]) {
- currentTime[i] = 0;
- continue;
- }
- currentTime[i] = result;
- break;
- }
- }
- void timer1AlarmClockInterruptHandler() {
- static int timeElapsed = 0;
- timeElapsed++;
- modifyTime(currentTime, 3, 1);
- if ((timeElapsed % 2) == 0) {
- currentButtonsState = *BUTTONS_DATA;
- }
- if ((timeElapsed % 25) == 0) {
- blinkerOnOff =~ blinkerOnOff;
- }
- *TIMER1_CTRL |= (1<<8);
- *IAR = 0b10;
- }
- void timer2AlarmClockInterruptHandler() {
- *TIMER2_CTRL |= (1<<8);
- alarmclock_state = RUN;
- *IAR = 0b100;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement