Attiny 13 input 'ON' time

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm back with my older project (Attiny 2313) but now i've changed the MCU for Attiny13A. 8Mhz, 8 divider, 8 prescaler, timer0.

This project is about checking the input(PB4) ON time coming from a mechanical switch, the time is not critical.
If the switch is pressed less then 200ms than should fired the output(PB3)and do not permit for new sample until the timing ends, after timing ends should not permit taking new sample at least 2s. ...else if the time is more than 350ms should turn off all outputs.
My problem is that the long timing is never checked only after the short timing is end doesn't matter if i hold the switch a longer time.And if i eliminate the status check from the longtime if(Status==0) then the short time will not be checked.

#include 
#include  

// turn signal input left
#define sw_on   PINB & (1<<4)
#define sw_off   ~PINB & (1<<4)

// turn signal output left
#define out_on  PORTB |= (1<<3)
#define out_off   PORTB &= ~(1<<3)

unsigned char LongTiming=0; 
unsigned char ShortTiming=0;
unsigned char Status=0; 

volatile unsigned char sw_time;  
volatile unsigned char count_no; 
volatile unsigned char off_time; 
volatile unsigned char on_time;

ISR(TIM0_OVF_vect)
{
volatile unsigned char CountTime = 0; 

   TCNT0 += 6; // Adjust count so we get 250 uS rollover period
	
   if (++CountTime == 25) //50 ms
   {
     CountTime = 0;

	 sw_time++; // 
	count_no++;
	off_time++;
	on_time++;
  }
   }

// main 
   int main (void)
{
// set port direction
DDRB |= (1 << 3); // output
DDRB |= (0 << 4); // input 

 // Setup Timer 0 
   TCCR0B |= (1 << CS01); // 8 divider
   TCNT0 = 0;            // Initial value 

   TIMSK0 |= (1 << TOIE0);      // Timer 0 Interrupt

  sei(); //  Enable global interrupts

	  for (;;)
	  
   {
   // main code

if (sw_on) // switch on
{

if (on_time > 1) // 50ms debounce
{

 if ((sw_time > 1) && (sw_time < 4)) // ShortTiming: 200ms
{
if (Status==0) // check for new sample status 
{
count_no=0; // reset counter
ShortTiming=1;// short timing activated
}
}

if  ((sw_time > 7) && (Status==0)) // LongTiming: 350ms
{
LongTiming=1; // long timing activated
ShortTiming=0; // reset short timing
}
off_time=0; // reset off time
}
}

// reset 
if (sw_off) // switch off
{
on_time=0; //debounce reset
sw_time = 0; // reset switch time 

// delay taking new sample
if (off_time==14) // delay reset mode 
{
Status=0; // reset sample status
LongTiming=0; // reset long timing
}
}

// compare 
if (ShortTiming==1) 
{
out_on; // output active
Status=1; // set NO sample status

if (count_no == 40) // 2000ms/2s 
{
out_off; // output is off
ShortTiming=0; // short timing reset
}
}
else 
{
out_off; // output is off
}

}
}
// END MAIN
Last Edited: Mon. Nov 14, 2011 - 02:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am having trouble reading your code as it is set out fairly poorly. Perhaps set it out a bit better, align open & closing braces etc.
Try defining your variable used in the ISR CountTime as volatile as well.
Also use the timer in CTC mode. because it will not be very accurate as there is a fair bit of ISR overhead before you initialize it.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you have shared variables between the main line code and the isr that are greater than one byte in size, therefore you will have atomicity issues. You haven't read my tutorial have you? See the tutorial section for the 'traps when using interrupts'.

You probably want to rewrite your code as a finite state machine - much easier to understand.

Sampling the input once doesn't count as debouncing!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

do mean some like this:

volatile unsigned char sw_time; 
volatile unsigned char sw_time1;

ISR(TIM0_OVF_vect)
{
volatile unsigned char CountTime = 0;

   TCNT0 += 6; // Adjust count so we get 250 uS rollover period
   
   if (++CountTime == 25) //50 ms
   {
     CountTime = 0;

    sw_time++;
  }
   } 

