Blink LED code with Atmega Registers

Hi,

I am unable to get the desired output using the Code below for the Blink LED code.

#include<avr/io.h>
#include "Arduino.h"

#define BAUDRATE 19200

void port_init()
{
DDRB &= ~(1 << PB7);
}

int main()
{
port_init();
int i = 0;
Serial.begin(BAUDRATE);
while (1)
{
Serial.print("IN While\n");
PORTB &= ~(1 << PB7);
Serial.print("LED OFF\n");
delay(100);
PORTB |= (1 << PB7);
Serial.print("LED ON\n");
delay(100);
}

return 0;

}

Regards,
Prem Annam

You need to call the "init()" method as the first thing in "main()".

What does this line do?

  int i = 0;

You never use 'i' anywhere.

Danois90:
You need to call the "init()" method as the first thing in "main()".

what does the init() method contains?
can you elaborate more so that it helps me in resolving it fast.

You’ve set the port bit to be an input...

And the “Serial” functions won’t work without having called init()

westfw:
You’ve set the port bit to be an input...

The problem is more likely that PB6+PB7 is used for the oscillator on Arduino and thus has no corresponding pin. But since OP does not name the hardware, that's just a guess.

westfw:
And the “Serial” functions won’t work without having called init()

what does Init() function contain for Serial Monitor? Without Serial monitor, functions also it's not working.

I made it as DDRB port as Output now yet not working.

Regards,
Prem Annam.

Think Twice before you reply. :slight_smile:

what does Init() function contain

sp. "init()".

You have the source; why don't you take a look?

TheMemberFormerlyKnownAsAWOL:
sp. "init()".

You have the source; why don't you take a look?

please look into the example given by the Arduino can you please tell me where is then Function called init() in it?

/*
DigitalReadSerial

Reads a digital input on pin 2, prints the result to the Serial Monitor

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}

Please Mention what does the init() Function Used for? Does it contain the Serial.begin() or?

Regards,
Prem Annam

Think Twice before you reply.

It is called from main () (in main.cpp) right before initVariant() is called, which is called before setup()
init can be found in wiring.c, IIRC

Please remember to use code tags when posting code.

You only need to call init because you have made your program with a main() function, which will replace the normal arduino main() function that would have called init() for you, before calling setup().