In: Computer Science
Answer the following question from the perspective of AVR (C language)
How to set the data direction (output/input) in (AVR, c language)?
Write a statement to turn ON a pin, say D3.
Write a statement to turn OFF a pin, say D3.
How to toggle a pin?
Answer: Please upvote if its upto your expectation or comment for any Query. Thanks
Note :-
1. We are using Atmega32 avr micro controller .
2. LED connected on pin number 3 of PORTD please check image for hardware connection.
3. To blink led we have to make this pin Output .
Program Plan :
1. Define Header file of AVR.
2. Define Micro controller frequency .
3. Make output to the pin .
4. set high on pin to turn on led ,low for turn off led and for blinking for a period make it toggle
Program :
1. Set output the Pin -> set 1 in DATA Direction Register(DDRD) of PORTD
DDRD|=(1<<3); //output the PORTD.3 pin
D3 means PIN 4 of PORTD so to effect particular pin we are shifting lef by 1 for 4 number pin . using OR sign to not effect the other pin of PORT.
2. Set Input -> set 0 in DDRD of PORTD
DDRD&=~(1<<3) //input the PORTD.3 pin
it will set 0 the D3 pin using AND operation to do not effect other pin .
3. Turn on D3 pin of PORTD
PORTD|=(1<<3) //set 1 to turn on led
4. Turn of D3 pin of PORTD
PORTD&=~(1<<3) //set 0 to turn off led
5. toggle the D3 pin
PORTD^=(1<<3) //toggle the D3 means EX-OR of status of pin
it will EXOR the value of PORTD3 pin value .
Program :
this program will turn on and off the led for 1 second
.
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h> //avr header file
#include <util/delay.h>
int main(void)
{
DRRD|=(1<<3) //Output
while(1) //infinite loop
{
PORTD|=1<<3 //turn on led
_delay_ms(1000); //1 second delay
PORTD&=~1<<3 //turn off led
_delay_ms(1000); //1 second delay
}