1 | /*
|
2 | *
|
3 | *Frequency measurement
|
4 | *Speed measurement
|
5 | */
|
6 | #include "compiler.h"
|
7 | #include "tc.h"
|
8 | #include "gpio.h"
|
9 | #include "TimerCont.h"
|
10 |
|
11 |
|
12 | //Initialization of the function exact_geschwindigkeit
|
13 | void exact_geschwindigkeit_init()
|
14 | { //brief Configuration functionality on a pin.
|
15 |
|
16 |
|
17 | //Parameters when initializing a counter in capture mode.
|
18 | tc_capture_opt_t capture_opt=
|
19 | {
|
20 | .channel = TIMER_CONTR_TC_CHANNEL, // channel selection
|
21 | .ldra =1, // rising edge of TIOA
|
22 | .ldrb =2, // falling edge of TIOA
|
23 | .etrgedg =0, // external Trigger Edge Selection none
|
24 | .cpctrg =1, // RC Compare has no effect on the counter and its clock
|
25 | .tcclks =1, // clock Selection Timer_clok 1
|
26 | .abetrg =1, // TIOA is used as an external trigger
|
27 | .ldbdis =false, // Counter clock disable with RB loading
|
28 | .ldbstop = false, // Counter clock stopped with RB loading
|
29 | } ;
|
30 | //Sets options for counter capture initialization.
|
31 | tc_init_capture(TIMER_CONTR_TC, &capture_opt);
|
32 |
|
33 | //Starts a counter.
|
34 | volatile avr32_tc_t *tc=TIMER_CONTR_TC;
|
35 | tc_start(TIMER_CONTR_TC, TIMER_CONTR_TC_CHANNEL);
|
36 | //configuration input PIN
|
37 | static const gpio_map_t TC_GPIO_MAP =
|
38 | {
|
39 | {PIN_FOR_SPEED,AVR32_TC0_A2_3_FUNCTION }
|
40 |
|
41 | };
|
42 | gpio_enable_module(TC_GPIO_MAP, sizeof(TC_GPIO_MAP) / sizeof(TC_GPIO_MAP[0]));
|
43 | }
|
44 | //Function exact_geschwindigkeit
|
45 | exact_geschwindigkeit (float *x,float *ges)
|
46 | { volatile avr32_tc_t *tc=TIMER_CONTR_TC;
|
47 |
|
48 |
|
49 | //initializing an array
|
50 | int tab[3]={0,0,0};
|
51 | //Writing in Register A and Register B
|
52 | tc_read_tc(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL);
|
53 | tc_write_ra(TIMER_CONTR_TC, TIMER_CONTR_TC_CHANNEL, tc_read_tc(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL));
|
54 | tc_write_rb(TIMER_CONTR_TC, TIMER_CONTR_TC_CHANNEL, tc_read_tc(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL));
|
55 | //filling the array
|
56 | tab[0]=tc_read_ra(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL);
|
57 | tab[1]=tc_read_rb(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL);
|
58 | float Ku=3.0f,Ui=10.0f;
|
59 | //restart the counter
|
60 | //Speed calculation
|
61 | *ges=Ku*Ui*(tab[1]-tab[0])/(tab[2]-tab[0]);
|
62 | //reinitialization of the array
|
63 | tab[2]=tab[0];
|
64 | *x=tc_read_tc(TIMER_CONTR_TC,TIMER_CONTR_TC_CHANNEL);
|
65 | }
|