Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Some help to understand this encoder timer tick code

Status
Not open for further replies.

kasunt

Newbie level 4
Joined
Jan 14, 2008
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,321
Could somebody please please help me understand the following code. Its running on atmega128 @ 16 Mhz. Its reading an encoder pulse. Uses timer3. The encoder CHA is connected to PIND2 (INT2) which gets triggered whenever there is an interrupt from low to high. CHB of the encoder is connected to PINE7. This has been written for a 512 CPR 2 channels encoder.

I cant quite understand why TCNT3>>3 in this case.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
SIGNAL(SIG_INTERRUPT2) 
{ 
   timer3_stop(); 
   Right_Odometer_Period = TCNT3>>3; 
   right_overflow_occurred = timer3_overflowed; 
   timer3_start(); 
    
   if ((PINE & 0x80) != 0) 
   { 
      right_odometer++; 
      right_up = 1; 
   } 
   else 
   { 
      right_odometer--; 
      right_up = 0; 
      Right_Odometer_Period = Right_Odometer_Period * -1; 
   } 
   Right_Velocity = Right_Odometer_Period; 
   averaging_array[array_ptr++] = Right_Odometer_Period; 
   if (array_ptr == 32) 
   { 
      arraysum = 0; 
      for (int i = 0; i<32; i++) 
      { 
       arraysum += averaging_array[i]; 
      } 
      average = arraysum>>5; 
      array_ptr = 0; 
   } 
} 
 
void timer3_init(void) 
{ 
   TCCR3A = 0x00; 
   TCNT3 = 0X0000; 
   ETIMSK = (ETIMSK | (1<<TOIE3)); 
   timer3_start(); 
} 
 
void timer3_start(void) 
{ 
   TCNT3 = 0X0000; 
   timer3_overflowed = 0; 
   TCCR3B = (1<<CS30)|(0<<CS31)|(0<<CS32); 
} 
 
void timer3_stop(void) 
{ 
   TCCR3B &= ~(1<<CS30)|(1<<CS31)|(1<<CS32); 
} 
 
SIGNAL(SIG_OVERFLOW3) 
{ 
   TCNT3 = 0X0000; 
   Right_Velocity = 0; 
   timer3_overflowed = 1;    
}

 

Sorry If the information is elementar for you, but did you realized that this shift performs a division by eight ?

+++
 

Yeah I realize that. But not sure why its a division of 8. Thats what I cant understand.
 

if variable averaging_array have magnitude of 8bit size, the division makes sense to result fit inside, due this Timer seems to be 16bit size.
( check rest of program where it is declared )

+++
 

averaging array is an int as well.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top