Newbie Here: Question About Common Ground Setup

castlecrazy

New Member
I just received my Picaxe 14M as well as a whole other bag of goodies and am very excited to join the family here. For my first project, I am trying to write a code to allow my Picaxe to control the new Xbox 360 Common Ground Controller that Microsoft has started producing. I have already wired and tested the controller and can simulate all of the functions that I want to by basically touching the appropriate wire to a ground connection. My question is this: When writing the code should I default all of the wires to a high state (+5V in this case) and then toggle them to low when I wish to "push" the button? I am unexperienced in how a common ground setup actually works so I am not sure if leaving it in a high state would be okay. Thanks for the help in advance!
 

castlecrazy

New Member
I guess I could also use the appropriate PNP/NPN transistor to open a ground switch by applying a 5V signal to one of the leads. I guess my main question boils down to this

When I use "low" in my program does it allow the pin to ground out or does it simply disconnect the positive voltage such as opening a switch?

Because if it does not allow the pin to ground out it is going to make this project much more difficult because it will either require 4 transistors or a quad analog switch.
 

Rickharris

Senior Member
The pin is connected to the picaxe ground - Unless you draw a high current >20 Ma there is no need for external interface items i.e. transistors.

I would expect the inputs to be simple signals.

You will need to common the controller and the picaxe ground.

i have no idea what the protocol for the Xbox controller mught be.
 

castlecrazy

New Member
The picaxe ground will by connected to the controller ground. So you are saying that in a low state the pin is connected to the picaxe ground, and when in a high state it is basically connected to the picaxe power pin?
 

moxhamj

New Member
A good newbie question. It was also one of the first questions I asked when I got into electronics. Low means connected to ground. High means connected to 5V. And open circuit means not connected to anything. "Tri state" is used by some digital chips to give low, high and open circuit all from the same pin, but most digital chips have just high or low outputs. Picaxes can be tri state - the open circuit state is when you set a pin (in code) to be an input - eg with readadc (you can ignore the value that comes back if the purpose is to set a pin as open circuit).

Re hacking the xbox, if the switch connects something to ground then making a pin low will do the same thing. But the xbox may be unhappy if you send 5V back up one of its wires. It might be safer to make the picaxe pin open circuit rather than high. If you get a voltmeter and measure the volts on the xbox pin and you get anything under or equal to 5V then you won't need a transistor. But if say you get 12V then that would zap the picaxe so you would need a transistor. 5V is a likely voltage as the xbox probably uses 5V logic.
 
Last edited:

jglenn

Senior Member
He needs to hook a dvm up to one of those wires, referenced to dc ground of the box, and see what voltage is floating there!

In any case I think an open collector npn would work, 1K base resistor to the picaxe digital out. Would protect the picaxe and the tv box.
 

castlecrazy

New Member
Dr_Acula: Thank you greatly for your reply. It answered a lot of the questions I was having. Glad to see that someone else on here was in the same shoes as me once. I do have another question about what you said. I looked up the readadc command and could not find anything about it's effects on the pin (i.e. causing open state). Could I use this command with the output pins on my Picaxe 14M, or only the input pins? Also, this may be a stupid question, but how do I differentiate between input and output pins in my code? I feel like I remember reading something about just the number referring to output pins, and using the prefix "pin" before the number (pin1, pin2, etc) references the input pins. Is that correct? And finally to combine both of these questions, would "readadc 1" set output pin 1 to open state? Thanks again for everyone's help!
 
Last edited:

moxhamj

New Member
The 14M has dedicated input pins and other output pins. There was a discussion recently about how to change those pins around so some pins could be turned into inputs and outputs. It is in the help manuals in an appendix but I can't say exactly where as I'm on a computer that has a wierd display that won't display pdfs properly.

On 08M chips most of the pins are both inputs and outputs at the same time, so you just go "low 2" to make pin 2 low and then readadc on pin 2 to make it high impedence.

For a 14M the choices would be to look up the poke commands that make pins into inputs and outputs (The 14M is a lot more versatile than it initially looks!).
Or you could use a transistor like a BC547 (worth 7c) and a 1k resistor driving the base (collector to the switch, emitter to ground) as suggested above. Looking at the writing on the transistor the pins are collector base emitter going from left to right.
Or another suggestion which might work is a 914 diode. Use an output pin and put the diode facing into the pin (the line on the diode). The diode will thus conduct current from the switch into the picaxe when the pin is low, but when the pin is high the diode won't conduct.
I must say the 'standard' way of doing this is with a transistor.
 
