Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:55 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Multi-pin OR problem.....

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 01:12am 23 Sep 2019
Copy link to clipboard 
Print this post

Hello.

I have an issue here where I need to check TWO pins on a battery charger module - CHG and STBY.  The pins are setup as inputs.

The code has to check both pins as part of an OR to see if the pins are in one of two states.  This is not working, even though testing of the pins at the command prompt under both states does reflect what should be going on.

TRUTH TABLE:

CHG    STBY    FUNCTION
-----------------------------
0      0       Not on charger
0      1       Charging
1      0       Standby(charging finished.  Only while ON charger)
1      1       (never happens)

Screenshot of my querying MMBASIC directly at the console:



The first query is while the unit is on charge, the second query is after the charging is complete.  You will see that this agrees with the table above.

But the problem is that when PIN(CHG)=1 and PIN(STBY)=0, the loop is NOT exiting to the next line.  The charger has finished charging, but my code keeps on fading the red LED in and out indicating it is still charging.  The LED should go BLUE when the charger module indicates the charging is complete.

This does not happen, even though the pins are in the correct state.

The second part of the OR is if both pins are now zero.  That means the charger has had its power removed - the unit has been unplugged from the charger.

Here is the full charging loop code:

  Quote      If Pin(CHG)=0 and PIN(STBY)=1 Then
     BFLAG=
0 'Clear battery flag now we are back on the charger
     Print "Charging battery."
     
Do
       
For X=1 To 100 'Fade up red LED
         PWM 1,100,0,X,0 'Green/Blue/Red on 1A/1B/1C
         Pause 2
       
Next
       
Pause 250 'Small delay before fading down
       For X=100 To 0 Step -1 'Fade down red LED
         PWM 1,100,0,X,0
         
Pause 2
       
Next
       
Pause 250 'Small delay before fading up
       Watchdog 10000
     
Loop Until (pin(CHG)=1 and pin(STBY)=0) OR (pin(CHG)=0 and Pin(STBY)=0) 'Do this till charging finished
     PWM 1,STOP 'Stop PWM channel for LED fading
     If Pin(CHG)=1 and PIN(STBY)=0 THEN
       
SetPin RLED,DOUT:SetPin GLED,DOUT:SetPin BLED,DOUT 'Reset LED pins
       Pin(RLED)=0:Pin(GLED)=0:Pin(BLED)=1 'Fully charged.
       Print "Charging cycle complete."


Can anyone help me understand why this is not working as expected?

IE: When pins CHG and STBY are 1 and 0 respectively, OR 0 and 0 respectively, the loop should exit - it does not when CHG is 1 and STBY is zero, but it DOES if I pull out the charger, and then both pins are zero.
Smoke makes things work. When the smoke gets out, it stops!
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 01:25am 23 Sep 2019
Copy link to clipboard 
Print this post

deleted - didn't look close enough at the posted code
Edited 2019-09-23 11:26 by erbp
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1114
Posted: 01:37am 23 Sep 2019
Copy link to clipboard 
Print this post

G,

I can't see anything wrong (but that doesn't mean a lot   ), but you could try sprinkling some print statements around the loop area to show you what is going on.

I would also take the watchdog out temporarily so there is no restart happening somewhere.

I assume the leds are fading as required?


If Pin(CHG)=0 and PIN(STBY)=1 Then
'****
print "On entry, Charge ",Pin(Chg),"Stby ",pin(Stby)
'****
     BFLAG=0 'Clear battery flag now we are back on the charger
     Print "Charging battery."
     Do
       For X=1 To 100 'Fade up red LED
         PWM 1,100,0,X,0 'Green/Blue/Red on 1A/1B/1C
         Pause 2
       Next
       Pause 250 'Small delay before fading down
       For X=100 To 0 Step -1 'Fade down red LED
         PWM 1,100,0,X,0
         Pause 2
       Next
       Pause 250 'Small delay before fading up
       Watchdog 10000
     Loop Until (pin(CHG)=1 and pin(STBY)=0) OR (pin(CHG)=0 and Pin(STBY)=0) 'Do this till charging finished

'****
print "On exit, Charge ",Pin(Chg),"Stby ",pin(Stby)
'****

     PWM 1,STOP 'Stop PWM channel for LED fading
     If Pin(CHG)=1 and PIN(STBY)=0 THEN
       SetPin RLED,DOUT:SetPin GLED,DOUT:SetPin BLED,DOUT 'Reset LED pins
       Pin(RLED)=0:Pin(GLED)=0:Pin(BLED)=1 'Fully charged.
       Print "Charging cycle complete."

panky.
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 02:53am 23 Sep 2019
Copy link to clipboard 
Print this post

We are missing the ENDIF's to follow the program but your test condition can be simplified
  Quote          LOOP UNTIL (PIN(CHG)=1 AND PIN(STBY)=0) OR (PIN(CHG)=0 AND PIN(STBY)=0) 'Do this till charging finished
       
       
LOOP UNTIL PIN(STBY)=0 'Do this till charging finished


The second line is functionally the same and easier to follow.

I prefer to do something like this:

  Quote    
 
DO
   chargeState =
PIN(CHG)*2 + PIN(STBY)
   
SELECT CASE chargeState
     
CASE 0
       
' Not on charger
     CASE 1
       
' Charging
     CASE 2
       
' Standby(charging finished.  Only while ON charger)
       quit = 1
     
CASE 3
       
' should never happen
   END SELECT
   
 
LOOP UNTIL quit = 1



but it depends on the rest of the program.
VK7JH
MMedit
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 05:18am 23 Sep 2019
Copy link to clipboard 
Print this post

Jim Is spot on above (as usual).

There is another way...

using the Port function returns an absolute value:

Select Case Port(CHG,1,STBY,1)
 Case 0 ' both pins 0
 Case 1 ' only CHG is hi
 Case 2 ' only STBY is hi
 Case 3 ' both pins hi
End Select

jus' sayin'
Edited 2019-09-23 15:24 by CaptainBoing
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 11:03pm 23 Sep 2019
Copy link to clipboard 
Print this post

Excellent ideas, thanks very much chums.  I like the port idea, and had never even considered that!  So simple, and should simplify things.  This gives me several things to try out.  Thanks again.
Smoke makes things work. When the smoke gets out, it stops!
 
Cyber

Senior Member

Joined: 13/01/2019
Location: Ukraine
Posts: 161
Posted: 07:01am 29 Sep 2019
Copy link to clipboard 
Print this post

Grogster, have you solved the problem? I'm intersted to know.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 03:30am 30 Sep 2019
Copy link to clipboard 
Print this post

I have not had a chance to play with this in the last few days, but I am hopeful the SELECT CASE idea will work.  I will try to play with this idea soon.  I will let the thread know what happens.  Are you having a similar issue?
Smoke makes things work. When the smoke gets out, it stops!
 
Cyber

Senior Member

Joined: 13/01/2019
Location: Ukraine
Posts: 161
Posted: 05:22am 30 Sep 2019
Copy link to clipboard 
Print this post

  Grogster said  Are you having a similar issue?

Not really.
I'm just starting my way into electronics, and I'm interested following the thread.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025