Want to turn on LED by shorting ATMEGA16 IC PIN

Thread Starter

lucky930

Joined Jan 7, 2024
16
Hello ,
I want to turn on LED when two IC PINs suppose PORTA1 and PORTA2 are shorted, As long as they are shorted the LED should glow and when not shorted then LED should be off.
Please help me withe the C embedded code.
 

Thread Starter

lucky930

Joined Jan 7, 2024
16
Are the pins configured as outputs, digital inputs, or analog inputs?
What I want is:
I'm working on ATMEGA16 IC. I want LED (Connected at PORTC0) to be turned ON only when PORTD0 and PORTD1 pin of IC are shorted(or joint) using for ex: soldiering iron, metal piece. As soon as I remove that metal piece and unshort the IC pins, the LED should turn off.

Obviously LED will be configured as output, but having confusing about those IC pins (PORTD0 and D1) whether to be configured as IP or OP
 

Thread Starter

lucky930

Joined Jan 7, 2024
16
C:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <inttypes.h>

#define LED 0
#define SHORTPIN1 1
#define SHORTPIN0 0 

int main() {
    DDRD &= ~(1 << SHORTPIN0 ) & ~(1 << SHORTPIN1); 
    DDRC |= (1 << Buzzer);
    while (1) {
        if ((PIND & (1 << SHORTPIN0)) && (PIND & (1 << SHORTPIN1)))
        {
            PORTB |= (1 << LED);
        }
        else
        {
            PORTB &= ~(1 << LED);
        }
    }
}

[B]This code doesn't give the expected result[/B]
 

MrChips

Joined Oct 2, 2009
30,805
What I want is:
I'm working on ATMEGA16 IC. I want LED (Connected at PORTC0) to be turned ON only when PORTD0 and PORTD1 pin of IC are shorted(or joint) using for ex: soldiering iron, metal piece. As soon as I remove that metal piece and unshort the IC pins, the LED should turn off.

Obviously LED will be configured as output, but having confusing about those IC pins (PORTD0 and D1) whether to be configured as IP or OP
You don't need both PORTD0 and PORTD1 pins. You can do it with just one pin configured as input.
Is it mandatory to use two pins?
 

Papabravo

Joined Feb 24, 2006
21,225
You make one pin an input and make the other pin an output. Then:
  1. Set the output pin low and if the input is low then proceed to step 2, otherwise they are not shorted.
  2. Set the output pin high and if the input is high then you can conclude they are shorted, otherwise they are not shorted.
 

Thread Starter

lucky930

Joined Jan 7, 2024
16
****Got it working now, using below code****

C:
[/B]

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#define LED 0
#define SHORTPIN1 1
#define SHORTPIN0 0 

int main() {
    DDRD &= ~(1 << SHORTPIN0) & ~(1 << SHORTPIN1); 
    PORTD |= ((1 << SHORTPIN0) | (1 << SHORTPIN1));
    
    DDRC |= (1 << LED);
    while (1) {
        if (((PIND & (1 << SHORTPIN1)) && (PIND & (1 << SHORTPIN1)))==0)
        {
            PORTC |= (1 << LED);
        }
        else
        {
            PORTC &= ~(1 << LED);
        }
    }
}
 

BobTPH

Joined Jun 5, 2013
8,957
You make one pin an input and make the other pin an output. Then:
  1. Set the output pin low and if the input is low then proceed to step 2, otherwise they are not shorted.
  2. Set the output pin high and if the input is high then you can conclude they are shorted, otherwise they are not shorted.
Except you don’t need a pin to check to see if another pin is at ground or V+.
 

Papabravo

Joined Feb 24, 2006
21,225
Except you don’t need a pin to check to see if another pin is at ground or V+.
Well you do if the act of shorting two pins together is what you want to detect. That is a different problem than whether a pin is high or low. It is also important that you not check the pin directly without either setting the output low or verifying that it is in that condition. I suppose you could have the input connected to a pullup resistor (internal or external). Then set or verify the output is low and check to see if the input is low.
 

Papabravo

Joined Feb 24, 2006
21,225
Sure, he probably wants to short them with a solder bridge. Other than that, how is it different than shorting a single pin to ground?
Or maybe a screwdriver tip. If the nearest ground is some distance away, he might have to go find a test probe or a jumper wire. It is sometimes hard to get into the mind of a TS. I spent half a century in the business and never found the motivation to do something like this. But, hey - I'm a boomer.
 
Top