Revisão 53
frequência calculada
myTimer.c | ||
---|---|---|
12 | 12 |
#include <stdint.h> |
13 | 13 |
|
14 | 14 |
#define SYSCLK 80000000L |
15 |
#define MYPBCLK 40000000 |
|
15 | 16 |
#define Internal_Clock_Period 1 |
16 | 17 |
#define Prescaler_Rate 1 // 1:1, 1:8, 1:64, 1:256 |
17 | 18 |
#define NRRATES 4 |
19 |
#define MAX_PR1 (2^16-1) |
|
18 | 20 |
|
19 | 21 |
/** |
20 | 22 |
* Inicializa o timer. |
21 | 23 |
* @param tempo Tempo em segundos. |
22 |
* @return 0 = sucesso; -1 = erro |
|
24 |
* @return 0 = sucesso; |
|
25 |
* -1 = erro; |
|
26 |
* -2 = não há valores válidos nem para prescaler nem para PR1 |
|
23 | 27 |
*/ |
24 | 28 |
int initialize(int tempo) { |
25 |
//2 x Internal_Clock_Period x Prescaler_Rate |
|
26 |
|
|
27 |
int prescaler_rates[] = {1,8,64,256}; |
|
29 |
int prescaler_rates[] = {1, 8, 64, 256}; |
|
28 | 30 |
//prescaler_rates_hex[] = {0x00,0x01,0x02,0x03}; |
29 |
int prescaler = 0;
|
|
30 |
int freq_aux;
|
|
31 |
int prescaler = -1;
|
|
32 |
double pr1_aux;
|
|
31 | 33 |
|
32 | 34 |
if (tempo == 0) { |
33 | 35 |
return -1; //ERRO |
34 | 36 |
} |
35 | 37 |
|
36 | 38 |
double frequencia = 1 / tempo; |
39 |
int i = 0; |
|
37 | 40 |
|
38 |
for(int i = (NRRATES - 1); i > 0 ; i++){ |
|
39 |
freq_aux = (int) (SYSCLK / prescaler_rates[i]); |
|
40 |
if(freq_aux > frequencia){ |
|
41 |
T1CONbits.TCS = 0x0; //PBCLK |
|
42 |
|
|
43 |
for (i = (NRRATES - 1); i > 0; i++) { |
|
44 |
|
|
45 |
pr1_aux = frequencia * prescaler_rates[i] / MYPBCLK; |
|
46 |
|
|
47 |
if (pr1_aux < MAX_PR1) { |
|
41 | 48 |
prescaler = i; |
42 | 49 |
break; |
43 | 50 |
} |
44 | 51 |
} |
45 | 52 |
|
53 |
//nao ha valores validos nem para prescaler nem para PR1 |
|
54 |
if(prescaler == -1){ |
|
55 |
return -2; |
|
56 |
} |
|
46 | 57 |
|
47 | 58 |
//timer 1 (tem 16 bits) |
48 | 59 |
TMR1 = 0x00; |
49 |
PR1 = 0xFFFF; // meter a conversao do tempo...
|
|
60 |
PR1 = 0xFFFF; // meter a conversao do tempo... |
|
50 | 61 |
|
51 | 62 |
// 00 = 1, 01 = 8, 10 = 64, 11 = 256 |
52 |
switch(prescaler){
|
|
63 |
switch (prescaler) {
|
|
53 | 64 |
case 0:T1CONbits.TCKPS = 0x00; |
54 | 65 |
break; |
55 | 66 |
case 1:T1CONbits.TCKPS = 0x01; |
56 | 67 |
break; |
57 |
case 2:T1CONbits.TCKPS = 0x02;
|
|
68 |
case 2:T1CONbits.TCKPS = 0x02; |
|
58 | 69 |
break; |
59 | 70 |
case 3: T1CONbits.TCKPS = 0x03; |
60 | 71 |
break; |
... | ... | |
62 | 73 |
break; |
63 | 74 |
} |
64 | 75 |
|
65 |
|
|
76 |
//enable das interrupções |
|
77 |
IFS0bits.T1IF = 0x1; // bit 4 |
|
78 |
IEC0bits.T1IE = 0x1; // bit 4 |
|
79 |
IPC1bits.T1IP = 0x1; // bits prioridade = 4:2 |
|
80 |
IPC1bits.T1IS = 0x1; // bits 1:0 |
|
81 |
|
|
82 |
|
|
66 | 83 |
return 0; |
67 | 84 |
} |
68 | 85 |
|
... | ... | |
73 | 90 |
T1CONbits.TSYNC = 0x0; |
74 | 91 |
T1CONbits.ON = 0x1; |
75 | 92 |
T1CONSET = 0x8000; |
93 |
|
|
94 |
TRISD = 0x00; //saida, serve de led |
|
95 |
LATD = 0x00; |
|
76 | 96 |
} |
77 | 97 |
|
78 | 98 |
/** |
... | ... | |
105 | 125 |
* @param priority Atribui prioridade. |
106 | 126 |
* @param subpriority Atribui subprioridade. |
107 | 127 |
*/ |
108 |
void __ISR()interrupt1(void) { |
|
109 |
/* Interrupt Service Routine code goes here */ |
|
128 |
//void __ISR()interrupt1(void) { |
|
110 | 129 |
|
130 |
void __attribute__((interrupt, no_auto_psv)) _ISR(void) { |
|
131 |
|
|
132 |
//desliga interrupcoes |
|
111 | 133 |
IFS0bits.T1IF = 0x0; // bit 4 |
112 |
IEC0bits.T1IE = 0x1; // bit 4 |
|
113 |
IPC1bits.T1IP = 0x1; // bits prioridade = 4:2 |
|
114 |
IPC1bits.T1IS = 0x1; // bits 1:0 |
|
134 |
IEC0bits.T1IE = 0x0; // bit 4 |
|
115 | 135 |
|
116 | 136 |
IFS0CLR = 0x00000010; //Clear Timer1 interrupt flag |
137 |
|
|
138 |
TRISD = 0x00; //saida, serve de led |
|
139 |
LATD = 0xFF; |
|
140 |
|
|
141 |
//liga interrupções |
|
142 |
IFS0bits.T1IF = 0x1; // bit 4 |
|
143 |
IEC0bits.T1IE = 0x1; // bit 4 |
|
117 | 144 |
} |
Também disponível em: Unified diff