CT_Lab_2_BitManipulations/bit_manipulations/app/main.c

129 lines
3.2 KiB
C
Raw Normal View History

2022-09-30 07:57:28 +02:00
/* -----------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zurich University of -
* -- _| |_| | | | |____ ____) | Applied Sciences -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ------------------------------------------------------------------
* --
* -- main.c
* --
* -- main for Computer Engineering "Bit Manipulations"
* --
* -- $Id: main.c 744 2014-09-24 07:48:46Z ruan $
* ------------------------------------------------------------------
*/
//#include <reg_ctboard.h>
#include "utils_ctboard.h"
#define ADDR_DIP_SWITCH_31_0 0x60000200
#define ADDR_DIP_SWITCH_7_0 0x60000200
#define ADDR_LED_31_24 0x60000103
#define ADDR_LED_23_16 0x60000102
#define ADDR_LED_15_8 0x60000101
#define ADDR_LED_7_0 0x60000100
#define ADDR_BUTTONS 0x60000210
// define your own macros for bitmasks below (#define)
/// STUDENTS: To be programmed
2022-09-30 08:35:44 +02:00
#define BRIGHT_MASK 0xC0
#define DARK_MASK 0xCF
2022-09-30 09:02:03 +02:00
#define BUTTON_MASK 0x0F
#define T0_MASK 0x01
2022-09-30 09:40:56 +02:00
#define T1_MASK 0x02
#define T2_MASK 0x04
#define T3_MASK 0x08
2022-09-30 07:57:28 +02:00
/// END: To be programmed
int main(void)
{
uint8_t led_value = 0;
// add variables below
/// STUDENTS: To be programmed
2022-09-30 09:02:03 +02:00
uint8_t button_value = 0;
uint8_t button_counter = 0;
2022-09-30 09:40:56 +02:00
uint8_t button_counter2 = 0;
2022-09-30 09:02:03 +02:00
int counted = 0;
2022-09-30 09:40:56 +02:00
int executed = 0;
uint8_t led_23_16_value = 0x00;
2022-09-30 07:57:28 +02:00
/// END: To be programmed
while (1) {
// ---------- Task 3.1 ----------
led_value = read_byte(ADDR_DIP_SWITCH_7_0);
/// STUDENTS: To be programmed
2022-09-30 09:40:56 +02:00
/// Task 3.1
2022-09-30 08:35:44 +02:00
led_value = (led_value | BRIGHT_MASK);
led_value = (led_value & DARK_MASK);
2022-09-30 09:02:03 +02:00
2022-09-30 09:40:56 +02:00
///Task 3.2
2022-09-30 09:02:03 +02:00
button_value = read_byte(ADDR_BUTTONS);
button_value = button_value & BUTTON_MASK;
2022-09-30 09:40:56 +02:00
2022-09-30 09:02:03 +02:00
if((button_value & T0_MASK) > 0) {
2022-09-30 09:40:56 +02:00
button_counter = button_counter + 1;
2022-09-30 09:02:03 +02:00
if(counted == 0) {
2022-09-30 09:40:56 +02:00
button_counter2 = button_counter2 + 1;
2022-09-30 09:02:03 +02:00
counted = 1;
}
} else {
counted = 0;
}
2022-09-30 09:40:56 +02:00
/// Task 3.3
if((button_value & T0_MASK) > 0) {
if(executed == 0) {
led_23_16_value = led_23_16_value>>1;
executed = 1;
}
} else if ((button_value & T1_MASK) > 0) {
if(executed == 0) {
led_23_16_value = led_23_16_value<<1;
executed = 1;
}
} else if ((button_value & T2_MASK) > 0) {
if(executed == 0) {
led_23_16_value = ~ led_23_16_value;
executed = 1;
}
} else if ((button_value & T3_MASK) > 0) {
if(executed == 0) {
led_23_16_value = read_byte(ADDR_DIP_SWITCH_7_0);
executed = 1;
}
} else {
executed = 0;
}
2022-09-30 09:02:03 +02:00
write_byte(ADDR_LED_15_8, button_counter);
2022-09-30 09:40:56 +02:00
write_byte(ADDR_LED_31_24, button_counter2);
write_byte(ADDR_LED_23_16, led_23_16_value);
2022-09-30 07:57:28 +02:00
/// END: To be programmed
write_byte(ADDR_LED_7_0, led_value);
// ---------- Task 3.2 and 3.3 ----------
/// STUDENTS: To be programmed
/// END: To be programmed
}
}