Picaxe 20X2 NXT experimenting platform

Recently I’ve been playing with a PICAXE 20X2, and interfacing it with the NXT. I have done this in the past, but this time I made more of an experiential platform. I have lots of breadboard space to expand and try all sorts of things.

The first thing I did was connect a LDO regulator to an ADC pin to measure a constant voltage. I can then use that constant value to calculate the supply voltage (NXT bus voltage), because the PICAXE ADC reference voltage is the supply voltage. Here is pseudo code to calculate the reference voltage:
Reference voltage(mv) = Constant voltage(mv) * max ADC value / RAW ADC value
Or with actual values based on a 3.3v regulator and a 10-bit ADC:
Reference = 3300 * 1023 / RAW
You don’t have to do the calculation in mv, but it is much better resolution if your processor doesn’t support floating point. If you do use mv, 3300 * 1023 = 3,375,900, so you will need 22+ bits.

Here is the schematic I used.

C1, C2, and C3 are used to help with potential noise on the NXT sensor supply lines. These don’t seem to be critical, but it never hurts to play it safe. C4 is very necessary. The LDO regulator is quite noisy without a cap on the output, and will cause very erratic readings.

The program the 20X2 is running is just allowing the NXT access to 10 10-bit ADCs. Here is the program in it’s current state:

hi2csetup i2cslave, %00110000

init:
 
  symbol ADC=w0     'Currently being read ADC value
  symbol puti=b2    'Incrementing value for the "put" command
  symbol readi=b3   'Incrementing value for reading different ADCs
  pullup %10100000  'No external pullups needed.

main:

  puti=0            'reset this
  readi=0           'reset this

  do
    readi=readi+1       'Increase by one
    readadc10 readi,ADC 'read ADC
    put puti,b0         'Put the first 8 bits
    puti=puti+1         'Increase by one
    put puti,b1         'Put the last 2 bits
    puti=puti+1         'Increase by one
  loop while readi<9    'repeat 9 times (once for each of the ADCs 1-9)

  readadc10 11,ADC      'Read ADC 11
  put 18,b0             'Put in the first 8 bits
  put 19,b1             'Put in the last 2 bits
     
  goto main

In this case I am using ADC 9, so the NXT reads registers 0x10 and 0x11.

Here is the NXC program for the NXT to display the battery and sensor supply voltage:

#define PICAXEPort S2
#define PICAXEaddr 0x30

byte Result[];

float NXTVoltage;

int RefVoltRAW;
float RefVolt;

string tempstr;

void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
  byte cmdbuf[];  // register number holder
	int             loop;
	byte            nByteReady = 0;

  ArrayBuild(cmdbuf, addr, reg);

  loop = STAT_COMM_PENDING;              //Wait until bus is free
  while (loop == STAT_COMM_PENDING ){    //        ''
    loop = I2CStatus(port, nByteReady);  //        ''
  }                                      //        ''

  I2CBytes(port, cmdbuf, cnt, outbuf)    //Read the registers
}

task main(){
  SetSensorLowspeed(PICAXEPort);
  while(true){

    NXTVoltage=BatteryLevel()/1000;

    MyI2CRead(PICAXEPort, PICAXEaddr, 16, 2, Result);
    RefVoltRAW=Result[0];
    RefVoltRAW+=Result[1]<<8;  //result = 10 bit ADC value

    RefVolt = 3.3 * 1023 / RefVoltRAW;

    sprintf(tempstr, "%.3f", NXTVoltage);
    string NXTVolt = "NXT = " + tempstr + "v";
    TextOut(12, LCD_LINE1, NXTVolt);

    sprintf(tempstr, "%.3f", RefVolt);
    string BusVolt = "Bus = " + tempstr + "v";
    TextOut(12, LCD_LINE2, BusVolt);

    Wait(50);
  }
}

It reads and calculates the voltage of the NXT bus (sensor supply), and also the NXT (battery). Then it formats the numbers into strings, and displays them.

Advertisements
This entry was posted in Electronics, Mindstorms, NXC, NXT, PICAXE and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s