need help with Xmega A1U, switch and LED Flashing

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

The LED should flash following the switch but it doesn't. Can anyone tell me what's the problem with my code?

#define F_CPU 32000000UL
#include <avr/io.h>
#include <util/delay.h>
#define LED_ON PORTC_OUT|=(1<<7)
#define LED_OFF PORTC_OUT&=~(1<<7)

int main(void)
{
PORTC_DIR|=(1<<7);
PORTQ_DIR&=~(1<<2);
   while (1)
   {
   if (!(PORTQ_IN&(1<<2)))
     {
  LED_ON;
  }
 else
  {
  LED_OFF;
  }}

 

Last Edited: Tue. Jul 18, 2017 - 09:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My external LED is connected to PortC7, The sw0 of Xmega I believe it's connected to PortQ2 from the inside board.

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

Mechanical switches have contact bounce.  They can make and break many times when switched.  I would wait at least 40 milliseconds after a pin change before checking the pin again.

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

Wenyu wrote:
The LED should flash following the switch but it doesn't.
What >>does<< happen?

 

Is your LED connected active-high or active-low?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Show schematic or image of setup (where we can see what is actually connected and how)

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

 When I pressed the button, the LED was actually slightly darker but did not blacked out. Is there also a touch function on the surface of the button? when I touched but not yet pressed the button, the program seemed to be working though. The board I am using is xmega128 a1u.

 

It should be active-low. I programmed if button is pressed, LED would be off.

 

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

The board is simply connected to a LED and a resistor. The button is built in the board.

 

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

What is the yellow wire connected to?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

I'm confused.  Pin 19 on EXT3 is Gnd, right?  Then the grey wire is the signal on pin 18?  And where does that go?

 

 

Note that PC7 is also routed to other circuitry.

 

When the button is not pressed, it will be floating.  So use the internal pullup.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Jul 19, 2017 - 03:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since PQ2 is not pulled up, internal pull-up must be enabled.

 

PORTQ.PIN2CTRL = 0b00011000;

 

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

kabasan wrote:

PORTQ.PIN2CTRL = 0b00011000;

 

 

Magic numbers aren't good, they're meaningless. If you come back to that line of code in 6 months or so, you're going to have to refer to a datasheet to know what setting PIN2CTRL to 0b00011000 does! That's why Atmel have very kindly provided you with bit mask and position #defines ;) also total assignment could change bits you didn't want to change.

 

PORTQ.PIN2CTRL |= PORT_OPC_PULLUP_gc;