Show Posts
Pages: [1]
1  Products / Arduino Due / Re: An example of Arduino Due as an USB host mass storage on: December 03, 2012, 03:50:26 am
Wow, this looks nice!

Can it be ported to arduino IDE easily?
2  Using Arduino / Project Guidance / Re: Migrate millis() to timer2 on 328? on: April 10, 2012, 09:18:26 am
Do you think you can share your files over here? I tried to do it but I couldn't. My micros() call would always display the max of unsigned long 4294...
3  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: April 09, 2012, 05:58:51 pm
Where should I initialize it? Timer0 was done automatically, wasn''t it? In what file does it start? Here's the complete wiring

Code:
/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  $Id$
*/

#include "wiring_private.h"

// the prescaler is set so that timer2 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER2_OVERFLOW (clockCyclesToMicroseconds(64 * 256))

// the whole number of milliseconds per timer2 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER2_OVERFLOW / 1000)

// the fractional number of milliseconds per timer2 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER2_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)

volatile unsigned long timer2_overflow_count = 0;
volatile unsigned long timer2_millis = 0;
static unsigned char timer2_fract = 0;

SIGNAL(TIMER2_OVF_vect)
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
unsigned long m = timer2_millis;
unsigned char f = timer2_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer2_fract = f;
timer2_millis = m;
timer2_overflow_count++;
}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer2_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer2_millis)
cli();
m = timer2_millis;
SREG = oldSREG;

return m;
}

unsigned long micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;

cli();
m = timer2_overflow_count;
#if defined(TCNT2)
t = TCNT2;
#elif defined(TCNT2L)
t = TCNT2L;
#else
#error TIMER 2 not defined
#endif

  
#ifdef TIFR2
if ((TIFR2 & _BV(TOV2)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV2)) && (t < 255))
m++;
#endif

SREG = oldSREG;

return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}

