|
|||||||
![]() |
Forum Index > General > Ask an Evil Mad Scientist! | ||
Requesting help porting C code into Arduino |
|||
| | | Printable Version |
|
Anonymous: Jeff Haas | ||||||||
|
I got some help from a friend on a microcontroller project, he did a lot of work, but just started a new job and doesn't have time to do the final step. He coded in C on an Atmega chip (with no Arduino bootloader, because it was easier for him) and I have the code, but there are some differences between what he did and what works in the Arduino environment. |
![]()
|
||||||||
|
|||||||||
|
Windell | ||||||||
|
Why do you need to do any conversion? The Arduino is essentially an Atmega development board with a brand name on it-- if it works on an ATmega328, it will still run on the Arduino, whether or not you do any conversion. Windell H. Oskay drwho(at)evilmadscientist.com http://www.evilmadscientist.com/ |
![]() Evil Scientist ![]() Status: offline
Registered: 06/15/06 |
||||||||
|
|||||||||
|
Anonymous: linxdev | ||||||||
Quote by: WindellWhy do you need to do any conversion? The Arduino is essentially an Atmega development board with a brand name on it-- if it works on an ATmega328, it will still run on the Arduino, whether or not you do any conversion.
|
|
||||||||
|
|||||||||
|
Anonymous: Jeff Haas | ||||||||
|
Linxdev is right - the code doesn't work in the IDE, and I was hoping to be able to tweak it and add a few features, use the IDE to download it, etc. I have a couple of regular Arduinos and just built a Diavolino, and was hoping not to have to get another piece of hardware for this project. PHP Formatted Code /** ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch * ---------------------------------------------------------------------------- * * Simple AVR demonstration. Controls a LED that can be directly * connected from OC1/OC1A to GND. The brightness of the LED is * controlled with the PWM. After each period of the PWM, the PWM * value is either incremented or decremented, that's all. * * $Id: group__demo__project.html,v 1.1.1.17 2008/11/06 22:24:49 joerg_wunsch Exp $ */ #include <inttypes.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <stdlib.h> #include "iocompat2.h" #include "pin_macros.h" #define INIT_TIMER_COMP 0xffff #define LOWBYTE(val) ((uint8_t)((val) & 0xff)) #define HIGHBYTE(val) ((uint8_t)(((val) & 0xff00) >> 8)) #define B_DATA_PORT_MASK (_BV(DDB0) | _BV(DDB1)) /* b1:0 output */ #define D_DATA_PORT_MASK (_BV(DDD2) | _BV(DDD3) | _BV(DDD4) | _BV(DDD5) | _BV(DDD6) | _BV(DDD7)) /* b7:2 output */ int foo(void) { return 2; } enum { UP, DOWN }; void toggle_b2(void) { PORTB ^= _BV(PORTB2); } void led_out (uint8_t val) { unsigned char bval; unsigned char dval; val = ~val; bval = PORTB & ~(B_DATA_PORT_MASK); dval = PORTD & ~(D_DATA_PORT_MASK); bval |= (val & B_DATA_PORT_MASK); dval |= (val & D_DATA_PORT_MASK); PORTB = bval; PORTD = dval; } void stop_timer1 (void) { TCCR1B = TCCR1B & ~(_BV(CS10) | _BV(CS11) | _BV(CS12)); } void start_timer1 (void) { TCCR1B |= _BV(WGM12); /* Put Timer 1 into CTC mode (Clear time on Compare match) */ TCCR1B |= _BV(CS11); /* clock prescale divide by 8 */ } ISR (TIMER1_OVF_vect) { led_out(0x1); } void set_compare_timer1 (uint16_t val) { OCR1AH = HIGHBYTE(val); OCR1AL = LOWBYTE(val); } #define INITSHIFT 3 int sit = (50<<INITSHIFT); int shift = INITSHIFT; ISR (TIMER1_COMPA_vect) { // static char x = 0; // static char y = 0xff; static unsigned int delay = 0; cli(); stop_timer1(); toggle_b2(); delay = (rand() & 0xffff); if (delay < 0x40) { delay = 0x40; } if (sit-- <= 0) { shift--; if (shift < 0) { shift = (INITSHIFT); } sit = (50<<INITSHIFT); } set_compare_timer1(delay>>shift); start_timer1(); led_out(shift); #if defined (NOTUSED) if (!x) { led_out(0x2); x=1; } else { y = ~y; led_out(y); } #endif sei(); } void ioinit (void) { DDRB = B_DATA_PORT_MASK | _BV(DDB2); /* b2 toggle val */ DDRD = D_DATA_PORT_MASK; PORTB = 0; PORTD = 0; TCCR1A = 0; TCCR1B = 0; /* set compare register OCR1A to max until first intr */ // set_compare_timer1(INIT_TIMER_COMP); set_compare_timer1(0x4000); TCCR1A = 0; /* Start clock */ // TCCR1B |= _BV(CS10) | _BV(CS12); /* divide by 1024 clock prescaler */ TCCR1B |= _BV(WGM12); /* Put Timer 1 into CTC mode (Clear time on Compare match) */ TCCR1B |= _BV(CS11); /* clock prescale divide by 8 */ } #define JVAL 100000; int main (void) { // int i; // long j = JVAL; ioinit (); srandom(0x12345678L); set_sleep_mode(SLEEP_MODE_IDLE); TIMSK1 = _BV (OCIE1A); cli(); sei (); // for (;;) { // if (j-- <= 0) { // j = JVAL; // i = rand(); // PORTB = (unsigned char) (i & DDRB); // PORTD = (unsigned char) (i & DDRD); // } // } sleep_enable(); sei(); /* loop forever, the interrupts are doing the rest */ for (;;) { sleep_cpu(); } return (0); } |
|
||||||||
|
|||||||||
|
Anonymous: linxdev | ||||||||
|
PHP Formatted Code tmp/h.cpp:36:23: error: iocompat2.h: No such file or directory tmp/h.cpp:37:24: error: pin_macros.h: No such file or directory
|
|
||||||||
|
|||||||||
|
Anonymous: Jeff Haas | ||||||||
|
Thank you! That is much more than I expected. |
|
||||||||
|
|||||||||
|
Windell | ||||||||
|
You're not alone. I write most of my "Arduino" code by writing AVR-GCC code, and then copy-pasting everything before the main loop into setup, and the main loop into loop.
I have a couple of regular Arduinos and just built a Diavolino, and was hoping not to have to get another piece of hardware for this project.
You *do not* have to get more hardware to use the ISP programming method-- you can actually use one Arduino as an ISP programmer. See here, and/or pick up our ISP shield if you only want to get *a little bit* of new hardware. Windell H. Oskay drwho(at)evilmadscientist.com http://www.evilmadscientist.com/ |
![]() Evil Scientist ![]() Status: offline
Registered: 06/15/06 |
||||||||
|
|||||||||
|
Windell | ||||||||
|
So... I copied and pasted your code into the Arduino IDE. It didn't compile because of these two lines: PHP Formatted Code #include "iocompat2.h"#include "pin_macros.h"
Windell H. Oskay drwho(at)evilmadscientist.com http://www.evilmadscientist.com/ |
![]() Evil Scientist ![]() Status: offline
Registered: 06/15/06 |
||||||||
|
|||||||||
|
Anonymous: linxdev | ||||||||
|
PHP Formatted Code void loop() { _loop(); } void setup() { }
|
|
||||||||
|
|||||||||
|
Windell | ||||||||
|
>The IDE must be smart enough to see a main(). Windell H. Oskay drwho(at)evilmadscientist.com http://www.evilmadscientist.com/ |
![]() Evil Scientist ![]() Status: offline
Registered: 06/15/06 |
||||||||
|
|||||||||
|
Anonymous: linxdev | ||||||||
![]() I looked around on the web and all I could find was .wav files. This project generates a live clicking sound, which varies in rate, and also uses an LM386 to drive an 8 ohm speaker. I've bought a Diavolino for the final build, but the port from C is a bit beyond me at this point.
|
|
||||||||
|
|||||||||
|
Anonymous: Jeff Haas | ||||||||
|
Wow, I finally get time to get back to this and there's a ton of info. Thanks! |
|
||||||||
|
|||||||||
|
Windell | ||||||||
|
I've tried twice, on two different computers, and it compiles just fine. Windell H. Oskay drwho(at)evilmadscientist.com http://www.evilmadscientist.com/ |
![]() Evil Scientist ![]() Status: offline
Registered: 06/15/06 |
||||||||
|
|||||||||
|
Anonymous: Jeff Haas | ||||||||
|
D'oh! I'd made a typo in how I commented out the two lines. |
|
||||||||
|
|||||||||
|
Anonymous: Jeff Haas | ||||||||
|
OK, it's uploaded but I can't quite figure out which pins he's using for output. |
|
||||||||
|
|||||||||
|
|
| All times are PDT. The time is now 04:56 PM. |
|
|
Octolively
Interactive LED kits
Meggy Jr RGB
LED matrix game
development kit.
Business-card sized
AVR target boards
Peggy 2
LED Pegboard kits