Skip to content

Commit

Permalink
Configuration of timer (TIM4) was revised (see issue #9).
Browse files Browse the repository at this point in the history
The functionality of the uptime counter was updated.
The timings for the menu was updated.
  • Loading branch information
mister-grumbler committed Dec 7, 2017
1 parent 02f5889 commit 4d3e26b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
1 change: 1 addition & 0 deletions include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
void initTimer();
void resetUptime();
unsigned long getUptime();
unsigned int getUptimeTicks();
unsigned char getUptimeSeconds();
unsigned char getUptimeMinutes();
unsigned char getUptimeHours();
Expand Down
15 changes: 5 additions & 10 deletions menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
#include "params.h"
#include "timer.h"

// TODO: these values should be corrected afterwards
// to correspond more precise to given time intervals.
#define MENU_AUTOINC_DELAY 1
#define MENU_1_SEC_PASSED 15
#define MENU_3_SEC_PASSED 45
#define MENU_5_SEC_PASSED 90
#define MENU_1_SEC_PASSED 32
#define MENU_3_SEC_PASSED MENU_1_SEC_PASSED * 3
#define MENU_5_SEC_PASSED MENU_1_SEC_PASSED * 5
#define MENU_AUTOINC_DELAY MENU_1_SEC_PASSED / 8

static unsigned char menuDisplay;
static unsigned char menuState;
Expand Down Expand Up @@ -110,7 +108,6 @@ void feedMenu (unsigned char event)
break;
}
} else if (menuState == MENU_SELECT_PARAM) {
// TODO: store into EEPROM on timeout and return to MENU_ROOT
switch (event) {
case MENU_EVENT_PUSH_BUTTON1:
menuState = menuDisplay = MENU_CHANGE_PARAM;
Expand Down Expand Up @@ -157,7 +154,6 @@ void feedMenu (unsigned char event)
break;
}
} else if (menuState == MENU_CHANGE_PARAM) {
// TODO: store into EEPROM on timeout and return to MENU_ROOT
switch (event) {
case MENU_EVENT_PUSH_BUTTON1:
menuState = menuDisplay = MENU_SELECT_PARAM;
Expand Down Expand Up @@ -209,7 +205,6 @@ void feedMenu (unsigned char event)
break;
}
} else if (menuState == MENU_SET_THRESHOLD) {
// TODO: on timeout store into EEPROM and return to MENU_ROOT
switch (event) {
case MENU_EVENT_PUSH_BUTTON1:
timer = 0;
Expand Down Expand Up @@ -247,7 +242,7 @@ void feedMenu (unsigned char event)
if (getButton2() || getButton3() ) {
blink = false;
} else {
blink = (bool) (getUptime() & 0x40);
blink = (bool) ( (unsigned char) getUptimeTicks() & 0x80);
}

if (timer > MENU_1_SEC_PASSED + MENU_AUTOINC_DELAY) {
Expand Down
98 changes: 56 additions & 42 deletions timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@
#include "menu.h"
#include "relay.h"

#define TICK_PRESCALE_VALUE 3
#define TICKS_IN_SECOND 500
#define BITS_FOR_TICKS 9
#define BITS_FOR_SECONDS 6
#define BITS_FOR_MINUTES 6
#define BITS_FOR_HOURS 5
#define BITS_FOR_DAYS 6
#define SECONDS_FIRST_BIT BITS_FOR_TICKS + 1
#define MINUTES_FIRST_BIT BITS_FOR_TICKS + BITS_FOR_SECONDS + 1
#define HOURS_FIRST_BIT BITS_FOR_TICKS + BITS_FOR_SECONDS + BITS_FOR_MINUTES + 1
#define DAYS_FIRST_BIT BITS_FOR_TICKS + BITS_FOR_SECONDS + BITS_FOR_MINUTES + BITS_FOR_HOURS + 1
#define BITMASK(L) ( ~ (0xFFFFFFFF << (L) ) )
#define NBITMASK(L) (0xFFFFFFFF << (L) )

/**
* Uptime counter
* |--Day--|--Hour--|--Minute--|--Second--|--Ticks--|
* 32 23 18 12 6 0
* 31 26 21 15 9 0
*/
static unsigned long uptime;
static unsigned char tickPrescaler;

/**
* @brief Initialize timer's configuration registers and reset uptime.
*/
void initTimer()
{
CLK_CKDIVR = 0x00; // Set the frequency to 16 MHz
TIM4_PSCR = 0x07;
CLK_CKDIVR = 0x00; // Set the frequency to 16 MHz
TIM4_PSCR = 0x07; // CLK / 128 = 125KHz
TIM4_ARR = 0xFA; // 125KHz / 250(0xFA) = 500Hz
TIM4_IER = 0x01; // Enable interrupt on update event
TIM4_CR1 = 0x05; // Enable timer
resetUptime();
Expand All @@ -55,28 +66,36 @@ void initTimer()
*/
void resetUptime()
{
tickPrescaler = 0;
uptime = 0;
}

/**
* @brief Gets raw value of bit-mapped uptime counter.
* |--Day--|--Hour--|--Minute--|--Second--|--Ticks--|
* 32 23 18 12 6 0
* 31 26 21 15 9 0
* @return value of uptime counter.
*/
unsigned long getUptime()
{
return uptime;
}

/**
* @brief Gets ticks part of uptime counter.
* @return ticks part of uptime.
*/
unsigned int getUptimeTicks()
{
return (unsigned int) (uptime & BITMASK (BITS_FOR_TICKS) );
}

/**
* @brief Gets seconds part of time being passed since last reset.
* @return seconds part of uptime.
*/
unsigned char getUptimeSeconds()
{
return (unsigned char) ( (uptime >> 7) & 0x3F);
return (unsigned char) ( (uptime >> SECONDS_FIRST_BIT) & BITMASK (BITS_FOR_SECONDS) );
}

/**
Expand All @@ -85,7 +104,7 @@ unsigned char getUptimeSeconds()
*/
unsigned char getUptimeMinutes()
{
return (unsigned char) ( (uptime >> 13) & 0x3F);
return (unsigned char) ( (uptime >> MINUTES_FIRST_BIT) & BITMASK (BITS_FOR_MINUTES) );
}

/**
Expand All @@ -94,7 +113,7 @@ unsigned char getUptimeMinutes()
*/
unsigned char getUptimeHours()
{
return (unsigned char) ( (uptime >> 19) & 0x1F);
return (unsigned char) ( (uptime >> HOURS_FIRST_BIT) & BITMASK (BITS_FOR_HOURS) );
}

/**
Expand All @@ -103,7 +122,7 @@ unsigned char getUptimeHours()
*/
unsigned char getUptimeDays()
{
return (unsigned char) ( (uptime >> 24) & 0xFF);
return (unsigned char) ( (uptime >> DAYS_FIRST_BIT) & BITMASK (BITS_FOR_DAYS) );
}

/**
Expand All @@ -113,45 +132,40 @@ unsigned char getUptimeDays()
void TIM4_UPD_handler() __interrupt (23)
{
TIM4_SR &= ~TIM_SR1_UIF; // Reset flag
tickPrescaler++;

if (tickPrescaler < TICK_PRESCALE_VALUE) {
return;
if ( ( (unsigned int) (uptime & BITMASK (BITS_FOR_TICKS) ) ) >= TICKS_IN_SECOND) {
uptime &= NBITMASK (SECONDS_FIRST_BIT);
uptime += (unsigned long) 1 << SECONDS_FIRST_BIT;

// Increment minutes count when 60 seconds have passed.
if ( ( (unsigned char) (uptime >> SECONDS_FIRST_BIT) & BITMASK (BITS_FOR_SECONDS) ) == 60) {
uptime &= NBITMASK (MINUTES_FIRST_BIT);
uptime += (unsigned long) 1 << MINUTES_FIRST_BIT;
}

// Increment hours count when 60 minutes have passed.
if ( ( (unsigned char) (uptime >> MINUTES_FIRST_BIT) & BITMASK (BITS_FOR_MINUTES) ) == 60) {
uptime &= NBITMASK (HOURS_FIRST_BIT);
uptime += (unsigned long) 1 << HOURS_FIRST_BIT;
}

// Increment days count when 24 hours have passed.
if ( ( (unsigned char) (uptime >> HOURS_FIRST_BIT) & BITMASK (BITS_FOR_HOURS) ) == 24) {
uptime &= NBITMASK (DAYS_FIRST_BIT);
uptime += (unsigned long) 1 << DAYS_FIRST_BIT;
}
}

tickPrescaler = 0;
uptime++;

// Increment minutes count when 60 seconds have passed.
if ( ( (unsigned char) (uptime >> 7) & 0x3F) == 0x3C) { // 60 seconds
uptime &= ~0x1EFF;
uptime += (unsigned long) 1 << 13;
}

// Increment hours count when 60 minutes have passed.
if ( ( (unsigned char) (uptime >> 13) & 0x3F) == 0x3C) { // 60 minutes
uptime &= ~0x7FFFF;
uptime += (unsigned long) 1 << 19;
}

// Increment days count when 24 hours have passed.
if ( ( (unsigned char) (uptime >> 19) & 0x1F) == 0x18) { // 24 hours
uptime &= ~0xFFFFFF;
uptime += (unsigned long) 1 << 24;
}

// Try not to call all refresh functions at once.
refreshDisplay();

if ( ( (unsigned char) uptime & 0x07) == 1) {
if ( ( (unsigned char) getUptimeTicks() & 0x0F) == 1) {
refreshMenu();
}

if ( ( (unsigned char) uptime & 0x3F) == 2) {
} else if ( ( (unsigned char) getUptimeTicks() & 0xFF) == 2) {
startADC();
}

if ( ( (unsigned char) uptime & 0x3F) == 3) {
} else if ( ( (unsigned char) getUptimeTicks() & 0xFF) == 3) {
refreshRelay();
}

refreshDisplay();
}

0 comments on commit 4d3e26b

Please sign in to comment.