I'm using MP430 series microcontroller. The functionality of the code is to do a specific task and then don't do anything (sense the tilt and glow an LED in a fixed amount of time). But due to some bug in the code, I'm noticing that a port pin gets '1' after some time. So now I want to disable all the ports once the task is completed. How can I disable these ports which initially I defined as Input and Output ports.

And my 2nd question is: if I'm disabling these ports then are there any chances that the output port (now disabled) will stay floating and generate some noise or something and trigger my output LED? This question is for all the microcontrollers (MSP430, PIC, AVR, etc.)

  • 6
    \$\begingroup\$ You should track down, understand, and fix the real source of your problem (your glitchy '1'). Probably not what you want to hear but this will suit your engineering skills for the better long-term. \$\endgroup\$ – Joel Wigton May 4 '16 at 16:35
  • 1
    \$\begingroup\$ sure thing. that i'll do. but this problem has raised questions in my mind. what is the direction of the port if I haven't set direction of the port (using PxDIR in msp430, TRISx in PIC, etc.) in the beginning of the code. what will be the value of this kind of port (unassigned) if I try to read this port or what will be the status of the LED if I connect it to a pin corresponding to this port. \$\endgroup\$ – Jayesh Parmar May 4 '16 at 16:42
  • 1
    \$\begingroup\$ Check the datasheet of the device. For the PIC series, they explicitly state what the default state is for each register. Others (had better) do the same. And if using an IDE/compiler, check it's documentation for default library intrinsics (if any) and any configuration options. Many IDEs like the PIC's MPLABx include tools like the Microchip Code Configurator which allows you to easily preset GPIO states. It generates initialization code to do this. On other micro's or without a configurator, you'd just write the code by hand. \$\endgroup\$ – rdtsc May 4 '16 at 16:54

Pretty much every microcontroller will have it's ports' default state as 'input'. So just set the ports as inputs and you should be all set. One potential gotcha is if the pin has an internal pull-up or pull-down set, which could cause some issues, depending on your circuit. Lastly, most datasheets provide the default state of the bits in a given register. So to get the register back to how they were after the chip powered on, simply set the bits to their default state.

  • \$\begingroup\$ r u sure that all the ports are defined as inputs at startup.? if so, why we initialize the ports directions in the beginning of the code? \$\endgroup\$ – Jayesh Parmar May 4 '16 at 16:36
  • \$\begingroup\$ I know that for AVR this is true, and I'm 99% sure it's true for PIC and every one else too. In any case, this information should be clearly described in the datasheet for the particular microcontroller. You may have to search around to find it, but it should be there. \$\endgroup\$ – Brendan Simpson May 4 '16 at 16:39
  • \$\begingroup\$ thanx. sure I'll try to look for the info in the datasheet. \$\endgroup\$ – Jayesh Parmar May 4 '16 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.