We are wanting to test programming an Arduino with C Language and avoiding the Arduino IDE. Used the example from:
The only change made to the program was to change the duty cycle and adjust the period to about one second.
// main.c in led project // #define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> #define BLINK_DELAY_MS 1000 #define ON_DELAY_MS 100 #define OFF_DELAY_MS BLINK_DELAY_MS - ON_DELAY_MS int main (void) { /* set pin 5 of PORTB for output*/ DDRB |= _BV(DDB5); while(1) { /* set pin 5 high to turn led on */ PORTB |= _BV(PORTB5); _delay_ms(ON_DELAY_MS); /* set pin 5 low to turn led off */ PORTB &= ~_BV(PORTB5); _delay_ms(OFF_DELAY_MS); } }
The project was created in the Code::Blocks IDE as an avr project.
Building the project in Code:Blocks was straight forward (although it is not currently installed in the normal path on my computer). I used the Arduino IDE to discover the correct command line instruction to transfer the hex file in the current environment and used copy and paste to manually transfer my cold::blocks generated hex file.
At the moment I am using a SB-Freeduino rather than the Arduino Uno. Turns out the price on the Uno from digikey is better than the Freeduino from Solarbotics so in the future I’ll be getting the Uno.
(tlxde)danp@localhost:~/project/led/bin/Release$ /home/danp/Downloads/arduino-1.8.1/hardware/tools/avr/bin/avrdude -C/home/danp/Downloads/arduino-1.8.1/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:led.elf.hex:i avrdude: Version 6.3, compiled on Dec 16 2016 at 12:32:01 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2014 Joerg Wunsch System wide configuration file is "/home/danp/Downloads/arduino-1.8.1/hardware/tools/avr/etc/avrdude.conf" User configuration file is "/home/danp/.avrduderc" User configuration file does not exist or is not a regular file, skipping Using Port : /dev/ttyUSB0 Using Programmer : arduino Overriding Baud Rate : 57600 AVR Part : ATmega328P Chip Erase delay : 9000 us PAGEL : PD7 BS2 : PC2 RESET disposition : dedicated RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00 Programmer Type : Arduino Description : Arduino Hardware Version: 2 Firmware Version: 1.16 Vtarget : 0.0 V Varef : 0.0 V Oscillator : Off SCK period : 0.1 us avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e950f (probably m328p) avrdude: safemode: hfuse reads as 0 avrdude: safemode: efuse reads as 0 avrdude: reading input file "led.elf.hex" avrdude: writing flash (176 bytes): Writing | ################################################## | 100% 0.07s avrdude: 176 bytes of flash written avrdude: verifying flash memory against led.elf.hex: avrdude: load data flash data from input file led.elf.hex: avrdude: input file led.elf.hex contains 176 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.05s avrdude: verifying ... avrdude: 176 bytes of flash verified avrdude: safemode: hfuse reads as 0 avrdude: safemode: efuse reads as 0 avrdude: safemode: Fuses OK (E:00, H:00, L:00) avrdude done. Thank you. (tlxde)danp@localhost:~/project/led/bin/Release$
A lot of information is available in the arv-libc source. See:
I have looked at different example including on in the source file stdio.h. The following was tested and worked:
// main.c #include <stdio.h> #include "uart.h" int main(void) { init_uart(); stdout = &uart_output; printf("Hello, world!\n"); while(1); return 0; }
// uart.c #include <avr/io.h> #include <stdio.h> #include "uart.h" #ifndef BAUD #define BAUD 9600 #endif #include <util/setbaud.h> /* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */ FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ); void init_uart(void) { UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ } int uart_putchar(char c, FILE *stream) { if (c == '\n') { uart_putchar('\r', stream); } loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; return 0; } int uart_getchar(FILE *stream) { loop_until_bit_is_set(UCSR0A, RXC0); return UDR0; }
// uart.h int uart_putchar(char c, FILE *stream); int uart_getchar(FILE *stream); void init_uart(void); /* http://www.ermicro.com/blog/?p=325 */ extern FILE uart_output; extern FILE uart_input;