Tako Kichi
New Member
Hi Folks,
I am in the process of writing my first PIC program (in PIC basic) for a hobby project and I have come across a problem that I just can't seem to resolve. I have literally spent tens of hours searching all the forums, lists, manufacturer data etc and I am still hitting a wall. I am hoping someone in here can point me in the right direction. I have a feeling that this is such a simple error that I am going to kick myself if/when I get it resolved! I am pleased that I have 95% of the program working considering I jumped into programming with both feet (this is NOT an easy first project) but this last little bit is begining to get tiresome :wink:
My program compiles just fine but when I run it through the 'Pic Simulator IDE' one of the sub-routines (and it just happens to be THE most important one) constantly gives an incorrect operation. I think the problem stems from the 'Random' command and its set-up, although I have completely removed the references to 'Random' in one version of the code and I still get the same error!
I have read all the references to 'Random' and I do understand that it will give the same pseudo random number from the same 'seed' so I was trying to 'seed' it by sampling the TMR0 timer. The idea was that the 'seed' would be placed in the variable just before the 'Random' command so that it will get a different 'seed' each time. I start the timer at the begining of the program so that it can free run off the internal clock (and I can watch it running on the simulator so I know it is running) then I read the TMR0 register and perform the 'Random' operation. The result is then truncated to give a number between 1 and 60. This is then converted to an equivalent time delay (in a loop.......I tried 'Pause' at first but I couldn't get that to work either!) Next I generate a second random number and depending on the value the program should choose between one of two options via an 'If....Then' statement.
My problem is that no matter what I do with regard to the 'Random' command (and even removing it) I still get the same time delay and the same option! I really do not understand what is going on at this point (as I said I am a newbie at this). I have included the pertinent bits of code below (the rest runs fine) and if anyone can spot the error please let me know so that I can stop banging my head against this wall :wink: The PIC in question BTW is a 16F818, using the internal oscillator option.
Thanks for any help.
Larry
-----------------------------
The set-up info..........
'set timer
option_reg = %11011000 'set TMR0 to internal clock
'set variables
RND VAR WORD 'set RND as variable
'set LOOP1 & 2 as variables for loops
LOOP1 VAR WORD 'set LOOP1 as variable
LOOP2 VAR WORD 'set LOOP2 as variable
The problem sub-routine.......
PB3SUB:
Low PBCONTROL 'turn off PB's
Low GLED 'turn off Green LED
High RLED 'turn on Red LED
'NOTE the above three lines work perfectly and the sub-routine falls apart from this point
RND=TMR0 'load TMR0 into RND
Random RND 'randomise RND
RND=(RND*60/65535)+1 'truncate RND TO fall between 1 and 60
For LOOP1=1 TO (RND*1000) 'pause for RND secs
Next LOOP1
RND=TMR0 'load TMR0 into RND a 2nd time
Random RND 'randomise RND a 2nd time
Low RLED 'turn off Red LED
IF RND>=32765 Then PB1SUB 'if RND is higher than mid point run PB1 sub-routine
GoTo PB2SUB 'if RND is lower than mid point run PB2 sub-routine
NOTE: The program ALWAYS gives the same time delay and ALWAYS returns the 'PB2SUB' routine instead of randomly delaying the time and randomly selecting between 'PB1SUB' and 'PB2SUB'
I am in the process of writing my first PIC program (in PIC basic) for a hobby project and I have come across a problem that I just can't seem to resolve. I have literally spent tens of hours searching all the forums, lists, manufacturer data etc and I am still hitting a wall. I am hoping someone in here can point me in the right direction. I have a feeling that this is such a simple error that I am going to kick myself if/when I get it resolved! I am pleased that I have 95% of the program working considering I jumped into programming with both feet (this is NOT an easy first project) but this last little bit is begining to get tiresome :wink:
My program compiles just fine but when I run it through the 'Pic Simulator IDE' one of the sub-routines (and it just happens to be THE most important one) constantly gives an incorrect operation. I think the problem stems from the 'Random' command and its set-up, although I have completely removed the references to 'Random' in one version of the code and I still get the same error!
I have read all the references to 'Random' and I do understand that it will give the same pseudo random number from the same 'seed' so I was trying to 'seed' it by sampling the TMR0 timer. The idea was that the 'seed' would be placed in the variable just before the 'Random' command so that it will get a different 'seed' each time. I start the timer at the begining of the program so that it can free run off the internal clock (and I can watch it running on the simulator so I know it is running) then I read the TMR0 register and perform the 'Random' operation. The result is then truncated to give a number between 1 and 60. This is then converted to an equivalent time delay (in a loop.......I tried 'Pause' at first but I couldn't get that to work either!) Next I generate a second random number and depending on the value the program should choose between one of two options via an 'If....Then' statement.
My problem is that no matter what I do with regard to the 'Random' command (and even removing it) I still get the same time delay and the same option! I really do not understand what is going on at this point (as I said I am a newbie at this). I have included the pertinent bits of code below (the rest runs fine) and if anyone can spot the error please let me know so that I can stop banging my head against this wall :wink: The PIC in question BTW is a 16F818, using the internal oscillator option.
Thanks for any help.
Larry
-----------------------------
The set-up info..........
'set timer
option_reg = %11011000 'set TMR0 to internal clock
'set variables
RND VAR WORD 'set RND as variable
'set LOOP1 & 2 as variables for loops
LOOP1 VAR WORD 'set LOOP1 as variable
LOOP2 VAR WORD 'set LOOP2 as variable
The problem sub-routine.......
PB3SUB:
Low PBCONTROL 'turn off PB's
Low GLED 'turn off Green LED
High RLED 'turn on Red LED
'NOTE the above three lines work perfectly and the sub-routine falls apart from this point
RND=TMR0 'load TMR0 into RND
Random RND 'randomise RND
RND=(RND*60/65535)+1 'truncate RND TO fall between 1 and 60
For LOOP1=1 TO (RND*1000) 'pause for RND secs
Next LOOP1
RND=TMR0 'load TMR0 into RND a 2nd time
Random RND 'randomise RND a 2nd time
Low RLED 'turn off Red LED
IF RND>=32765 Then PB1SUB 'if RND is higher than mid point run PB1 sub-routine
GoTo PB2SUB 'if RND is lower than mid point run PB2 sub-routine
NOTE: The program ALWAYS gives the same time delay and ALWAYS returns the 'PB2SUB' routine instead of randomly delaying the time and randomly selecting between 'PB1SUB' and 'PB2SUB'