1
+ #include "main.h"
2
+ #include "Timers.h"
3
+
4
+ //Incializa estructura Tcfg de forma que T0_Init configure
5
+ //al TIMER0 con los valores de Reset (no modifica registros):
6
+ void T0_deInit (tT0config * Tcfg ) {
7
+ Tcfg -> modo = Normal ;
8
+ Tcfg -> pre = des ;
9
+ Tcfg -> OCA = sNo ;
10
+ Tcfg -> OCB = sNo ;
11
+ }
12
+ //Incializa estructura Tcfg de forma que T1_Init configure
13
+ //al TIMER1 con los valores de Reset (no modifica registros):
14
+ void T1_deInit (tT1config * Tcfg ) {
15
+ Tcfg -> modo = Normal1 ;
16
+ Tcfg -> pre = des ;
17
+ Tcfg -> OCA = sNo ;
18
+ Tcfg -> OCB = sNo ;
19
+ }
20
+ //Incializa estructura Tcfg de forma que T2_Init configure
21
+ //al TIMER2 con los valores de Reset (no modifica registros):
22
+ void T2_deInit (tT2config * Tcfg ) {
23
+ Tcfg -> modo = Normal ;
24
+ Tcfg -> pre = des2 ;
25
+ Tcfg -> OCA = sNo ;
26
+ Tcfg -> OCB = sNo ;
27
+ }
28
+
29
+ //Configura pines como salidas, y sus Modos de Salida: toggle, clear o set (1, 2 o 3):
30
+ void T0_Init (tT0config * Tcfg ) {
31
+ if (Tcfg -> OCA ) DDRD |=(1 <<DDB6 ); //OC0A es PD6 en Atmega328
32
+ if (Tcfg -> OCB ) DDRD |=(1 <<DDB5 ); //OC2B es PD5
33
+ //Los tipos enumerados son tipo int.
34
+ TCCR0A = (Tcfg -> OCA <<COM0A0 )|(Tcfg -> OCB <<COM0B0 )|(Tcfg -> modo & 0x03 ); //Configura Modo Operacional,..
35
+ TCCR0B = (((Tcfg -> modo )<<1 )& 8 )|Tcfg -> pre ; //..Prescaler y Modo de Salida en los dos registros TCCR.
36
+ }
37
+ void T1_Init (tT1config * Tcfg ) {
38
+ if (Tcfg -> OCA ) DDRB |=(1 <<DDB1 ); //OC1A es PB1
39
+ if (Tcfg -> OCB ) DDRB |=(1 <<DDB2 ); //OC1B es PB2
40
+ TCCR1A = (Tcfg -> OCA <<COM1A0 )|(Tcfg -> OCB <<COM1B0 )|(Tcfg -> modo & 0x03 );
41
+ TCCR1B = (((Tcfg -> modo )<<1 )& 0x18 )|(Tcfg -> pre );
42
+ }
43
+ void T2_Init (tT2config * Tcfg ) {
44
+ if (Tcfg -> OCA ) DDRB |=(1 <<DDB3 ); //OC2A es PB3
45
+ if (Tcfg -> OCB ) DDRD |=(1 <<DDB3 ); //OC2B es PD3
46
+ TCCR2A = (Tcfg -> OCA <<COM2A0 )|(Tcfg -> OCB <<COM2B0 )|(Tcfg -> modo & 0x03 );
47
+ TCCR2B = (((Tcfg -> modo )<<1 )& 8 )|Tcfg -> pre ;
48
+ }
49
+
50
+ //Habilitacion de interrupciones de software de los timers:
51
+ void T0_Interrupts (tT0INTconfig * T0i ) {
52
+ TIMSK0 = (T0i -> IT_compb )<<OCIE0B |(T0i -> IT_compa )<<OCIE0A |(T0i -> IT_ovf )<<TOIE0 ;
53
+ TIFR0 = (T0i -> IT_compb )<<OCF0B |(T0i -> IT_compa )<<OCF0A |(T0i -> IT_ovf )<<TOV0 ; //apagado de flag
54
+ }
55
+ void T1_Interrupts (tT1INTconfig * T1i ) {
56
+ TIMSK1 = (T1i -> IT_ic )<<ICIE1 |(T1i -> IT_compb )<<OCIE1B |(T1i -> IT_compa )<<OCIE1A |(T1i -> IT_ovf )<<TOIE1 ;
57
+ TIFR1 = (T1i -> IT_ic )<<ICF1 |(T1i -> IT_compb )<<OCF1B |(T1i -> IT_compa )<<OCF1A |(T1i -> IT_ovf )<<TOV1 ;
58
+ }
59
+ void T2_Interrupts (tT2INTconfig * T2i ) {
60
+ TIMSK2 = (T2i -> IT_compb )<<OCIE2B |(T2i -> IT_compa )<<OCIE2A |(T2i -> IT_ovf )<<TOIE2 ;
61
+ TIFR2 = (T2i -> IT_compb )<<OCF2B |(T2i -> IT_compa )<<OCF2A |(T2i -> IT_ovf )<<TOV2 ; //borra solo..
62
+ } //..los flags de int habilitadas
63
+
64
+ void configurarTimer0 (tT0y2modo modoOperacionT0 , tT0y1pre prescalerT0 , tTsalida modoSalidaA , tTsalida modoSalidaB ) {
65
+ T0_deInit (& T0cfg );
66
+ T0cfg .modo = modoOperacionT0 ;
67
+ #ifdef EstadoInicialActivado
68
+ T0cfg .pre = prescalerT0 ;
69
+ #endif
70
+ #ifdef EstadoInicialDesactivado
71
+ T0cfg .pre = des ; //TIMER0 comienza en Estado Desactivado
72
+ #endif
73
+ T0cfg .OCA = modoSalidaA ;
74
+ T0cfg .OCB = modoSalidaB ;
75
+ T0_Init (& T0cfg );
76
+ valorPrescaler__T0 = valorPrescalerT0y1 (prescalerT0 );
77
+ OCR0A = (uint16_t )( (ANCHO_PULSO_INICIAL_A /1000 )* (F_CPU /valorPrescaler__T0 ) + 1 ); //Servo A
78
+ OCR0B = (uint16_t )( (ANCHO_PULSO_INICIAL_G /1000 )* (F_CPU /valorPrescaler__T0 ) + 1 ); //Servo G
79
+ }
80
+ void configurarTimer1 (tT1modo modoOperacionT1 , tT0y1pre prescalerT1 , tTsalida modoSalidaA ,
81
+ tTsalida modoSalidaB ) {
82
+ /* Config Timer1:
83
+ T1cfg esta declarada en Timers.h como variable tipo tT1config
84
+ tT1config esta definida en Timers.h como una estructura con campos
85
+ de diversos tipos enumerados, lo que facilita su llenado.*/
86
+ T1_deInit (& T1cfg ); //Incializa estructura de forma que T1_Init configure al TIMER1 con los valores
87
+ //de Reset (buena practica), aunque no es necesario si llenamos todos los parametros.
88
+ T1cfg .modo = modoOperacionT1 ;
89
+ #ifdef EstadoInicialActivado
90
+ T1cfg .pre = prescalerT1 ;
91
+ #endif
92
+ #ifdef EstadoInicialDesactivado
93
+ T1cfg .pre = des ; //TIMER1 comienza en Estado Desactivado
94
+ #endif
95
+ T1cfg .OCA = modoSalidaA ;
96
+ T1cfg .OCB = modoSalidaB ;
97
+ // Hasta aqui solamente se ha cargado la variable T1cfg con datos, no se ha
98
+ // modificado ningun registro del Timer1 del micro.
99
+ T1_Init (& T1cfg ); // Llama a la funcion que va a configurar al Timer1 con los
100
+ // parametros cargados en T1cfg.
101
+ valorPrescaler__T1 = valorPrescalerT0y1 (prescalerT1 );
102
+ ICR1 = (uint16_t )( (T_ms /1000 )* (F_CPU /valorPrescaler__T1 ) + 1 ); //Controla Periodo de los PWM en Timer1
103
+ OCR1A = (uint16_t )( (ANCHO_PULSO_INICIAL /1000 )* (F_CPU /valorPrescaler__T1 ) + 1 ); //Servo B
104
+ OCR1B = (uint16_t )( (ANCHO_PULSO_INICIAL /1000 )* (F_CPU /valorPrescaler__T1 ) + 1 ); //Servo M
105
+ }
106
+ void configurarTimer2 (tT0y2modo modoOperacionT0y2 ,tT2pre prescalerT2 , tTsalida modoSalidaA ,
107
+ tTsalida modoSalidaB ) {
108
+ T2_deInit (& T2cfg );
109
+ T2cfg .modo = modoOperacionT0y2 ;
110
+ #ifdef EstadoInicialActivado
111
+ T2cfg .pre = prescalerT2 ;
112
+ #endif
113
+ #ifdef EstadoInicialDesactivado
114
+ T2cfg .pre = des2 ; //TIMER2 comienza en Estado Desactivado
115
+ #endif
116
+ T2cfg .OCA = modoSalidaA ;
117
+ T2cfg .OCB = modoSalidaB ;
118
+ T2_Init (& T2cfg );
119
+ valorPrescaler__T2 = valorPrescalerT2 (prescalerT2 );
120
+ OCR2A = (uint16_t )( (ANCHO_PULSO_INICIAL_C /1000 )* (F_CPU /valorPrescaler__T2 ) + 1 ); //Servo C
121
+ //OCR2B = (uint16_t)( (ANCHO_PULSO_INICIAL/1000)*(F_CPU/valorPrescaler__T2) +1 );
122
+ }
123
+ uint16_t valorPrescalerT0y1 (tT0y1pre prescalerT0y1 ) { //Valor numerico del prescaler para TIMER0 y TIMER1
124
+ switch (prescalerT0y1 ) {
125
+ case des :
126
+ return 0 ;
127
+ break ;
128
+ case prex1 :
129
+ return 1 ;
130
+ break ;
131
+ case prex8 :
132
+ return 8 ;
133
+ break ;
134
+ case prex64 :
135
+ return 64 ;
136
+ break ;
137
+ case prex256 :
138
+ return 256 ;
139
+ break ;
140
+ case prex1024 :
141
+ return 1024 ;
142
+ break ;
143
+ case Ext_Fneg :
144
+ return 0 ;
145
+ break ;
146
+ case Ext_Fpos :
147
+ return 0 ;
148
+ break ;
149
+ default :
150
+ return 0 ;
151
+ break ;
152
+ }
153
+ }
154
+ uint16_t valorPrescalerT2 (tT2pre prescalerT2 ) { //Valor numerico del prescaler para TIMER2
155
+ switch (prescalerT2 ) {
156
+ case des2 :
157
+ return 0 ;
158
+ break ;
159
+ case pre2x1 :
160
+ return 1 ;
161
+ break ;
162
+ case pre2x8 :
163
+ return 8 ;
164
+ break ;
165
+ case pre2x32 :
166
+ return 32 ;
167
+ break ;
168
+ case pre2x64 :
169
+ return 64 ;
170
+ break ;
171
+ case pre2x128 :
172
+ return 128 ;
173
+ break ;
174
+ case pre2x256 :
175
+ return 256 ;
176
+ break ;
177
+ case pre2x1024 :
178
+ return 1024 ;
179
+ break ;
180
+ default :
181
+ return 0 ;
182
+ break ;
183
+ }
184
+ }
0 commit comments