/* Example code with timer interrupt that will create an interruption each
* 1ms using timer1 and prescalar of 64.
Calculations (for 1ms):
System clock 16 Mhz and Prescalar 64;
Timer 1 speed = 16Mhz/64 = 250 Khz
Pulse time = 1/250 Khz = 4us
Count up to = 1ms / 4us = 250 (so this is the value the OCR register should have)*/
#define LED_RED A0
#define LED_ORANGE A1
#define LED_GREEN A2
#define SWITCH_INPUT 4
#include "SevSeg.h"
SevSeg sevseg; //Initiate a seven segment controller object
typedef enum {
E_SWITCH_POWER_OFF,
E_SWITCH_POWER_ON,
} SWITCH_Power_State_E;
typedef enum {
RED_LED_STATE,
ORANGE_LED_STATE,
GREEN_LED_STATE,
} Light_State_E;
unsigned char COUNT_10ms=0;
unsigned char COUNT_40ms=0;
unsigned char COUNT_100ms=0;
void Task_10ms();
void Task_40ms();
void Task_100ms();
void Timer_init();
void digitalpin_init();
void seven_segment_init();
void display_count();
unsigned char count=0;
unsigned char number;
/* Variables */
/*==========================================================================*/
SWITCH_Power_State_E SWITCH_Control_state_re;
Light_State_E Light_led_state_re;
//static uint8_t power;
unsigned char Switch;
/* Private functions prototypes */
/*==========================================================================*/
void Power_Init(void);
void Control_fun(void);
void Power_Off_state_entry_fun(void);
void Power_Off_state_Cntrl(void);
void Power_On_state_entry_fun(void);
void Power_On_state_Cntrl(void);
void red_Off_state_entry_fun(void);
void red_On_state_entry_fun(void);
void red_Control_fun(void);
void orange_Off_state_entry_fun(void);
void orange_On_state_entry_fun(void);
void orange_Control_fun(void);
void green_Off_state_entry_fun(void);
void green_On_state_entry_fun(void);
void green_Control_fun(void);
void display(void);
void switchh(void);
void setup()
{
Serial.begin(9600);
digitalpin_init();
Timer_init(); //Set the pin to be OUTPUT
seven_segment_init();
Power_Init();
}
void loop() {
// put your main code here, to run repeatedly:
if(COUNT_10ms >= 10)
{
COUNT_10ms = 0;
Task_10ms();
}
if(COUNT_40ms >= 40)
{
COUNT_40ms = 0;
Task_40ms();
}
if(COUNT_100ms >= 100)
{
COUNT_100ms = 0;
Task_100ms();
}
}
//With the settings above, this IRS will trigger each 1ms.
ISR(TIMER1_COMPA_vect)
{
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
//Increment count
COUNT_10ms++;
COUNT_40ms++;
COUNT_100ms++;
}
//init functions
void digitalpin_init()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(SWITCH_INPUT, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_ORANGE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
}
void Timer_init()
{
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000011; //Set CS12 to 1 so we get prescalar 64
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 31250*/
OCR1A = 250; //Finally we set compare register A to this value
sei(); //Enable back the interrupts
}
void seven_segment_init()
{
byte numDigits = 2;
byte digitPins[] = {2, 3};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, };
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = true; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,updateWithDelays, leadingZeros);
sevseg.setBrightness(90);
}
void Task_10ms()
{
switchh();
}
void Task_40ms()
{
}
void Task_100ms()
{
Control_fun();
}
void Power_Init(void)
{
Power_Off_state_entry_fun();
}
void switchh()
{
Switch = digitalRead(SWITCH_INPUT);
}
void Control_fun(void)
{
switch(SWITCH_Control_state_re)
{
case E_SWITCH_POWER_OFF:
Power_Off_state_Cntrl();
break;
case E_SWITCH_POWER_ON:
Power_On_state_Cntrl();
display_count();
break;
}
}
void Power_Off_state_entry_fun(void)
{
SWITCH_Control_state_re = E_SWITCH_POWER_OFF;
red_Off_state_entry_fun();
orange_Off_state_entry_fun();
green_Off_state_entry_fun();
}
void Power_Off_state_Cntrl(void)
{
if(Switch == HIGH)
{
Power_On_state_entry_fun();
}
else
{
/* do nothing*/
}
}
void Power_On_state_entry_fun(void)
{
SWITCH_Control_state_re = E_SWITCH_POWER_ON;
red_On_state_entry_fun();
}
void Power_On_state_Cntrl(void)
{
if(Switch == LOW)
{
Power_Off_state_entry_fun();
}
else
{
Sub_Control_fun();
}
}
void Sub_Control_fun()
{
switch(Light_led_state_re)
{
case RED_LED_STATE:
red_Control_fun();
case ORANGE_LED_STATE:
orange_Control_fun();
case GREEN_LED_STATE:
green_Control_fun();
break;
}
}
void red_Off_state_entry_fun(void)
{
digitalWrite(LED_RED,LOW);
}
void red_On_state_entry_fun(void)
{
number=59;
Light_led_state_re = RED_LED_STATE;
digitalWrite(LED_RED,HIGH);
}
void red_Control_fun(void)
{
if (number<=0)
{
red_Off_state_entry_fun();
orange_On_state_entry_fun();
}
else
{
//do nothing
}
}
void orange_Off_state_entry_fun(void)
{
digitalWrite(LED_ORANGE,LOW);
}
void orange_On_state_entry_fun(void)
{
Light_led_state_re = ORANGE_LED_STATE;
digitalWrite(LED_ORANGE,HIGH);
number=9;
}
void orange_Control_fun(void)
{
if (number <= 0)
{
orange_Off_state_entry_fun();
green_On_state_entry_fun();
}
else
{
}
}
void green_Off_state_entry_fun(void)
{
digitalWrite(LED_GREEN,LOW);
}
void green_On_state_entry_fun(void)
{
Light_led_state_re = GREEN_LED_STATE;
digitalWrite(LED_GREEN,HIGH);
number = 59;
//sevseg.setNumber(number);
}
void green_Control_fun(void)
{
if (number<=0)
{
green_Off_state_entry_fun();
red_On_state_entry_fun();
}
else
{
//Do nothing
}
}
void display_count()
{
count++;
if (count==0||count>=10)
{
count=0;
sevseg.setNumber(number);
number--;
}
sevseg.refreshDisplay();
}