I recently decided to learn direct port manipulation instead of builtin arduino routines.
First I tried this piece of code
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("on");
delay(500);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("off");
delay(500);
}
This program on running instantly established connection with the serial monitor on my arduino IDE.
But when I try this code
int main(void)
{
Serial.begin(9600);
DDRB = B11111111;
while(1)
{
PORTB = PORTB | 0x20;
Serial.println("off");
_delay_ms(500);
PORTB = PORTB & 0xDF;
Serial.println("on");
_delay_ms(500);
}
}
But this program is taking some time, about 5-10 seconds to establish serial connection. Also delay() is not working too. When I use delay() instead of _delay_ms(), the entire arduino board freezes like a computer and stops blinking the LED instead LED is fully ON.
I tried is on several programs, while "setup-loop" program connects instantly, AVR C programs does not.
why does it happen?
main()
initializes all the appropriate hardware and timers. You're circumventing that with your ownmain()
so nothing Ardunioy works.