LED Blink Sequence on AVR using C

 avatar
subratasarkar20
c_cpp
a month ago
888 B
1
Indexable
Never
#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
	DDRB = 0xFF; 		// set all the pin from register B as i/p
	PORTB = 0X00; 		// clear register initially
	
	int bitPosition; 		// variable to count the bit/pin position
	int direction = 0; 		// variable to keep track of the LED's direction
	
	while (1)
	{
		if (direction == 0) 		
		{
			for (bitPosition = 0; bitPosition < 8; bitPosition++) 		// first loop to turn on all the LEDs from PINBO to PINB7 
			{
				PORTB |= 1 << bitPosition; 			// to set the register pins
				_delay_ms(100);
			}
		}
		
		direction = 1;
		
		if (direction == 1)
		{
			for (bitPosition = 7; bitPosition >= 0; bitPosition--) // second loop to turn off all the LEDs from PINB7 to PINB0
			{
				PORTB &= ~ (1 << bitPosition); 			// to reset the register pins
				_delay_ms(100);
			}
		}
		
		direction = 0;
	}
}
Leave a Comment