I am hoping someone can give me some advice on how to use Timer 0 and Timer 1 as counters. I want to use it to count the amount of time that a pressure is applied to a FSR and the amount of time inbetween pressures been applied. So maybe Timer 0 for the time pressure is applied, and Timer 1 for the time between pressures.
I have been reading over the datasheet for the ATmega328, but honestly, it's not helping much, and I am used to datasheets, but mostly for PIC16s. Maybe that's my problem, I have been looking for the equivalent of a Global Interrupt Enable bit etc. And I have been looking for info on prescalers etc.
I am using the millis() function at the minute, and it's kind of working ..but kind of not also. The applied pressure is always lasting for more than 10ms, so that causes problems in the data.
My supervisor suggested using timers as counters ...So I want to investigate that path. Plus, I am not great with Timers, so this could be a good way to learn ...jumping in at the deep end so to speak.
In this case I don't see anything to be gained by accessing the timers directly. I assume your trigger is connected to an ADC pin, so you'd still need to start and stop the timer in software.
Much easier to let the timers run as normal and access the counters via the built-in timing functions. For millisecond-precision timing the millis() function should be fine.
The applied pressure is always lasting for more than 10ms, so that causes problems in the data.
tim7:
In this case I don't see anything to be gained by accessing the timers directly. I assume your trigger is connected to an ADC pin, so you'd still need to start and stop the timer in software.
Much easier to let the timers run as normal and access the counters via the built-in timing functions. For millisecond-precision timing the millis() function should be fine.
The applied pressure is always lasting for more than 10ms, so that causes problems in the data.
Can you explain a bit more?
Well, the time the pressure is being applied for is a variable. It could last for, roughly, half a second, or it could last five minute. However, I need (or have been asked to) gather data every 10ms. I have been using the millis() function, with a delay after it, for testing that the system worked ...i.e. pressure 1 is applied at x time ....pressure 2 is applied at y time, y - x = time between pressures, and it works fine with me waiting long enough past the delay before I apply pressure. However, because I cannot know how long a pressure will be applied for, I cannot build in a correct delay which will allow me to get good readings.
So, if I use a timer, and the reading on the analogRead goes above a specific value, I can start a time, and when it goes below a specific value, I can stop the timer, therefore getting the time the pressure is applied for. This can go on in the background while other sensor data is been read at 10ms.
The Global Interrupt Enable bit is bit 7 in the status register. I think the datasheet is mostly well-written, but often I find myself jumping around between sections to solve a particular problem. Here is a paper on AVR timers that I really liked.
From the datasheet, Rev. 8271D–AVR–05/11:
7.3.1 SREG – AVR Status Register
The AVR Status Register – SREG – is defined as:
• Bit 7 – I: Global Interrupt Enable
The Global Interrupt Enable bit must be set for the interrupts to be enabled. The individual interrupt
enable control is then performed in separate control registers. If the Global Interrupt Enable
Register is cleared, none of the interrupts are enabled independent of the individual interrupt
enable settings. The I-bit is cleared by hardware after an interrupt has occurred, and is set by
the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by
the application with the SEI and CLI instructions, as described in the instruction set reference.