typedef enum {
KEY_OFF,
KEY_ON,
} KEY_State;
// typedef enum {
// IGN_OFF,
// IGN_ON,
// } IGN_State;
static unsigned char KEY_Control_state;
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;
//tatic KEY_State key_state_control
void KEY_Control_fun(void)
{
switch(KEY_Control_state)
{
case KEY_OFF:
//Serial.println("OFF");
KEYIGN_OFF_state_Cntrl();
break;
case KEY_ON:
Serial.println("ON");
KEYIGN_ON_state_Cntrl();
break;
}
}
void KEYIGN_ON_state_Cntrl()
{
if(KEY_Control_state=HIGH && IGN1_state == HIGH && IGN2_state == HIGH)
{
digitalWrite(13,HIGH);
}
else{
KEYIGN_OFF_state_Cntrl();
}
}
void KEYIGN_OFF_state_Cntrl()
{
if (KEY_Control_state == LOW || IGN1_state == LOW || IGN1_state == LOW)
{
digitalWrite(13,LOW);
}
else{
// KEY_Control_fun();
}
}
void Read_Button2()
{
IGN1_state = digitalRead(3);
Serial.println("1");
}
void Read_Button3()
{
IGN2_state =digitalRead(4);
Serial.println("2");
}
void Read_Button1()
{
KEY_Control_state= digitalRead(2);
Serial.println("3");
// Serial.println(KEY_Control_state);
// Serial.println(IGN1_state);
// Serial.println(IGN2_state);
}
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_Button1();
Read_Button2();
Read_Button3();
}
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();
}
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*/
}
}