#define RED_LED 12
#define Button_Pin 7
unsigned int counter10msec=0;/*Counter for 10 milli seconds*/
/*unsigned char LED_STATE=0;*/
//unsigned int counter100msec=0;/*Counter for 100 millisecond*/
void setup() {
Serial.begin(115200);
Digitalpin();
Timer();
}
void loop()
{
if(counter10msec>=(10))
{
counter10msec=0;
Task_10ms();
}
else{
/*Do Nothing*/
}
}
/*With the settings above, this IRS will trigger each count */
ISR(TIMER0_COMPA_vect){
TCNT0 = 0; /*First, set the timer back to 0 so it resets for next interrupt*/
counter10msec++;
//counter100msec++;
}
void Digitalpin(void)
{
pinMode(RED_LED, OUTPUT);
pinMode(Button_Pin, INPUT);
}
void 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.*/
TCCR0A = 0; /* Reset entire TCCR1A to 0 */
TCCR0B = 0; /* Reset entire TCCR1B to 0 */
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR0B |= B00000011; /* Set CS10 and CS11 to 1 so we get prescalar 64 */
/*3. We enable compare match mode on register A*/
TIMSK0 |= B00000010; /*Set OCIE1A to 1 so we enable compare match A */
/*4. Set the value of register A to 31250*/
OCR0A = 250; /*Finally we set compare register A to this value*/
sei(); /*Enable back the interrupts*/
}
/* Function For 10 ms Delay*/
void Task_10ms(void)
{
Read_Button();
}
void Read_Button(void)
{
unsigned char Button_State = digitalRead(Button_Pin);/*monitor the button state*/
if (Button_State==HIGH)
{
Task_100ms();
}
else
{
LED_OFF();
}
}
/* Function For 100 ms Delay*/
void Task_100ms(void)
{
LED_ON();
}
void LED_ON()
{
digitalWrite(RED_LED,HIGH);
}
void LED_OFF(void)
{
digitalWrite(RED_LED,LOW);
}