Results 1 to 6 of 6

Thread: Using switches with avr atmega8l

  1. #1
    Junior Member User
    Join Date
    Jan 2008
    Location
    Delhi
    Posts
    15

    Default Using switches with avr atmega8l

    sir i have now succesfully interfaced a 7 segmented display with avr( thanks to u guys at RI for helping me get started..)
    I wanted to ask that how can we interface a switch such that if that switch is pressed a pin receives 5v and an led(..or any other trigger..)and another port glows...
    also can avr recept an input high of 3v
    the code that i used in codevision is given below..

    #include <mega8.h>
    #include <delay.h>

    // Declare your global variables here

    void main(void)
    {


    // Input/Output Ports initialization
    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTB=0x00;
    DDRB=0x00;

    // Port C initialization
    // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTC=0x00;
    DDRC=0x00;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
    PORTD=0x00;
    DDRD=0x01;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    TCCR0=0x00;
    TCNT0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer1 Stopped
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    MCUCR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    while (1)
    {
    if(PINC.0==1)
    {

    PORTD.0=1;

    }
    }
    }
    thanks

  2. #2

    Default

    You will have to wire the switch such that one of its terminals goes to ground and other goes to the required IO pin. Then configure that pin as output by writing 0 to the DDRxn bit and enable the internal pullups for that pin by setting PORTxn to 1. Enabling the internal pullup will connect that pin to Vcc through a high value resistor. This makes sure there is a known potential at the pin even when the switch is not activated.

    With this setup, the pin goes low every time the switch is pressed. You can sense the state of the pin by reading its PINx bit.

    The following were two pages I got from a Google search -

    http://www.scienceprog.com/reading-a...-using-winavr/
    http://www.micahcarrick.com/avr-tuto...-debounce.html

    The threshold voltage for an Atmega8 to detect a high at the pin is around 2V when its Vcc is at 5V. You may refer the "Pin Thresholds and
    Hysteresis" page under the "ATmega8 Typical Characteristics" section of the Atmega8 datasheet for more details.
    Probots
    Online Robot Store : www.probots.co.in - Robot Kits , Development Boards and Components
    Blog : www.probots.co.in/blog - Probots' Official Blog
    Twt : www.twitter.com/probots - Probots' Official Tweets

  3. #3
    Junior Member User
    Join Date
    Jan 2008
    Location
    Delhi
    Posts
    15

    Default thanks

    thanks a ton for the reply sir..will try this on codevision avr..

  4. #4
    Junior Member User
    Join Date
    Jan 2008
    Location
    Delhi
    Posts
    15

    Default

    sir i used the following program( as u suggested)..but it didnt work:
    #include <mega8.h>

    // Declare your global variables here

    void main(void)
    {
    // Declare your local variables here

    // Input/Output Ports initialization
    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTB=0x00;
    DDRB=0x00;

    // Port C initialization
    // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
    // State6=T State5=T State4=T State3=T State2=T State1=0 State0=P
    PORTC=0x01;
    DDRC=0x02;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTD=0x00;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    TCCR0=0x00;
    TCNT0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer1 Stopped
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    MCUCR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    while (1)
    {
    if(PINC.0==0)
    PORTC=0b01000000;
    else
    PORTC=0b00100000;

    }
    }
    plz reply asap

  5. #5
    Junior Member User
    Join Date
    Jan 2011
    Location
    Hyderabad
    Posts
    26

    Default

    to which port is the led connected. and to which port is the switch connected.

  6. #6
    Junior Member User
    Join Date
    Jan 2011
    Location
    Hyderabad
    Posts
    26

    Default

    Try making these changes to the code and also the circuit.
    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=Out Func3=Out Func2=Out Func1=Out Func0=In
    // State7=T State6=T State5=T State4=0 State3=0 State2=0 State1=0 State0=T
    PORTB=0x00;
    DDRB=0x1E;

    // Port C initialization
    // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State6=T State5=T State4=P State3=P State2=P State1=P State0=P
    PORTC=0x1F;
    DDRC=0x00;

    Connect the led's to the portb.1,2,3 and 4.And switches to the portc.0,1,2,3.You need to connect the switches to the ground in order to make them work, and in the infinite while loop add this code.

    if(PINC.0==0)
    {
    PORTB=0x00;
    PORTB.1=1;
    }
    else
    {
    PORTB=0x00;
    PORTB.2=1;
    }

    Reply to me whether it is working or no

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •