-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
346 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"board": "arduino:avr:mega", | ||
"configuration": "cpu=atmega2560", | ||
"sketch": "examples/test_timer/test_timer.ino" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Linux", | ||
"includePath": [ | ||
"/home/sansil/Documents/arduino-1.8.5/hardware/arduino/avr/**", | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [], | ||
"compilerPath": "/usr/bin/clang", | ||
"cStandard": "c11", | ||
"cppStandard": "c++17", | ||
"intelliSenseMode": "clang-x64", | ||
"forcedInclude": [ | ||
"/home/sansil/Documents/arduino-1.8.5/hardware/arduino/avr/cores/arduino/Arduino.h" | ||
] | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# SoftwareTimer | ||
Software Timer library for Arduino | ||
## SoftwareTimer | ||
|
||
### Software Timers library for Arduino | ||
|
||
Create software timers from one hardware timer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
name=Software Timer | ||
version=0.0.1 | ||
name=Software Timers | ||
version=0.1.1 | ||
author=Santiago Silva Dalla <sansil32@gmail.com> | ||
maintainer=Santiago Silva Dalla <sansil32@gmail.com> | ||
sentence=Software timer library. | ||
paragraph=Software timer library. | ||
category=Data Storage | ||
sentence=A library to create software timers from one hardware timer. | ||
paragraph=Software timers library. | ||
category=Timing | ||
url=https://github.com/sansil/SoftwareTimer | ||
architectures=* | ||
architectures=avr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,88 @@ | ||
#include "hardwareTimer.h" | ||
|
||
void start_hardware_timer(TIME unTime){ | ||
/* | ||
* Seteo el timer en multiplo de 1seg. | ||
* unTime esta en ms. | ||
void start_hardware_timer(TIME unTime) | ||
{ | ||
/* | ||
* Set timer on 1sec. | ||
* unTime in ms. | ||
* prescaler 8 max_ms : 32 ms | ||
* prescaler 64 max_ms : 262ms | ||
* prescaler 256 max_ms : 1048 ms | ||
* prescaler 1024 max_ms : 4164 ms | ||
*/ | ||
|
||
|
||
TIME ticks=0; | ||
float prescaler=1; | ||
TIME ticks = 0; | ||
float prescaler = 1; | ||
cli(); | ||
|
||
if ((unTime>=0) & (unTime <= 32)){ | ||
if ((unTime >= 0) & (unTime <= 32)) | ||
{ | ||
prescaler = 8.0; | ||
}else if((unTime>=33) & (unTime <= 262)){ | ||
} | ||
else if ((unTime >= 33) & (unTime <= 262)) | ||
{ | ||
prescaler = 64.0; | ||
}else if ((unTime>=263) & (unTime <= 1048)){ | ||
} | ||
else if ((unTime >= 263) & (unTime <= 1048)) | ||
{ | ||
prescaler = 256.0; | ||
}else if((unTime>=1049) & (unTime <= 4164)){ | ||
} | ||
else if ((unTime >= 1049) & (unTime <= 4164)) | ||
{ | ||
prescaler = 1024.0; | ||
}else{ | ||
prescaler = 1024.0; //TODO control de error | ||
} | ||
// ticks = round((unsigned long)((float)unTime*15624)/1000); //1s 15624 ticks max value 65535 | ||
//Serial.println(ticks); | ||
// disable global interrupts | ||
TCCR1A = 0; // set entire TCCR1A register to 0 | ||
TCCR1B = 0; // same for TCCR1B | ||
//OCR1A = ticks; | ||
OCR1A = round((16000.0f/prescaler)*unTime - 1); | ||
// turn on CTC mode: | ||
else | ||
{ | ||
prescaler = 1024.0; //TODO: control de error | ||
} | ||
|
||
TCCR1A = 0; // set entire TCCR1A register to 0 | ||
TCCR1B = 0; // same for TCCR1B | ||
TCNT1 = 0; | ||
|
||
OCR1A = round((16000.0f / prescaler) * unTime - 1); | ||
TIFR1 = 0xFF; // reset flags | ||
// turn on CTC mode: | ||
TCCR1B |= (1 << WGM12); | ||
// Set CS10 and CS12 bits for 1024 prescaler: | ||
switch ((int)prescaler) { | ||
case 8: | ||
TCCR1B |= (1 << CS11); | ||
break; | ||
case 64: | ||
TCCR1B |= (1 << CS10); | ||
TCCR1B |= (1 << CS11); | ||
break; | ||
case 256: | ||
TCCR1B |= (1 << CS12); | ||
break; | ||
case 1024: | ||
default: | ||
TCCR1B |= (1 << CS10); | ||
TCCR1B |= (1 << CS12); | ||
break; | ||
} | ||
// enable timer compare interrupt: | ||
// Set CS10 and CS12 bits for 1024 prescaler: | ||
switch ((int)prescaler) | ||
{ | ||
case 8: | ||
TCCR1B |= (1 << CS11); | ||
break; | ||
case 64: | ||
TCCR1B |= (1 << CS10); | ||
TCCR1B |= (1 << CS11); | ||
break; | ||
case 256: | ||
TCCR1B |= (1 << CS12); | ||
break; | ||
case 1024: | ||
default: | ||
TCCR1B |= (1 << CS10); | ||
TCCR1B |= (1 << CS12); | ||
break; | ||
} | ||
// enable timer compare interrupt: | ||
TIMSK1 |= (1 << OCIE1A); | ||
// enable global interrupts: | ||
sei(); | ||
} | ||
|
||
ISR(TIMER1_COMPA_vect) { | ||
/* | ||
* En la interrupcion hago un update con el timepo que expiro y seteo un nuevo timer con el valor de timer_next( si es que hay uno) | ||
*/ | ||
ISR(TIMER1_COMPA_vect) | ||
{ | ||
|
||
TCCR1A = 0; | ||
TCCR1B = 0; //apago el timer sino queda ciclico por defecto | ||
TCCR1B = 0; //stop timer | ||
time_now = update_time_now(); | ||
timers_update(time_now - time_timer_set); // actualizo el nuevo timer_next | ||
/* start physical timer for next shortest time if one exists */ | ||
if (timer_next) { | ||
time_timer_set = time_now; | ||
start_hardware_timer(timer_next->time); | ||
|
||
timers_update(time_now - time_timer_set); // update new timer_next | ||
/* start physical timer for next shortest time if one exists */ | ||
if (timer_next) | ||
{ | ||
time_timer_set = time_now; | ||
start_hardware_timer(timer_next->time); | ||
} | ||
} | ||
|
||
TIME update_time_now(){ | ||
TIME update_time_now() | ||
{ | ||
return millis(); | ||
} |
Oops, something went wrong.