# Can someone find the fault in my MikroBasic prog please?

Status
Not open for further replies.

#### bigal_scorpio

##### Active Member
Hi to all,

Thanks to a discussion on general I have been pointed in the right direction to get my project running.

I want to run a Laptop PC in the back of the car and need it to go into standby when the ignition is off and awake when ignition turned on. This seems simple just to send a pulse to a relay that shorts out the switch on the lappy.

I got that far but then got bogged down with the problem as you need to send a pulse to turn on and off according to the ignition position.

I have got as far as the pulse circuit and it was ok, but could get out of sync with a quick on off of the key so I asked the forum for ideas and a member gave me the idea of using the power led for feedback (on steady is on and flashing is standby, so I wrote a bit more code to implement it but it doesn't work as I expected!

GPIO.0 is the ignition feed and GPIO.1 is the feed from the power LED.

Turning on is fine with power to GPIO.0 and all is ok when GPIO.1 is constant.

The problem occurs when I switch off ignition ie GPIO.0 = 0 and GPIO.1 is on, it does send the off pulse needed but then after the delay it goes on sending it even though the ignition is off and the Power LED is only pulsing at about 1hZ.

What am I doing wrong here? I have tried various ways and thought I had it a few times but it just keeps on sending the pulse when it shouldn't.

I must be nearing the flashing limit of the pic now!

The PIC is a 12F629 and the code is below, please have a look.

Code:
program PCtimer
DIM fla as byte
DIM lamp as byte
DIM b as byte
DIM i as byte

CMCON = 07
TRISIO = %00000011
GPIO = %00000000

main:
lamp = 0 'reset lamp count

For b = 1 to 10
delay_ms(200)
fla = (GPIO.1)
lamp = lamp + fla
next b

IF (GPIO.0 = 1) and lamp <9 THEN   'on criteria
goto pulse
End if

IF (GPIO.0 = 0) and lamp >7 THEN   'off criteria
goto pulse
End if

GOTO main

PULSE:

GPIO.2 = 1         'set pulse on GPIO.2 for 1 sec
DELAY_MS(1000)
GPIO.2 = 0

GPIO.4 = 1         'show by LED on GPIO.4 that delay is working

For i = 1 to 5   'set period to wait before checking status again
DELAY_MS(1000)
NEXT i

GPIO.4 = 0         'turn off delay LED
GOTO main

End.

Al

#### bigal_scorpio

##### Active Member

Hi to all,

Please, please have a look at my problem, I'm totally baffled why it doesn't do what it should.

Al

#### Pommie

##### Well-Known Member
I'm assuming that your lamp (LED) flashes at a given rate. You are looking at it in a random way. I don't know if flashing is standby or on but I'm thinking you need to know if it's flashing or not.

Here is my guess at the code you need,
Code:
Steady=true		'set steady true
For b = 1 to 200	'watch for 2 seconds
delay_ms(10)	'a small delay
if(GPIO.1=0) then	'if not on then
Steady=False    'it's not an always on LED
endif
next b

' at this point if Steady=true then it's not flashing

Mike.

#### bigal_scorpio

##### Active Member
Hi Mike,

Thanks for the much needed reply.

I have never used false and though I can see how it is much better the way you do it I am just a little baffled about how to integrate your result into my program?

1) What do I need to "DIM" steady as byte, word etc

2) What if the 1 to 200 cycle ends on a true? or does the instance of ANY false make the steady= false regardless of where in the cycle it is?

I have tried what I thought would work below but the pulse still recurs every 3 seconds or so even though GPIO.0 and 1 are held low.

Code:
program PCtimer
DIM lamp as byte
DIM b as byte
DIM i as byte

CMCON = 07
TRISIO = %00000011
GPIO = %00000000

main:
lamp = 0 'reset lamp count

For b = 1 to 200	'watch for 2 seconds
delay_ms(10)	'a small delay
if(GPIO.1=0) then	'if not on then
Steady=False    'it's not an always on LED
end if
next b

IF (GPIO.0 = 1) and steady = false THEN   'on criteria
goto pulse
End if

IF (GPIO.0 = 0) and steady = true THEN   'off criteria
goto pulse
End if

GOTO main

PULSE:

GPIO.2 = 1         'set pulse on GPIO.2 for 1 sec
DELAY_MS(1000)
GPIO.2 = 0

'GPIO.4 = 1         'show by LED on GPIO.4 that delay is working

'For i = 1 to 5   'set period to wait before checking status again
'DELAY_MS(1000)
'NEXT i

'GPIO.4 = 0         'turn off delay LED
GOTO main

End.

Any ideas.......Al

Last edited:

#### Pommie

##### Well-Known Member
I don't know MicroBasic but you can make steady a byte and set it to 0 for false and 1 for true.

The loop waits 2 seconds and if the pin goes low at any time then steady is false.

The best way to do this is to break out of the for .. next loop when a pulse is detected but I don't know how to do that in MicroBasic.

Mike.

#### bigal_scorpio

##### Active Member
Hi to all,

Mike, I have tried your method but still no success.

Something in my code is letting the pulse routine be accessed all the time when both inputs are low. DOH!!!

Can anyone who knows how show me a routine in MikroBasic that can check if a pin is permanently high or high low high low etc?

I really need to get to grasp with this as it is the last thing holding up my project.

Thanks.........Al

#### Pommie

##### Well-Known Member
I can't see why it doesn't work.

You could try adding parentheses to the comparisons,
IF (GPIO.0 = 1) and (steady = false) THEN

Mike.

Status
Not open for further replies.

Replies
3
Views
2K
Replies
3
Views
2K
Replies
18
Views
4K
Replies
23
Views
4K
Replies
7
Views
1K