Hi Henrik,
Sorry for the late reply.
I see you've been making a lot of progress, good job!
That'll be down to mackrackit, yourself and a few others, thanks for sticking with me, it's very much appreciated.
Remember that PBP executes one statement at the time. In this case you set the pin high, then you pause, then you start the pulsein statement which waits for a positive going signal which isn't coming since the pin is already set high so it times out and returns zero.
I was running the program through my mind as I was going of to sleep last night and realised
just that.
On top of that, as Mackrackit says, PULSIN is meant to be used on pins configured as inputs, I'm not sure if it works like you have it.
Another mistake, I got the PULSIN idea into my mind and rushed to write some code and overlooked setting the the REGISTER pin to INPUT.Good practice would be to set the I/O REGISTER then start writing code, I'll try and stick with that from now on.
And, why do you need to measure it? Why not simply "print" the value returned by the RANDOM command?
Another gap in my knowledge base I'm afraid, I didn't know you could 'print' the value.
PAUSE works with units of ms if RANDOM returns 100 you divide that by 14 and get 7 so you'll get a pause of 7ms, if RANDOM returns 34567 you divide that by 14 and you'll get 2469ms pause.
Spot on Henrik, just what I needed to know.
I'm trying to write a program for a project which must have a RANDOM delay of zero to three seconds.
So how about this:
65535 / 21.845 gives 3000ms as a Maximum value. Any number generated will be lower than 65535 therefore shorter than 3 seconds!
Code:
ANSEL = %00000000 'Disable analog select so ports work as digital i/o.
CMCON0 = %00000111 'Disable analog comparators.
TRISA = %00000000 'Set PORTA as OUTPUT.
PORTA = %00000000 'Set PORTA pins all low.
TRISC = %00000000 'Set PORTC as OUTPUT.
PORTC = %00000000 'Set PORTC pins all low.
DEFINE OSC 4
X VAR WORD 'Number between 1 & 65535
Y var WORD 'Scaled down value of X
Z VAR BYTE 'The PAUSED value in 10us Increments
main:
RANDOM X 'Random number between 1 & 65535
LET Y = (x /22) 'Scaled down value of X
PAUSE Y 'LED on for time period of RAN_X_OVER_22
HIGH PORTA.2 'LED PORTA.2 lights up
PAUSE 500
low PORTA.2 'LED off
GOTO MAIN: 'Do it all again
Dave
Bookmarks