Now connect your hardware or modify your hardware as shown in circuit diagram
Pin number 40 (PORTA0) is connected to positive pin of a led through a 220 ohm resistor . and first pin of the push button is connected to the pin number 1 (PORTB0) and then second pin is connect to the ground . Make sure that pin number 10 is connected to the +5 volt ie VCC and pin number 11 is connected to ground (0 volt) . Now open your Atmel studio and modify your code as follows
What you have just did is
DDRA|=(1<<PINA0); make your led pin (only that pin ) in output pin because it is used for led .here led is an output device .
DDRB|=(0<<PINB0); make your button pin in input state because the button is used to give input to the microcontroller . and according to state of buton your output will be observed . If you pressed the button the led will change its state.
Now inside your while loop (infinite loop)
if (bit_is_clear(PINB,0)) This is if condition and the function bit_is_clear inside the if condition is used to check whether the pin is pressed or not . bit_is_cleared means the pin where the button is located is cleared or not ie it is made 0 or not . if the pin is cleard (ie pressed to ground ). Then the inside content is executed . else it will skip the if condition and the control of flow of program goes outside the if condition.
Note: you can use bit_is_set(pin_name,pin_number) instead of bit_is_clear(pin_name ,pin_number) function and for this you have to make sure that the pin using button is made low first then this function is used to set ie making one .PORTA^=(1<<PINA0);and then inside if condition . If the button is pressed the led will toggle . note : here we did not include the util/delay.h header file it is not necessary because delay function is not used here
Build this code and hex file is generated . Now upload that thing to your microcontroller . and you the led will go ON if it is in off state and and it will OFF if it is in ON state . try it .
If you tried it something amazing is happened right . It seems like your button did not worked properly ! you are probably wondering that something is happening it is called bouncing of button so the output is not correctly obtained . in that case you should modify your circuit by adding a 200 nf capacitor as shown in circuit diagram below to debouncing the button.
Adding the the capacitor with the button remove the bouncing effect of the button by holding the charge for some time in its plate.
If you pressed the button the state of button does not change immediately . it will bounce like this
due to mechanical spring inside the button the state do not change immediately
So if you place a capacitor then this can be eliminated .
But you can solve the bouncing problem of the button without adding capacitor you can modify your code count the 1 and 0 of the bounced value then according to these value you can do the job inside the button . you should do that thing as shown below.
First you declared the one_count and zero_count variable so that you can measure the number of 1s and 0s while pressing the button ie it is the bouncing the button state .Then the one_count is incremented each time you pressed the button finally if the number of 1s (ie one_count) reached the certain level (in this case it is 100) and if the button is not pressed (pressed=0) then the button is made pressed and the led is made toggle .
This code is a little bit lengthy for more buttons are needed to project then for each button all the code should be write This might be too long and which might not be appropriate for a good programmer so we need to make a small header file for button so that we do not need to write all code instead we just only call a function , we will talk about the header files and libraries in the next chapter .