chipKIT® Development Platform

Inspired by Arduino™

mysterious behaviur for newbie

Created Sun, 27 Nov 2011 10:32:36 +0000 by fiddler


Sun, 27 Nov 2011 10:32:36 +0000

I have this piece of code and I get 3 outputs (LED5, 6 and 7) turned ON unexpected. I have looked at the code for a while but as I'm new to C I struggle a bit.

Would be appreciate if someone could explain it to me

cheers fiddler

//****************************************** /* IO for Chipmax 32 IO for Chipkit Basic IO */

//Max32 and Basic IO Board

const int iSlide_SW_1 = 2; const int oPWM_OC1 = 3; const int iButton_1 = 4; const int oPWM_OC2 = 5; const int oPWM_OC3 = 6; const int iSlide_SW_2 = 7; const int iSlide_SW_3 = 8; const int oPWM_OC4 = 9; const int ainPotentiometer = 54;

const int I2C_Data = 58; const int I2C_Clock = 59;

const int oLED_8 = 70; const int oLED_7 = 71; const int oLED_6 = 72; const int oLED_5 = 73; const int oLED_4 = 74; const int oLED_3 = 75; const int oLED_2 = 76; const int oLED_1 = 77;

const int iButton_2 = 78; const int iSlide_SW_4 = 79; const int iButton_3 = 80; const int iButton_4 = 81;

// variables will change: int tempMem_0 = 0; // variable for reading the pushbutton status int tempMem_1 = 0; int tempMem_2 = 0; int tempMem_3 = 0; int tempMem_4 = 0; int tempMem_5 = 0; int tempMem_6 = 0; int tempMem_7 = 0;

// variables will change: //int buttonState = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(oLED_1, OUTPUT);
pinMode(oLED_2, OUTPUT); pinMode(oLED_3, OUTPUT); pinMode(oLED_4, OUTPUT); pinMode(oLED_5, OUTPUT); pinMode(oLED_6, OUTPUT); pinMode(oLED_7, OUTPUT); pinMode(oLED_8, OUTPUT);

// initialize the pushbutton pin as an input: pinMode(iButton_1, INPUT);
pinMode(iButton_2, INPUT);
pinMode(iButton_3, INPUT); pinMode(iButton_4, INPUT); pinMode(iSlide_SW_1, INPUT); pinMode(iSlide_SW_2, INPUT); pinMode(iSlide_SW_3, INPUT); pinMode(iSlide_SW_4, INPUT); }

// Main Loop********************** void loop(){ function1(); function2(); function3(); } //******************************

void function1()

void function2() { // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (tempMem_0 == HIGH) {
// turn LED on:
digitalWrite(oLED_1, HIGH);
} else { // turn LED off: digitalWrite(oLED_1, LOW); } return; }

void function3() { // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (tempMem_3 == HIGH) {
// turn LED on:
digitalWrite(oLED_4, HIGH);
} else { // turn LED off: digitalWrite(oLED_4, LOW); } return; }


Sun, 27 Nov 2011 21:21:36 +0000

Firstly switches need some method of debounce , is there some reason to make the active position of the button / switch, high , I don't have max32 documentation , are your pin numbers and IO straps correct.


Mon, 28 Nov 2011 05:32:23 +0000

I'm not worried about debounce yet. Pin and IO numbers are correct and work when I test them individually.

might have something do with making switch high.


Mon, 28 Nov 2011 06:36:58 +0000

If I turn everything Off (LOW) in the setup, its works all good.


Mon, 28 Nov 2011 06:43:52 +0000

Okay, next is how I do get MpIde to output Assembly listing ? By using the Shift key when compiling, I can see there are some .elf files but no .asm files

Also can find the /tmp/ directory where above .elf files supposed to hiding, hmm

Running Linux


Mon, 28 Nov 2011 09:52:30 +0000

I question your ' not worried about debounce' your loop code could have run several thousand times while the switch is moving from a solid low to solid high thus causing noise on other pins... sorry I can't help with file questions . I presume the switch / pull up resistor arrangement is OK are you using the MAX32 3.3v , how much current are you taking from the supply pin ?

Darth Maker

Mon, 28 Nov 2011 23:53:47 +0000

If it works using digitalWrite(x, LOW), then you could just use that. It's possible that the pins are set high somewhere in the Core or bootloader. Simply adding the line PORTA = 0; might be enough.

Shout out to the makers and testers:

I don't have a Max32 to test with. Does the Max32 set the pins high? If it does, they should probably be brought low again before Setup();.


Tue, 29 Nov 2011 07:22:36 +0000

Im just trying to get board to work with the Basic IO board and get to understand all the bits and pieces with any regards for debounce. Easy enough to add later when I get the basics sorted. I'll have read the specs for the pull-ups resistors. Did read somewhere that they are either weak or not there, but not sure. Already found one mistake in the documentation where the Basic IO board had incorrect pin number stated.


Tue, 29 Nov 2011 10:09:15 +0000

Are you not using external pullups then ? then you will have to enable internal pullups. but not all pins are catered for check the PIC 32 family data sheet DS61143H-page 23 ,77 unfortunatly with this SW they don't seem to be enabled when the pin is set to input as i understand with Arduino. I agree with getting the basics . Debounce is not complicated but can be made so , this works for me

if (digitalRead(-pin-)== LOW){ /// HIGH if opposite delay(50); // trial and error setting if (digitalRead(-pin-)== LOW) take active action ; }

You may wish to stop or delay repeated activity. this was used to increment a value via a button so holding it ON was required.


Wed, 30 Nov 2011 08:26:00 +0000

50mS should be amble for debounce unless you have serious bouncy switches :-)

I usually read a Port at a time:

Read Port A apply mask for unwanted inputs binary AND save

wait 30mS, if using an OS, bugger of and do some other work

Read Port A apply mask for unwanted inputs binary AND save

compare saved inputs discard if not the same otherwise save

Then I just access each semaphore to check the state, instead of reading an actual pin. As long as you update the port read frequently enough, its fine for normal slow switches. Wont work for fast encoders and the like, tho


Wed, 14 Dec 2011 00:01:12 +0000

Already found one mistake in the documentation where the Basic IO board had incorrect pin number stated.

Please let me know about any mistakes you find in the documentation so that I can get them corrected.

Gene Apperson Digilent