LED-general programming question

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,sorry if ive posted wrong place i just wanted to ask a general programming question,ive ended up hear,not sure why i cant make a new post,not sure if ive piggy backed onto some one elses post,but hear goes.I have made a dev board called pixel loader wich is in the form of a usb dongle and uses usbasploader for bootloader,now in the bootloader source code there are post programming options,now i want to enable a status led to flash on bootloader entry,it says the led has to go to vcc not ground,my board has an led but goes to gnd,so i want to sink the led if possible,hear is the code

#if LED_PRESENT
#define LED_INIT() { }
#define LED_EXIT() { USB_DDRPORT(LED_PORT) &= ~(1<<LED_BIT); }
#define LED_BLINK() { USB_DDRPORT(LED_PORT) ^= (1<<LED_BIT); }
#else
#define LED_INIT()
#define LED_EXIT()
#define LED_BLINK()
#endif

How do i enable the led to sink,any help would be great.this is the part that needs to be changed,ime new to programming,i have spent lots of time trying it myself,ime used to arduino so ime not sure how to work with plain old c++ ime used to pinmode etc digitalwright etc any help on setting ports in and out in plane c would be great too.thanks

Last Edited: Mon. Aug 28, 2017 - 09:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define LED_EXIT() { USB_DDRPORT(LED_PORT) &= ~(1<<LED_BIT); }

Will set the port bit low to turn on an LED that is connected to VCC, or turn off an LED connected to GND.

Do this to set the port bit high.

 

#define LED_EXIT() { USB_DDRPORT(LED_PORT) |= (1<<LED_BIT); }

This will set the port bit high to turn on an LED connected to GND, or turn off an LED connected to VCC.

 

In either case, the LED needs a current limiting resistor (330 ohm for VCC = 5v) connected in series with the port pin.

 

Jim

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do i enable the led to sink

You've got that backwards.  The existing code is written to sink current through the LED.  You are trying to source current to it.

 

It looks like they've tried to save a few bytes of flash by relying on the PORT bit to remain at its reset-default state of 0.  The LED is being turned on and off by manipulating the DDR bit alone.

 

In order to achieve what you want, you'll need to switch things around a bit.

 

I don't know what USB_DDRPORT() has been #define'd as, but I can probably assume it just does some magic to turn a PORT register macro into DDR register macro.  For sourcing, you'll need to initialise the I/O pin as an output, then manipulate the PORT bit.  Maybe:

#if LED_PRESENT
  #define LED_INIT() { USB_DDRPORT(LED_PORT) |= (1<<LED_BIT); }
  #define LED_EXIT() { LED_PORT &= ~(1<<LED_BIT);  USB_DDRPORT(LED_PORT) &= ~(1<<LED_BIT); }
  #define LED_BLINK() { LED_PORT ^= (1<<LED_BIT); }
#else
  #define LED_INIT()
  #define LED_EXIT()
  #define LED_BLINK()
#endif

Where did you find your code?  The closest Google could come up with is this:

https://github.com/hackerspaceshop/usblinky/blob/master/software/builds/v1.4/micronucleus/bootloaderconfig.h:

#define LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PB1

#if LED_PRESENT
  #define LED_INIT(x) LED_PORT &=~_BV(LED_PIN);
  #define LED_EXIT(x) LED_DDR &=~_BV(LED_PIN);
  #define LED_MACRO(x) if ( x & 0xd ) {LED_DDR&=~_BV(LED_PIN);} else {LED_DDR|=_BV(LED_PIN);}
#else
  #define LED_INIT(x)
  #define LED_EXIT(x)
  #define LED_MACRO(x)
#endif

Perhaps a different version of the same bootloader you're using?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Aug 28, 2017 - 05:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thread split.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote:

Where did you find your code?  The closest Google could come up with is this:

https://github.com/hackerspaceshop/usblinky/blob/master/software/builds/v1.4/micronucleus/bootloaderconfig.h:

OK, so if that's anything like what you have, this is how USB_DDRPORT is defined:

#define USB_CONCAT(a, b)            a ## b
#define USB_CONCAT_EXPANDED(a, b)   USB_CONCAT(a, b)

#define USB_OUTPORT(name)           USB_CONCAT(PORT, name)
#define USB_INPORT(name)            USB_CONCAT(PIN, name)
#define USB_DDRPORT(name)           USB_CONCAT(DDR, name)

In which case, the suggested code I gave you earlier won't work, since it relies on a fully qualified LED_PORT macro e.g:

#define LED_PORT PORTB

... whereas the macros I dug up at the above link are expecting something more like:

#define LED_PORT B

If that is so, then you might have more success with this:

#if LED_PRESENT
  #define LED_INIT() { USB_DDRPORT(LED_PORT) |= (1<<LED_BIT); }
  #define LED_EXIT() { USB_OUTPORT(LED_PORT) &= ~(1<<LED_BIT);  USB_DDRPORT(LED_PORT) &= ~(1<<LED_BIT); }
  #define LED_BLINK() { USB_OUTPORT(LED_PORT) ^= (1<<LED_BIT); }
#else
  #define LED_INIT()
  #define LED_EXIT()
  #define LED_BLINK()
#endif

 

In truth, there is a slight flaw in the original code.  It's better to wrap compound statements in a do{}while(0); construct:

#if LED_PRESENT
  #define LED_INIT() do { USB_DDRPORT(LED_PORT) |= (1<<LED_BIT); } while (0);
  #define LED_EXIT() do { USB_OUTPORT(LED_PORT) &= ~(1<<LED_BIT);  USB_DDRPORT(LED_PORT) &= ~(1<<LED_BIT); } while (0);
  #define LED_BLINK() do { USB_OUTPORT(LED_PORT) ^= (1<<LED_BIT); } while (0);
#else
  #define LED_INIT()
  #define LED_EXIT()
  #define LED_BLINK()
#endif

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]