The Random header that I'm looking at needs a little help. First try dimensioning the array "dim RND(1)" and constant "RND(1) = 5" in main, just to see if it works.
For the stated function, the array would need to be dimensioned to "dim RND(255)", if I read that right? Does your PIC have enough Ram for this?
There is a circular dependency built into your function. Random cannot be evaluated without knowing the seed number first. But RND(RandomMax) seed needs to know Random!! Suggest trying one calculation first off a known seed, then passing that Random to a variable to be used in the function.
Or last but not least, you have hit the dreaded 2k page boundary limit of the 16f devices. Check your programmer data memory, or the .lst file to see the usage. The 18f's are the way to go for large programs.
Regards,
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi thanks for the advice. The RANDOM comes from the random.h file and randommax is nothing more then the maximum number I want to get back (ie 6 for a six sided dice). There is a chance, in fact it is probably likely, that I am not setting up random.h properly. Here is the whole program which may (or may not LOL) put it into context:
'================================================================
'===== SET DIRECTION OF THE PORTS
'================================================================
Dir Pin1 IN
Dir Pin2 IN
Dir Pin3 IN
'Pin4 ===> MCLR
'Pin5 ===> Vss
Dir Pin6 OUT
Dir Pin7 OUT
Dir Pin8 OUT
Dir Pin9 OUT
Dir Pin10 OUT
Dir Pin11 OUT
Dir Pin12 OUT
Dir Pin13 OUT
'Pin14 ===> Vdd
'Pin15 ===> OSC2
'Pin16 ===> OSC1
Dir Pin17 IN
Dir Pin18 IN
Startup:
Wait StartUpDelay
INITRANDOM
'================================================================
'===== MAIN PROGRAM LOOP
'================================================================
Main:
rnd_no= RND(6)
portb=rnd_no+1
WAIT 1 s
Goto Main
'================================================================
'===== SUBROUTINES AND FUNCTIONS
'================================================================
function RND(RandomMax)
RND = Random % RandomMax
end function
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Why not just use one of your input pin, a button, and a timer? Check out the Timer section in help. Take the result and send to to PortB. Better yet, divide the timer result into six equal bins (with conditional statements like IF), and send the result to a seven segment display.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am using the "RANDOM" function in random.h but when I use it I get the compiler giving the error:
Aborting due to runtime error 6 (out of bounds array access) at line 1368 of gcbasic.bas::ADDSUBLINE()
I have the function in a sub:
function RND(RandomMax)
RND = Random % RandomMax
end function
Although it happens even if I call RANDOM in the main program as well
Any ideas as to the problem?
Cheers!
SR
The Random header that I'm looking at needs a little help. First try dimensioning the array "dim RND(1)" and constant "RND(1) = 5" in main, just to see if it works.
For the stated function, the array would need to be dimensioned to "dim RND(255)", if I read that right? Does your PIC have enough Ram for this?
There is a circular dependency built into your function. Random cannot be evaluated without knowing the seed number first. But RND(RandomMax) seed needs to know Random!! Suggest trying one calculation first off a known seed, then passing that Random to a variable to be used in the function.
Or last but not least, you have hit the dreaded 2k page boundary limit of the 16f devices. Check your programmer data memory, or the .lst file to see the usage. The 18f's are the way to go for large programs.
Regards,
Kent
Hi thanks for the advice. The RANDOM comes from the random.h file and randommax is nothing more then the maximum number I want to get back (ie 6 for a six sided dice). There is a chance, in fact it is probably likely, that I am not setting up random.h properly. Here is the whole program which may (or may not LOL) put it into context:
'================================================================
'===== CHIP SETUP
'================================================================
#chip 16F84A, 4
#define StartUpDelay 10 ms
'================================================================
'===== DEFINE THE PORTS
'================================================================
#define Pin1 PORTA.2
#define Pin2 PORTA.3
#define Pin3 PORTA.4
'Pin4 ===> MCLR
'Pin5 ===> Vss
#define Pin6 PORTB.0
#define Pin7 PORTB.1
#define Pin8 PORTB.2
#define Pin9 PORTB.3
#define Pin10 PORTB.4
#define Pin11 PORTB.5
#define Pin12 PORTB.6
#define Pin13 PORTB.7
'Pin14 ===> Vdd
'Pin15 ===> OSC2
'Pin16 ===> OSC1
#define Pin17 PORTA.0
#define Pin18 PORTA.1
'================================================================
'===== SET DIRECTION OF THE PORTS
'================================================================
Dir Pin1 IN
Dir Pin2 IN
Dir Pin3 IN
'Pin4 ===> MCLR
'Pin5 ===> Vss
Dir Pin6 OUT
Dir Pin7 OUT
Dir Pin8 OUT
Dir Pin9 OUT
Dir Pin10 OUT
Dir Pin11 OUT
Dir Pin12 OUT
Dir Pin13 OUT
'Pin14 ===> Vdd
'Pin15 ===> OSC2
'Pin16 ===> OSC1
Dir Pin17 IN
Dir Pin18 IN
Startup:
Wait StartUpDelay
INITRANDOM
'================================================================
'===== MAIN PROGRAM LOOP
'================================================================
Main:
rnd_no= RND(6)
portb=rnd_no+1
WAIT 1 s
Goto Main
'================================================================
'===== SUBROUTINES AND FUNCTIONS
'================================================================
function RND(RandomMax)
RND = Random % RandomMax
end function
Why not just use one of your input pin, a button, and a timer? Check out the Timer section in help. Take the result and send to to PortB. Better yet, divide the timer result into six equal bins (with conditional statements like IF), and send the result to a seven segment display.