Go Down

Topic: Please explain this (nunchuck) code to me (Read 789 times) previous topic - next topic

CrashingDutchman

I am having a hard time to understand the code below. It is part of the code that can be used with the Wii Nunchuck and the thingm adapter for the Nunchuck. I understand the code is to define power and gnd for the Nunchuk, but I don't see what it does exactly.

Code: [Select]
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
   DDRC |= _BV(pwrpin) | _BV(gndpin);
   PORTC &=~ _BV(gndpin);
   PORTC |=  _BV(pwrpin);
   delay(100);  // wait for things to stabilize        
}


From the comment in the first line I understand this code is used to power the nunchuck. I also would like to know what (similar) code can be used to shut off power (if that is even possible).

Thank you in advance!

mem

that code does the same as this:

Code: [Select]
static void nunchuck_setpowerpins()
{
  pinMode(16, OUTPUT);   // Arduino analog pin 2
  pinMode(17, OUTPUT);   // Arduino analog pin 3
  digitalWrite(16,LOW);
  digitalWrite(17,HIGH);
  delay(100);  // wait for things to stabilize
}


you can turn off power by doing digitalWrite(17,LOW);

CrashingDutchman

Thank you mem, that I understand.

Should I read up about the DDRC and PORTC as they are used in the code I posted or is the code you posted just as good?

Osgeld

direct port manipulation as in the example you provided is many times faster, but many times harder to understand for most people

http://www.arduino.cc/en/Reference/PortManipulation

retrolefty

Also code using direct port accessing will not be as portable, as for example a standard Arduino and the mega model have different port assignments for their pins.

Lefty

mem

#5
Dec 10, 2009, 07:21 pm Last Edit: Dec 10, 2009, 07:22 pm by mem Reason: 1
Quote
direct port manipulation as in the example you provided is many times faster

with a delay of 100ms in there, it makes no difference in this case  ;)


Quote
Also code using direct port accessing will not be as portable, as for example a standard Arduino and the mega model have different port assignments for their pins.

true, but the arduino pin numbers would also need to be changed for the mega, I think 18 and 19 are the correct numbers for the nunchuck adapter power pins on the mega

CrashingDutchman

Now I understand the code, but I run into something that might be a problem.

I am working on a rather time sensitive program. I am generating ppm stream for r/c transmitter. All is running fine as long as I don't use the code mentioned in the first post of this thread. All frames are almost as long as I want them, there are only a few microseconds differences between frames.

As soon as I use the mentioned code, the differences between time frames are so much that the generated stream is not recognized anymore by my transmitter. This is WITH the Nunchuck itself connected. When I remove the Nunchuck adapter it seems that all is back working normally.

I am NOT yet using any code to read from the Nunchuck, I am just powering it.

Is it better to power my Nunchuck from +5V on the Arduino or should I share ground of Nunchuck on pin 16 with GND on Arduino?

Any ideas are appreciated!

Go Up