Author Topic: Using PORTAN as digital inputs  (Read 8865 times)

Offline fcereja

  • Newbie
  • *
  • Posts: 13
    • View Profile
Using PORTAN as digital inputs
« on: April 04, 2009, 06:35:37 PM »
Hello Mark,
I've started an new application and I simply want to use AN0 to AN3 as digital inputs.
I programmed the following in the init part of the new included task.
      DDRAN &= ~(PORT_AN_BIT0 | PORT_AN_BIT1 | PORT_AN_BIT2 | PORT_AN_BIT3);                                             
When I launch the simulator, the PORT AN still shows all I/O as outputs.
Where must the init code be inserted in so the simulator really sees the inputs and allows changing the input state.

Thank for help, best regards

Francis

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Using PORTAN as digital inputs
« Reply #1 on: April 04, 2009, 08:13:01 PM »
Hi Francis

The AN pins default to GPIO-input. Therefore if they are displaying as outputs it is due to the fact that code is initialising them as outputs.

In the demo project this is the case since they are used as general purpose outputs which can be controlled on the web page.

If your reprogramming is not working I suspect that it is because it is being executed before the demo initialises the port bits as outputs and therefore it is being overwritten.

There are two possibilities:
a) remove the demo initialisation (in this case you theoretically don't have to program then as inputs at all since the default to inputs, but it is still good practice to perform it).
b) Add your initialisation after the demo initialisation, thus reverting it.

The demo initialisation takes place in the call
        fnInitialisePorts();                                             // set up ports as required by the user
in application.c. If you add your initialisation after this point in the code it will then work. There shouldn't be any thing special required for the simulator.

I just tested the following and it worked correctly:
        fnInitialisePorts();                                             // set up ports as required by the user
        DDRAN &= ~(PORT_AN_BIT0 | PORT_AN_BIT1 | PORT_AN_BIT2 | PORT_AN_BIT3); 


If you prefer to remove all demo output initialisation you can comment out the following block in debug.c, routine fnInitialisePorts():

// User defined outputs used by keypad so they may not be set here
    // Initialise user ports as outputs with default values
    sPort = 'a';                                                         // first user output port
    fnSetUserPortOut(temp_pars->temp_parameters.usUserDefinedOutputs);   // configure all user outputs to default states

    while (sPort < 'q') {                                                // until last port
        fnConfigOutputPort(sPort++);                                     // configure port bits as outputs
    }


Regards

Mark


Offline fcereja

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Using PORTAN as digital inputs
« Reply #2 on: April 04, 2009, 08:44:46 PM »
Hi Mark,

effectively it works well. On the simulator it's possible now to change the value of each input. To read the inputs I simply use
"port = PORTAN" and then a mask for each input. The "port" variable stays always at 0. (printed out with "fnDebugDec(port, 0, 0);").
Are the changes on the GUI normally reported to PORTAN or not?

Regards,
Francis

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Using PORTAN as digital inputs
« Reply #3 on: April 04, 2009, 09:22:30 PM »
Hi Francis

This is a common mistake when working with the Coldfire Ports.

PORTAN is an output register and contains the output value. It doesn't contain the input value which you are trying to read.

To read input states you need to read PORTIN_SETAN.

Regards

Mark

Offline fcereja

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Using PORTAN as digital inputs
« Reply #4 on: April 04, 2009, 09:42:49 PM »
Hi mark,

thank you, there are so many registers and configurations. I will work now with the M52235 user's guide always open.

Regards
Francis