This is probably not a very relative topic to Arduino community because it investigate AVR-GCC code.
It's just that I'm running my current project with AVR-GCC coding but I compile and upload the code with Arduino IDE. I'm very near to switch my coding with Arduino C++, because I'm excited to try classes and the cool features of C++.
The issue now is that I'm trying a simple function which returns 16-bit value to the main loop and show that value on the Serial monitor.
I've tested two versions of code, 1st Arduino sketch, 2nd AVR-GCC sketch.
The problem is that with the AVR-GCC sketch, when I compile and upload the code, first I get 8-bit, then delays for like 5 seconds then start to show the actual 16-bit value.
These are the sketches:
Arduino sketch, working with no problems:
uint16_t tft24_read_id(void);
uint16_t tft_id;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
tft_id = tft24_read_id();
Serial.println(tft_id,HEX);
delay(500);
}
uint16_t tft24_read_id(void){
uint16_t id=0x1211;
return id;
}
What happens if you put a "_delay_ms(1000)" right after "Serial.begin" in "main"? It may be caused by the serial connection not being completely ready when you start to use it.
Danois90:
What happens if you put a "_delay_ms(1000)" right after "Serial.begin" in "main"? It may be caused by the serial connection not being completely ready when you start to use it.
Same thing, I even tired 10 seconds
paulvha:
first thing to do in Main is call:
init(); // needed for things like clocks, timers and uart
This is just a test code, I didn't include any init functions because the problem is because of the AVR sketch.
6v6gt:
Maybe also this helps and it implies that _delay_ms(500) is too high for the 16MHz clock you get with say a Uno and offers work arounds : https://www.avrfreaks.net/forum/delayms-problem
I did a simple test code in Arduino sketch with millis(); to measure the period of _delay_ms(500);
void setup() {
// put your setup code here, to run once:
DDRB = 0xFF;
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long t1,t2,r;
t1 = millis();
_delay_ms(500);
t2 = millis();
r = t2-t1;
Serial.println(r,DEC);
}
And the results ranges from 502 - 504, so I think the delay functions whether they are for the Arduino or AVR, they perform the same.
But I think it has to do with the Arduino libraries and how they are connected with other libraries. Because Serial library isn't intended to work with AVR sketch I guess.
I think it's the time to start coding with C++, I just was excited to write my projects with AVR-GCC.
The code that actually gets called can be found off the IDE path:
hardware\arduino\avr\cores\arduino\main.cpp
As you can see, there are several function calls before setup() is called:
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
void setupUSB() __attribute__((weak));
void setupUSB() { }
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
Either use the Arduino framework and its libraries or don't.
Your second sketch is not allowing the Arduino framework to initialize its environment but then you are attempting to use Arduino library code. That is likely to have issues.
Either use Arduino or stick to using only avr-gcc supplied code and avr-gcc supplied libraries like avr libC.
If you are not wanting to use the Arduino environment, I'd suggest that you not use the Arduino IDE and then write your own makefiles.
paulvha:
you have to include init() as a first call in main() order to set the timers correct. always.... (see init())
You're absolutely right! Problem solved, sorry didn't get you at the first time. I thought you mean that I have to call whatever init functions I have.
Thank you again
econjack:
The code that actually gets called can be found off the IDE path:
hardware\arduino\avr\cores\arduino\main.cpp
As you can see, there are several function calls before setup() is called:
Yes thank you, but the Arduino.h is really clear about these function. It's just the way Arduino manages its combined libraries.
bperrybap:
Either use the Arduino framework and its libraries or don't.
Your second sketch is not allowing the Arduino framework to initialize its environment but then you are attempting to use Arduino library code. That is likely to have issues.
Either use Arduino or stick to using only avr-gcc supplied code and avr-gcc supplied libraries like avr libC.
If you are not wanting to use the Arduino environment, I'd suggest that you not use the Arduino IDE and then write your own makefiles.
--- bill
Thank you for the recommendations and the tips
But anyway the problem solved with only calling the init(); function.
You also right I really better work with only AVR-GCC libraries and codes or Arduino with its libraries.
I just needed the Serial functions to display data. I also launched the Atmel Studio 7 and also found a terminal window to display data, so everything is OK now.