Tako Kichi
New Member
Hi Guys,
I have posted a couple of times in the past and had great responses so I hope I can get some more help this time.
This is my first attempt at programming a PIC and it is quite an ambitious project. My parts/programmer (Epic) have finally arrived and I loaded the code with great expectations but unfortunately Mr. Murphy stepped in and decided to mess things up!
When I wrote the code I put it through the demo version of 'PIC Simulator IDE' and after a couple of tweaks it ran perfectly, EVERYTHING worked as it should. However, when I load the code into my PIC 16F818 I can only get two out of the three subroutines to work.
PB1SUB and PB2SUB work fine but PB3SUB executes the first two lines and then hangs for as long as you have the button depressed and then it returns to the main routine. This in itself is odd as the first line should turn off the pushbutton so the length of time it is held should be irrelevant.
Interestingly if I 'rem' out all the lines that refer to the random function and add a line of delay code between 'High RLED' and 'Low RLED' I can get the red LED to come on for that period.
I have played around with different methods of doing this, using different values, using the 'Random' command and not using it, different methods of seeding etc. but nothing seems to work.
I am now stuck as I don't know what to try next. Can anyone make any suggestions on how I can achieve my goal of getting a random time delay in the range 1-30 seconds followed by a random choice of either PB1SUB or PB2SUB.
The code is below........
Thanks,
Larry
I have posted a couple of times in the past and had great responses so I hope I can get some more help this time.
This is my first attempt at programming a PIC and it is quite an ambitious project. My parts/programmer (Epic) have finally arrived and I loaded the code with great expectations but unfortunately Mr. Murphy stepped in and decided to mess things up!
When I wrote the code I put it through the demo version of 'PIC Simulator IDE' and after a couple of tweaks it ran perfectly, EVERYTHING worked as it should. However, when I load the code into my PIC 16F818 I can only get two out of the three subroutines to work.
PB1SUB and PB2SUB work fine but PB3SUB executes the first two lines and then hangs for as long as you have the button depressed and then it returns to the main routine. This in itself is odd as the first line should turn off the pushbutton so the length of time it is held should be irrelevant.
Interestingly if I 'rem' out all the lines that refer to the random function and add a line of delay code between 'High RLED' and 'Low RLED' I can get the red LED to come on for that period.
I have played around with different methods of doing this, using different values, using the 'Random' command and not using it, different methods of seeding etc. but nothing seems to work.
I am now stuck as I don't know what to try next. Can anyone make any suggestions on how I can achieve my goal of getting a random time delay in the range 1-30 seconds followed by a random choice of either PB1SUB or PB2SUB.
The code is below........
Code:
'set timer
option_reg = %11011000 'set TMR0 to internal clock
'set I/O pins
TRISA = %00000000 'set PORTA pins (unused pins as OP)
TRISB = %00010110 'set PORTB pins (unused pins as OP)
'rename pins to logical names
PBCONTROL VAR PORTB.0 'rename RB0 to PBCONTROL
PB1 VAR PORTB.1 'rename RB1 to PB1
PB2 VAR PORTB.2 'rename RB2 to PB2
PB3 VAR PORTB.4 'rename RB4 to PB3
BUZZER VAR PORTB.3 'rename RB3 to BUZZER
GLED VAR PORTB.5 'rename RB5 to GLED
RLED VAR PORTB.6 'rename RB6 to RLED
'set pin status (high/low)
High PBCONTROL 'set PBCONTROL high
Low BUZZER 'set BUZZER low
High GLED 'set GLED high
Low RLED 'set RLED low
'set variables
RND VAR WORD 'set RND as variable
'set LOOP1 & 2 as variables for loop
LOOP1 VAR WORD 'set LOOP1 as variable
LOOP2 VAR WORD 'set LOOP2 as variable
'start main routine
GoTo Main 'goto main routine
'main program
Main:
IF PB1 = 0 Then PB1SUB 'check PB1 status if low go to PB1SUB
IF PB2 = 0 Then PB2SUB 'check PB2 status if low go to PB2SUB
IF PB3 = 0 Then PB3SUB 'check PB3 status if low go to PB3SUB
GoTo Main 'go back to the top
'subroutine list
PB1SUB: Low PBCONTROL 'turn off PB's
Low GLED 'turn off Green LED
High BUZZER 'turn on BUZZER
For LOOP1=1 TO 500 'pause for 5 secs
Next LOOP1
Low BUZZER 'turn off buzzer
High GLED 'turn on Green LED
High PBCONTROL 'turn on PB's
GoTo Main 'return to main prog
PB2SUB: Low PBCONTROL 'turn off PB's
Low GLED 'turn off Green LED
For LOOP1=1 TO 30 'set loop1 length
High BUZZER 'turn on BUZZER
For LOOP2=1 TO 8 'pause
Next LOOP2
Low BUZZER 'turn off BUZZER
For LOOP2=1 TO 8 'pause
Next LOOP2
Next LOOP1 'go back around loop1 until complete
High GLED 'turn on Green LED
High PBCONTROL 'turn on PB's
GoTo Main 'return to main prog
PB3SUB:
Low PBCONTROL 'turn off PB's
Low GLED 'turn off Green LED
High RLED 'turn on Red LED
RND=TMR0 & $1F 'load last 5 bits of TMR0 into RND
Random RND 'randomise RND
RND = (RND // 31) + 1 'Returns a number between 1-30
For LOOP1=1 TO (RND*100) 'pause for RND secs
Next LOOP1
RND=TMR0 & $1F 'load last 5 bits of TMR0 into RND a 2nd time
Random RND 'randomise RND a 2nd time
Low RLED 'turn off Red LED
RND = (RND // 31) + 1 'Returns a number between 1-30
IF RND>=15 Then
GoTo PB1SUB 'if RND is higher than mid point run PB1 sub-routine
Else
GoTo PB2SUB 'if RND is lower than mid point run PB2 sub-routine
EndIF
GoTo MAIN
End 'end of program
Thanks,
Larry