ECE 376: Embedded Systems

Homework 4
  1. All tests didn't make any noise so I couldn't find the clocks per instruction

a.

unsigned char i;

while(1) {
    i = (i + 1) % 32;
    if(i == 0) PORTC += 1;
}

b.

unsigned char i;

while(1) {
    i = (i + 1) % 35;
    if(i == 0) PORTC += 1;
}

c.

unsigned char i;
unsigned long int A, B, C;

A = 123456789;
B = 2731;

while(1) {
    i = (i + 1)% 32;
    if(i == 0) PORTC += 1;
    C = A / B;
}

d.

float A, B, C;

A = sqrt(3);
B = sqrt(2);

while(1) {
    PORTC += 1;
    C = A / B;
}
  1. int main() {
     TRISB = 0xFF;
     TRISC = 0x00;
     TRISD = 0x00;
    
     PORTC = 0xFF;
     PORTD = 0x00;
    
     while (PORTC != 0) {
         unsigned char prev_state = 0x00;
         // Get the button being pressed
         while (PORTB == 0);
         prev_state = PORTB;
         while (PORTB != 0);
         // Update game state
         prev_state |= (prev_state << 1) | (prev_state >> 1);
         PORTC ^= prev_state;
         PORTD++;
     }
    
     return 0;
    }

Size of compiled C code .hex file: 444 Bytes

  1. 
    char *messages_ptr[12] = {
     "It is certain",
     "It is decidedly so",
     "Without a doubt",
     "Yes, definitely",
     "Reply hazy. Try again",
     "Ask again later",
     "Better not tell you now",
     "Cannot predict  now",
     "Don't count on  it",
     "My reply is no",
     "Outlook not so  good",
     "Very doubtful"
    };

int main() { // Initialize Ports TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00;

LCD_Init();

for (unsigned char i = 0; i < 12; i++) {
    lcd_print(messages[i]);
    Wait_ms(1000);
}

return 0;

}

inline void lcd_print(char *ptr) { LCD_Inst(0x01); // Clears

unsigned char count = 0;
while (*ptr) {
    // Output character
    LCD_Write(*ptr);
    Wait_ms(100); // I like the effect it give and it makes it works so it stays
    // Increment counters
    ptr++;
    count++;
    // Move to second row if out of space
    if (count == 16) LCD_Move(1, 0);
}

![](Images/HW04_2.gif)

5. 
```c
inline unsigned char get_random_number();

int main() {
    // Initialize Ports
    TRISA = 0x00;
    TRISB = 0xFF;
    TRISC = 0x00;
    TRISD = 0x00;

    LCD_Init();

    for (char i = 0; i < 5; i++) {
        unsigned char temp = get_random_number();
        LCD_Inst(0x01);
        LCD_Out(temp, 2, 0);
    }

    return 0;
}

inline unsigned char get_random_number() {
    unsigned char rand = 0;
    while (RB0 == 0) rand++;
    while (RB0 != 0) rand++;
    return rand % 12;
}

*Note that the video only have 4 random numbers displayed because the last one gets cleared before it can be shown...oops

  1. inline unsigned char get_random_number() {
     unsigned char rand = 0;
     // One
     while (RB0 == 0) rand++;
     while (RB0 != 0) rand++;
     // Two
     while (RB0 == 0) rand++;
     while (RB0 != 0) rand++;
     // Three
     while (RB0 == 0) rand++;
     while (RB0 != 0) rand++;
    
     return rand % 12;
    }
  2. 
    #pragma warning disable 520,1385
    #include "lcd.h"

/**

  • I would like to just start by apologizing for the C code below
  • If anyone ever asks "Why rust", just point them to this and say
  • "Count the vulnerabilities" */

char *messages[12] = { "It is certain", "It is decidedly so", "Without a doubt", "Yes, definitely", "Reply hazy. Try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Don't count on it", "My reply is no", "Outlook not so good", "Very doubtful" };

inline void lcd_print(char *ptr); inline unsigned char get_random_number(); inline void wait_for_it();

int main() { // Initialize Ports TRISA = 0x00; TRISB = 0xFF; TRISC = 0x00; TRISD = 0x00;

LCD_Init();

// Game loop
while (1) {
    lcd_print("  Magic 8-Ball  Ask Your Question");
    unsigned char temp = get_random_number();
    wait_for_it();
    lcd_print(messages[temp]);
    Wait_ms(5000);
}

}

inline void lcd_print(char *ptr) { LCD_Inst(0x01); // Clears

unsigned char count = 0;
while (*ptr) {
    // Output character
    LCD_Write(*ptr);
    Wait_ms(50); // I like the effect it give and it makes it works so it stays
    // Increment counters
    ptr++;
    count++;
    // Move to second row if out of space
    if (count == 16) LCD_Move(1, 0);
}

}

inline unsigned char get_random_number() { unsigned char rand = 0; // One while (RB0 == 0) rand++; while (RB0 != 0) rand++; // Two while (RB0 == 0) rand++; while (RB0 != 0) rand++; // Three while (RB0 == 0) rand++; while (RB0 != 0) rand++;

return rand % 12;

}

inline void wait_for_it() { LCD_Inst(0x01); // Clears Wait_ms(5000 / 4); LCD_Write('.'); // Thinking a little Wait_ms(5000 / 4); LCD_Write('.'); // Thinking a little more Wait_ms(5000 / 4); LCD_Write('.'); // Really starting to think! Wait_ms(5000 / 4); }



8. 

![](Images/HW04_4.gif)