My first AVR C program- LED not blinking

int main(void)
{
    clock_prescale_set(clock_div_128); //set the clock to use the 8MHz internal clock divided by 128 for an internal frequency of 250KHz, consumes 0.4mA while not sleeping.

    while (1) 
    {
        PORTD4 == 1;//Set pin 13 of the micro to high.
        _delay_ms(1000);
        PORTD4 == 0;//Set pin 13 of the micro to low;
        _delay_ms(1000);
    }
}

The problem is how you "set" the port, you are using == which is a comparison operator, not an assignment. Try just using =.

void main()
{
    DDRD = 0xFF;         //PD as output
    PORTD= 0x00;         //keep all LEDs off

    while(1)
    {
        PORTD &= 0b11110111;       //turn LED off
        _delay_ms(500);   //wait for half second
        PORTD |= 0b00001000;       //turn LED on 
        _delay_ms(500);   //wait for half second
    }        
}

You may also have to set the direction of the port somewhere. You would do this by setting the DDRD to be 0b00001000, which should set Pin 4 of Port D to an output.


Three problems:

  1. If you're delay.h, you need to define the CPU speed. #define F_CPU 8000000UL in your case.
  2. Port D needs to be set as an output by setting bit 4 in the DDRD register, typically before the superloop. DDRD = 0b00010000
  3. PORTD4 is not how you access that port pin. You would use PORTD, similarly to how I demonstrated the DDRD register above. Personally, I would rewrite your superloop to:

    while (1) 
    {
        PORTD ^= 0b00010000;
        _delay_ms(1000);
    }
    

Tags:

C

Port

Avr