Last edited:

castlecrazy

New Member
So I have looked all over on this site in addition to google (there is surprisingly little information about picaxe projects there) to try and find some simply explanation of how to declare a input pin as an output pin. All I have been able to find is some cryptic descriptions of the poke and peek commands, but I still have no idea how to use them. I was under the impression that the 14M's pin designations could not be changed. I am very interested in how to do this if it is possible because I only want to have 3 inputs and I really need all the others to be output pins. Maybe I am just not looking for the right thing, but either way, I could really use some direction on how to do this.

And Dr_Acula, you mentioned the ability of the 08M to switch an output pin from low to open by saying "low 2", "readadc 2", but would that work with a 14M as well where all the pins are already designated as either input or output by default. Thanks everyone!
 

castlecrazy

New Member
Ahhh. Thanks eclectic. I kept looking through manual 2 and never thought to look through one as everything there seemed pretty basic. Ok, so with that out of the way, now the only main question I still have is how to utilize the readadc command to put a pin in an open state. For your basic output 0 pin with a switch on input pin 1, would it just be:

main:
readadc 0 ' puts output pin 0 in open state and ignores return value
if pin1 = 1 then high 0 ' if input pin 1 is high, applies ~4.5V to pin 0
goto main

is that correct? Thanks guys!
 

Technical

Technical Support
Staff member
Input pins are on the left side of the 14M, you do not need to use readadc or anything else to set them up as inputs, that is done by default.

So just connect to them and use a input decision such as

if pin1 = 1 then
....
else
...
end if

Output pins are on the right, again you do not need to setup them up. A command such as 'high 0' will switch output 0 on.

Study the pinout digram in manual 1 so you don't get confused, there are both inputs and outputs with the same number (but they are different physical legs).
 
Last edited:

moxhamj

New Member
Eclectic has found the reference. I think part of the problem here is that while many people have read those pages I'm not sure if anyone has actually used portc commands on a 14M. However I'm fairly sure this has been done on other chips. Reading through the manual I think a 14M can do what you want but I'm not exactly sure of the code. It is going to be only 4-5 lines of code if it can be done. We want an output to go low and then we want to convert the same physical leg to an analog input. It will be physical legs 7,8,9 and 10 as they are the adc pins. Physical legs 5 and 6 might also work as they are also digital inputs. I'm finishing work soon - I might fire up an 14M on a breadboard and see if I can get this working myself. Meanwhile, can Technical help here please?
 
Last edited:

moxhamj

New Member
Ok, got it working. This code will 'push' up to 6 buttons on an xbox or a calculator. The buttons are ones that short to ground and the code changes between 'low' and 'open circuit'. On is active low, and off is when the button is not pressed so it is open circuit. This uses the advanced settings on the 14M.

For testing, each pin drives a led but in an active low configuration - eg 5V to 1k resistor to led anode and the led cathode goes to the pin. Thus the led lights when the pin is low and doesn't light when the pin is open circuit (The led would also not light if the pin was high but that condition never exists in this code).

Here is the code. And also here is a really dumb question myself - what (and where in the help) are the code tags for code? (edit - thanks to BCJKiwi have added the code tags)

Code:
' circuit for driving xbox controller or calculator or any device where a physical button
' connects something to ground. Drives 6 buttons either connected to ground or open circuit.
' note the terminolgy where 'on' denotes a button being pressed where it is connected to ground
' and 'off' means the button is not pressed, ie open circuit


start:b0=0
      let dirsc=%00000000' poctc0-5 all open circuit
' example code to turn leg10 on and off

main:gosub leg10on
     pause 1000
     gosub leg10off
     pause 1000
     goto main
     
' code for turning pins either low or high impedance     
	
	
leg5on:b0=b0 or %00100000' leg 5 as output (C5=bit5)	
	let dirsc=b0
	low portc 5
	return
	
leg5off:b0=b0 and %00011111' bit5 low	
	let dirsc=b0
	return
	
leg6on:b0=b0 or %00010000' leg 6 as output (C4=bit 4)
      let dirsc=b0
      low portc 4
      return
      
leg6off:b0=b0 and %00101111' bit 4 low
       let dirsc=b0
       return      	
	
