20 February 2013

Project #1:Determining Android Charging power requirements

Greetings!
Well its been a while, and life got crazy, but I'm back with our next post. This one with ADC good. :)


The Process:
The next step in my project is to determine the power requirements for an android phone while charging. The idea is plot the Voltage vs. Current curve, I.E: power, over the length of time in which it takes to charge. This will show how much power, over what duration of time the solar charging source must to provide.
The method to my madness runs something like this, charge several different types of phone, with different power sources and collect the data. This will confirm if there are any different current draws with different power sources on different phones.
With this in mind there are three different parts to this step to this part of the project, the phones, the power sources, and the data collections. Lets start with the data collections.

Analog to Digital Voltage and Current Sampling  Circuit:
To collect the charge curve data I'm using a BS2px24 Basic stamp PIC connected to a MCP-3002 2 channel analog to digital converter reading voltage from a current shunt in series the USB charge circuit for the phone. See below for the circuit:









Circuit Break down:
There are Three parts to this circuit, the current shunt in the charging circuit, the MCP 3002 and the Basic stamp. Each part is mostly KISS and should be easy to follow, but for those less familiar with Electronics I'll break it down.
First the current shut, this is composed of 4, 10 Ohm, 1/2 Watt resistors in parallel with each other. this will make for about .4 Ohms of resistance in  series with the charging circuit. Now ideally you would want to use precision resistors for this as you will need to be spot on with your measurement here. But I'm building this mostly out of scrap so I just used 10% components and then measured the resistance.
The idea of the current shut is that it will drop a small portion of the voltage from the charge circuity that is directly proportional to the current being used in the circuit  You can use : E/R=I where E=voltage, R=the resistance pack(.4 Ohms) and I=the current. So if you measure .53V DC across the resistor pack that means you have 1.32A DC flowing through the charge circuit.

This voltage that is dropped across the resistor pack, as well as the voltage applied to the charge port of the phone is picked up by the MCP3002.



The voltage from the current shunt is sent to the channel 1 input and the voltage at the phone charge port is sent to channel 0. For those who are not familiar with Analog to Digital converters let me give you a quick run down.
The whole function of a ADC(Analog to Digital Converter) is to take a very fluid analog signal and convert it into a 10 bit binary word which a computer or microprocessor can understand. A microprocessor can only understand predefined states, which in this case is >+5V DC(which is a binary 1) or <2 V DC(which is a binary 0). Well is this is not going to work for my project because I need to measure voltages like .53V DC. If I hooked this up right to the I/O(input output) pin it would read 0 all the time. This is where the ADC comes in. It has a sampler which will sample the voltage at a high rate(in this case 10,000/s), convert it into a binary word and send it to the PIC.
The next step of fun comes in getting this binary word to the PIC. This is done via synchronous serial on the Dout pin of the ADC. But before I get to far lets start with the first step of setting the ADC up.
The MCP-3002, according to the data sheet, has two different input modes, and two different read out modes. To set these, mode command bits need to be sent to the ADC in a predefined order. This also needs to be coupled with pluses on the clock pin for each command as well as toggling of the enable pin.
Below is a timing chart so the states of all the input and out pins over the duration of time it takes to send the input commands and get the 10 bit reading back.



I've labeled the different events on the chart so you can follow along with the flow of things.
Something to clarify for those who are new to this, the clock pluses on the CLK pin control when the ADC will receives or send a bit. This synchronizes the PIC and the ADC so no bits are lost in the course of data transfer.
The other thing to point out is the CS pin, this pin pulls the ADC out of standby mode. With out this pin high, the chip will do nothing!
It is important to see that the information above is critical to the functionality of the project as a whole. With out this reliable data transfer we would never be able to get the data we need for this part of the project.

This leads me into the next part of the project, the Basic Stamp, or as I have been calling it the PIC(Programmable IC)

The BS2px24:
This is where it all comes together, in the PIC. The PIC gathers the data words from the ADC, converts them to decimal numbers, formats them and then sends them out to the RS232 port. To aid us in walking in how the PIC does this lets walk through the code. Below is the code in its entirety  I'll show chunks as we walk through it.
' {$STAMP BS2px}
' {$PBASIC 2.5}

SetupPins:
'Setup the pin Functions.
'P0=RS232 in.
INPUT 0
'P1=RS232 out
OUTPUT 1
'P2=SPI in
INPUT 2
'P3=SPI out
OUTPUT 3
'P4=CS(Chip enable)
OUTPUT 4
'P5=Clk(Clock for syncing the SPI communications)
OUTPUT 5

