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.
''AdemonstrationprogramforGCGBandStartPic18'''--- https://pic-projekte.de/blog/startpic18/'''--------------------------------------------------------------------------------------------------------------------------------'''This program named LottoGen demonstrates the capabilities of a LCD display.'''ItshowstheusageofHardwarePWM,RandomandKeypress'''This Program schow for every Key pressed a Random Integer between 1 and 49'''TheLCDisusingan4-bitbusforconnectivity.'''For the LCD connection - please refer to the program.'''@authorDarkDau'''@licence GPL'''@version1.0'''@date 30.07.2017'''********************************************************************************'Set chip model required:#chip 18f25k22, 16'the16inthepreviouschipstatementmeansthehintforthecompilerforcomputingdelays.' It does NOT set the Hardware in any way!#config FOSC = INTIO67,WDTEN = OFF,MCLRE = EXTMCLR, PWRTEN = ON ,CPD_ON'FOSC=INTIO67//InternalOscillator,16Mhz(thedefault)'PWRTEN = OFF // Power Up Timer (Disabled)'CPD=OFF//DataEEReadProtect(Disabled)'Optional, to shrink down the resulted HEX output#define USE_HPWMCCP2 FALSE#define USE_HPWMCCP3 FALSE#define USE_HPWMCCP4 FALSE'led2DirPORTB.5OutPORTB=b'00100000'wait500ms'Now toggle the LEDsPORTB = b'00000000'wait 500 ms'SetupLCDParameters#define LCD_IO 4'#define LCD_NO_RW 'AufdemStartPicwirdRWbenutzt!#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'SettheCCP1pintooutputmodeinordertodriveLCDBrightness'Address is for StartPic18 only!DIR PORTC.2 out 'KeyPBforStartPic18only!#define DOWN 0#define UP 1#define SWITCH PORTB.0'setup switch (PB)dir SWITCH in'-----Mainbodyofprogramcommenceshere.CLS' 12345678 PRINT "LottoGen" locate 1,0 PRINT "Press PB"'Turnupbrightnesstoamiddlevalueinca1secondForBright=1to100HPWM1,40,Brightwait10msnextfirstrun=1DoForeverif(SWITCH=DOWN)thenwait10msif(SWITCH=DOWN)Then' Key PB pressed if firstrun then 'Randomize_seedafterthefirstkeypressed,' because that makes randomize more random :-) #define RANDOMIZE_SEED firstrun = 0 end if 'LightupLED2PORTB=b'00100000'ZAHL=randomDOuntilZAHL>0ZAHL=randomLOOPifZAHL<50thenLocate0,0'12345678 print " " locate 0,0 Print ZAHL end if end if else 'KeyPBnotpressedLEDsoffPORTB=b'00000000'endifloop
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.
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
Good luck winning the Lotto!!
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.
Yourc code looks great. Clear and structured.
Let us know the result of the Lotto!!!
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
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%)
Cool, good hint an learnt again about the cleverness of GCB
A port to a 877a.
:-)
Thanks, will study the improvments @home