leg7on:b0=b0 or %00001000' leg7 as output (C3)
     let dirsc=b0
     low portc 3' connect low
     return	
     
leg7off:b0=b0 and %00110111' preserve all bits and C3 low
     let dirsc=b0
     return     
	
leg8on:b0=b0 or %00000100' leg8 as output (C2)	
     let dirsc=b0
     low 5' connect low
     return
     
leg8off:b0=b0 and %00111011' preserve all bits and C2 low
     let dirsc=b0
     return    
     
leg9on:b0=b0 or %00000010' leg9 as output (C1)
     let dirsc=b0
     low 4
     return
     
leg9off:b0=b0 and %00111101
      let dirsc=b0
      return
      
leg10on:b0=b0 or %00000001' leg10 as output (C0)
      let dirsc=b0
      low 3
      return
      
leg10off:b0=b0 and %00111110' bit0 low
      let dirsc=b0
      return
 
Last edited:

moxhamj

New Member
Have written this up with a schematic at http://drvernacula.topcities.com/controlling_devices_with_buttons.htm

This is a nifty use for a 14M. In fact, having played with the portc functions I must say this chip may end up being more versatile than the 08M. There are a few little tricks such as the fact that making an output high or low for pins on the left side of the chip uses instructions like 'low portc 3' wheras the ones on the right work with 'high 5' etc.

It also gets a bit confusing between legs, pins and portc pins, so leg 7 is input 0 and output C3. The diagram on page 70 of the manual explains this.

And there are still 5 unused pins!
 

castlecrazy

New Member
Wow guys. Very helpful stuff here! Great work. I think this will help a lot of others like myself. Dr_Acula, while looking through your code I did have one question. I understand everything that is happening, I just have a question about your process in part of it. For example with toggling pin 10, you wrote this:

leg10on:b0=b0 or %00000001' leg10 as output (C0)
let dirsc=b0

I get that this is declaring pin 10 to be an output, but why didn't you just write:

leg10on:let dirsc=%00000001' leg10 as output (C0)

Is there some reason that would not work? I really can't find any use for b0 anywhere in the code, but maybe there is something I am missing. I am used to coding in Standard C, so most of this is a little new to me.
 

Technical

Technical Support
Staff member
Dr_aculas code only changes that one pin status. A direct 'let dirsc=' command affects all pins on portc (leg10 as output, but all others become inputs as you are allocating them a 0 at the same time)
 

castlecrazy

New Member
Ah, I see. So it is really more appending a one to the dirsc command so all the other pins input/output statuses will remain unchanged except for adding pin 10 as an output.
 

moxhamj

New Member
Indeed, and that was actually the complicated bit of the code. If you only want to turn on and off one pin then the code is only a couple of lines. But if you want to change one pin and preserve 5 others then you need a way of storing the status of all the other pins. Register b0 (or any other register) can be used to do this.

There are 8 binary bits in b0 and this code uses 6 of them (the leftmost two are ignored). Logic AND and OR are used. In simple terms, if input 1 AND 2 are both high then the output is high, and if input 1 OR input 2 are high then the output is high. So if you have a bit that is 1 and you want to keep it as 1 then you can AND it with a 1. But if you AND it with a 0 then it forces the bit to be zero. So if you have %11111101 and AND it with any number then all the bits will stay the same except the second bit from the right which will be forced to be a 0.

Similarly with OR you can force a bit to be a 1. If you OR any number with say %00000010 then all the bits stay the same except the second bit from the right which is forced to be a 1.