SetupCons:
'Set freindly names for all the I/O pins.
RS232IN CON 0
RS232OUT CON 1
DIN CON 2
DOUT CON 3
CS CON 4
CLK CON 5

SetVars:
'Setup our Variables.
'VIN will be the 10 bit Value repersenting the Voltage read by the MCP3002
VIN VAR Word
'ACDIN will be the bit flag telling the MCP3002 which port to use.
'port 0 is used for Voltage readings and port 1 is use for Current.
AdcIN VAR Bit
'Setup our timing output
TickVal VAR Word
'And a temp tick Var
TickTemp VAR Word

Main:
'Make Sure our Vars are rest
TickVal=0
TickTemp=0
VIN=0
DO
  'Set our port for Voltage
  AdcIN = 0
  'First we get our Voltage
  GOSUB InitMCP3002
  GOSUB ReciveDataBits
  GOSUB Tick
  GOSUB SerialOut
  'Then our Current
  'Set the port for Current
  AdcIN=1
  GOSUB InitMCP3002
  GOSUB ReciveDataBits
  GOSUB Tick
  GOSUB SerialOut
LOOP

Tick:
'Sets up a processes timer which will time stamp all values sent out.
PAUSE 1
TickTemp=TickVal+1
TickVal=TickTemp
RETURN

InitMCP3002:
'Initaite the MCP3002 by sending config bits and a Start bit
'Set clk and cs pins high to indicate idle
HIGH CS
PULSOUT CLK, 5
'Send the start bits by brings CS low and sending a 1 to CLK and DIN
LOW CS
SHIFTOUT DIN, CLK, 0, [1\1]
'Set Mode to Single Ended Mode by sending a 1 on the DIN
SHIFTOUT DIN, CLK, 0, [1\1]
'Set the input to Port with ADCIN Var.
'0 for port 0 and 1 for port 1.
SHIFTOUT DIN, CLK, 0, [ADCIN\1]
'Setting the Format
SHIFTOUT DIN, CLK, 0, [0\1]
'One more clock cycle for the Null bit
PULSOUT CLK, 5
RETURN

ReciveDataBits:
'Read the 10 bit Digital value from the MCP3002.
'The max input(anything over Vdd) will be 1023.

'Read 10 bits from the DOUT pin
VIN = 0
SHIFTIN DOUT, CLK, 0, [VIN\10]
RETURN

SerialOut:
'This sends the raw values to the RS232 Caputer device for further processing.
'Send the voltage out the RS232 port
IF AdcIN =0 THEN
SEROUT RS232OUT, 3313, [DEC5 TickVal," ,",DEC VIN," ,"]
ELSE
SEROUT RS232OUT, 3313, [DEC5 TickVal," ,",DEC VIN," ,",CR,LF]
ENDIF
RETURN


Before we dig into the code I would like to make a few notes on this PIC. For info on the BS2px24 you can go to The Parallax Web Site see all that they have. The langues is call PBASIC, its a low level langue in the form of a high level langue in that it is formatted much like BASIC, but does more low level functions. You can got the link above and find more info on PBASIC as well. Also Note 'this is a comment.  Anything with a ' in front of it is a comment in this langue. 
Now on to the code!

Setup:

' {$STAMP BS2px}
' {$PBASIC 2.5}

SetupPins:
'Setup the pin Functions.
'P0=RS232 in.
INPUT 0
'P1=RS232 out
OUTPUT 1
'P2=SPI in
INPUT 2
'P3=SPI out
OUTPUT 3
'P4=CS(Chip enable)
OUTPUT 4
'P5=Clk(Clock for syncing the SPI communications)
OUTPUT 5

First we must tell PBASIC how to compile on load to the PIC. The first two lines do this, it tells the compiler that this is a BS2px and that its using PBASIC version 2.5.
BS2px24 PICs have 16 I/O pins labeled P0-P15, before we do anything with these pins we have to tell them what their function is going to be. This is what the "SetupPins:" Subsection is all about. You can see above each pin constant I have put a comment as to its function.
SetupCons:
'Set freindly names for all the I/O pins.
RS232IN CON 0
RS232OUT CON 1
DIN CON 2
DOUT CON 3
CS CON 4
CLK CON 5

Next we set our constants up, these are all names we give to the Pin. I've set the names up to be the function of the pins so it easier to follow the code.

SetVars:
'Setup our Variables.
'VIN will be the 10 bit Value repersenting the Voltage read by the MCP3002
VIN VAR Word
'ACDIN will be the bit flag telling the MCP3002 which port to use.
'port 0 is used for Voltage readings and port 1 is use for Current.
AdcIN VAR Bit
'Setup our timing output
TickVal VAR Word
'And a temp tick Var
TickTemp VAR Word
Here we set the variables used in the in the program, they are self explanatory for the most part. You get some more clarification when you see them used.

