Question

I am a beginner and I want to know whats wrong with this code. I want to count the number of times the push button is being pushed in portA. Then show this values using the LEDS in portC. Thanks

Était-ce utile?

La solution

You need braces around a multi-statement block, if you want to use it as the body of an if (or for or whatever) statement:

else if (PORTA.RA2==1) {
    count = count+1;
    PORTC = count;
}

otherwise only the first statement is conditional; so your code executes PORTC = count; every time, whatever the result of the if tests.

I like to put braces around all such blocks, even there's only a single statement, so I can't forget to add them if I add more statements later.

Also, main must return int not void, and you should take more care formatting your code to match its logical structure.

UPDATE: Also, you never initialise count, so it has an arbitrary floating-point value. You want a small integer type, since it's only supposed to take integer values from 0 to 16, and you need to initialise it:

char count = 0;

Autres conseils

If you're setting TRISA to 1 that means the only input on that port is RA0, but you are trying to use RA2. Be sure to clear the ANSELA0 bit. Make sure you set the config bits properly or else your code might not run.

To avoid getting downvoted in the future:

  • Choose an informative question title.
  • Properly indent your code.
  • Say the exact PIC you are using and what board it is on.
  • Say what development environment and compiler you are using.
  • Provide pictures of your setup so we can check your wiring.
  • Most importantly, tell us exactly how you are testing the code, what the expected result is, and what you are actually observing.

My company offers more advice here: http://www.pololu.com/support

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top