Show Posts
Pages: 1 [2] 3 4 5
16  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 08:27:08 pm
Thanks for the thorough description Bill.

If the variables in question have been confirmed to be ISR access only by the Timer0 overflow ISR and we have disabled the interrupt, is it necessary to provide explicit atomic access protection or is 'standard' access sufficient from the function in question?

Best Regards,
17  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 06:52:04 pm
I still don't understand why you find it desirable to reset the millis() value. Understanding how it works is one thing. The source code is there, so have at it. Are you under the impression that when the millis() value rolls over that there is some chance of a problem occurring?

Thanks for the reply Jack.  I've made some mistakes here, and learned much from the responses to this post.  You all have made me further consider atomic access (found, variable scope and volatility, as well as other pitfalls.

No I am not worried about roll over, but rather wish to understand the underpinnings to help with better design.

Thanks to all for the responses.

18  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 04:42:02 pm
Thank you all for the comments, and the catch on volatile.

I was mistaken in thinking if it was declared volatile in the originating unit, I only need to declare as extern within the sketch.  Found a thread on the subject here I have modified the code in Post #1 to add the missing volatile statements.

If I accept the risks, is there a reason the Post 1 reset function will not work (with the added volatile for the two variables)?
19  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 10:48:35 am

Maybe my stopwatch class is usefull - -

 ... tempus fugit ...

Thank you for the suggestion.  I will study the library for application.


20  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 10:32:21 am
And, if the clock ticks while you are mucking around with these non-volatile, non-atomic values? Your timer is now hosed.

You still haven't explained WHY.

Thank You.

On why - among other reasons I wish to gain a better understanding on the Arduino core structure and functional interdependencies and share findings.

The extern variables cited are volatile as defined in wiring.c.  TCNT0 is an 8 bit register with single instruction access, the function Timer0_reset() in Post 1 disables Timer0 interrupts and stops the timer while zeroing the state variables and registers.  A search of the core files for TCNT0 and the two variables reveals that access for these is restricted to the wiring.c source file.

Can you please elaborate further on what specific elements of the Timer are hosed by the function?

Best Regards,
21  Using Arduino / Programming Questions / Re: Reset millis() clock on: April 29, 2012, 09:33:37 am
Note the following two key lines that bring access to the two Timer0 state variables in the core file wiring.c

extern unsigned long timer0_overflow_count;
extern unsigned long timer0_millis;

these plus TCNT0 are the counters that all need to be reset.

There is a last state variable timer0_fract does not appear to need to be reset.

22  Using Arduino / Programming Questions / Reset millis() clock on: April 29, 2012, 09:24:03 am
In searching, it appears that this request comes up periodically and is in general discouraged due to potential side effects.,44844.0.html  -> This thread gives hints and warnings but no final solution.

For my applciation I desire to reset the millis() clock.  After studying wiring.c and the data sheet, I have come up with the following Timer0_reset function that appears to work well in the attached demo sketch, run on an Uno board under Arduino 1.0.  

// Millis demo with Timer0 reset

#include <avr/io.h>

unsigned long currentTime = 0; // this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime = 0; // no time has passed yet
byte currentState = 0; // the default state
unsigned int wait = 1000; // set delay value in milliseconds

extern volatile unsigned long timer0_overflow_count;
extern volatile unsigned long timer0_millis;

