Can I Program my Arduino in C?

Hi The title says it all really

Can I program my Arduino in C and how do I go about it?

cheers

Is there any other way?

dicky96: Hi The title says it all really

Can I program my Arduino in C and how do I go about it?

cheers

Certainly. The arduino programming language is C/C++, you just have to learn about it's supplied functions and libraries and few things it does for you automatically like adding the main() function and not requiring function prototyping.

Lefty

Probably I was not specific enough

OK lots more info:

I am programming a video overlay controller that pops stuff up on a HD screen at various locations (bit like the OSD on your TV)

This is controlled by a PC

The Arduino is meant to recieve 'commands' from the PC on the serial (usb) bus

I then send these 'commands' to the overlay chip on the i2c bus in a form it can understand. By Commands I mean 'display this text box in this size and this colour at these co-ordiantes' sort of commands

The actual graphic data is sent to the overlay chip on the SPI bus (this can be done once at program boot, or dynamically if there is too much data for the overlay chip to store in one go) This data basically defines the actual text strings, icons and boxes

I have an PC application that allows me to design the overlay elements (and select which ones are displayed at various times) this is a WYSIWYG application and it generates for me 'state machine' to control the overlay display, in the form of c source and header files which are intended to be ported to a host microcontroller (I was hoping to use the Arduino). The 'state machine' reacts to the commands coming from the PC and tells the overlay to perform the required action. It says that the C source is designed to be 'easily portable' to a microcontroller

The WYSIWYG app also generates for me a flash memory file which is the graphics / text / icon data I need to send on the SPI bus to the internal memory on the overlay chip. I think it will all fit OK.

I've already sorted out the i2c communication withthe overlay board and tested it

What I need to figure out now is how to get these C files into the Atmel 328? Can I do it via the IDE or do I need an AVR C compiler?

Also it says I need to complete a couple of skeleton functions (it calls these the 'hardware abstraction layer') that does the actual i2c and spi initialisation and communications. The Arduino can already do that but I need to know how to integrate it all

I may also need some external flash memory as I don't know how big an executable the C isgoing to compile to, but I haven't got that far yet

AS you can probably figure from above I am fairly familiar with electronics and programming (more php and assembler than C but I can find my way around a C program) but I have not worked with microcontrollers before

Advice welcome

Rich

Can I do it via the IDE

Yes. Make your sketch, a copy of the blink will do. In the folder that has the sketches name you put your .c and .h files and restart the IDE. When you open the sketch again each file will be in the IDE in a separate tab.

I may also need some external flash memory as I don't know how big an executable the C isgoing to compile to, but I haven't got that far yet

The AVR chips the arduino boards use can't execute code from external flash memory. However some boards have rather large built in flash space, 256KB for the mega2560 board. SRAM memory used for variables, arrays, stack space, heap memory is usually the limiting factor for arduino sketches. Just 2KB on a Uno board.

Lefty

Grumpy_Mike:

Can I do it via the IDE

Yes. Make your sketch, a copy of the blink will do. In the folder that has the sketches name you put your .c and .h files and restart the IDE. When you open the sketch again each file will be in the IDE in a separate tab.

Though note, that the things the IDE does behind your back (including Arduinio.h, creating function prototypes for each of the functions in your .ino/.pde file so that you don't have to do it) are not done for .c/.cc/.h files.

If you need lots of SRAM, go to a 1284P board - 128K Flash, 4x with an Uno has, and 16K SRAM, twice what a Mega has. Or add more SRAM to a Mega if you need its 256K flash: http://www.ruggedcircuits.com/html/products.html

1284P board: http://www.crossroadsfencing.com/BobuinoRev17/ On board or offboard USB/Serial support Bare boards $6 mailed in US (am considering offering as a kit also, had a couple of requests so far) Update your IDE with maniacbug's files: https://github.com/maniacbug/mighty-1284p

hey guys thanks for all the advice

I will try to compile the code and see what happens next, then no doubt come running for advice lol :roll_eyes:

I also need to speak to the tech support where they make this overlay board as there are a couple of thngs I am very unclear about

BTW is this the best section on the forum for this thread? Don't want to break any protocols here

Rich

BTW is this the best section on the forum for this thread?

Yes I would say it is fine.

Don't want to break any protocols here

Have you read the how to use this forum sticky post at the top?

Hi guys

OK I got a little further with speccing this project

I will need to work with a data construct of 13448 bytes (this is all the data needed to hold the info for the screen overlay) which I need to upload to the overlay chip on boot. Let's call it the 'overlay construct'. This is sent to the overlay chip on the SPI bus

I also need to manage a small bitmap to the overlay chip control registers to select which graphical objects are displayed/hidden - this is 64 bytes, Let's call it the 'control construct'. This is sent to the overlay chip on the i2c bus

Of this overlay construct, much of it is constants (font bitmaps etc)

The maximum amount of dynamic data within the overlay from the point of view of my application, is 2240 bytes (if I wanted to handle 64 text boxes and 128 icons which is maximum capability of the overlay chip) however for my application the amount of dynamic data is likely to be 256 bytes or less (186 bytes on my best estimate) as I will only be handling a few objects

The overlay chip can either act as an SPI master and request data from my AVR, or act as a slave and I can push whatever data I wish into the overlay

I think I would be better using the latter option?

My software would be basically:

start: initialise serial (usb) SPI and i2c load initial data to overlay

loop: read a command from the PC on usb (triggered by an interrupt??) update overlay construct (if needed) update control construct send the updated overlay construct to the overlay chip (SPI) send the updated control construct to the overlay chip (i2c) go to sleep

I could be doing this at roughly frame rate - 50Hz for dynamic text or moving objects, though a slower rate would be quite acceptable

From the above advice do I use Mega1284? It would leave a few Kb of ram free for my application, as well as the 13Kb data structure

cheers Rich

Given that the mega has:-

Flash Memory 128 KB of which 4 KB used by bootloader SRAM 8 KB EEPROM 4 KB Clock Speed 16 MHz

Then you will have to keep your data construct of 13448 bytes in flash memory. This means it is fixed. It could be changed during transfer but that is it. Will that suffice?

As I see things, the dynamic part of the data is a max of about 2k and probably for my application about 256 bytes so yeah that would work if I load all the static data (About 11k) into flash memory to send to the overlay chip on startup and just deal with the dynamic stuff (about 2k) in RAM afterwards

That's feasible on these micros yes?

dicky96: As I see things, the dynamic part of the data is a max of about 2k and probably for my application about 256 bytes so yeah that would work if I load all the static data (About 11k) into flash memory to send to the overlay chip on startup and just deal with the dynamic stuff (about 2k) in RAM afterwards

That's feasible on these micros yes?

On AVR systems (i.e. all Arduinos except the Due), you need to explicitly copy stuff from the read-only memory to your limited ram area before you can use it. See here for more details: http://www.arduino.cc/en/Reference/PROGMEM.

I believe on Arm processors (Due, Teensy 3.0, etc.) you can just declare it const, and as long you don't try to modify it, it can be used in normal addressing.

you need to explicitly copy stuff from the read-only memory to your limited ram area before you can use it.

No you don't. You can simply move the data one byte at a time from flash memory to your device.

If the setup is as you describe it then a mega should work for you.