How to read a register on the 32U4

Dear all,
I am designing a Leonardo based radiometer with a USB-charged LiPo-battery.
As far as I understood the 16/32U4 datasheet (7766F-AVR-11/10) p. 263 f., there is a register called USBSTA, whose last bit is set if the VBUS-Pin of the MCU sees more than 1.4 V. If I would know how to read the register I could define certain actions to take place only if there is a USB connection (like charging the battery, or data transmission to the host computer) or if there is none (like data aquisition mode).
Unfortunately, my programming skills are too scarce for that, and my finds in google seem to have no effect in the IDE. Here in the forum, "register" normally shows how to do something in I2C or SPI.
It would be very helpful if somebody could give me a hint or link to the right direction. Some knowledge about this would allow for a deeper look into the MCUs guts and surely enhance the overall understanding of it.
Thank you very much, have a good time
Seb

have you tried using the register directly?

if ((USBSTA & (1 << VBUS)) != 0)
{
  // bit is set, MCU sees more than 1.4V
}

if you dig into page 408 of the document (chapter 31. Register Summary), you'll see that the register at address 0xD9 is the usb state (hence the name USBSTA and the VBUS bit is 0 (LSB). if you also look into the header files, you'll find these defined:

file: hardware/tools/avr/avr/include/avr/iom32u4.h
---
#define USBSTA _SFR_MEM8(0xD9)
#define VBUS 0

that should do the job :slight_smile:

Uh, that was fast! Thanks for the answer, I will try it as soon as I get home.

Hi,
I tried this:

const int led = 13;

void setup() {
pinMode (13, OUTPUT);
}
void loop() {
 if ((USBSTA & (1 << VBUS)) != 0)
{
  digitalWrite(led, HIGH);
delay (250);
}
digitalWrite(led, LOW);
delay(250);
}

and then I put 9 V to VIN and pulled the USB. The LED kept blinking. So does it when I disconnect and just connect 9V through VIN. I have to check the schematics whether the 5V from the voltage regulator somehow sneak to the VUSB-pin, but if not, then there is something not working with that code attempt. Or am I wrong? #define VBUS 0 does not override/prevent any bitsetting of the MCU, does it? It is just the initial setting, right?!
Good night, all! (Well, Europeans at least...)

the #define isn't required as it is part of the standard headers - as for how it should work, i'm not familiar with it 100%.

Hey,

I am working on the same thing.
I found this but can not get those example to work.
http://forum.arduino.cc/index.php?topic=28567.0

BUT your code works!! Thanks! :slight_smile:

const int GREEN_LED = 10;
const int RED_LED = 3;

void setup() {
  pinMode (GREEN_LED, OUTPUT);
  pinMode (RED_LED, OUTPUT);
}
void loop() {
  
if ((USBSTA & (1 << VBUS)) != 0)
  digitalWrite(GREEN_LED, HIGH);                // It goes here when the USB is plug in the Arduino
else
  digitalWrite(RED_LED, HIGH);
  
}

I am using a Arduino Pro Micro from Sparkfun (5V 16Mhz).
I plug the USB and Green LED light up. I plug 6V at Vin and Red LED light up.

NOW I can work on getting low power sleep on the Arduino 32u4. If you go to sleep and turn OFF USB you can not upload code anymore!! With this code, I will be able to enable the USB when it is plug in!

Thanks to both of you!

Now look at that! It seems the problem is the short delay in my sketch. I measured the voltage on the VBUS pin after disconnecting USB, and it takes about 5 seconds to fall below 1.4V. Then it works as expected (right now with a Leonardo).
Thanks very much for the tip, ardiri, and for trying it in a different setup, robo_maniac.
Maybe I will put a pulldown resistor to the pin to make the transition quicker; when there is USB I do not have to be overly cautious with a couple of mAmps.
Cheers