int main (void)
{

cli();

 sw_time1=sw_time;

sei();

}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sw_time1 doesn't need to be volatile in this instance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Count_Time needs to be static to keep the value between subsequent ISR's.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

unfortunately doesn't work passing variable in cli(); sei();

int main (void) 
{ 

cli(); 

 sw_time1=sw_time; 
count_no1=count_no;
 sw_time1= sw_time; 

sei(); 
for(;;)
{

}

} 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

doesn't work

Define "doesn't work". If the above really is the entire program the optimizer will likely discard it and no code would be generated to update the variables. Also you are doing it OUTSIDE of the infinite loop so it will only do it once (in microseconds) when power is first applied - then never again.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i've tried inside loop but noting. the reference value will never be reached ... is like the counter remain with the last value or '0' and never increase.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

is like the counter remain with the last value or '0' and never increase.

Like I say the optimzer will have eaten you code - the variable will not be seen to change as no code has been generated to change it. Try checking the .lss - I bet it's an empty RJMP loop.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ISR(TIM0_OVF_vect)
{
volatile unsigned char CountTime = 0;

   TCNT0 += 6; // Adjust count so we get 250 uS rollover period
   
   if (++CountTime == 25) //50 ms

"CountTime" will never reach 25.
It will be cleared every time when entering ISR.

It should be

ISR(TIM0_OVF_vect)
{
static unsigned char CountTime;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i used in my project.

static unsigned char CountTime;

that's work but isn't passing the variable values.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@
mzchristensen

Yes, You have the CODE tags sorted out.
Now the next thing to do is to have the indentation done correctly in the source code. When it is and then you paste it & use the CODE tags the formatting is maintained and the code is soooooo much easier to read.
In fact I don't bother reading code unless it is formatted!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@
mzchristensen

Yes, You have the CODE tags sorted out.
Now the next thing to do is to have the indentation done correctly in the source code. When it is and then you paste it & use the CODE tags the formatting is maintained and the code is soooooo much easier to read.
In fact I don't bother reading code unless it is formatted!
I have reformatted your main as follows, but I found an extra closing brace at the end , so I gave it the flick.

// main 
int main (void) 
{ 
	// set port direction 
	DDRB |= (1 << 3); // output 
	DDRB |= (0 << 4); // input 

	// Setup Timer 0 
    TCCR0B |= (1 << CS01); // 8 divider 
    TCNT0 = 0;            // Initial value 

    TIMSK0 |= (1 << TOIE0);      // Timer 0 Interrupt 

    sei(); //  Enable global interrupts 

    for (;;) 
    { 
		// main code 
		if (sw_on) // switch on 
		{ 

			if (on_time > 1) // 50ms debounce 
			{ 
				if ((sw_time > 1) && (sw_time < 4)) // ShortTiming: 200ms 
				{ 
				if (Status==0) // check for new sample status 
				{ 
					count_no=0; // reset counter 
					ShortTiming=1;// short timing activated 
				} 
			} 
			if  ((sw_time > 7) && (Status==0)) // LongTiming: 350ms 
			{ 
				LongTiming=1; // long timing activated 
				ShortTiming=0; // reset short timing 
			} 
			off_time=0; // reset off time 
		}	 
	} 

	// reset 
	if (sw_off) // switch off 
	{ 
		on_time=0; //debounce reset 
		sw_time = 0; // reset switch time 

		// delay taking new sample 
		if (off_time==14) // delay reset mode 
		{ 
			Status=0; // reset sample status 
			LongTiming=0; // reset long timing 
		} 
	} 
	
	// compare 
	if (ShortTiming==1) 
	{ 
		out_on; // output active 
		Status=1; // set NO sample status 

		if (count_no == 40) // 2000ms/2s 
		{ 
			out_off; // output is off 
			ShortTiming=0; // short timing reset 
		} 
	} 
	else 
	{ 
		out_off; // output is off 
	} 
  
} // END MAIN

The only time to do the proper formatting is when you are writing the code!

Quote:
that's work but isn't passing the variable values.

What is doing the passing & what is doing the receiving & where is the prototype?

Quote:
unfortunately doesn't work passing variable in cli(); sei();

cli(); & sei(); have no arguments to pass!

If I was you, I would change the ISR (to a CTC) and just set a flag in the ISR....Just one statement. Nice and short as all good ISR's should be.

TIM0_COMPA_vect
{
   timer_flag=1;  //flag to main that ISR has ocurred 
}

Then you service that flag in main.

  if(timer_flag)
   {
      timer_flag=0;
      //do what you have to do

   } 

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've rearranged the code:

#include  
#include  

#define sw_on   PINB & (1<<4) 
#define sw_off   ~PINB & (1<<4) 

#define out_on  PORTB |= (1<<3) 
#define out_off   PORTB &= ~(1<<3) 

unsigned char LongTiming=0; 
unsigned char ShortTiming=0; 
unsigned char Status=0; 

volatile unsigned char sw_time;  
volatile unsigned char count_no; 
volatile unsigned char off_time; 
volatile unsigned char on_time; 

volatile unsigned char sw_time1;  
volatile unsigned char count_no1; 
volatile unsigned char off_time1; 
volatile unsigned char on_time1; 

ISR(TIM0_OVF_vect) 
{ 
static unsigned char CountTime = 0; 

   TCNT0 += 6; // Adjust count so we get 250 uS rollover period 
    
   if (++CountTime == 25) //50 ms 
   { 
     CountTime = 0; 

   sw_time++;
   count_no++; 
   off_time++; 
   on_time++; 
  } 
} 

// main 
   int main (void) 
{ 
// set port direction 
DDRB |= (1 << 3); // output 
DDRB |= (0 << 4); // input 

 // Setup Timer 0 
   TCCR0B |= (1 << CS01); // 8 divider 
   TCNT0 = 0;            // Initial value 

   TIMSK0 |= (1 << TOIE0);      // Timer 0 Interrupt 

     for (;;) 
     
   { 
cli();

	sw_time1=sw_time;  
	count_no1=count_no; 
	off_time1=off_time; 
	on_time1=on_time; 

sei(); //  Enable global interrupts 
   // main code 


if (sw_on) // switch on 
{ 
off_time1=0; // reset off time 
	if ((on_time1 > 1) && (Status==0))// 50ms debounce 
	{ 
 		if ((sw_time1 > 1) && (sw_time1 < 4)) // ShortTiming: 200ms 
		{ 
			if (sw_time1 > 7)  // check for new sample status 
			{ 
			LongTiming=1; // long timing activated 
			ShortTiming=0; // reset short timing 

		} else
			{
			count_no1=0; // reset counter 
			ShortTiming=1;// short timing activated
			}
		} 
	} 
} 

// reset 
if (sw_off) // switch off 
{ 
	on_time1=0; //debounce reset 
	sw_time1 = 0; // reset switch time 

	// delay taking new sample 
	if (off_time1==13) // delay reset mode 
	{ 
		Status=0; // reset sample status 
		LongTiming=0; // reset long timing 
	} 
} 

// compare 
if (ShortTiming==1) 
{ 
	out_on; // output active 
	Status=1; // set NO sample status 

	if (count_no1 == 40) // 2000ms/2s 
	{ 
		out_off; // output is off 
		ShortTiming=0; // short timing reset 
	} 
} 
else 
{ 
	out_off; // output is off 
} 

} //end for(;;) 

} 
// END MAIN 

@clawson here is the .lss, sometimes is working, but most of the case isn't.

test_blink2.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0000013a  00000000  00000000  00000074  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .bss          0000000c  00800060  00800060  000001ae  2**0
                  ALLOC
  2 .debug_aranges 00000020  00000000  00000000  000001ae  2**0
                  CONTENTS, READONLY, DEBUGGING
  3 .debug_pubnames 000000bc  00000000  00000000  000001ce  2**0
                  CONTENTS, READONLY, DEBUGGING
  4 .debug_info   0000016b  00000000  00000000  0000028a  2**0
                  CONTENTS, READONLY, DEBUGGING
  5 .debug_abbrev 0000007f  00000000  00000000  000003f5  2**0
                  CONTENTS, READONLY, DEBUGGING
  6 .debug_line   00000172  00000000  00000000  00000474  2**0
                  CONTENTS, READONLY, DEBUGGING
  7 .debug_frame  00000030  00000000  00000000  000005e8  2**2
                  CONTENTS, READONLY, DEBUGGING
  8 .debug_str    000000fd  00000000  00000000  00000618  2**0
                  CONTENTS, READONLY, DEBUGGING

Disassembly of section .text:

00000000 <__vectors>:
   0:	09 c0       	rjmp	.+18     	; 0x14 <__ctors_end>
   2:	16 c0       	rjmp	.+44     	; 0x30 <__bad_interrupt>
   4:	15 c0       	rjmp	.+42     	; 0x30 <__bad_interrupt>
   6:	15 c0       	rjmp	.+42     	; 0x32 <__vector_3>
   8:	13 c0       	rjmp	.+38     	; 0x30 <__bad_interrupt>
   a:	12 c0       	rjmp	.+36     	; 0x30 <__bad_interrupt>
   c:	11 c0       	rjmp	.+34     	; 0x30 <__bad_interrupt>
   e:	10 c0       	rjmp	.+32     	; 0x30 <__bad_interrupt>
  10:	0f c0       	rjmp	.+30     	; 0x30 <__bad_interrupt>
  12:	0e c0       	rjmp	.+28     	; 0x30 <__bad_interrupt>

00000014 <__ctors_end>:
  14:	11 24       	eor	r1, r1
  16:	1f be       	out	0x3f, r1	; 63
  18:	cf e9       	ldi	r28, 0x9F	; 159
  1a:	cd bf       	out	0x3d, r28	; 61

0000001c <__do_clear_bss>:
  1c:	10 e0       	ldi	r17, 0x00	; 0
  1e:	a0 e6       	ldi	r26, 0x60	; 96
  20:	b0 e0       	ldi	r27, 0x00	; 0
  22:	01 c0       	rjmp	.+2      	; 0x26 <.do_clear_bss_start>

00000024 <.do_clear_bss_loop>:
  24:	1d 92       	st	X+, r1

00000026 <.do_clear_bss_start>:
  26:	ac 36       	cpi	r26, 0x6C	; 108
  28:	b1 07       	cpc	r27, r17
  2a:	e1 f7       	brne	.-8      	; 0x24 <.do_clear_bss_loop>
  2c:	2e d0       	rcall	.+92     	; 0x8a 
2e: 83 c0 rjmp .+262 ; 0x136 <_exit> 00000030 <__bad_interrupt>: 30: e7 cf rjmp .-50 ; 0x0 <__vectors> 00000032 <__vector_3>: volatile unsigned char count_no1; volatile unsigned char off_time1; volatile unsigned char on_time1; ISR(TIM0_OVF_vect) { 32: 1f 92 push r1 34: 0f 92 push r0 36: 0f b6 in r0, 0x3f ; 63 38: 0f 92 push r0 3a: 11 24 eor r1, r1 3c: 8f 93 push r24 static unsigned char CountTime = 0; TCNT0 += 6; // Adjust count so we get 250 uS rollover period 3e: 82 b7 in r24, 0x32 ; 50 40: 8a 5f subi r24, 0xFA ; 250 42: 82 bf out 0x32, r24 ; 50 if (++CountTime == 25) //50 ms 44: 80 91 63 00 lds r24, 0x0063 48: 8f 5f subi r24, 0xFF ; 255 4a: 80 93 63 00 sts 0x0063, r24 4e: 89 31 cpi r24, 0x19 ; 25 50: b1 f4 brne .+44 ; 0x7e <__SREG__+0x3f> { CountTime = 0; 52: 10 92 63 00 sts 0x0063, r1 sw_time++; 56: 80 91 68 00 lds r24, 0x0068 5a: 8f 5f subi r24, 0xFF ; 255 5c: 80 93 68 00 sts 0x0068, r24 count_no++; 60: 80 91 64 00 lds r24, 0x0064 64: 8f 5f subi r24, 0xFF ; 255 66: 80 93 64 00 sts 0x0064, r24 off_time++; 6a: 80 91 65 00 lds r24, 0x0065 6e: 8f 5f subi r24, 0xFF ; 255 70: 80 93 65 00 sts 0x0065, r24 on_time++; 74: 80 91 6b 00 lds r24, 0x006B 78: 8f 5f subi r24, 0xFF ; 255 7a: 80 93 6b 00 sts 0x006B, r24 } } 7e: 8f 91 pop r24 80: 0f 90 pop r0 82: 0f be out 0x3f, r0 ; 63 84: 0f 90 pop r0 86: 1f 90 pop r1 88: 18 95 reti 0000008a
: // main int main (void) { // set port direction DDRB |= (1 << 3); // output 8a: bb 9a sbi 0x17, 3 ; 23 DDRB |= (0 << 4); // input 8c: 87 b3 in r24, 0x17 ; 23 8e: 87 bb out 0x17, r24 ; 23 // Setup Timer 0 TCCR0B |= (1 << CS01); // 8 divider 90: 83 b7 in r24, 0x33 ; 51 92: 82 60 ori r24, 0x02 ; 2 94: 83 bf out 0x33, r24 ; 51 TCNT0 = 0; // Initial value 96: 12 be out 0x32, r1 ; 50 TIMSK0 |= (1 << TOIE0); // Timer 0 Interrupt 98: 89 b7 in r24, 0x39 ; 57 9a: 82 60 ori r24, 0x02 ; 2 9c: 89 bf out 0x39, r24 ; 57 9e: 20 91 62 00 lds r18, 0x0062 a2: 90 91 61 00 lds r25, 0x0061 a6: 01 c0 rjmp .+2 ; 0xaa <__stack+0xb> a8: 21 e0 ldi r18, 0x01 ; 1 for (;;) { cli(); aa: f8 94 cli sw_time1=sw_time; ac: 80 91 68 00 lds r24, 0x0068 b0: 80 93 6a 00 sts 0x006A, r24 count_no1=count_no; b4: 80 91 64 00 lds r24, 0x0064 b8: 80 93 69 00 sts 0x0069, r24 off_time1=off_time; bc: 80 91 65 00 lds r24, 0x0065 c0: 80 93 67 00 sts 0x0067, r24 on_time1=on_time; c4: 80 91 6b 00 lds r24, 0x006B c8: 80 93 66 00 sts 0x0066, r24 sei(); // Enable global interrupts cc: 78 94 sei // main code if (sw_on) // switch on ce: b4 9b sbis 0x16, 4 ; 22 d0: 19 c0 rjmp .+50 ; 0x104 <__stack+0x65> { off_time1=0; // reset off time d2: 10 92 67 00 sts 0x0067, r1 if ((on_time1 > 1) && (Status==0))// 50ms debounce d6: 80 91 66 00 lds r24, 0x0066 da: 82 30 cpi r24, 0x02 ; 2 dc: 98 f0 brcs .+38 ; 0x104 <__stack+0x65> de: 22 23 and r18, r18 e0: 89 f4 brne .+34 ; 0x104 <__stack+0x65> { if ((sw_time1 > 1) && (sw_time1 < 4)) // ShortTiming: 200ms e2: 80 91 6a 00 lds r24, 0x006A e6: 82 30 cpi r24, 0x02 ; 2 e8: 68 f0 brcs .+26 ; 0x104 <__stack+0x65> ea: 80 91 6a 00 lds r24, 0x006A ee: 84 30 cpi r24, 0x04 ; 4 f0: 48 f4 brcc .+18 ; 0x104 <__stack+0x65> { if (sw_time1 > 7) // check for new sample status f2: 80 91 6a 00 lds r24, 0x006A f6: 88 30 cpi r24, 0x08 ; 8 f8: 10 f0 brcs .+4 ; 0xfe <__stack+0x5f> fa: 90 e0 ldi r25, 0x00 ; 0 fc: 03 c0 rjmp .+6 ; 0x104 <__stack+0x65> LongTiming=1; // long timing activated ShortTiming=0; // reset short timing } else { count_no1=0; // reset counter fe: 10 92 69 00 sts 0x0069, r1 102: 91 e0 ldi r25, 0x01 ; 1 } } } // reset if (sw_off) // switch off 104: b4 99 sbic 0x16, 4 ; 22 106: 09 c0 rjmp .+18 ; 0x11a <__stack+0x7b> { on_time1=0; //debounce reset 108: 10 92 66 00 sts 0x0066, r1 sw_time1 = 0; // reset switch time 10c: 10 92 6a 00 sts 0x006A, r1 // delay taking new sample if (off_time1==13) // delay reset mode 110: 80 91 67 00 lds r24, 0x0067 114: 8d 30 cpi r24, 0x0D ; 13 116: 09 f4 brne .+2 ; 0x11a <__stack+0x7b> 118: 20 e0 ldi r18, 0x00 ; 0 LongTiming=0; // reset long timing } } // compare if (ShortTiming==1) 11a: 91 30 cpi r25, 0x01 ; 1 11c: 51 f4 brne .+20 ; 0x132 <__stack+0x93> { out_on; // output active 11e: c3 9a sbi 0x18, 3 ; 24 Status=1; // set NO sample status if (count_no1 == 40) // 2000ms/2s 120: 80 91 69 00 lds r24, 0x0069 124: 88 32 cpi r24, 0x28 ; 40 126: 09 f0 breq .+2 ; 0x12a <__stack+0x8b> 128: bf cf rjmp .-130 ; 0xa8 <__stack+0x9> { out_off; // output is off 12a: c3 98 cbi 0x18, 3 ; 24 12c: 21 e0 ldi r18, 0x01 ; 1 12e: 90 e0 ldi r25, 0x00 ; 0 130: bc cf rjmp .-136 ; 0xaa <__stack+0xb> ShortTiming=0; // short timing reset } } else { out_off; // output is off 132: c3 98 cbi 0x18, 3 ; 24 134: ba cf rjmp .-140 ; 0xaa <__stack+0xb> 00000136 <_exit>: 136: f8 94 cli 00000138 <__stop_program>: 138: ff cf rjmp .-2 ; 0x138 <__stop_program>
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mzchristensen wrote:
If the switch is pressed less then 200ms than should fired the output(PB3)and do not permit for new sample until the timing ends, after timing ends should not permit taking new sample at least 2s. ...else if the time is more than 350ms should turn off all outputs.

The hardest thing should be to clarify this specification.

1.
What means "less then 200ms"?
You must also name the shortest width.

2.
What happens in between 200ms..350ms?

I would simple execute the mainloop with a constant delay of 10ms:
_delay_ms(10)
And then use a counter, how many time was over.

It seems, your device was intended to separate kids from older people.
Since older people often unable to press shorter than 200ms.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This project is about a turn signal control for my motorcycle. called Comfort blinker or one touch turn signal..
If i activate the turn lever for less then a complete ON time than release the turn lever, the timed flash mode will be activated, will flash 3 times(3 ON) else will work normal, it flash till the turn lever will be released.

full ON ~350ms.
full OFF ~310ms.

I've made a timing diagram

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i found the solution for

cli(); 

   sw_time1=sw_time;  
   count_no1=count_no; 
   off_time1=off_time; 
   on_time1=on_time; 

sei(); //  Enable global interrupts 

i've always reset the

 sw_time1=0;  
   count_no1=0; 
   off_time1=0; 
   on_time1=0;

instead

 sw_time=0;  
   count_no=0; 
   off_time=0; 
   on_time=0;

but i'm still struggling with the switch ON time counting