ECE 376: Embedded Systems

Homework 6

Note: This uses a self-built library called piclib. You can view the header file here and the source file here

Questions 1-3

1. The Requirements

This program will take the input of the variable resistor on the PIC board and using the A/D, change the brightness of the Neo-Pixel corresponding to the button being pushed. e.g. RB0 controls the bottom Neo-Pixel.

2.a. The Flowchart

2.b. The Code
#pragma warning disable 520,1385
#include "piclib.h"

int main() {
    TRISB = 0xFF;
    ad_converter_init();
    wait(100);

    u8_t color_data[NEO_PIXEL_COUNT * 3];
    for (u8_t i = 0; i < NEO_PIXEL_COUNT * 3; i++) color_data[i] = 0;
    neopixel_send(color_data);

    while (1) {
        u8_t buttons = PORTB;
        if (!buttons) continue;

        u8_t index = 0;
        while (!(buttons & 0x01)) {
            index += 3;
            buttons >>= 1;
        }

        u8_t brightness = (u8_t) (ad_converter_read(PORTA_0) >> 2);
        color_data[index] = brightness;
        color_data[index+1] = brightness;
        color_data[index+2] = brightness;

        neopixel_send(color_data);
        wait(10);
    }

    return 0;
}
3. Validation

Note: The GIFs may take a second to load

Question 5
#pragma warning disable 520,1385
#include "piclib.h"

int main() {
    TRISB = 0x01;
    lcd_init();
    serial_init();

    i8_t DIE = 0, X = 0;
    while(1) {
        while(!(PORTB & 0x01));
        while(PORTB & 0x01) {
            DIE = (DIE + 1) % 6;
            X = (X + 1) % 7;
        }
        DIE = DIE + 1;
        if(X == 0) DIE = 6;

        lcd_goto(1,0); 
        lcd_append_int(DIE, 1, 0);
        serial_append_int(DIE, 1, 0);
        serial_append(',');
    }

    return 0;
}
Output Data Bin 1: 1,2,1,6,4,3,4,5,6,4,6,6,2,1,6,1,1,4,3,6,5,6,3,3,6,2,5,4,6,3,6,6,3,5,6,5,2,5,4,2,1,6,2,3,3,6,6,6,5,6,3,5 Bin 2: 1,4,5,3,6,1,2,4,4,6,3,4,6,3,5,1,1,4,6,6,3,4,1,2,1,6,6,6,3,3,3,4,4,3,1,4,1,6,6,3,2,1,3,2,4,6,2,1,4,2,4,6 Bin 3: 3,4,1,5,6,2,6,6,6,4,3,4,6,6,3,4,4,6,1,1,3,5,4,6,6,4,2,2,2,3,6,3,5,3,5,3,5,5,5,2,3,4,6,4,6,6,4,5,4,3,6,5 Bin 4: [1,4,3,3,1,1,1,4,3,5,2,3,3,6,2,5,6,6,5,5,6,3,1,2,3,5,1,6,5,6,6,6,6,3,4,4,3,5,6,3,6,6,6,1,2,6,1,3,2,4,4,3]; Bin 5: 5,4,2,4,5,6,1,4,2,4,6,1,3,1,5,6,1,3,5,6,2,3,2,4,5,6,2,2,6,6,2,4,2,4,2,4,6,6,5,1,5,2,6,6,2,4,2,1,6,1,1,3 Bin 6: 1,5,6,5,6,6,1,1,6,6,1,6,3,2,4,6,2,5,4,5,1,3,5,4,5,3,1,3,3,5,6,2,6,2,3,6,4,1,3,4,5,2,6,6,3,1,4,1,1,4,2,6];
expected_freq = length(bin_1) / 6;

actual_freq_1 = nnz(bin_1 == 1);
actual_freq_2 = nnz(bin_2 == 2);
actual_freq_3 = nnz(bin_3 == 3);
actual_freq_4 = nnz(bin_4 == 4);
actual_freq_5 = nnz(bin_5 == 5);
actual_freq_6 = nnz(bin_6 == 6);

chi_squared_1 = ((expected_freq - actual_freq_1)^2 / expected_freq);
chi_squared_2 = ((expected_freq - actual_freq_2)^2 / expected_freq);
chi_squared_3 = ((expected_freq - actual_freq_3)^2 / expected_freq);
chi_squared_4 = ((expected_freq - actual_freq_4)^2 / expected_freq);
chi_squared_5 = ((expected_freq - actual_freq_5)^2 / expected_freq);
chi_squared_6 = ((expected_freq - actual_freq_6)^2 / expected_freq);

chi_squared = chi_squared_1 + chi_squared_2 + chi_squared_3 + chi_squared_4 + chi_squared_5 + chi_squared_6;

disp(chi_squared);

% Output:
%   5.1538

Probability of Rejection: 60.260%

I would say no, this is not a fair dice as the probability of rejection is more than 50%