void delay(unsigned long ms)
{
uint16_t start = (uint16_t)micros();

while (ms > 0) {
if (((uint16_t)micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}

/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
// calling avrlib's delay_us() function with low values (e.g. 1 or
// 2 microseconds) gives delays longer than desired.
//delay_us(us);

#if F_CPU >= 16000000L
// for the 16 MHz clock on most Arduino boards

// for a one-microsecond delay, simply return.  the overhead
// of the function call yields a delay of approximately 1 1/8 us.
if (--us == 0)
return;

// the following loop takes a quarter of a microsecond (4 cycles)
// per iteration, so execute it four times for each microsecond of
// delay requested.
us <<= 2;

// account for the time taken in the preceeding commands.
us -= 2;
#else
// for the 8 MHz internal clock on the ATmega168

// for a one- or two-microsecond delay, simply return.  the overhead of
// the function calls takes more than two microseconds.  can't just
// subtract two, since us is unsigned; we'd overflow.
if (--us == 0)
return;
if (--us == 0)
return;

// the following loop takes half of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
us <<= 1;
    
// partially compensate for the time taken by the preceeding commands.
// we can't subtract any more than this or we'd overflow w/ small delays.
us--;
#endif

// busy wait
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
}

void init()
{
// this needs to be called before setup() or some functions won't
// work there
sei();

// on the ATmega168, timer 2 is also used for fast hardware pwm
// (using phase-correct PWM would mean that timer 2 overflowed half as often
// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR2A) && defined(WGM21)
sbi(TCCR2A, WGM21);
sbi(TCCR2A, WGM20);
#endif  

// set timer 2 prescale factor to 64
#if defined(__AVR_ATmega128__)
// CPU specific: different values for the ATmega128
sbi(TCCR2, CS22);
#elif defined(TCCR2) && defined(CS21) && defined(CS20)
// this combination is for the standard atmega8
sbi(TCCR2, CS21);
sbi(TCCR2, CS20);
#elif defined(TCCR2B) && defined(CS21) && defined(CS20)
// this combination is for the standard 168/328/1282/2562
sbi(TCCR2B, CS21);
sbi(TCCR2B, CS20);
#elif defined(TCCR2A) && defined(CS21) && defined(CS20)
// this combination is for the __AVR_ATmega645__ series
sbi(TCCR2A, CS21);
sbi(TCCR2A, CS20);
#else
#error Timer 2 prescale factor 64 not set correctly
#endif

// enable timer 2 overflow interrupt
#if defined(TIMSK) && defined(TOIE2)
sbi(TIMSK, TOIE2);
#elif defined(TIMSK2) && defined(TOIE2)
sbi(TIMSK2, TOIE2);
#else
#error Timer 2 overflow interrupt not set correctly
#endif

// timers 1 and 2 are used for phase-correct hardware pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 52% duty cycle

TCCR1B = 0;

// set timer 1 prescale factor to 64
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
sbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
sbi(TCCR1, CS11);
sbi(TCCR1, CS10);
#endif
// put timer 1 in 8-bit phase correct pwm mode
#if defined(TCCR1A) && defined(WGM10)
sbi(TCCR1A, WGM10);
#elif defined(TCCR1)
#warning this needs to be finished
#endif

// set timer 2 prescale factor to 64
#if defined(TCCR2) && defined(CS22)
sbi(TCCR2, CS22);
#elif defined(TCCR2B) && defined(CS22)
sbi(TCCR2B, CS22);
#else
#warning Timer 2 not finished (may not be present on this CPU)
#endif

// configure timer 2 for phase correct pwm (8-bit)
#if defined(TCCR2) && defined(WGM20)
sbi(TCCR2, WGM20);
#elif defined(TCCR2A) && defined(WGM20)
sbi(TCCR2A, WGM20);
#else
#warning Timer 2 not finished (may not be present on this CPU)
#endif

#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
sbi(TCCR3B, CS30);
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
sbi(TCCR4B, CS40);
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
sbi(TCCR5B, CS50);
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
#endif

#if defined(ADCSRA)
// set a2d prescale factor to 128
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
// XXX: this will not work properly for other clock speeds, and
// this code should use F_CPU to determine the prescale factor.
sbi(ADCSRA, ADPS2);
sbi(ADCSRA, ADPS1);
sbi(ADCSRA, ADPS0);

// enable a2d conversions
sbi(ADCSRA, ADEN);
#endif

// the bootloader connects pins 0 and 1 to the USART; disconnect them
// here so they can be used as normal digital i/o; they will be
// reconnected in Serial.begin()
#if defined(UCSRB)
UCSRB = 0;
#elif defined(UCSR0B)
UCSR0B = 0;
#endif
}


Moderator edit: quote tags converted to code tags (Nick Gammon)
4  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: April 09, 2012, 05:32:47 pm
I've got only T0 and T5 routed to pins on a MEGA. I want them to count falling edges independently, so i need my timing functions moved to the other 8bit timer.

LE: pff... The functions are still using timer 0. I tried your frequency example to see wether timer0 registers interfere with the posted version of wiring, and they did.. I think I'm better off reinventing millis/micros myself on another timer, if I want to tamper with timer 0 registers..
Actually, your first example on the website gets stucked after one loop when it reaches delay with my new wiring and timer 2 replaced with 0 on your sketch
5  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: April 09, 2012, 12:25:41 pm
I'm wondering wether these modifications in wiring.c are enough to change millis/micros/delay from timer 0 to timer 2 because I want to use the first example here http://gammon.com.au/forum/?id=11504 to count 2 frequencies on timers 0 and 5 (2 input sensors).
Code:
/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA

  $Id$
*/

#include "wiring_private.h"

// the prescaler is set so that timer2 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER2_OVERFLOW (clockCyclesToMicroseconds(64 * 256))

// the whole number of milliseconds per timer2 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER2_OVERFLOW / 1000)

// the fractional number of milliseconds per timer2 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER2_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)

volatile unsigned long timer2_overflow_count = 0;
volatile unsigned long timer2_millis = 0;
static unsigned char timer2_fract = 0;

SIGNAL(TIMER2_OVF_vect)
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
unsigned long m = timer2_millis;
unsigned char f = timer2_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer2_fract = f;
timer2_millis = m;
timer2_overflow_count++;
}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer2_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer2_millis)
cli();
m = timer2_millis;
SREG = oldSREG;

