avr-gcc and .c files compilation for AtMega328P-PU - Page 1
Forum section: electronics / general

author: psysc0rpi0n
author_rank: Frequent Contributor
timestamp: October 06, 2016, 07:26:28 am
content: Hello!I'm trying to write a small program with Arduino C style coding. But I would like to use avr-gcc or even avr-g++ to compile, link and create the .hex files to upload onto the chip.The problem is that I'm using Arduino specific functions such as Serial.begin, AnalogRead and so on and when compiling the .c file, avr-gcc is complaining about non defined functions. I have been searching Google and all I can find is examples of blinking LEDs that doesn't need those functions so I have no idea how to replace those functions.So it would be handy if I could use header files that makes possible the compilation of the program straight forward... Is this possible?

author: voltsandjolts
author_rank: Unknown
timestamp: October 06, 2016, 07:55:39 am
content: Maybe:http://stackoverflow.com/questions/13260508/using-the-arduino-libraries-for-regular-avr-code

author: hasithvm
author_rank: Contributor
timestamp: October 06, 2016, 08:01:40 am
content: Quote from: psysc0rpi0n on October 06, 2016, 07:26:28 amSetting up the UART (serial port) and the ADC are very straightforward - have you looked at the datasheet? You may be able to get away without pulling in the Arduino libs.This has a nice example of how to setup the hardware serial port:http://maxembedded.com/2013/09/the-usart-of-the-avr/And the ADC:http://maxembedded.com/2011/06/the-adc-of-the-avr/

author: nuno
author_rank: Frequent Contributor
timestamp: October 06, 2016, 08:12:16 am
content: Arduino is using C++ stuff, if you're trying to compile a .c file it will fail.Also agree you should try leaving the Arduino stuff all together... otherwise, why would you leave the Arduino IDE? If it's just because it completely sucks as a code editor, you can configure it to use an external file editor.

author: psysc0rpi0n
author_rank: Frequent Contributor
timestamp: October 06, 2016, 08:37:15 am
content: @voltsandjolts, @hasithvm, I'll keep those links for later reference.@nuno, yes, that is also a reason! But the main reason was that I wanted to do things by hand... This way I could understand better how things work in the background. I see tutorials where macros and other definitions are used such as PORTx, PINx and few commands to set ports and pins as output, input, low or high. I can understand that and make use of them, but I cannot use functions such as the ones I mentioned because I don't know how to make reference to them!But I can also set a different text editor, indeed!

author: nuno
author_rank: Frequent Contributor
timestamp: October 06, 2016, 09:06:15 am
content: Quote from: psysc0rpi0n on October 06, 2016, 08:37:15 amThen you must ditch out Arduino stuff completely. Those "Serial functions" are implemented in the Arduino libraries, you won't learn anything if you keep using them. All you need is ATmega328's datasheet, and programming skills (this is a generic skill). Start with the simpler stuff (like port I/O) then slowly move on to the more complex things and work on your programming skills as you go. You can look at other peoples' code, there's lots of it for AVRs. You have tutorials even in your mother tongue at lusorobotica.

author: dannyf
author_rank: Super Contributor
timestamp: October 06, 2016, 09:37:00 am
content: Quotethe stock arduino environment does that.

author: psysc0rpi0n
author_rank: Frequent Contributor
timestamp: October 06, 2016, 09:55:00 am
content: Quote from: dannyf on October 06, 2016, 09:37:00 amYes, but uses a Makefile or so where it includes the folders with the needed header files are! The '-I/path/to/folder' flag!

author: bingo600
author_rank: Super Contributor
timestamp: October 06, 2016, 04:12:17 pm
content: I'we been using this onehttps://github.com/sudar/Arduino-Makefilehttp://hardwarefun.com/tutorials/compiling-arduino-sketches-using-makefile/Bingo

author: westfw
author_rank: Super Contributor
timestamp: October 06, 2016, 09:14:26 pm
content: QuoteOTOH, you can learn a lot by looking at the arduino code that implements their serial functions, copying it into your program, and making it work in a pure C environment. Yeah, there's cruft in there to "simplify" reusing code on different chips with different numbers of uarts. Understanding that is good for you too.I don't understand why so many people cry "You should ditch the Arduino libraries" without mentioning the "you should READ the arduino libraries."

author: nuno
author_rank: Frequent Contributor
timestamp: October 06, 2016, 10:35:59 pm
content: There are many tutorials (even in the OP's mother tongue as I mentioned) with better teaching and simpler code than looking at Arduino libraries' implementation with the purpose of learning AVR bare metal programming. It's useful to look at them if you're interested in knowing how are they implemented because you want to know what resources are they using, what's the performance, and such things, IMHO.

author: psysc0rpi0n
author_rank: Frequent Contributor
timestamp: October 07, 2016, 05:09:45 pm
content: Quote from: nuno on October 06, 2016, 10:35:59 pmCan you give me an example?

author: nuno
author_rank: Frequent Contributor
timestamp: October 07, 2016, 05:17:41 pm
content: http://lusorobotica.com/index.php?topic=2838.15