-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path05_main_couting_debouncer.c
More file actions
186 lines (155 loc) · 4.58 KB
/
05_main_couting_debouncer.c
File metadata and controls
186 lines (155 loc) · 4.58 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* 05_main_couting_deboucer.c
*
* Created on: Mar 27, 2020
* Author: Renan Augusto Starke
* Instituto Federal de Santa Catarina
*
* Exemplo de debouce de botão por contagem.
* Faz a verificação de botão periodicamente utilizando
* o comparador 0 do Timer A0
*
* Recomenda-se que o período de amostragem esteja entre 1 a 10ms
* com N=12 amostras para deteção do botão. Ajustar os tempos conforme
* necessário.
*
*
* .
* /|\ +
* | +
* | +
* | + <-- Comparador 0 (TACCR0 -> TIMER0_A0_VECTOR) - Debouncer do botão
* | +
* | +
* | +
* | +
* | +
* | +
* +----------------------------------------->
*
* - Comparadores podem ser configurados para qualquer valor
* entre 0 e 65535. IRQs devem ser habilitadas individuais
* nos respectivos registradores.
*
*
* MSP430G25553
* -----------------
* /|\| XIN|-
* | | |
* --|RST XOUT|-
* | |
* BOTAO --> | P1.3 P1.0 | --> LED VERMELHO
* | P1.6 | --> LED VERDE
* | |
*
*/
#ifndef __MSP430G2553__
#error "Clock system not supported for this device"
#endif
/* System includes */
#include <msp430.h>
/* Project includes */
#include "lib/gpio.h"
#include "lib/bits.h"
#include "displays/simple_display_mux.h"
#define BUTTON_PORT P1
#define BUTTON BIT3
#define LED_PORT P1
#define LED_VM BIT0
#define LED_VD BIT6
#define BUTTON_SAMPLES (12)
/**
* @brief Configura sistema de clock para usar o Digitally Controlled Oscillator (DCO).
* Utililiza-se as calibrações internas gravadas na flash.
* Exemplo baseado na documentação da Texas: msp430g2xxx3_dco_calib.c
* Configura ACLK para utilizar VLO = ~10KHz
* @param none
*
* @retval none
*/
void init_clock_system(){
/* Configuração do MCLK em 8MHz */
/* Se calibração foi apagada, para aplicação */
if (CALBC1_8MHZ==0xFF)
while(1);
DCOCTL = 0;
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;
/* Configura ACLK para usar VLO: ~10kHZ */
BCSCTL3 |= LFXT1S_2;
}
/**
* @brief Configura temporizador A.
*
* @param none
*
* @retval none
*/
void config_timerA_0(){
/* Timer A0:
*
*
* TASSEL_2 -> Clock de SMCLK.
* MC_2 -> Contagem crescente.
* ID_3 -> Prescaler = /8
*/
TA0CTL = TASSEL_2 | MC_2 | ID_1;
/* IRQ por comparação entre contagem e comparador 0 */
TA0CCTL0 = CCIE;
/* Valor de comparação é 50000 */
TA0CCR0 = 20000;
}
int main(void)
{
volatile uint16_t my_data = 0;
/* Desliga watchdog imediatamente */
WDTCTL = WDTPW | WDTHOLD;
/* Configura botões e LEDs*/
/* LEDs como saída (nível alto). Botão como entrada (nível baixo) */
PORT_DIR(BUTTON_PORT) = LED_VM | LED_VD;
/* Ativação dos Resistores */
PORT_REN(BUTTON_PORT) = BUTTON;
/* Resistor de pull up */
PORT_OUT(BUTTON_PORT) = BUTTON;
/* Configurações de hardware */
config_timerA_0();
init_clock_system();
/* Entra em modo de economia de energia com IRQs habilitadas */
__bis_SR_register(LPM0_bits + GIE);
while (1){
/* Desligar CPU novamente */
__bis_SR_register(LPM0_bits);
}
}
/* ISR0 do Timer A: executado no evento de comparação comparador 0 (TACCR0)
*
* Utilizado para o debouncer por contagem: faz a verificação de botão
* periodicamente. Caso N amostras são estáveis, detecta-se que o botão foi
* pressionado.
*
* */
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) Timer_A (void)
#else
#error Compiler not supported!
#endif
{
static uint8_t counter = BUTTON_SAMPLES;
/* Debug: Piscar quando executar a ISR */
CPL_BIT(PORT_OUT(LED_PORT), LED_VD);
/* Se botão apertado: borda de descida */
if (!TST_BIT(PORT_IN(BUTTON_PORT), BUTTON)) {
/* Se contagem = 0, debounce terminado */
if (!(--counter)) {
/* Colocar aqui código da aplicação referente ao botão */
/* Acorda função main
__bic_SR_register_on_exit(LPM0_bits); */
CPL_BIT(PORT_OUT(LED_PORT), LED_VM);
}
}
else
counter = BUTTON_SAMPLES;
}