NAME: RICHARD BA TEBE JUNIOR
MATRICULE: UBa23PH126
DEPARTMENT: DATA SCIENCE
COURSE: MICROCONTROLLERS
Exercise 1:
1)
At startup, the 7-segment display should show "0".
This is achieved by initializing the 7-segment display to the value
corresponding to "0" using the lookup table for a common-anode display.
Code Snippet:
char seg_values[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80,
0x90};
int count = 0; // Initialize counter to 0
void display_number(int num) {
PORTB = seg_values[num]; // Output the corresponding 7-segment value
void main() {
TRISB = 0x00; // Set PORTB as output
PORTB = 0x00; // Clear PORTB
display_number(0); // Display initial value 0
}
2)
When the UP button is pressed, the counter (count) is incremented.
If the current value is 9, pressing UP should reset it back to 0.
A delay is added for debouncing.
Code :
if (Button(&PORTA, 0, 1, 1)) { // Check if UP button is pressed
delay_ms(200); // Debounce delay
count = (count + 1) % 10; // Increment with wrap-around to 0 after 9
display_number(count); // Update display
3)
When the DOWN button is pressed, the counter (count) is decremented.
If the current value is 0, pressing DOWN should reset it to 9.
A delay is added for debouncing.
Code:
if (Button(&PORTA, 1, 1, 1)) { // Check if DOWN button is pressed
delay_ms(200); // Debounce delay
count = (count - 1 < 0) ? 9 : count - 1; // Decrement with wrap-around to 9
after 0
display_number(count); // Update display
}
4)
When the RESET button is pressed, the counter (count) is reset to 0.
The LED D2 is turned off as part of the reset functionality.
A delay is added for debouncing.
Code:
if (Button(&PORTA, 2, 1, 1)) { // Check if RESET button is pressed
delay_ms(200); // Debounce delay
count = 0; // Reset counter to 0
LED_D2 = 0; // Turn off LED D2
display_number(count); // Update display
5)
When the LAUNCH button is pressed, the LED D2 is turned on.
The delay duration is determined by the current value of the counter (count)
in seconds.
After the delay, the LED D2 is turned off.
A delay is added for debouncing.
Code:
if (Button(&PORTA, 3, 1, 1)) { // Check if LAUNCH button is pressed
delay_ms(200); // Debounce delay
LED_D2 = 1; // Turn on LED D2
delay_ms(count * 1000); // Keep LED on for 'count' seconds
LED_D2 = 0; // Turn off LED D2
Complete Program:
Combining all the subparts:
// MikroC Program for Exercise 1
#define LED_D2 LATB.F7 // Define LED D2
char seg_values[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80,
0x90};
int count = 0;
void display_number(int num) {
PORTB = seg_values[num]; // Display number on 7-segment
void main() {
TRISA = 0x0F; // RA0-RA3 as input for buttons
TRISB = 0x00; // PORTB as output for 7-segment and LED
PORTA = 0x00; // Clear PORTA
PORTB = 0x00; // Clear PORTB
display_number(0); // Display initial value 0
while (1) {
if (Button(&PORTA, 0, 1, 1)) { // UP Button
delay_ms(200);
count = (count + 1) % 10;
display_number(count);
if (Button(&PORTA, 1, 1, 1)) { // DOWN Button
delay_ms(200);
count = (count - 1 < 0) ? 9 : count - 1;
display_number(count);
if (Button(&PORTA, 2, 1, 1)) { // RESET Button
delay_ms(200);
count = 0;
LED_D2 = 0;
display_number(count);
}
if (Button(&PORTA, 3, 1, 1)) { // LAUNCH Button
delay_ms(200);
LED_D2 = 1;
delay_ms(count * 1000);
LED_D2 = 0;
Exercise 2:
1)
All LEDs are initially turned off, and the system waits for 1 second before
proceeding to the next steps.
Code:
void initialize_leds() {
PORTB = 0x00; // Turn off all LEDs
delay_ms(1000); // Wait for 1 second
2)
The LEDs are toggled on and off for 10 cycles.
Each cycle consists of the LEDs turning on and off with a delay of 500ms
between states.
Code:
void blink_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0xFF; // Turn on all LEDs
delay_ms(500); // Wait 500ms
PORTB = 0x00; // Turn off all LEDs
delay_ms(500); // Wait 500ms
3)
Odd LEDs (connected to RB0, RB2, RB4, RB6) are turned on, while even LEDs
(RB1, RB3, RB5, RB7) are turned off, and vice versa.
This toggling continues for 10 cycles.
Code:
void toggle_odd_even_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0x55; // Odd LEDs on (binary: 01010101)
delay_ms(500); // Wait 500ms
PORTB = 0xAA; // Even LEDs on (binary: 10101010)
delay_ms(500); // Wait 500ms
}
4)
The first four LEDs (connected to RB0–RB3) are turned on, while the last four
LEDs (RB4–RB7) are turned off, and vice versa.
This toggling continues for 10 cycles.
Code:
void toggle_first_last_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0x0F; // First four LEDs on (binary: 00001111)
delay_ms(500); // Wait 500ms
PORTB = 0xF0; // Last four LEDs on (binary: 11110000)
delay_ms(500); // Wait 500ms
5)
The above sequences (initialization, blinking, toggling odd/even LEDs, and
toggling first/last LEDs) are repeated indefinitely.
This ensures the circuit operates continuously while power is supplied.
Code:
void main() {
TRISB = 0x00; // Configure PORTB as output
PORTB = 0x00; // Clear PORTB
while (1) { // Infinite loop
initialize_leds();
blink_leds();
toggle_odd_even_leds();
toggle_first_last_leds();
Complete Program:
Combining all the subparts:
// MikroC Program for Exercise 2
void initialize_leds() {
PORTB = 0x00; // Turn off all LEDs
delay_ms(1000); // Wait for 1 second
}
void blink_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0xFF; // Turn on all LEDs
delay_ms(500); // Wait 500ms
PORTB = 0x00; // Turn off all LEDs
delay_ms(500); // Wait 500ms
void toggle_odd_even_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0x55; // Odd LEDs on (binary: 01010101)
delay_ms(500); // Wait 500ms
PORTB = 0xAA; // Even LEDs on (binary: 10101010)
delay_ms(500); // Wait 500ms
void toggle_first_last_leds() {
for (int i = 0; i < 10; i++) {
PORTB = 0x0F; // First four LEDs on (binary: 00001111)
delay_ms(500); // Wait 500ms
PORTB = 0xF0; // Last four LEDs on (binary: 11110000)
delay_ms(500); // Wait 500ms
}
void main() {
TRISB = 0x00; // Configure PORTB as output
PORTB = 0x00; // Clear PORTB
while (1) { // Infinite loop
initialize_leds();
blink_leds();
toggle_odd_even_leds();
toggle_first_last_leds();