Electronic – My first AVR C program- LED not blinking

avrcport

I created a simple program to be put onto a ATMega328P chip (Datasheet). I wrote my first program in ATMEL studio that I wanted to use to Blink an LED at a rate of 0.5Hz with extremely low power usage. Here's my code:

/*
 * GccApplication1.c
 *
 * Created: 11/12/2017 8:56:49 PM
 * Author : Brice
 */ 

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

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);
    }
}

What I expect this code to do to this micro-controller is to turn PORTD register 4 on for 1 second, then turn it off for a second.

currently, The PortD4 will not have any voltage changes to it, and remains grounded while the device is powered.

Any explanation of this would be appreciated!

For this instance, I'm using the Arduino Pro mini board for prototyping..

pin 4 of the Pro mini is connected to the common Anode of an RGB led, and the G pin of the LED is connected from a 510 Ohm resistor to GND. I've tested the wiring with a working arduino, and the led even lights at 3.3 Volts pretty brightly.

enter image description here

Best Answer

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.