I must say that writing this code needs a clear head (pointless to try after a new year's glass of champagne for instance) and it is really easy to make a simple mistake like using an OR instead of an AND or writing a binary number with 7 bits instead of 8. I went through and tested each subroutine individually with a led, so if you end up using this then maybe cut and paste and then delete rather than copying it by hand. Hope this helps :)
 

castlecrazy

New Member
Well thanks to all your help I have assembled the controller and PIC. It has 3 buttons as well as a mode selector. There is a flush programming port on the bottom of the right handle for quick access to the 14M. Here are a few pics of the progress:





All the outputs are working on the C0-C5 pins that have the ability to switch to open circuit. Can the other two output pins go open circuit or are they limited because they cannot be configured as either inputs or outputs? Also, when running the controller with rechargeable batteries the controller refuses to connect to the console and just flashes its buttons. My guess is that the PIC is drawing too much current and due to the fact that rechargeables usually have a lower voltage than 2 AA's, this is making the controller think it is out of batteries. Is there anything I can do to keep a higher voltage with the 14M connected and running? Maybe a capacitor or some sort?
 

moxhamj

New Member
Great effort. I think 6 is the limit on a 14M as the other output pins are high or low. You could use those pins to drive BC547s with 2k7 into the base if you like.

Re the power supply, is the controller running on 2 AAs? If so and they are rechargeables then that would be getting a bit low for a picaxe. There are all sorts of solutions including having two seperate supplies, but I'd just use two alkalines for the moment.
 

castlecrazy

New Member
Well my controllers have two options for power; 2 AA's Alkaline batteries (runs the picaxe and controller fine) or a rechargeable battery pack. The rechargeable pack will run just the controller, but when it has to power both the picaxe and the controller all I get is a flashing ring of led's on the controller dome which signifies low battery. Will I have to just waste a little money on the AA's (I go through about a pair a week), or can you think of a better solution?
 

BCJKiwi

Senior Member
The rechargeables if they are nicads will only deliver a nominal 1.25 volts ea for a total of 2.5V.
The alkalines will deliver a nominal 1.5v for a total of 3V.

In theory the 14M is supposed to run on as little as 2v (Microchip's PIC12F684 Datasheet) but that is less than we normally use and I'm not sure if the PICAXE implementation needs more.

If you want rechargeables and the system won't take higher voltages or you can't fit a bigger battery array in, then you will need to use alkalines, or, altrnatively a voltage step-up circuit to supply the 14m from the rechargeables.

The Dr has a voltage boost circuit but it requires a coil etc - he can give you more detail.

There are also charge pump ICs from Maxim and others but most seem to want at least 2.7v input and will deliver up to 100mA.
However check this out;
http://www.ednchina.com/download/maxim/maxim/pdf/ds/1699.pdf?type=pdf
Will only deliver 20mA but that could be enough since you are only switching inputs to ground and have no outputs. You should be able to run a 14M on that (it idles at microA - just powered up a 14m with a program in it but nothing connected and it draws about 0.8 of 1mA. If you have a DMM you could check the current draw when its running in your application). Don't know who might have stock near where ever you may be.
 
Last edited:

BCJKiwi

Senior Member
If you use a step up then you should be able to run off the existing rechargeables.

The Dr has indicated a viable alternative - if the 14M current is really low you might get away with a separate supply to the PICAXE from a coin cell battery (3v) or 3 alkalines - just common up the 0V rails from the PICAXE supply to the XBOX controller 0v. They will last for a very long time if the PICAXE current is really low.
Energiser reckon their current technology Alkalines will run for around 45 hrs at 50mA before they get down to 1V (per cell). So if you have 3 cells for a nominal 4.5V to supply the 14M and only draw 10mA you should get 225 hrs (or at 5mA 450 hrs) and still have a 3v supply to the 14M.
The downside to a separate supply is you need to turn it on/off as well. Depending on the On/Off for the controller, you may be able to rig up a second switch off the same button.

I think if the current was well under the 20mA I'd go for the Max679.

If you can slow the PICAXE down you will get more life as well.
You would also want to tie the unused inputs (legs 3 and 4) low as well via say 100k+ resistors to reduce current drain.
You might want to check out this thread (there are many others as well);
http://www.picaxeforum.co.uk/showthread.php?t=8353&highlight=unused+inputs
which has a good discusion on the things to do to reduce current and to run on low supply Voltage. It may be the DisableBOD is all you need!
 
Last edited:

castlecrazy

New Member
Ok, so it has been a few months now and I have just been using the controller with the standard AA's as I didn't want to hassle with the other options. It is working great and I have added a few extra outputs now including a couple NPN transistors that connect some of the more difficult buttons on the controller that were not as simple as grounding out one connection to trick the controller into thinking a button was pressed. I have done all of the suggestions in this thread including connect all unused pins to ground through a resistor, run disablebod, yet the picaxe and controller can still not run of rechargeables. So I am back to try and solve this problem.

The Dr has a voltage boost circuit but it requires a coil etc - he can give you more detail.

Could you maybe post that schematic or send me a link so I could check it out?

Also, if I used the setfreq command to slow the picaxe down, what would be a good frequency to set it to so that it will still work for gameplay (needs to be relatively fast so when I want to shoot it shoots within adequate time). Currently I am using "m8".

Thanks for the help so far!
 
Top