typedef enum {
KEY_OFF,
KEY_ON,
} KEY_State;
typedef enum {
LED_OFF,
LED_ON,
} LED_State;
typedef struct {
unsigned char KeyInSW_IN:1;
unsigned char IGN1_state:1;
unsigned char IGN2_state:1;
}T_DoorOpenSta;
static T_DoorOpenSta IGN_State;
// static unsigned char KeyInSW_IN;
// static unsigned char IGN1_state;
// static unsigned char IGN2_state;
static unsigned char Count_10msec=0;
static unsigned char Count_40msec=0;
static unsigned char Count_100msec=0;
static KEY_State KEYIGN_CNTRL_State;
static LED_State LED_CNTRL_State;
void TASK_10msec(void);
void TASK_40msec(void);
void TASK_100msec(void);
void LED_Control(void);
void Read_Button(void);
void Init_Timer(void);
/* Function Prototype */
void KEY_Control_fun(void);
void KEYIGN_OFF_en_fun(void);
void KEYIGN_OFF_du_fun(void);
void KEYIGN_ON_en_fun(void);
void KEYIGN_ON_du_fun(void);
/* ====================================== */
void KEY_Control_fun(void)
{
switch(KEYIGN_CNTRL_State)
{
case KEY_OFF:
KEYIGN_OFF_du_fun();
break;
case KEY_ON:
KEYIGN_ON_du_fun();
break;
}
}
void KEYIGN_OFF_en_fun()
{
LED_CNTRL_State=LED_OFF;
LED_Control();
KEYIGN_CNTRL_State = KEY_OFF;
}
void KEYIGN_OFF_du_fun()
{
if(IGN_State.KeyInSW_IN ==HIGH && IGN_State.IGN1_state == HIGH && IGN_State.IGN2_state == HIGH)
{
KEYIGN_ON_en_fun();
}
else{
/* do nothing */
}
}
void KEYIGN_ON_en_fun()
{
LED_CNTRL_State = LED_ON;
LED_Control();
KEYIGN_CNTRL_State = KEY_ON;
}
void KEYIGN_ON_du_fun()
{
if (IGN_State.KeyInSW_IN == LOW || IGN_State.IGN1_state == LOW || IGN_State.IGN2_state == LOW)
{
KEYIGN_OFF_en_fun();
}
else
{
/* do nothing */
}
}
void Read_Button()
{
IGN_State.KeyInSW_IN= digitalRead(2);
IGN_State.IGN1_state = digitalRead(3);
IGN_State.IGN2_state =digitalRead(4);
}
void Init_Timer(void)
{
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 011 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
}
ISR(TIMER1_COMPA_vect)
{
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
Count_10msec++;
Count_40msec++;
Count_100msec++;
}
void TASK_10msec()
{
Read_Button();
}
void TASK_40msec()
{
}
void TASK_100msec()
{
KEY_Control_fun();
}
void pin_INTI(void)
{
pinMode(13, OUTPUT); //Set the pin to be OUTPUT
pinMode(2, INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Init_Timer();
pin_INTI();
KEYIGN_OFF_en_fun();
}
void loop() {
// put your main code here, to run repeatedly:
if(Count_10msec>=10) /* 10msec*/
{
Count_10msec = 0;
TASK_10msec();
}
else
{
/*do nothing*/
}
if(Count_40msec>= 40) /* 40 msec*/
{
Count_40msec = 0;
TASK_40msec ();
}
else
{
/*do nothing */
}
/* 100msec*/
if(Count_100msec>= 100)
{
Count_100msec = 0;
TASK_100msec () ;
}
else
{
/*do nothing*/
}
}
void LED_Control (void)
{
digitalWrite(13,LED_CNTRL_State);
}