Lnp Project

Embed Size (px)

Citation preview

  • 8/11/2019 Lnp Project

    1/12

  • 8/11/2019 Lnp Project

    2/12

    Lottery Number Predictor Perry Andrews Page 2

    Contents

    Introduction....................................................................................................................2

    Design and Development...............................................................................................3

    Construction...................................................................................................................9

    Conclusion ...................................................................................................................12

    References....................................................................................................................12

    Figure 1 - Flow Diagram................................................................................................3

    Figure 2 - Circuit Diagram.............................................................................................8

    Figure 3 - Prototype .......................................................................................................9

    Figure 4 Finished Project ..........................................................................................10

    Figure 5 - Copper Track Breaks...................................................................................11

    Table 1 - Option Register Values...................................................................................4Table 2 - Parts List .......................................................................................................10

    Introduction

    This project has been developed at the request of a family member. They wanted a

    device that could be used to generate random numbers to be used with the UK

    national lottery.

    A 2 digit LED display is used to display a number between 1 and 49. A button is used

    to generate a new random number which is then shown on the display.

  • 8/11/2019 Lnp Project

    3/12

    Lottery Number Predictor Perry Andrews Page 3

    Design and Development

    The design is simple. Some of the program we have used before in the dice projects.

    The new part is driving a 2 digit display using only 9 outputs. Seven outputs are used

    for the 7 segments plus one output for each digit. The trick is to put the number on the

    first digit then turn it on briefly before putting the second number on the second digit

    and then turning that on briefly. Only one digit is on at a time so you can have a

    different number on each digit. The digits are turned on and off alternately very fast

    so that the eye cannot see the digits being turned on and off.

    The flow diagram below shows the program logic:

    Start

    Set Hardware

    Registers

    Set Displays to 0

    Has button beenpressed?

    Has button beenreleased?

    Display variable

    Increment variableHas variable reached

    50?Reset variable to 1

    NO

    YES YES

    YES

    NO NO

    Figure 1 - Flow Diagram

    The display code using the interrupts has been borrowed from Rob Miles who wrote

    the C for PIC Micros course. When the code is already there and working why re-

    invent the wheel? This is not shown in the flow diagram above.

  • 8/11/2019 Lnp Project

    4/12

    Lottery Number Predictor Perry Andrews Page 4

    The timer pre-scaler is set as follows:

    Four clock pulses are used to execute an instruction so the clock updates at of the

    crystal frequency. The crystal we are using is 4MHz so the clock is updated at 1MHz.

    The pre-scaler is set to divide by 32 which gives 1000000 / 32 = 31,250Hz.

    The interrupt is generated each time the clock reaches 256 so we divide the number

    above by 256 which gives us 31,250 / 256 = 122Hz. The interrupt occurs 122 times

    every second. As we are not timing anything this is accurate enough.

    To set the pre-scaler to divide by 32 we set the Option Register to 196.

    The table below gives the Option Register values:

    PS2 PS1 PS0 Divide by Value

    0 0 0 2 1920 0 1 4 193

    0 1 0 8 194

    0 1 1 16 195

    1 0 0 32 196

    1 0 1 64 197

    1 1 0 128 198

    1 1 1 256 199

    Table 1 - Option Register Values

  • 8/11/2019 Lnp Project

    5/12

    Lottery Number Predictor Perry Andrews Page 5

    The C program based on the flow diagram is shown below:

    /* Lottery Number Predictor */

    /* (c) Perry Andrews Jan. 2005 */

    /* 1st Revision */

    constunsignedcharenable [2] = { 1, 2 } ;

    constunsignedcharpatterns [10] =

    /* 0 1 2 3 4 5 */

    { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x99,

    /* 6 7 8 9 */

    0x83, 0xf8, 0x80, 0x98 } ;

    /* number of LEDs in our display */

    #defineDISPLAY_SIZE 2

    /* segment patterns for our LEDs */

    unsignedcharsegments [DISPLAY_SIZE] ;

    /* counter used by our refresh */

    unsignedcharled_counter = 0 ;

    /* each time refresh is called it */

    /* sets the display for a led and */

    /* moves on to the next */

    voidrefresh ( void) {

    /* turn off all the LEDS */

    PORTA = 0 ;/* set segments for the led */

    PORTB = segments [led_counter] ;

    /* turn the led on */

    PORTA = enable [led_counter ] ;

    /* move on to the next led */

    led_counter = led_counter + 1 ;

    /* see if we fell off the end */

    if( led_counter == DISPLAY_SIZE ) {

    led_counter = 0 ;

    }

    }

    /* display just loads the pattern */

    /* into the segment. refresh will */

    /* read it later */

    voiddisplay ( unsignedchardigit,

    unsignedcharpos ) {

    segments [pos] = patterns [digit] ;

    }

    voidinterrupt( void) {

    /* if the timer has overflowed */

    /* bit 2 of INTCON is set high */

    if( INTCON & 4 ) {/* clear the bit to turn */

  • 8/11/2019 Lnp Project

    6/12

    Lottery Number Predictor Perry Andrews Page 6

    /* off this interrupt */

    clear_bit( INTCON, 2 );

    /* refresh display */

    refresh();

    }

    }

    unsignedcharget_button ( void) {

    unsignedchari;

    unsignedcharoldv, newv ;

    oldv = input_pin_port_a (4 ) ;

    i = 0 ;

    while( i < 20 ) {

    newv = input_pin_port_a (4 ) ;if( oldv == newv ) {

    i++ ;

    }

    else{

    i = 0 ;

    oldv = newv ;

    }

    }

    /* we have a steady value */

    returnoldv ;

    }

    voidmain(void)

    {

    unsignedinti ;

    unsignedintj ;

    unsignedcharr;

    unsignedcharr1;

    unsignedcharr2;

    /* Select the Register bank 1 */

    set_bit ( STATUS, RP0 ) ;

    /* set all of PORTB for output */TRISB = 0x00 ;

    /* set 0 & 1 of PORTA for output */

    TRISA = 0x1c ;

    /* Configure the OPTION register */

    /* to produce timer interrupts */

    /* and set the pre-scaler */

    OPTION_REG = 196;

    /* now use Register bank 0 */

    clear_bit ( STATUS, RP0 ) ;

    /* Configure the INTCON register */

  • 8/11/2019 Lnp Project

    7/12

    Lottery Number Predictor Perry Andrews Page 7

    /* to enable timer interrupts */

    INTCON = 160;

    // set all digits to zero

    display (0,0);

    display (0,1);r = 1;

    // Main program loop

    while(1) {

    // Wait for Switch A4

    while( get_button () == 0 ) ;

    while( get_button () == 1 ) {

    // generate a random number

    r = r + 1;

    if( r == 50 )

    r = 1;

    }// Display number

    r2 = r % 10;

    r1 = r / 10;

    display (r1,0);

    display (r2,1);

    }

    }

    The program has five sub programs including main which all C programs must

    have. main is the first program to start and this is where the main program logic

    resides. The other sub programs are:

    refresh this is called from the interrupt sub program to refresh the displays.

    display used to load the display with the required numbers.

    interrupt this is called every time the interrupt is triggered.

    get_button gets the button pressed on port RA4 after de-bouncing first.

    The main program first sets the required number of outputs and sets up the timer to

    generate an interrupt about 100 times every second. Each digit is refreshed at about 50

    times a second (as there are two digits) which is more than enough to avoid flicker.

    It then sets the displays to 0 and waits for the button. After the button is pressed a

    loop continuously updates a variable by adding 1 to its previous value. When thevariable reaches 50 it is reset to 1. This is done as fast as the program can run so a

    number of iterations can be completed in the briefest of button presses. When the

    button is released the number in the variable is displayed. The number is random

    because you are not able to press the button for exactly the same amount of time on

    each press.

    The program uses about 218 blocks of the 1024 blocks allowed.

  • 8/11/2019 Lnp Project

    8/12

    Lottery Number Predictor Perry Andrews Page 8

    The circuit below uses two transistors to switch the display digits. Outputs RA0 and

    RA1 are switched on alternately by the interrupt routine. The rest of the circuit is

    similar to the 7-segment dice project:

    100

    22pf

    IN3

    OUT1

    GND

    2

    78L05

    9V

    4MHz100

    100

    100

    100

    100

    100

    22K

    22pf

    1K100

    10n100n

    S1 = Select Number

    S1

    a

    b

    c

    d

    e

    f

    g

    a

    b

    c

    d

    e

    f

    g

    Q1

    BC337

    Q2

    BC337

    1K

    1K

    a

    b

    c

    d

    e

    f

    g

    Dual Common Anode Display

    OSC116

    OSC215

    RB0

    RB1

    RB2

    RB3

    6

    7

    8

    9

    Vcc1

    14

    MCLR4

    RA43

    RB4

    RB5

    RB6

    RB7

    10

    11

    12

    13

    PIC16F84A

    RA32

    RA21

    18RA1

    17 RA0 GND

    5

    Figure 2 - Circuit Diagram

    SW1 is used to pick a random number.

  • 8/11/2019 Lnp Project

    9/12

    Lottery Number Predictor Perry Andrews Page 9

    Construction

    After testing the program on the PIC development board I collected together all the

    required components and built the test circuit on breadboard. This is an easy way to

    test all the components and to give an idea of layout in the final permanent

    construction. The breadboard is shown below:

    Figure 3 - Prototype

    The transistors can be any general purpose NPN type transistor such as BC108 or BC

    548. I used BC337 because I had them spare.

    This project will be constructed using Vero strip board, as it is quick to work with. I

    intend putting the final PCB into a box so the project can be used by other family

    members. A parts list is shown on the next page:

  • 8/11/2019 Lnp Project

    10/12

    Lottery Number Predictor Perry Andrews Page 10

    Description Quantity Maplin Code

    Vero Strip Board 1 pc 100mm x 60mm JP49D

    PIC16F84A 1 VS87U

    2 Digit Common Anode Display 1 BY66W

    BC337 NPN Transistors 2 QB68Y4MHz Crystal 1 RR83E

    22pf Capacitors 2 RA34M

    100nf Capacitor 1 RA49D

    10nf Capacitor 1 RA44X

    1K0 Resistor 3 M1K

    100R Resistor 8 M100R

    22K Resistor 1 M22K

    78L05 5v Regulator 1 QL26D

    PCB Switch 1 KR88V

    18Pin DIL Socket 1 HQ76HTable 2 - Parts List

    The final project is shown below:

    Figure 4 Finished Project

    You can use the picture above to help locate the components on the Vero board. Youonly need to know where the breaks in the copper tracks are. The board as pictured

    above and below have the copper tracks running from top to bottom (vertically). The

    breaks are shown by red dots in the picture on the following page:

  • 8/11/2019 Lnp Project

    11/12

    Lottery Number Predictor Perry Andrews Page 11

    Figure 5 - Copper Track Breaks

    The breaks are made on the under side (the copper side obvious really).

    Solder the components in the following order:

    1. Links2. Resistors3. IC socket4. Display5. Switch6. Capacitors & Crystal7. Transistors and Regulator8. Battery connections

    Check for solder splashes and the breaks are in the correct place. Test the regulator

    output voltage is 5V before plugging in the PIC microcontroller. If everything is ok

    plug in the PIC and connect the battery

  • 8/11/2019 Lnp Project

    12/12

    Lottery Number Predictor Perry Andrews Page 12

    Conclusion

    This is a basic design that just allows a random number to be displayed on a 2 digit

    display. The number will be between 1 and 49. A number of enhancements could be

    made to this basic design such as:

    Animate the display while the button is pressed

    Use sleep to put the circuit into a low power mode to save the battery.

    Make the number range configurable

    These will be subject to future projects.

    References

    Matrix Multimedia Ltd - http://www.matrixmultimedia.co.uk/C for PICmicro microcontrollers by Rob Miles

    PIC Tutor development board

    Maplin Electronic Supplies - http://www.maplin.co.uk/

    Supplier of all components including the PIC development board.

    Crownhill Associates - https://www.crownhill.co.uk/index.php

    Supplier of PIC developer boards and PIC Micros.

    Pelnet http://www.pelnet.co.uk/elect/index.html

    My electronics page where this project and others can be downloaded.