Is there a simple, "hello world" example available in pure C (not Arduino code) that can be compiled and ran in the Arduino IDE out there? Bit twiddling / etc works fine, I am hoping to see results in the Serial Monitor.
Thank you all.
Is there a simple, "hello world" example available in pure C (not Arduino code) that can be compiled and ran in the Arduino IDE out there? Bit twiddling / etc works fine, I am hoping to see results in the Serial Monitor.
Thank you all.
Clarification: I am good with using ports and pins under pure C, I would like to have status messages show on the screen now. So far I have not been able to do it in the Arduino IDE.
void setup ()
{
Serial.begin (115200);
Serial.println ("Hello World");
}
// -----------------------------------------------------------------------------
void loop ()
{
}
Arduino code is C/C++ so you can use "pure C" and still use arduino code, but I think what your looking for is to program it "bare metal" without using any of the arduino functions.
The "Hello World" for microcontrollers is blinking an LED and that would look something like this:
// define CPU speed
#define F_CPU 16000000UL
// include delay.h from the AVR/GCC libraries
#include <util/delay.h>
void setup() {
// initialize digital pin 8 as an output.
DDRB = DDRB | B00000001; // The Port B Data Direction Register - read/write
// this is safer as it sets pin 8(PB0) as outputs
// without changing the value of pins PB6 &PB7, which are Oscillator pins
}
void loop() {
PORTB = 0B00000001; // sets digital pin 8(PB0) HIGH
_delay_ms(1000); // wait for a second
PORTB = 0B00000000; // sets digital pin 8(PB0) LOW
_delay_ms(1000);// wait for a second
}
But that isn't quite what you want either is it?
My guess is that you want to print things to a terminal( such as the serial monitor in the arduino IDE) without using Arduino's Serial library. And that is way beyond just a simple "Hello World"
If you would like to program AVR microcontroller's directly such as the Atmega 328P then I suggest going to a forum where that is the focus such as: AVRFREAKS
Also here is a link to a tutorial series: ATmega328P Programming AVR microcontroller
note tutorial #7 shows how to set up Serial comms.
also that you can still use the Arduino IDE if you like.
gcjr: thank you. Pure C, not arduino code. Thanks.
Yes, that is what I am looking for. I will check out your sugestion
Hutkikz: Maybe there is a straightforward way to just dump my text messages to a text file I can open later?
Arduino doesn't have printf, but does have sprintf, however, sprintf doesn't support floats. can us dtostrf
char s [80];
void setup ()
{
Serial.begin (115200);
sprintf (s, "Hello World");
Serial.println (s);
}
// -----------------------------------------------------------------------------
void loop ()
{
}
You could look into the HardwareSerial source code to figure out how serial communication is setup and used.
The Arduino IDE will assemble a C source code file based on your sketch. The assembled file will contain a lot of prototypes and other stuff along with the int main() {} function which will setup hardware, call void setup() {} once and then enter a infinite loop which repeatedly calls void loop() {} and void serialEvent() {} if necessary. This is how the IDE works and if you want to code on the "bare metal" and create your own int main() {} function, you should use a different IDE.
Some boards are not only for Arduino. For example the Raspberry Pi Pico can run many things, for example 'C' with a main() function and printf() and a stdio. Perhaps the ESP32 can also do that.
This is for the Raspberry Pi Pico:
#include <stdio.h>
int main()
{
stdio_init_all(); // Initialize stdio to the Serial port
printf("Pure \"C\" Hello World");
return 0;
}
The simulation in Wokwi: Pure "C" Hello World - Wokwi Arduino and ESP32 Simulator
Wokwi redirects the Serial port to certain pins and a different build environment is used for pure C, therefor I have not tried it with a Pico board in real life.
Arduino uses a software layer that is compatible for multiple boards. This is the Arduino forum and we are Arduino-enthusiasts. I don't understand why you would want to use pure 'C' that most likely will run on just one board and can not use the libraries for Arduino.
There is no such thing as pure anything. All useful programs presumably do some input and output, that means they are talking to the real world through hardware, that means they cannot be pure.
There is no such thins as "arduino code". The language you are using is C++.
a7
huh?
whether a language can access memory mapped I/O doesn't make it pure
there's a grammar description for C as well as C++ (just like any language) BNF for C. can't find the BNF for c++.
gcc still compiles .c file per the C language and .cpp per c++
The Whole point of Arduino is to make an easy and straightforward way to program these things.
If you wish to not use Arduino then you will have to do all the hard work yourself there is just not another way. Arduino is what makes it easy.
Maybe this helps.
I use it when I want to debug/print something with an Attiny2313. With only 2KB serial.print costs me >50% of its memory, while below compiles to 304 bytes. (Attinycore)
#include <util/delay.h>
int main(void) {
USART_Init(38400); // 38400 baud is maximum
char printbuffer[7]; //The ASCII of the integer will be stored in this char array
const static uint8_t Sometext[] = " and counting !\r\n"; // A string array with some fixed text
while (1) { //Infinite loop
int8_t a = a + 1; // increasing value that will be printed
itoa(a, printbuffer, 10); //(integer, yourBuffer, base)
USART_putstring(printbuffer); // print the
USART_putstring(Sometext);
_delay_ms(2000); //Delay for 2 seconds so it will re-send the string every 2 seconds
}
}
void USART_Init( unsigned int baud )
{
/* Set baud rate */
baud = F_CPU / (16LL * baud) - 1;
UBRRH = (unsigned char)(baud >> 8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
// UCSRB = (1 << RXEN) | (1 << TXEN);
UCSRB = (1 << TXEN); // transmitter only
/* Set frame format: 8data, 2stop bit */
// UCSRC = (1 << USBS) | (3 << UCSZ0);
UCSRC = (3 << UCSZ0);// 8-bit , 1stop bit
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1 << UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
void USART_putstring(char* StringPtr) {
while (*StringPtr != 0x00) {
USART_Transmit(*StringPtr);
StringPtr++;
}
}
cout and printf() don't work unless you create a function or library for your particular hardware because there is no standard display and no operating system. Nor is there a standard keyboard interface or standard file storage so a lot of Standard C++ doesn't work.
You can write your own print() function that calls serial.Println() but you might not consider that "pure C++".
There IS Standard ANSI/ISO C++ and I'd consider that "pure" C++.
Compilers for computers can compile ANSI/ISO C++, but you have the opposite problem... Standard C++ doesn't include any graphics, color, sound, or mouse. For example, the Microsoft C++ compiler for Windows includes the complete standard library plus additional libraries. And the Windows, OS-X and Windows libraries are different and it's not easy to port a "real" application from one platform to another.
But there are some portable GUI development platforms.
Standard C++ itself is portable and it can be complied to run on any platform that has a standard complier.
Even though Standard C++ is missing a lot of things, it's still very-extensive. Several years ago I tried to find a book that covers the complete language and all I could find was the published language standard itself which is very "technical" and not that useful for learning.
I accept that. Now, how about a program written purely in "pure" C++?
As you go on to imply, not much can happen without "contamination" creeping in…
I'm still very much a C programmer if any kind of programmer at all, and I find most modern languages to be more involved with all that usually comes along for the ride.
I looked into Java or was it Javascriot or some other "language"… virtually all of C covered in like 3 chapters then 15 more chapters of stuff I've enjoyed writing for myself.
a7
???
Uh, HutKikz: I never said I don't want to use Arduino. If I had a way to dump what ever Serial.print() says to a Windows readable ASCII file that would solve my problem.
Have you heard of the XY problem : https://xyproblem.info/
You don't have to use the Arduino Serial Monitor, you can use any Serial Terminal application. Some have logging.
I'm using linux, and I don't know what is used in Windows.
Agreed.