misc; and working on the timer function

partial-rewrite
Ben Blazak 2013-05-06 02:31:52 -07:00
parent 75e0336f6b
commit 5e1877cb82
4 changed files with 28 additions and 50 deletions

View File

@ -125,8 +125,8 @@
according to the C standard, and I didn't see AVRs mentioned; and AVRs don't
typically run an OS anyway), but still quite interesting.
* [integral conversion from negative signed to unsigned integer]
(http://www.rhinocerus.net/forum/language-c-moderated/649336-integral-conversion-negative-signed-unsigned-integer.html)
* [Signed to unsigned conversion in C - is it always safe?]
(http://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe)
What happens if you return `-1` from a `uint8_t` function (or assign `-1` to
a `uint8_t`)?

View File

@ -210,8 +210,8 @@ uint8_t teensy__init(void) {
// keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
kb__led__all_off(); // (just to put the pins in a known state)
TCCR1A = 0b10101001; // set and configure fast PWM
TCCR1B = 0b00001001; // set and configure fast PWM
TCCR1A = 0b10101001; // set and configure fast PWM
TCCR1B = 0b00001001; // set and configure fast PWM
// I2C (TWI)
twi__init(); // on pins D(1,0)

View File

@ -24,6 +24,7 @@
// - chorded keys
// - layers
// - making layouts
// - changing the meaning of the LEDs
// - put the tutorials in the readme.md of
// ".../firmware/keyboard/ergodox/layout"

View File

@ -9,15 +9,24 @@
*
* Prefix: `timer__`
*
* TODO: make sure this is how we want to do things...
* TODO: document and write header
* TODO: notes
* - it's unnecessary to keep 32 (or even 16) bit resolution if you don't need
* it; functions that don't should cast to uint8_t or uint16_t
* - use `end-start` for determining time difference. since the values are
* unsigned, this will work across overflows, for up to the maximum amount of
* time representable by the type you're using
* - `timer__init()` should be called once by `main()`
* `timer__init()` is meant to be called once, on startup, by `main()`
*
* See the accompanying '.md' file for more documentation.
*
*
* Usage Notes:
*
* - It's unnecessary to keep 32-bit (or even 16-bit) resolution when storing
* the value returned by `timer__get_milliseconds()` if you don't need it.
* Casting to a smaller unsigned value should be safe (as long as you cast
* *all* the values you plan to compare with each other to the same type!).
*
* - Use `end_time - start_time` for determining time difference. Since the
* returned values are unsigned (and you should be storing them in unsigned
* variables as well) this will work across overflows, for up to the maximum
* amount of milliseconds representable by the type you're using. (See [this
* answer] (http://stackoverflow.com/a/50632) on <http://stackoverflow.com/>
* if you're curious as to why.)
*/
@ -31,43 +40,10 @@ static volatile uint32_t _milliseconds;
// ----------------------------------------------------------------------------
void timer__init(void) {
// /TODO
// references:
//
// Newbie's Guide to AVR Timers
// tutorial by Dean Camera
// http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106
//
// the atmega32u4 spec sheet
//
// the reference somewhere about what happens to unsigned things when
// negative values are assigned to them
// --- python3 ---
// >>> f_cpu = 16000000
// >>> .001/((1/f_cpu)*64)
// 250.00000000000003
// >>> (.001*f_cpu)/(64)
// 250.0
// ---------------
// table
// ------------------------------------------
// prescale value ticks per millisecond
// -------------- ---------------------
// 1 16000
// 8 2000
// 64 250
// 256 62.5
// 1024 15.625
// - we want a period = 1 millisecond = .001 seconds
// - cpu = 16 MHz; we can prescale at clk_io/64 to get 250 prescaled ticks
// per millisecond (which fits into a uint8_t, just barely)
//
// so we want to use TCCR0A, on the CTC (clear timer on compare match)
// mode, with compare interrupt enabled
TCCR0A = 0b10000010;
TCCR0B = 0b00000011;
TIMSK0 = 0b00000010;
OCR0A = 250;
}
uint32_t timer__get_milliseconds(void) {
@ -76,6 +52,7 @@ uint32_t timer__get_milliseconds(void) {
// ----------------------------------------------------------------------------
// TODO: document this in the '.md' file too
ISR(TIMER0_COMPA_vect) {
_milliseconds++;
}