void setup() {
pinMode(13, OUTPUT); //the LED we're going to blink
pastTime = millis();    

void loop() {

unsigned long timePassed;
// currentTime is now the current time (again)
currentTime = millis();
// this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over
// to a small positive value, the time that has passed
timePassed = currentTime - pastTime;

Serial.println (currentTime, DEC);

if(timePassed >= wait) { // part1 of the state engine
switch(currentState) { // part2 of the state engine, its this simple, an if and a switch

case 0: // the default state.. turn the led on!
digitalWrite(13, HIGH); // light the LED up!
pastTime = millis(); // store a new pastTime
currentState = 1; // the next state that should be executed is 1
break; // break out of the switch, else it'll execute the case below this one too

case 1:
digitalWrite(13, LOW);   // turn the LED off again
pastTime = millis(); // store a new pastTime
currentState = 0; // turning the LED on again is done in state 0

delay (50); // delay a little to make serial monitor more readable

if (currentTime > 10000) {
Timer0_reset(); // reset Timer0 every 10000 milliseconds
Serial.println("millis timer reset to zero...");

void Timer0_reset () {

   // NOTE: to set a bit, a bit mask is OR'ed with the variable
// to clear a bit, the mask is inverted and then AND'ed with the variable

TIMSK0 &= ~(1<<TOIE0); // clear Timer0 overflow interrupt enable bit
TCCR0B &= ~((1<<CS02) | (1<<CS01)| (1<<CS00)); // stop Timer0 by clearing CS02, CS01, CS00
TIFR0 = 0; // clear any pending Timer0 interrupts

TCNT0 = 0; // zero out Timer0 TCNT 8 bit register
timer0_overflow_count = 0; // zero out millis() overflow counter (extern from wiring.c)
timer0_millis = 0; // zero out millis() counter (extern from wiring.c)

TIMSK0 |= (1<<TOIE0); // set Timer0 overflow interrupt enable bit

// restart Timer0 by setting prescale factor back to 64 (this is same code as found in init()
// function from wiring.c, except sbi call has been replaced with &=/|= equivilent)
// this combination is for the standard 168/328/1280/2560
// sbi(TCCR0B, CS01);
// sbi(TCCR0B, CS00);
TCCR0B |= ((1<<CS01) | (1<<CS00));


Does anyone see any potential mistakes or pitfalls I may have overlooked?

Edit: Fixed missing volatile statements, variable size per Reply #8 and #11 below

23  Using Arduino / Microcontrollers / Re: Arduino Tiny on: March 25, 2012, 08:05:53 pm
There are instructions in the forum for upgrading to the latest version of WinAVR.  If you can't find them, let me know.

Thanks once again for the help.  I updated my Arduino WinAVR.  I had to copy over avrdude.exe 5.11 and avrdude.conf from Arduino 1.0 folder to get my Pololu AVR USB Programmer to work but everything is now functional!

24  Using Arduino / Microcontrollers / Re: Arduino Tiny on: March 25, 2012, 05:44:54 am

I am trying to compile a small test sketch for Attiny4313 using latest core and Arduino 1.0 IDE.  The sketch below compiles for Attiny2313, compiling under Attiny4313 throws an error:

unknown MCU 'attiny4313' specified
Known MCU names:

Is attiny4313 recognized in the (older) version of WinAVR included with ARDUINO 1.0, or am I doing something wrong?


/* Attiny4313 example (G.Small, 24Mar2012)
Example of Attiny4313 on Arduino Tiny core, with serial comms test

                MISO  1 |o o| 2  +Vcc
(SCL, to UNO A5) SCK  3 |o o| 4  MOSI (SDA, to UNO A4)
              /RESET  5 |o o| 6  GND


           (D 17) PA2  1|    |29  VCC
      RX   (D  0) PD0  2|    |19  PB7 (D  16)
      TX   (D  1) PD1  3|    |18  PB6 (D  15)
           (D  2) PA1  4|    |17  PB5 (D  14)
           (D  3) PA0  5|    |16  PB4 (D  13)*
      INT0 (D  4) PD2  6|    |15  PB3 (D  12)*
      INT1 (D  5) PD3  7|    |14  PB2 (D  11)*
           (D  6) PD4  8|    |13  PB1 (D  10)
          *(D  7) PD5  9|    |12  PB0 (D  9)
                  GND 10|    |11  PD6 (D  8)

 * indicates PWM port

 These arrays map port names (e.g. port B) to the appropriate addresses
 for various functions (e.g. reading and writing)


#define LED_PIN           6                     // ATtiny Pin 8 (D6)

void setup(){
  pinMode(LED_PIN,OUTPUT);                      // for general DEBUG use
  LED_Blink(LED_PIN,2);                         // show it's alive

void loop() {
  int test = 31;
  LED_Blink(LED_PIN,1);                         // show it's alive

// LED blinker for status
void LED_Blink(byte pin, byte times){
  for (byte i=0; i< times; i++){
    delay (1000);
    delay (1000);
25  Using Arduino / Microcontrollers / Re: Arduino Tiny on: December 03, 2011, 07:52:28 am

Thanks again to Coding Badly for work on Tiny.

I have an application I am working on making use of 16 bit Timer1 with interrupts.

If I do not plan to use mills/delay in my app, is it possible for me to disable Timer0 within core, or would the be major surgery?

26  Using Arduino / Microcontrollers / Re: Arduino Tiny on: November 05, 2011, 07:22:17 am
In Reply #28 and #29, I reported success with BHogan TinyWireS I2C library on an ATtiny85.  I can now report that, following similar steps I have an ATtiny84 I2C slave talking to an Uno.  Code is the same for master and slave.  TinyWireS did have to get tweaked to add ATtiny84 # defines in usiTwiSlave.c.  Using Arduino Tiny core and Pololu AVR USB Programmer.

Interesting, in this case it is working fine without any external pull-ups.

27  Using Arduino / Microcontrollers / Re: Uploading sketches to Attiny85 in various ways? on: October 13, 2011, 04:18:31 am
i've used USBAVR Programmer for programming ATTiny85 in Arduino and gcc projects, works great and how-to on Windows is documented in thread you linked above.  Can't go wrong for $19.95.
28  Using Arduino / Microcontrollers / Re: Assembly language in Arduino. on: September 18, 2011, 04:43:11 am
If you need more than a few lines of asm, you could contemplate moving from Arduino IDE to native avr-libc.
29  Using Arduino / Microcontrollers / Re: Is the Arduino in trouble on: August 29, 2011, 05:17:34 am
Am I the only one that thinks "OVERKILL" to use a 32 bit processor for some (actually most) of the stuff we see done with Arduino?

There are applications for which 32 bit is overkill, and there are also applications that limited by 8 bit.

With 32 bit embedded chips dropping to ever low price points (ARM, PIC32...) Arduino needs a path to 32 bits without breaking the existing 8 bit support.  8 bit is great for getting started with Blink and is more than enough for many applications.  As Arduino makes embedded processing more accessible thanks to Arduino philosophy, new previously unimagined apps will show up.  Some will be limited by 8 bit capabilities.  The past can be used to predict the future here.
Should be interesting to watch, the market will progress.
30  Development / Other Software Development / Re: 1-Wire Slave on: July 26, 2011, 04:52:39 am
Bad news.  Overdrive will be later.  The response to a master-read has to be within 1.0 microseconds.  That's about 16 machine instructions (a small overrun would probably be OK).  I suspect it will work but I want to pin down the other "high risk" implementation details before tackling the complexity of an assembly language interrupt service routine.

OK thanks.

This got me wondering how Arduino OneWire lib based master could be prepared for a 1 microsecond slave response to master-read without any hardware assist.  It appears that OneWire Master lib OneWire::read_bit() code has a 3 microsecond delay for driving pin low, followed by a 9 microsecond delay at float before reading pin value.  So maybe Arduino OneWire Master lib allows more response time than the minimum listed in Dallas spec, and 1 uS is taking excess margin?

Arduino OneWire Master Lib Source -->

Anyway, thanks for on-going consideration.

Pages: 1 [2] 3 4 5