PDA

View Full Version : Using switches with avr atmega8l



irobocop
01-30-2011, 09:48 PM
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

pratheek
01-31-2011, 09:48 AM
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-avr-button-status-using-winavr/
http://www.micahcarrick.com/avr-tutorial-switch-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.

irobocop
01-31-2011, 11:49 AM
thanks a ton for the reply sir..will try this on codevision avr..

irobocop
01-31-2011, 09:55 PM
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

dheeraj91
02-12-2011, 01:17 PM
to which port is the led connected. and to which port is the switch connected.

dheeraj91
02-12-2011, 01:20 PM
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