Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 29, 2013, 10:39:41 pm
Hi Nick,

You code works for my example, but it doesn't actually solve the problem in my real world code.

Your start() function, does the same as the resume() function in the latest version of the library, ie it just sets the config reg so that the counter carries on, but it doesn't set its value back to zero.

Actually, I'm not too keen on the way that Timer1.cpp tries to set TCNT1 to zero and disables interrups while its waiting for TCNT1 to return to zero; according to the code, this is to prevent a phantom interrupt.
However disabling all the interrupts could mean that another interrupt e.g. one of the built in timers e.g. for Nano's would miss an interrupt, or an external interrupt could be missed.

I think a better way to do this is to would be either to have a flag that is set, when resetting TCNT1 which the ISR checks for, and the ISR just returns if the flag is set.
Alternatively, I'm not sure how to do this, but... Change the ISR vector to point to a dummy function when setting TCNT1 and set it back to the normal ISR when TCNT has returned to zero and "phantom" interrupt has occurred.

Anyway, for the moment by hacky code works, so I need to get on with other parts of the project, and I'll revisit this later if I get time.

Thanks again

Roger
2  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 29, 2013, 06:02:54 pm
Thanks Nick,

I'll give your version of the library a go, but as you say, using the library looks like its more problematic than just programming the timer directly in the code.
But normally I tend to use pre-written libraries as they are generally well written and save a lot of effort.

Cheers

Roger
3  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 29, 2013, 04:23:06 am
Hi Nick,

Your code looks to be a bit different from the truck on GoogleCode, but whether it would fix the bug / issue I have, I'm not entirely sure.

Can you post all of your code for timer1.cpp and I'll give it a try

Thanks

Roger
4  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 29, 2013, 12:43:53 am
Hi Nick,

The code on GoogleCode doesn't seem to look the same as you posted.

i.e http://code.google.com/p/arduino-timerone/source/browse/trunk/TimerOne.cpp

(code for the latest release - r11) Oct 2012.

Code:
void TimerOne::resume()                         // AR suggested
{
  TCCR1B |= clockSelectBits;
}

void TimerOne::restart()                // Depricated - Public interface to start at zero - Lex 10/9/2011
{
        start();                               
}


void TimerOne::start()  // AR addition, renamed by Lex to reflect it's actual role
{
  unsigned int tcnt1;
 
  TIMSK1 &= ~_BV(TOIE1);        // AR added
  GTCCR |= _BV(PSRSYNC);                // AR added - reset prescaler (NB: shared with all 16 bit timers);

  oldSREG = SREG;                               // AR - save status register
  cli();                                                // AR - Disable interrupts
  TCNT1 = 0;                   
  SREG = oldSREG;                       // AR - Restore status register
        resume();
  do {  // Nothing -- wait until timer moved on from zero - otherwise get a phantom interrupt
        oldSREG = SREG;
        cli();
        tcnt1 = TCNT1;
        SREG = oldSREG;
  } while (tcnt1==0);
 
//  TIFR1 = 0xff;                       // AR - Clear interrupt flags
//  TIMSK1 = _BV(TOIE1);              // sets the timer overflow interrupt enable bit
}

It appears that start() is attempting to set the counter to zero

  TCNT1 = 0; 

and then tries to wait for the value of the volative TCNT1 to goto zero to prevent a phantom interrupt occurring.

I put some print statements in my code, and my setup code gets past start(), so Timer1::start() is not locking up waiting to TCNT1 to be zero, but for some reason when it completes, the timer doesn't run (well it doesnt call the ISR)

Looking at the code, start() appears to call cli();     but not sei(); so I initially thought that the issue was that interrupts were being left in a disabled state, but I tried adding a call to sei(); at the end of start() and it didnt appear to fix things - however I may have changed other code at the same time, as by then it was quite late, and I may have made another mistake in my code.

I guess I should go back to the beginning with a very simple test program and try adding a sei() at the end of start() just to confirm that doing so definitely doesn't fix the problem
5  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 28, 2013, 04:49:30 pm
The whole project is a remote control ac mains dimmer controller based on various designs on the web.

The Arduino get a zero crossing pulse every 10ms from a opto isolator connected to the mains via a signal conditioning circuit.

The zero crossing detector is connected to D2 and hence uses Interrupt 0 to call an ISR
in the zero crossing ISR, the timer needs to be started, as the triac that controls the AC load need to be triggered at a pre-defined time period after the zero crossing.
The time period is basically between zero and 10mS.

In the timer ISR, the output that drives the triac( via an opto + signal conditioning),  is set to HIGH.
(Note this output is set to LOW in the zero crossing ISR).

