Menu

LottoGen for PIC18F25K22

Bernd
2017-07-30
2017-07-31
  • Bernd

    Bernd - 2017-07-30

    Hi,after playing around a bit with GCB I maneged my first little Program for generating Numbers between 1 and 49.
    In Germany that's the Lottozahlen :-)
    Nothing important, but I didn't found no example Code for the 18F25K22 so I made one.

    ''A demonstration program for GCGB and StartPic18
    '''--- https://pic-projekte.de/blog/startpic18/
    '''--------------------------------------------------------------------------------------------------------------------------------
    '''This program named LottoGen demonstrates the capabilities of a LCD display.
    '''It shows the usage of Hardware PWM, Random and Key press
    '''This Program schow for every Key pressed a Random Integer between 1 and 49
    '''The LCD is using an 4-bit bus for connectivity.
    '''For the LCD connection - please refer to the program.
    '''@author  DarkDau 
    '''@licence GPL
    '''@version 1.0
    '''@date    30.07.2017
    '''********************************************************************************
    'Set chip model required:
    
    #chip 18f25k22, 16
    ' the 16 in the previous chip statement means the hint for the compiler for computing delays.
    ' It does NOT set the Hardware in any way!
    #config FOSC = INTIO67,WDTEN = OFF,MCLRE = EXTMCLR, PWRTEN = ON  ,CPD_ON
    'FOSC = INTIO67   // Internal Oscillator, 16 Mhz (the default)
    'PWRTEN = OFF     // Power Up Timer (Disabled)
    'CPD = OFF        // Data EE Read Protect (Disabled)
    
    'Optional, to shrink down the resulted HEX output
    #define USE_HPWMCCP2 FALSE
    #define USE_HPWMCCP3 FALSE
    #define USE_HPWMCCP4 FALSE
    
    'led 2
    Dir PORTB.5 Out
    PORTB = b'00100000'
    wait 500 ms
    'Now toggle the LEDs
    PORTB = b'00000000'
    wait 500 ms
    
    'Setup LCD Parameters
    #define LCD_IO 4
    '#define LCD_NO_RW ' Auf dem StartPic wird RW benutzt!
    #define LCD_Speed fast
    '--- Define Hardware settings for StartPic18 
    
    #define LCD_RS PORTA.6
    #define LCD_Enable PORTA.4
    #define LCD_DB4 PORTA.0
    #define LCD_DB5 PORTA.1
    #define LCD_DB6 PORTA.2
    #define LCD_DB7 PORTA.3
    #define LCD_RW  PORTA.5
    
    'Set the CCP1 pin to output mode in order to drive LCD Brightness
    'Address is for StartPic18 only!
    DIR PORTC.2 out
     ' Key PB  for StartPic18 only!
    #define DOWN        0
    #define UP          1
    #define SWITCH      PORTB.0
    'setup switch (PB)
    dir SWITCH in
    
    ' ----- Main body of program commences here.
        CLS
    '      12345678
        PRINT "LottoGen"
        locate 1,0
        PRINT "Press PB"
    'Turn up brightness to a middle value in ca 1 second
        For Bright = 1 to 100
            HPWM 1, 40, Bright
            wait 10 ms
        next
    firstrun =1
    Do Forever
        if ( SWITCH = DOWN ) then
           wait 10 ms
           if ( SWITCH = DOWN ) Then
                ' Key PB pressed 
                if firstrun then
                    ' Randomize_seed after the first key pressed, 
                    ' because that makes randomize more random :-)  
                    #define RANDOMIZE_SEED
                    firstrun = 0
                end if
                ' Light up LED2
                PORTB = b'00100000'
                ZAHL= random
                DO  until ZAHL > 0
                    ZAHL = random 
                LOOP
                if ZAHL < 50 then       
                    Locate 0,0
                    '12345678
                    print "        "
                    locate 0,0
                    Print ZAHL
                end if
            end if
            else
                ' Key PB not pressed LEDs off
                PORTB = b'00000000'
           end if
    loop
    

    Chip resource usage:

    Chip Model: 18F25K22

    Program Memory: 1070/16384 words (6.53%)

    RAM: 92/1536 bytes (5.99%)

    I'm not the owner or creator of StartPic18, but I'm using it as my Dev Platform.
    I do have some PCB left over, I give away them for a few Bucks, just contact me.

     

    Last edit: Bernd 2017-07-30
  • Anobium

    Anobium - 2017-07-31

    Good luck winning the Lotto!!

     
  • Bernd

    Bernd - 2017-07-31

    Yes thats sounds good, Wednesday evening I will know more ;-)
    Bw: I am not a programmer (anymore) so if you see improvements for this tiny code I would appreciate it.

     
  • Anobium

    Anobium - 2017-07-31

    Yourc code looks great. Clear and structured.

    Let us know the result of the Lotto!!!

     
  • stan cartwright

    stan cartwright - 2017-07-31

    Bed, you could try - random1to49=random * 100 : random1to49 = random1to49 / 530 +1
    This will give random as 1 to 49 if as whole numbers -- integers.
    I also use a 18f25k22. Many pins to experiment with and GCB makes it easy.
    ...not to win the lotto though :)

     

    Last edit: stan cartwright 2017-07-31
  • Bernd

    Bernd - 2017-07-31

    Yeah good tip, but I thought only using 1 third of numbers realm would be ok, because of the speed.
    The spread of randomness should be the same?
    The downside of your method is using Math library, I guess it will blow off my code, but I will try it.
    It is just a programming exercise.

    Ok, the Difference is not that big as I have expected:
    Chip resource usage:

    Chip Model: 18F25K22

    Program Memory: 1148/16384 words (7.01%)

    RAM: 93/1536 bytes (6.05%)

    Cool, good hint an learnt again about the cleverness of GCB

     
  • Anobium

    Anobium - 2017-08-03

    A port to a 877a.

    :-)

     
  • bed

    bed - 2017-08-03

    Thanks, will study the improvments @home

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.