DEV Community

SUNG CHUL
SUNG CHUL

Posted on • Updated on

Blink an LED

My code

#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB |= 0x0F;   // xxxx1111

    PORTB = 0x01;
    while (1)
    {   
        _delay_ms(500);

        PORTB = PORTB << 1;
        if(PORTB == 0x10)
            PORTB = 0x01;
    }
}
Enter fullscreen mode Exit fullscreen mode

chatGPT's code

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN PB0

DDRB |= (1 << LED_PIN);
int main(void){
    while (1) {
        PORTB |= (1 << LED_PIN);  // turn the LED on
        _delay_ms(500);  // delay for 500 milliseconds
        PORTB &= ~(1 << LED_PIN);  // turn the LED off
        _delay_ms(500);  // delay for 500 milliseconds
    }
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Image description

Easy

Top comments (0)