The reason I need to init the timer before its used and then stop it, is because if I init the timer in the zero crossing ISR it takes quite a lot of time (at least if I used Timer1's init code), and this effects when the Timer ISR gets triggered.

So the optimal way to do this is to pre initialise the Timer and get it to wait, (stop) and then start it in the zero crossing ISR.

And in the Timer ISR, the timer needs to be stopped, as it needed to be start again from time zero in the zero crossing ISR, otherwise the timer will not be synchronised with the zero crossing's which is the whole point of the exercise.

Currently my work around, using Timer1, is to call stop() and then to set the timer counter to zero, but this causes a faux interrupt to the timer ISR, so I have to have a volative global which I set in the code that sets the counter to zero, so that I know that the interrupt is likely to be a faux one.
However this a hack, and I'd rather to it correctly.

It seems to be that the Timer1 library doesn't really work that well for this sort of thing, and I may as well write the code myself.

 
6  Using Arduino / Programming Questions / Re: Timer1 library start() doesn't seem to work on: November 28, 2013, 04:31:20 am
Agreed the state thing is a bug, but not the same bug.


i.e Here is the original demo code from the Timer1 library, called ISRBlink, but I've added the 2 lines that stop() and then start() the timer

This still doesn't work :-(


Code:
#include <TimerOne.h>
 
void setup()
{
  // Initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards
  pinMode(13, OUTPUT);   
 
  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  Timer1.stop();
  Timer1.start();
}
 
void loop()
{
  // Main code loop
  // TODO: Put your regular (non-ISR) logic here
}
 
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
    // Toggle LED
    digitalWrite( 13, digitalRead( 13 ) ^ 1 );
}
7  Using Arduino / Programming Questions / Timer1 library start() doesn't seem to work on: November 28, 2013, 12:22:22 am
Hi,

I'm trying to use the Timer1 library (http://playground.arduino.cc/Code/Timer1 )

But the start() function doesn't seem to work

Code:
#include <TimerOne.h>
 
 int state=0;

void setup()
{

  pinMode(13, OUTPUT);   
 
  Timer1.initialize(1000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  Timer1.stop();
  Timer1.start();
//Timer1.resume();
}
 
void loop()
{
  // Main code loop
  // TODO: Put your regular (non-ISR) logic here
}
 
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
    state=state^1;
    digitalWrite( 13, state );
}

i.e I need to be able to init the timer and then start it some time later. I don't want to call initialize just before I need the timer to start, as I need the timer to start straight away and initialize() runs quite a lot of setup code.

However in my simple example above, the timerisr doesn't get called.

If you replace the stop() command with resume() it works fine, but the timer isn't reset to zero.
And I need to keep stopping the timer and starting it again.

The only solution I have found is just to set  TCNT1 = 0;  and then later call
  Timer1.resume(); instead of start(), but as document in the Timer1 source code, setting TCNT1 =0 causes the interrupt handler to get called, on the next tick.

My current work around is to set a flag just before setting TCNT1 =0, and in the ISR, if the flag is set, I treat this as a false interrupt caused by the resetting of TCNT1
Code:
  isResttingTimer=true;
  TCNT1 = 0;
Code:
void timerIsr()
{
  if (isResttingTimer)
  {
    isResttingTimer=false;
    return;
  }

Does anyone know the correct / best way to do this. Am I using the Timer1 library incorrectly.

BTW. I've tested on a Uno and Nano with the same results :-(
8  Using Arduino / Microcontrollers / Re: arduinoISP ATtiny45 Yikes! on: May 16, 2013, 05:32:47 am
Just in case this is any help to anyone reading this thread...

I had similar issues using a usbasp programmer to program an ATTiny85.

The solution I found in the end was to use 5V instead of 3.3V (there is a jumper on my usbasp that selects the voltage that is supplied from the programmer board - which I was using to supply the board)
9  Using Arduino / Audio / Warning. Switching module to 5V seems to have destroyed the SD Card :-( on: November 26, 2012, 03:42:13 am
Just a note in case anyone else tries this.

I bought one of these modules on eBay and following some instructions from someone on YouTube, I resoldered the jump link to set the voltage to 5V instead of 3.3V (there are 3 solder pads and you join 2 of them).

I did this because I could not get the module to make any sounds, even though the (busy) LED was flashing as if it was receiving commands etc.

Anyway. Following switching the module to 5V, it still didn't work, but when I tried to re-load the AD4 files, the SD card was no longer readable.

So it looks like the module may work on 5V, but not all SD cards (well not mine).
Hence I'd not advise anyone to try this unless the know their SD card is 5V tolerant.
 
10  Using Arduino / Networking, Protocols, and Devices / Web scraping with Ethercard (enc28j60) example on: November 22, 2012, 02:14:02 am
Hi,

In case anyone is interested, I've written a web scraping example using the Ethercard library, (for enc28J60 based modules)

Ethercard has recently been updated to be able to retrieve a whole web pages, in 512byte (packet size) chunks.

I've written written some functions to search for a specific marker string within the web page, which are designed to work across packets to find the target text on a normal web page.

I've posted an example which retrieves my current weather conditions for Melbourne Australia

Full details are on by blog at http://www.rogerclark.net/?p=499

I'm keen to improve the code, so if it doesn't work for specific web pages, please feel free to leave a comment on the blog and I'll take a look at the problem

Cheers

Roger Clark
Freelance web and iPhone app developer / Producer. Melbourne Australia. www.rogerclark.net
11  Using Arduino / Programming Questions / Re: Ethercard lib. and webclient example with big sites on: November 06, 2012, 06:09:47 am
Get the latest version of Ethercard from GitHub
, there is a new function
  persistTcpConnection(bool persist)


Set it to true, and your callback will be called for each data packet that is recieved in response to you browseUrl call

Pages: [1]