diff --git a/drivers/efm8bb1/src/hal.c b/drivers/efm8bb1/src/hal.c index 8b49c08..71a66cf 100644 --- a/drivers/efm8bb1/src/hal.c +++ b/drivers/efm8bb1/src/hal.c @@ -223,8 +223,9 @@ void load_timer1(const uint16_t value) void pca0_init(void) { // default pca acts normally when controller is idle + // default source is system clock divided by 12 // default pca CF overflow is disabled - PCA0MD |= CPS__SYSCLK_DIV_12; + //PCA0MD &= ~CPS__SYSCLK_DIV_12; // enable both positive and negative edge triggers PCA0CPM0 |= CAPP__ENABLED; @@ -233,12 +234,12 @@ void pca0_init(void) void pca0_run(void) { - PCA0CN0 |= CR__RUN; + CR = true; } void pca0_halt(void) { - PCA0CN0 &= ~CR__RUN; + CR = false; } @@ -263,11 +264,13 @@ void clear_capture_flag(void) CCF0 = 0; } -// FIXME: explain counts to time conversion constant +// the time constant is explained in the rcswitch.c file + unsigned long countsToTime(const unsigned long duration) { unsigned long converted; - converted = duration * 2; + + converted = duration / 2; return converted; } \ No newline at end of file diff --git a/drivers/ob38s003/inc/hal.h b/drivers/ob38s003/inc/hal.h index 28058d4..3871f7e 100644 --- a/drivers/ob38s003/inc/hal.h +++ b/drivers/ob38s003/inc/hal.h @@ -234,7 +234,7 @@ extern void disable_serial_interrupt(void); extern bool global_interrupts_are_enabled(void); void load_timer0(const uint16_t load); -extern uint16_t get_timer2(void); +extern uint16_t get_capture_mode(void); extern void clear_capture_flag(void); diff --git a/drivers/ob38s003/src/hal.c b/drivers/ob38s003/src/hal.c index 2f9f09b..0613f1c 100644 --- a/drivers/ob38s003/src/hal.c +++ b/drivers/ob38s003/src/hal.c @@ -195,9 +195,9 @@ void init_timer2_as_capture(void) //(i.e., P1.6 or RDATA which is input from D0 of radio) CCEN = 0x60; - // timer 2 input frequency from prescaler + // timer 2 input frequency from prescaler (i.e., TSPS set to 1/24 below) // timer 2 mode 0 auto reload (generated by a timer 2 overflow) - // timer 2 is clocked with 1/4 (0x51) or 1/24 (0xD1) of the oscillator frequency (prescaler select bit) + // timer 2 is clocked with 1/24 of the oscillator frequency (prescaler select bit) // (e.g., 0.25 microseconds per count) T2CON = 0xD1; } @@ -208,7 +208,7 @@ bool global_interrupts_are_enabled(void) } -uint16_t get_timer2(void) +uint16_t get_capture_mode(void) { return (CCH1 << 8) | CCL1; } @@ -223,10 +223,11 @@ unsigned char get_stack_pointer(void) return SP; } -// FIXME: on counts to time conversion +// the time constant is explained in the rcswitch.c file unsigned long countsToTime(const unsigned long duration) { unsigned long converted; + converted = (duration * 3) / 2; return converted; diff --git a/drivers/ob38s003/src/timer_interrupts.c b/drivers/ob38s003/src/timer_interrupts.c index 7e7e4fc..9891d3f 100644 --- a/drivers/ob38s003/src/timer_interrupts.c +++ b/drivers/ob38s003/src/timer_interrupts.c @@ -151,7 +151,7 @@ void timer1_isr(void) __interrupt (d_T1_Vector) //----------------------------------------------------------------------------- void timer2_isr(void) __interrupt (d_T2_Vector) { - uint16_t currentCapture = get_timer2(); + uint16_t currentCapture = get_capture_mode(); capture_handler(currentCapture); diff --git a/src/rcswitch.c b/src/rcswitch.c index 91146b9..56740f1 100644 --- a/src/rcswitch.c +++ b/src/rcswitch.c @@ -190,22 +190,22 @@ void capture_handler(const uint16_t currentCapture) // FIXME: no magic numbers // FIXME: seems like a bad idea to make wrap around calculation depend on variable type, what if it changes // if overflow, we must compute difference by taking into account wrap around at maximum variable size - duration = ULONG_MAX - previous + current; + duration = UINT_MAX - previous + current; } else { duration = current - previous; } - // FIXME: no magic numbers // e.g., EFM8BB1 // e.g. (1/(24500000))*(49/2) = 1 microsec // e.g. (1/(24500000/12))*2 = 0.9796 microsec + // so need to do the inverse to go from counts to time (i.e., counts * 1/2 = time) // (1/(24500000/12))*dec(0xFFFF) = 32.0987755 millisecs max // e.g., OBS38S003 // e.g. prescale at (1/4) at 16 MHz, four counts are needed to get one microsecond - // e.g. prescale at (1/24) at 16 MHz + // e.g. prescale at (1/24) at 16 MHz, 2/3 counts are need to get one microsecond + // so inverse is counts * 3/2 = time // e.g., (1/(16000000/24)) * dec(0xFFFF) = 98.30 milliseconds maximum can be counted - // FIXME: show why 3/2 conversion works duration = countsToTime(duration); // from oscillscope readings it appears that first sync pulse of first radio packet is frequently not output properly by receiver