Sub-routines:
Tick:
'Sets up a processes timer which will time stamp all values sent out.
PAUSE 1
TickTemp=TickVal+1
TickVal=TickTemp
RETURN

Here we have a simple timing setup. Sadly PBASIC is a little limited in its arithmetic functions, so I had to do the mess with TickTemp=Tickval+1.

InitMCP3002:
'Initaite the MCP3002 by sending config bits and a Start bit
'Set clk and cs pins high to indicate idle
HIGH CS
PULSOUT CLK, 5
'Send the start bits by brings CS low and sending a 1 to CLK and DIN
LOW CS
SHIFTOUT DIN, CLK, 0, [1\1]
'Set Mode to Single Ended Mode by sending a 1 on the DIN
SHIFTOUT DIN, CLK, 0, [1\1]
'Set the input to Port with ADCIN Var.
'0 for port 0 and 1 for port 1.
SHIFTOUT DIN, CLK, 0, [ADCIN\1]
'Setting the Format
SHIFTOUT DIN, CLK, 0, [0\1]
'One more clock cycle for the Null bit
PULSOUT CLK, 5
RETURN

Here is the programming behind the first part of our timing diagram. You can see where we set out CS high and send out a pulse on the CLK pin. Here we doing it for 5 clock cycles, this makes it long enough for the ADC to see it as a clock pluse.
 Next we pull the CS low to turn the chip on and then send a 1 bit out the Din pin and send a clock pulse out. This starts off the set up process on the ADC that we spoke of above. You can see the rest of this in the comments and compare it to the timing diagram.

ReciveDataBits:
'Read the 10 bit Digital value from the MCP3002.
'The max input(anything over Vdd) will be 1023.

'Read 10 bits from the DOUT pin
VIN = 0
SHIFTIN DOUT, CLK, 0, [VIN\10]
RETURN

Here we are shifting in to our 10 bit ADC output value into the VIN variable. If you noticed in the variable set section we set this variable up as a word, which is 16 bits long. This is a limitation of PBASIC in how it sets up it variables. You can have a single bit, a nibble(4 bits), a byte(8 bits), or a word( 16 bytes). This secontion is not quite as clear, the line "SHIFTIN DOUT, CLK, 0, [VIN\10]" is doing the shifting in. The SHIFTIN DOUT is telling the PIC to recive bits on the DOUT pin. Then use the CLK pin to send sync pulse out to the ADC. The 0, [VIN/10] tells the PIC to not change the order the bits come in and to put the bits into VIN and that there are 10 bits to receive.


SerialOut:
'This sends the raw values to the RS232 Caputer device for further processing.
'Send the voltage out the RS232 port
IF AdcIN =0 THEN
SEROUT RS232OUT, 3313, [DEC5 TickVal," ,",DEC VIN," ,"]
ELSE
SEROUT RS232OUT, 3313, [DEC5 TickVal," ,",DEC VIN," ,",CR,LF]
ENDIF
RETURN

This is our last sub-routine,  Here we take our values and send them out to computer's RS232 port. We have to first figure out what port we are reading. If AdcIN is set to 0 then we are reading the port voltage and will send that info out. If we are reading port 1 then we are reading the shunt voltage and we will send that value out. Along with each value we will send out the TickVal so we know how many milliseoncds into the test we are.

Main Code Section:
Here is where it all comes together, we stitch all the sub-routines together and make it work!
Main:
'Make Sure our Vars are rest
TickVal=0
TickTemp=0
VIN=0
DO
  'Set our port for Voltage
  AdcIN = 0
  'First we get our Voltage
  GOSUB InitMCP3002
  GOSUB ReciveDataBits
  GOSUB Tick
  GOSUB SerialOut
  'Then our Current
  'Set the port for Current
  AdcIN=1
  GOSUB InitMCP3002
  GOSUB ReciveDataBits
  GOSUB Tick
  GOSUB SerialOut
LOOP


So first we make sure all variables are set to zero, then we dive into a loop. This keeps the process going none stop so we can just keep making measurements until the PIC is turned off. So in the loop we first read port 0 for our charge port voltage, we set up the ADC, read the bits, count the time, then send it to out the RS232 port. Next we do the same thing on port 1 for the shunt voltage reading. Then we loop till the cows come home!

Next up, Conclusions!!!!
Stay tuned for the test results and what the next testing step will be. :)