Calculate timer values

This commit is contained in:
Olaf Rempel 2020-01-12 19:16:07 +01:00
parent d1e3e5be08
commit 1216082a4f
2 changed files with 15 additions and 8 deletions

View File

@ -28,6 +28,12 @@
#include "target.h" #include "target.h"
#include "uart.h" #include "uart.h"
#define TIMER_IRQFREQ_MS 10
/* convert milliseconds to timer ticks */
#define TIMER_MSEC2TICKS(x) ((x * F_CPU) / (TIMER_DIVISOR * 1000ULL))
#define TIMER_MSEC2IRQCNT(x) (x / TIMER_IRQFREQ_MS)
#define EV_NONE 0x00 #define EV_NONE 0x00
#define EV_STATE_ENTER 0x01 #define EV_STATE_ENTER 0x01
#define EV_BUTTON_PRESSED 0x02 #define EV_BUTTON_PRESSED 0x02
@ -95,7 +101,8 @@ static void reset_statemachine(uint8_t events)
/* remove all events */ /* remove all events */
events = EV_NONE; events = EV_NONE;
timer = 0; /* stop timer */ /* stop timer */
timer = TIMER_MSEC2IRQCNT(0);
spi_init(0); spi_init(0);
@ -122,7 +129,7 @@ static void reset_statemachine(uint8_t events)
{ {
events &= ~(EV_STATE_ENTER); events &= ~(EV_STATE_ENTER);
timer = 1; /* timeout 10ms */ timer = TIMER_MSEC2IRQCNT(10);
/* put device in ISP mode */ /* put device in ISP mode */
RESET_ACTIVE(); RESET_ACTIVE();
@ -153,7 +160,7 @@ static void reset_statemachine(uint8_t events)
{ {
events &= ~(EV_STATE_ENTER); events &= ~(EV_STATE_ENTER);
timer = 5; /* timeout 50ms */ timer = TIMER_MSEC2IRQCNT(50);
/* put device in RUN mode */ /* put device in RUN mode */
RESET_INACTIVE(); RESET_INACTIVE();
@ -598,7 +605,7 @@ static void cmdloop(void)
ISR(TIMER0_OVF_vect) ISR(TIMER0_OVF_vect)
{ {
/* restart timer */ /* restart timer */
TCNT0 = TIMER_RELOAD; TCNT0 = 0xFF - TIMER_MSEC2TICKS(TIMER_IRQFREQ_MS);
static uint8_t prev_pressed; static uint8_t prev_pressed;
if (ISP_CHECK()) if (ISP_CHECK())

View File

@ -8,9 +8,8 @@
* Fuse H: 0xDA (512 words bootloader, jtag disabled) * Fuse H: 0xDA (512 words bootloader, jtag disabled)
* Fuse L: 0xFF (ext. Crystal) * Fuse L: 0xFF (ext. Crystal)
*/ */
#define F_CPU 7372800 #define F_CPU 7372800ULL
#define BAUDRATE 115200 #define BAUDRATE 115200
#define TIMER_RELOAD (0xFF - 72) /* 10ms @7.3728MHz */
#define ISP_RESET PORTB1 /* to target */ #define ISP_RESET PORTB1 /* to target */
#define ISP_LED PORTB3 /* low active */ #define ISP_LED PORTB3 /* low active */
@ -36,6 +35,7 @@
#define USE_DISPLAY 0 #define USE_DISPLAY 0
#define TIMER_DIVISOR 1024
#define TIMER_INIT() { /* timer0, FCPU/1024, overflow interrupt */ \ #define TIMER_INIT() { /* timer0, FCPU/1024, overflow interrupt */ \
TCCR0 = (1<<CS02) | (1<<CS00); \ TCCR0 = (1<<CS02) | (1<<CS00); \
TIMSK = (1<<TOIE0); \ TIMSK = (1<<TOIE0); \
@ -54,9 +54,8 @@
* Fuse H: 0xDC (512 words bootloader) * Fuse H: 0xDC (512 words bootloader)
* Fuse L: 0xE2 (internal osc) * Fuse L: 0xE2 (internal osc)
*/ */
#define F_CPU 8000000 #define F_CPU 8000000ULL
#define BAUDRATE 115200 #define BAUDRATE 115200
#define TIMER_RELOAD (0xFF - 78) /* 10ms @8MHz */
/* trim internal oscillator to get "good" baudrate */ /* trim internal oscillator to get "good" baudrate */
#define OSCCAL_VALUE 0x80 #define OSCCAL_VALUE 0x80
@ -96,6 +95,7 @@
#define DISP_D5 PORTD6 #define DISP_D5 PORTD6
#define DISP_D6 PORTD7 #define DISP_D6 PORTD7
#define TIMER_DIVISOR 1024
#define TIMER_INIT() { /* timer0, FCPU/1024, overflow interrupt */ \ #define TIMER_INIT() { /* timer0, FCPU/1024, overflow interrupt */ \
TCCR0B = (1<<CS02) | (1<<CS00); \ TCCR0B = (1<<CS02) | (1<<CS00); \
TIMSK0 = (1<<TOIE0); \ TIMSK0 = (1<<TOIE0); \