EmbeddedGame Report

Embed Size (px)

Citation preview

  • 7/28/2019 EmbeddedGame Report

    1/20

    Table of Contents

    Type chapter level (level 1) .................................................................................................... 1

    Type chapter level (level 2) ............................................................................................................. 2

    Type chapter title (level 3) .................................................................................................................... 3

    Type chapter level (level 1) .................................................................................................... 4

    Type chapter level (level 2) ............................................................................................................. 5

    Type chapter title (level 3) .................................................................................................................... 6

    Game ReportEmbedded Control

    Sam Ellis

    Sonike Hendricks

    Section 4 - Side B

    10.7.2012

  • 7/28/2019 EmbeddedGame Report

    2/20

    2

    Table of Contents Page

    Introduction 3

    Design Objectives

    Hardware 3

    Software 4

    System Development

    Hardware 5

    Software 6

    Results and Conclusions 8

    References 10

    Participation 10

    Appendix 11

  • 7/28/2019 EmbeddedGame Report

    3/20

    3

    Introduction

    Lab 1-1 and Lab 1-2 were skill building laboratories introducing the SiLabs C8051 EVB

    microcontroller, SDCC C compiler, protoboard, modular C program development, digital inputs

    and outputs, timer overflow interrupts, and analog-to-digital conversions. Push buttons and a

    slide switch inputs were programmed to initiate outputs in the LEDs, BiLED and buzzer. The

    addition of timer overflow interrupts for time-based functions helped create the interactive

    LED-Pushbutton matching game. In this game the player had to push the matching button for

    the LED that was randomly lit. The number of correct matches was displayed at the end of the

    game.

    In Lab 2 the LED-matching game would be further developed with the application of

    analog to digital conversion. The LITEC Hex-Bin game is a hexadecimal-binary conversion game

    where a random hexadecimal number, 4-bits or smaller, is generated. The player has a

    predetermined time period, delay time, to enter its binary equivalent, before the input is then

    checked for correctness. The result for each round is displayed on the command terminal in

    addition to the final score, which is displayed at the end of six rounds.

    The Hex-Bin game will utilize four push buttons for binary input, four LEDs lit to indicate

    the input value at each bit, a slide switch to reset game, and a potentiometer to control gamespeed via an analog input. The lab objective consisted of constructing hardware, while

    developing C code that calculated the delay time using potentiometer, randomly generated the

    hexadecimal number, read and stored the input value, checked input value for correctness,

    stored the result, ended game after six rounds, and displayed games final score.

    Design Objectives - Hardware

    Lab 1-1

    o Familiarization with the protoboard, the use of the multimeter, and the logic probe.o Configuration of a LED on the protoboard to allow it to be switched on /off by one of

    the C8051s digital output ports.

    o Configuration of a bi-color LED to be switched on/off by a pair of the C8051s outputports.

  • 7/28/2019 EmbeddedGame Report

    4/20

    4

    o Configuration of pushbuttons and a slide switch on the protoboard to allow them to beread by the C8051s digital input ports.

    o Configuration of a buzzer to allow it to be turned on/offby one of the C8051s outputports

    Lab 1-2

    o Additional LED output was added to Lab 1-1 circuit.Lab 2

    o Familiarization with the use of the multimeter.o Configuration of a potentiometer to provide variable voltage input to the analog input

    port, Port 1, of the C8051.

    o Addition of two LEDs and two pushbuttons to the Lab 1-2 circuit.Design Objectives - Software

    Lab 1-1

    o Configuration of specific pins on ports 2 and 3 on the C8051 EVB as digital inputs andoutputs.

    o Development of a program that enables C8051 EVB to read the pushbuttons and theslide switch as digital inputs and to use the information to enable the LED, BiLED, and

    buzzer as digital outputs.

    Lab 1-2

    o Configuration of Timer0 for proper timer interrupts using special registry functions.o Creation of a random number function that generates numbers between 0 and 2 with

    no two consecutive numbers repeated.

    o Development of a function that uses random number generation to decide LEDcombination, and then checks correctness of player inputs after 1 second.

    o Development of a main function that repeats the game function 10 times then printsout the player results for all turns.

    Lab 2

    o Configuration of the ADC to read an analog voltage from the potentiometer and convertit into a digital decimal number.

  • 7/28/2019 EmbeddedGame Report

    5/20

    5

    o Edit the previous random number function so that it generates random numbers from0-15.

    o Development of a function that uses the AD conversion of the potentiometer todetermine the game speed.

    o Develop a function that decreases delay_time by 10% for each round of the game.o Development of a function that checks the players input against the games output.o Development of a game function that generates a hexadecimal number that player must

    enter in binary form to the four pushbuttons. It the checks for a correct match, stores

    the result and repeats for six rounds. At the end of the game a final score is printed.

    System Development Hardware

    The circuitry of Lab 2 will be described given that it is comprised of hardware from Lab

    1. A schematic and picture of this circuitry can be found in the report appendix. The Hex-Bin

    Game utilizes four push buttons, four LEDs, a BiLED, a buzzer, a slide switch, a potentiometer,

    and two 74F365 hex buffer chips. The buffer chips are used in order to isolate components

    from the C8051 EVB and protect it. The gates of the buffer chip supply a component with the

    voltage of power sent by the microcontroller. Ports 2 and 3 are used for the digital inputs and

    outputs; LEDs (P2.2, P2.3, P3.5, P3.6), pushbuttons (P2.4, P2.6, P3.0, P3.1), slide switch (P2.0),

    the BILED (P3.3, P3.4), and the buzzer (P3.7). Port 1 is used exclusively to read analog input

    from the potentiometer (P1.1). The pushbuttons and LEDs make up the player input circuit. The

    slide switch, BiLED, and Buzzer the system response circuit, while the potentiometer manages

    game speed.

    The pushbuttons, slide switch and potentiometer are connected directly to the EVB

    Connector pins as there is no need to wire them through the buffer chips. The slide switch is

    used to start the game when switched to the on position. The pushbuttons sense playerdigital inputs to the game. The LEDs are wired through buffer chip gates for isolation purposes.

    They are used to display the players input for each round of the game, becoming lit once a

    matching pushbutton is pressed. The BiLED is wired to a buffer chip also with two digital

    outputs from the microcontroller. The BiLED displays the status of play of the game with

  • 7/28/2019 EmbeddedGame Report

    6/20

    6

    changes in color; green and red. The buzzer is wired to a buffer chip gate and is used to signal

    an incorrect guess for a round. The potentiometer is wired to Port 1 and configured to a

    maximum voltage of 2.4V, and varying the voltage affects game speed. All components have

    matching current limiting resistors for optimal component operation.

    The slide switch being switched to on initiates the game. For a player input the

    pushbuttons represent a 4 bit binary number with logic low at a pushbutton for 1 and logic high

    for 0. The LEDs visually display this entry with an LED being lit for 1 with logic low at the pin and

    remaining unlit for 0 with logic high. The BiLED is turns green during game play and red at all

    other times the game is on, with the appropriate combination of logic low and logic high at the

    BiLED legs. If the player input is incorrect the buzzer sounds until the start of a new round. At

    the end of the game all components turn off until the slide switch is switched off and then

    on again.

    Over all assembly of the hardware went smoothly due to a well-organized protoboard.

    However some problems encountered were errors in EVB pin connections, incorrect orientation

    of LEDs or BiLED. Troubleshooting can be done easily using the multimeter and logic probe

    along side iterated function test software as was done in this experiment with much success.

    System Development

    Software

    Lab 1-1 was the beginning of the integration between hardware and software in

    Embedded Control. After learning about the binary and hexadecimal number systems in

    lecture, the appropriate ports needed to be initialized. This was completed in a port

    initialization function called near the beginning of the main function. The initialization used bit

    masking to set the appropriate bits to either one or zero. The outputs are set to one, while the

    inputs are set to zero and a high impedance state. To set a one, an or bit mask is used, with a

    hex number applying the one to the correct bit of the byte. An and mask is used for the

    opposite.

    Another important aspect of establishing the code was setting the bits to be initialized

    as talked about above. This process assigns certain bits to a hardware item, such as a light

  • 7/28/2019 EmbeddedGame Report

    7/20

    7

    emitting diode or a push button. Along with the code portion, a pin out form was completed in

    parallel with this step to keep everything organized between hardware and software.

    Lab 1-1 was an introduction to turning on LEDs, and a buzzer based upon what push

    buttons were currently pressed. To establish an endless loop to run the program, a while (1)

    command was used in the main function. The while command runs what is inside it as long as

    it is receiving a true signal, and because any number that is not zero is true, the command runs

    until the program is manually stopped. Inside the loop, the program first checks if slide switch

    is on. If the slide switch is not in the on position, pressing the push buttons will have no effect.

    However, is the slide switch is on; the program looks for any of three possible scenarios of the

    push buttons. Based upon which one of the cases is true in the instant, either a red LED will

    shine, a green LED will shine, or a buzzer will be powered. To power an output, a value of zero

    is assigned to it in the software. A BiLED works a little differently; 2 signals must be sent, and if

    they are both the same, no light will show. However, if one value is a zero and the other non-

    zero, the respective color will shine from the BiLED.

    Lab 1-2 was built upon the knowledge gained from Lab 1-1. A lot of new functions

    needed to be added; including the use of an internal timer, a print statement, and a random

    number generator. Lab 1-2 also included a round counter and a variable array to keep track of

    the players score during the game. In the lab, a system clock was used as the internal timer.

    This clock needed to be initialized in a function by selecting the correct clock, clearing the four

    least significant bits, and putting timer zero to mode one. In addition, both the low byte and

    the high byte of register timer zero needed to be cleared. Also, an interrupt initialization

    function needed to be completed to enable interrupts. This was completed by setting bit seven

    of the interrupt special functions register to one. After the setup, a segment of code was

    written to add one to the counting variable for each overflow detected. In the system clock, it

    takes 337 overflows to equal one second. With the overflow calculation, it made it possible to

    use timing events in the game.

    Another important component of the game in Lab 1-2 was the random number

    generator. This was accomplished by using the mod operation to return the remainder of a

  • 7/28/2019 EmbeddedGame Report

    8/20

    8

    number divided by the chosen value. The game array was used to keep track of the players

    score. At the end of each round, a true or a false signal was assigned to the array, and it printed

    a statement according to the answer. The print statement checks the value, and writes the

    appropriate response in the output window (Secure CRT). After all this happens, one is added

    to the round counter in the for statement, which allows the game to run for a total of 10

    turns.

    All the previous knowledge gained from the completion of Lab 1-1 and Lab 1-2 was

    applied to create the base for Lab 2. In addition to the past functions, an audio to digital

    conversion needed to be used with the potentiometer. By using an audio read function, a wait

    time was established based on how much current was detected by the potentiometer. This

    wait value was decreased by ten percent each round to increase the difficulty of the game.

    Another new code item added was the ability to account for the bouncing effect of the push

    buttons. This was completed by setting a new variable equal to the current count value, and

    waiting until the count value surpassed the new variable by seven numbers. If the button was

    truly pressed, it toggles the value of the adjacent LED; turning it off if it was on, and vice versa.

    At the end of the time limit, values are assigned to the four LEDs as if they were the first four

    bits in a byte. The total added value is checked against the randomly generated hexadecimal

    number and the output window displays if the user input the correct answer or not.

    Overall, the software necessary for completing Lab 2 was heavily built off of the work

    put in to the previous two labs. Lab 2 added a couple of more functions, but the most

    challenging part was integrating it all together. By keeping everything organized and

    commented, it was manageable to review the code and see where it was failing during the

    construction. The end result was a working game of changing a hexadecimal number to a

    binary one using push buttons and LEDs that was both challenging and rewarding.

    Results and Conclusions

    No difficulties were met in the development of Lab 1 and all objectives were easily

    satisfied. Lab 2 provided a little more challenge than the previous labs, Labs 1-1 and 1-2,

    requiring smaller iterations of development. After the hardware was assembled, a simple

  • 7/28/2019 EmbeddedGame Report

    9/20

    9

    systems check program was developed to test that all the inputs and outputs were functioning

    correctly. The potentiometer was tested using a multimeter to check and calibrate the

    maximum and minimum voltage to a range of 0V and 2.4V. T he potentiometers analog input

    was then converted to a digital value and used to determine the variable delay_time. The speed

    of each round was changed, as the potentiometer was rotated clockwise and counterclockwise

    between rounds. The player input was then recorded in the sensor functions and checked in the

    check function for correctness.

    One problem encountered was mechanical bouncing in the pushbuttons and slide

    switch, which may cause the microcontroller to believe that the button or switch has been

    pressed multiple times for one push. This problem was solved using a delay of a couple

    overflows in order to avoid the false pushes being read.

    Overall the LITEC Hex-Bin game performed as expected. The decrease in delay_time for

    each round added a nice increase in game difficulty. Matching the hexadecimal output with a

    binary input was very challenging when the game was set to its fastest speed. This Lab has been

    a great hands-on experience and we are happy to continue building further LITEC skills in Lab 3.

  • 7/28/2019 EmbeddedGame Report

    10/20

    10

    References

    P. Schoch, A. Gutin, S. Lee, C. Sankar, LITEC Lab Manual Version 14.2, 2012

    Participation

    Sonike Hendricks was responsible for the hardware sections of the lab and report; including the

    hardware objectives, hardware system development and parts of the results and conclusion. He

    was responsible for Appendix A and D.

    Sam Ellis was responsible for the software sections of the lab and report; including Software

    objectives, software system development, and parts of the results and conclusions section. He

    was also responsible for Appendices B and C.

    Sam Ellis Sonike Hendricks

    ________________________________ ________________________________

  • 7/28/2019 EmbeddedGame Report

    11/20

    11

    Appendix A - Lab 2 Circuitry Schematic

  • 7/28/2019 EmbeddedGame Report

    12/20

    12

    Appendix B - Lab 2 Flow Chart

    Appendix C - Lab 2 C Code

    /* Names: Sam Ellis and Sonike Hendricks

    Section: 04

    Side: B

    Date: September 27, 2012

    File name: Lab 2

    Description: This code randomly generates a hex digit and displays it to the prompt,

    then checks the user's input against the microcontrollers output.

    */

    #include // include files. This file is available online

    #include

    #include

    //-----------------------------------------------------------------------------

    // Function Prototypes

    //-----------------------------------------------------------------------------

    void Port_Init(void); // Initialize ports for input and output

    void Timer_Init(void); // Initialize Timer 0

  • 7/28/2019 EmbeddedGame Report

    13/20

    13

    void Interrupt_Init(void); //Initialize interrupts

    void Timer0_ISR(void) __interrupt 1;

    void ADC_Init(void); //initialize ADC

    unsigned char read_AD_input(unsigned char n); //ADC

    void random(void); //Randomly light an LED

    void check (void); //Check user imput against output

    int sensorSS (void); //Check slide switch

    int sensor1 (void); //Check pushbutton one

    int sensor2 (void); //Check pushbutton two

    int sensor3 (void); //Check pushbutton three

    int sensor4 (void); //Check pushbutton four

    void BiLED (int redGreen); //if zero, red; if one, green

    void Game_Play (void); //stores the game play function

    //-----------------------------------------------------------------------------

    // Global Variables

    //-----------------------------------------------------------------------------__sbit __at 0xB6 LED0; //LED0, associated with Port 3 Pin 6

    __sbit __at 0xB5 LED1; //LED1, associated with Port 3 Pin 5

    __sbit __at 0xA3 LED2; //LED2, associated with Port 2 Pin 3

    __sbit __at 0xA2 LED3; //LED3, associated with Port 2 Pin 2

    __sbit __at 0xB3 BILED1; // BiLED1, associated with Port 3 Pin 3

    __sbit __at 0xB4 BILED2; // BiLED2, associated with Port 3 Pin 4

    __sbit __at 0xB7 BUZZER; // Buzzer, associated with Port 3 Pin 7

    __sbit __at 0xA0 SS; // Slide switch, associated with Port 2 Pin 0

    __sbit __at 0xB0 PB0; // Push button 1, associated with Port 3, Pin 0

    __sbit __at 0xB1 PB1; // Push button 2, associated with Port 3, Pin 1

    __sbit __at 0xA4 PB2; // Push button 3, associated with Port 2, Pin 4__sbit __at 0xA6 PB3; // Push button 4, associated with Port 2, Pin 6

    __sbit __at 0x91 Pot; // Potentiometer, associated with Port 1, Pin 1

    unsigned int counts = 0;

    int correct=0; //number of correct answers;

    int randomN=0; //what is the random number?

    int wait=0; //how long to wait for input

    int i=0; //what number of games played

    int temp=0; //counter place holder

    int hold1; //is button 1 being held? If so stop toggling until not held;

    int hold2; //is button 2 being held? If so stop toggling until not held;

    int hold3; //is button 3 being held? If so stop toggling until not held;

    int hold4; //is button 4 being held? If so stop toggling until not held;

    unsigned int var1 = 0;

    float ADCVal; //pot value [digital]

    float lastADCVal; //what was the pot value for last round

  • 7/28/2019 EmbeddedGame Report

    14/20

    14

    //***************

    void main(void)

    {

    Sys_Init(); // System Initialization

    Port_Init(); // Initialize ports 2 and 3

    Interrupt_Init();

    Timer_Init(); // Initialize Timer 0

    ADC_Init(); //Initialize ADC

    putchar(' '); // the quote fonts may not copy correctly into SiLabs IDE

    Game_Play(); //main game function

    }

    //***************

    void Game_Play(void)

    {

    while(1){

    wait=0;

    ADCVal=lastADCVal=0;

    i=0;

    BiLED(0); //turn biLED off

    LED0=LED1=LED2=LED3=1;

    //printf("Turn Slide Switch on to begin \r\n\r");

    while(sensorSS()); //Wait until slide switch is off

    while(!sensorSS()); //Wait until slide switch is on

    while(i

  • 7/28/2019 EmbeddedGame Report

    15/20

    15

    {

    temp=counts; //sets a number to set up to prevent bouncing

    while((temp+7)>counts); //wait 20ms after button press to

    debounce

    if(sensor1()) //If button was truly pressed,

    {

    LED0=!LED0; //toggle value of corresponding LED;

    hold1=1; //set hold to true;

    }

    }

    else if(!sensor1()) hold1=0;

    if(sensor2()&&(hold2!=1))

    {

    temp=counts;

    while((temp+7)>counts); //wait 20ms after button press to

    debounce

    if(sensor2()) //If button was truly pressed,

    {LED1=!LED1; //toggle value of corresponding LED;

    hold2=1; //set hold to true;

    }

    }

    else if(!sensor2()) hold2=0;

    if(sensor3()&&(hold3!=1))

    {

    temp=counts;

    while((temp+7)>counts); //wait 20ms after button press to

    debounce

    if(sensor3()) //If button was truly pressed,{

    LED2=!LED2; //toggle value of corresponding LED;

    hold3=1; //set hold to true;

    }

    }

    else if(!sensor3()) hold3=0;

    if(sensor4()&&(hold4!=1))

    {

    temp=counts;

    while((temp+7)>counts); //wait 20ms after button press to

    debounce

    if(sensor4()) //If button was truly pressed,

    {

    LED3=!LED3; //toggle value of corresponding LED;

    hold4=1; //set hold to true;

    }

    }

    else if(!sensor4()) hold4=0;

    }

  • 7/28/2019 EmbeddedGame Report

    16/20

    16

    check(); //calls check function to see if user input was correct

    ++i; //add one to the turn counter

    }

    counts=0; //reset timer

    while(counts 4)

    {

    printf("You did well! \r\n");

    }

    else

    {

    printf("You need to study you hex numbers!");

    }

    correct=0;

    }}

    //***************

    void Port_Init(void)

    {

    //Port 3

    P3MDOUT |= 0xF8; //Port 3 Output pins (3,4,5,6 & 7) set to push-pull mode

    P3MDOUT &= 0XFC; //Port 3 Input pins (0 & 1) set to open-drain mode

    P3 |= ~0XFC; //Port 3 Input pins set to high impedance

    //Port 2P2MDOUT |= 0X0C; //Port 2 Output pins (2,3) set to push pull mode

    P2MDOUT &=0XAE; //Port 2 Input pins (4,6) set to open-drain mode

    P2 |= ~0XAE; //Port 2 Input set to high impedance

    //Port 1

    P1MDIN &= ~0X02; //Set P1.1 for analog input

    P1MDOUT &= ~0X02; //Set P1.1 to open drain

    P1 |= 0X02; //Send logic 1 to input pin P1.1

    }

    void ADC_Init(void)

    {

    REF0CN = 0x03; //Set vref to internal 2.4V reference

    ADC1CN = 0x80; //Enable ADC1

    ADC1CF |= 0x01; //set ADC gain to 1;

    }

    void Interrupt_Init(void)

    {

  • 7/28/2019 EmbeddedGame Report

    17/20

    17

    IE |= 0x02; // enable Timer0 Interrupt request

    EA = 1; // enable global interrupts

    }

    //***************

    void Timer_Init(void)

    {

    CKCON |= 0x08; // Timer0 uses SYSCLK

    TMOD &= 0xF0; // clear the 4 least significant bits

    TMOD |= 0x01; // Timer0 mode setting;

    TR0 = 0; // Stop Timer0

    TL0 = 0; // Clear low byte of register T0

    TH0 = 0; // Clear high byte of register T0

    }

    //***************

    void Timer0_ISR(void) __interrupt 1{

    counts++;

    }

    unsigned char read_AD_input(unsigned char n) //ADC

    {

    AMX1SL = n; //set pin 1.n as the analog input for adc

    ADC1CN = ADC1CN & ~0x20; //clear the "Conversion Completed" flag

    ADC1CN = ADC1CN | 0x10; //intiate A/D conversion

    while((ADC1CN & 0x20) == 0x00); //wait for conversion to complete

    return ADC1;

    }

    /******************************************************************************/

    int sensorSS(void)

    {

    if (!SS) return 1; //if SS is on, output true

    else return 0; //else output false

    }

    int sensor1(void)

    {

  • 7/28/2019 EmbeddedGame Report

    18/20

    18

    if (!PB0) return 1; //if PB0 is pressed, output true

    else return 0; //else output false

    }

    int sensor2(void)

    {

    if (!PB1) return 1; //if PB1 is pressed, output true

    else return 0; //else output false

    }

    int sensor3(void)

    {

    if (!PB2) return 1; //if PB2 is pressed, output true

    else return 0; //else output false

    }

    int sensor4(void)

    {

    if (!PB3) return 1; //if PB3 is pressed, output true

    else return 0; //else output false

    }

    void random(void)

    {

    randomN=((rand())%15); //find a random number between 0x00 and 0x0Fprintf("The number is: 0x0%X\r\n", randomN);

    }

    void check (void)

    {

    int value=0;

    if(!LED3) value+=1; //code to calculate the decimal number

    if(!LED2) value+=2; //based on what LEDs are lit up

    if(!LED1) value+=4;

    if(!LED0) value+=8;

    printf("You have entered: 0x0%X\r\n", value);

    if(value==randomN) //if user was right

    {

    correct+=1; //add one to total user score

    printf("You are correct! \r\n\r\n");

    }

    else //if user was wrong

    {

  • 7/28/2019 EmbeddedGame Report

    19/20

    19

    BUZZER=0; //turn buzzer on

    printf("Your answer is incorrect. \r\n\r\n");

    }

    }

    void BiLED (int redGreen) //if zero, off; if one, red; if two, green

    {

    if(redGreen==0)

    {

    BILED1 = 0;

    BILED2 = 0; // Turn the BiLED off.

    }

    if(redGreen==1)

    {

    BILED1 = 0;

    BILED2 = 1; // Turn the BiLED to red.

    }

    else if(redGreen==2){

    BILED1 = 1;

    BILED2 = 0; // Turn the BiLED to green.

    }

    }

  • 7/28/2019 EmbeddedGame Report

    20/20

    Appendix B - Lab 2 Circuitry Photo