ECE 376: Embedded Systems

Homework 8

Questions 1-4

Flow Chart

C Code
#pragma warning disable 520,1385,1311,1510,1518,2053
#include "piclib.h"

i32_t time;
u8_t running;

void __interrupt() timer2_interrupt() {
    time--;
    if (PORTB) running = 0;
    if (time == 0) PORTA = 0xFF;
    timer2_end();
}

int main() {
    time = 0;
    running = 0;

    ADCON1 = 15;

    TRISA = 0x00;
    PORTA = 0x00;

    TRISB = 0x01;
    PORTB = 0x00;

    timer2_init();
    lcd_init();

    lcd_send(CLEAR);
    lcd_append_all("Press Button 0  to Start!");

    while (!PORTB) time++;
    while (PORTB) time++;
    // Wait 100ms for denoising
    wait(100);

    time %= 40000;
    time += 30000;

    running = 1;
    timers_enable();
    lcd_send(CLEAR);
    lcd_append_all("Current time:");

    while (running) {
        lcd_goto(1, 0);
        lcd_append_int(time, 5, 4);
    }

    lcd_send(CLEAR);
    lcd_append_all("Time delta:");
    lcd_goto(1, 0);
    lcd_append_int(-time, 5, 4);

    // Get another uniqe button push
    while (PORTB);
    while (!PORTB);
    while (PORTB);

    return 0;
}
Validation

Student T-Test
Run Time
1 0.0289
2 -0.1474
3 -0.2182
4 -0.4372
5 -0.2560

90% Confidence Interval: -0.367 sec <-> -0.045 sec

Questions 5-8

Flow Chart

C Code
#pragma warning disable 520,1385,1311,1510,1518,2053
#include "piclib.h"

void __interrupt() timer2_interrupt() {
    PORTC ^= 1;
    timer2_end();
}

int main() {
    ADCON1 = 15;

    TRISC = 0x00;
    TRISB = 0xFF;
    lcd_init();

    lcd_send(CLEAR);
    lcd_append_all("Press Any Button to Play!");

    u8_t previous_input = PORTB;

    while (1) {
        if (PORTB == previous_input) continue;
        previous_input = PORTB;
        switch (previous_input) {
            case 0x00:
                timers_disable();
                break;
            case 0x01:
                timers_disable();
                timer2_init(15, 177, 3);
                timers_enable();
                break;
            case 0x02:
                timers_disable();
                timer2_init(15, 158, 3);
                timers_enable();
                break;
            case 0x04:
                timers_disable();
                timer2_init(15, 149, 3);
                timers_enable();
                break;
            case 0x08:
                timers_disable();
                timer2_init(15, 133, 3);
                timers_enable();
                break;
            case 0x10:
                timers_disable();
                timer2_init(15, 118, 3);
                timers_enable();
                break;
            case 0x20:
                timers_disable();
                timer2_init(15, 112, 3);
                timers_enable();
                break;
            case 0x40:
                timers_disable();
                timer2_init(15, 100, 3);
                timers_enable();
                break;
            case 0x80:
                timers_disable();
                timer2_init(15, 89, 3);
                timers_enable();
        }
    }

    return 0;
}
Validation

Two buttons?

Based off the code, it should just continue playing whatever was pressed first as the program only updates states when the buttons change, so when more than one button is pushed, it will try updating, the switch statement won't match any cases, and it will just continue doing the same thing.

In practice, it does as expected and just holds the previous tone.