return m;
}

unsigned long micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;

cli();
m = timer2_overflow_count;
#if defined(TCNT2)
t = TCNT2;
#elif defined(TCNT2L)
t = TCNT2L;
#else
#error TIMER 2 not defined
#endif

 
#ifdef TIFR2
if ((TIFR2 & _BV(TOV2)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV2)) && (t < 255))
m++;
#endif

SREG = oldSREG;

return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}

void delay(unsigned long ms)
{
uint16_t start = (uint16_t)micros();

while (ms > 0) {
if (((uint16_t)micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}

/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
// calling avrlib's delay_us() function with low values (e.g. 1 or
// 2 microseconds) gives delays longer than desired.
//delay_us(us);

#if F_CPU >= 16000000L
// for the 16 MHz clock on most Arduino boards

// for a one-microsecond delay, simply return.  the overhead
// of the function call yields a delay of approximately 1 1/8 us.
if (--us == 0)
return;

// the following loop takes a quarter of a microsecond (4 cycles)
// per iteration, so execute it four times for each microsecond of
// delay requested.
us <<= 2;

// account for the time taken in the preceeding commands.
us -= 2;
#else
// for the 8 MHz internal clock on the ATmega168

// for a one- or two-microsecond delay, simply return.  the overhead of
// the function calls takes more than two microseconds.  can't just
// subtract two, since us is unsigned; we'd overflow.
if (--us == 0)
return;
if (--us == 0)
return;

// the following loop takes half of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
us <<= 1;
   
// partially compensate for the time taken by the preceeding commands.
// we can't subtract any more than this or we'd overflow w/ small delays.
us--;
#endif

// busy wait
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
}

void init()
{
// this needs to be called before setup() or some functions won't
// work there
sei();

// on the ATmega168, timer 2 is also used for fast hardware pwm
// (using phase-correct PWM would mean that timer 2 overflowed half as often
// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR0A) && defined(WGM01)
sbi(TCCR0A, WGM01);
sbi(TCCR0A, WGM00);
#endif 

// set timer 0 prescale factor to 64
#if defined(__AVR_ATmega128__)
// CPU specific: different values for the ATmega128
sbi(TCCR0, CS02);
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
// this combination is for the standard atmega8
sbi(TCCR0, CS01);
sbi(TCCR0, CS00);
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
// this combination is for the standard 168/328/1280/2560
sbi(TCCR0B, CS01);
sbi(TCCR0B, CS00);
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
// this combination is for the __AVR_ATmega645__ series
sbi(TCCR0A, CS01);
sbi(TCCR0A, CS00);
#else
#error Timer 0 prescale factor 64 not set correctly
#endif

// enable timer 0 overflow interrupt
#if defined(TIMSK) && defined(TOIE0)
sbi(TIMSK, TOIE0);
#elif defined(TIMSK0) && defined(TOIE0)
sbi(TIMSK0, TOIE0);
#else
#error Timer 0 overflow interrupt not set correctly
#endif

// timers 1 and 2 are used for phase-correct hardware pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle

TCCR1B = 0;

// set timer 1 prescale factor to 64
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
sbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
sbi(TCCR1, CS11);
sbi(TCCR1, CS10);
#endif
// put timer 1 in 8-bit phase correct pwm mode
#if defined(TCCR1A) && defined(WGM10)
sbi(TCCR1A, WGM10);
#elif defined(TCCR1)
#warning this needs to be finished
#endif

// set timer 2 prescale factor to 64
#if defined(TCCR2) && defined(CS22)
sbi(TCCR2, CS22);
#elif defined(TCCR2B) && defined(CS22)
sbi(TCCR2B, CS22);
#else
#warning Timer 2 not finished (may not be present on this CPU)
#endif

// configure timer 2 for phase correct pwm (8-bit)
#if defined(TCCR2) && defined(WGM20)
sbi(TCCR2, WGM20);
#elif defined(TCCR2A) && defined(WGM20)
sbi(TCCR2A, WGM20);
#else
#warning Timer 2 not finished (may not be present on this CPU)
#endif

#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
sbi(TCCR3B, CS30);
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
sbi(TCCR4B, CS40);
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
#endif

#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
sbi(TCCR5B, CS50);
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
#endif

#if defined(ADCSRA)
// set a2d prescale factor to 128
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
// XXX: this will not work properly for other clock speeds, and
// this code should use F_CPU to determine the prescale factor.
sbi(ADCSRA, ADPS2);
sbi(ADCSRA, ADPS1);
sbi(ADCSRA, ADPS0);

// enable a2d conversions
sbi(ADCSRA, ADEN);
#endif

// the bootloader connects pins 0 and 1 to the USART; disconnect them
// here so they can be used as normal digital i/o; they will be
// reconnected in Serial.begin()
#if defined(UCSRB)
UCSRB = 0;
#elif defined(UCSR0B)
UCSR0B = 0;
#endif
}
6  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: March 25, 2012, 07:26:25 am
If I take into account the actual frequencies I want to measure:

1. Engine 26-77 Hz triggered by analog voltage>value
2. 2xWheel speed max 28 Hz digital low trigger
3. Fuel 86-260 Hz digital high trigger at 50% duty

the only one that may be too fast to catch in a loop polling cycle is the last one. But I then want to save the data to an SD card using SdFat and BufferedWriter, a value for each every 30 ms (30Hz aprox) allowing me to buffer 2KB of data and to display it on a LCD through serial every second (it takes max 6 ms to transmit data at 115200 bps).

The interrupts I'd declare might interfere with the latter 2 so I would have to disable them when I get to that part. Would it actually worth it going through the trouble for sensors 2 and 3?
7  Using Arduino / Storage / Re: Faster data logging with SdFat and a RTOS on: March 24, 2012, 11:10:40 pm
I have a program that calculates RPM, speed, fuel from different sensors (2 digital 2 analog) and outputs the info through serial3 (arduino mega2560) onto an LCD screen. I'm trying to implement data acquisition too. What would be the easiest way to record some buffered CSV to a file?
Right now i'm thinking of something like

Code:
obufstream bout(buf, sizeof(buf));
bout<<i<<','<<j<<','<<k<<','<<endl;
logfile<<buf<<flush;

but this doesn't do any buffering except creating a string for all the values together. Any tips?
8  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: March 24, 2012, 10:43:48 pm
Thanks for the explanation! Because of this, I think I will use digitalReadFast in a loop and miss the beginning of the spark rather than have micros() loose an overflow.
9  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: March 24, 2012, 09:46:55 pm
The ISR would only be "c=1" unlocking a function in the loop that measures RPM and so on. I do not know how much that would take, or if it gives any performance boost instead of just polling digitalRead in the loop.
10  Using Arduino / Programming Questions / Re: Timer0 - millis/micros pause during ISR on: March 24, 2012, 09:39:29 pm
So if I call micros outside the ISR it should still increment, even if the arduino had sit in the ISR for let's say 20 micros?
11  Using Arduino / Programming Questions / Timer0 - millis/micros pause during ISR on: March 24, 2012, 09:18:19 pm
Does Timer0 pause (not counting micros/millis anymore) during an ISR defined on other timers?

Thanks!
12  Using Arduino / Installation & Troubleshooting / Re: Arduino Mega2560 can't upload sketch on: March 23, 2012, 06:04:59 pm
what do you mean you spiked them?
Pages: [1]