esp-2014-15 / tp1g8 / TimerLib.X / myTimer.c @ 51
Histórico | Ver | Anotar | Download (2,54 KB)
1 |
/*
|
---|---|
2 |
* File: myTimer.c
|
3 |
* Author: ana
|
4 |
*
|
5 |
* Created on 30 de Setembro de 2014, 18:01
|
6 |
*/
|
7 |
|
8 |
#include <xc.h> |
9 |
#include <plib.h> |
10 |
#include <stdio.h> |
11 |
#include <stdlib.h> |
12 |
#include <stdint.h> |
13 |
|
14 |
#define SYSCLK 80000000L |
15 |
#define Internal_Clock_Period 1 |
16 |
#define Prescaler_Rate 1 // 1:1, 1:8, 1:64, 1:256 |
17 |
#define NRRATES 4 |
18 |
|
19 |
/**
|
20 |
* Inicializa o timer.
|
21 |
* @param tempo Tempo em segundos.
|
22 |
* @return 0 = sucesso; -1 = erro
|
23 |
*/
|
24 |
int initialize(int tempo) { |
25 |
//2 x Internal_Clock_Period x Prescaler_Rate
|
26 |
|
27 |
int prescaler_rates[] = {1,8,64,256}; |
28 |
//prescaler_rates_hex[] = {0x00,0x01,0x02,0x03};
|
29 |
int prescaler = 0; |
30 |
int freq_aux;
|
31 |
|
32 |
if (tempo == 0) { |
33 |
return -1; //ERRO |
34 |
} |
35 |
|
36 |
double frequencia = 1 / tempo; |
37 |
|
38 |
for(int i = (NRRATES - 1); i > 0 ; i++){ |
39 |
freq_aux = (int) (SYSCLK / prescaler_rates[i]);
|
40 |
if(freq_aux > frequencia){
|
41 |
prescaler = i; |
42 |
break;
|
43 |
} |
44 |
} |
45 |
|
46 |
|
47 |
//timer 1 (tem 16 bits)
|
48 |
TMR1 = 0x00;
|
49 |
PR1 = 0xFFFF; // meter a conversao do tempo... |
50 |
|
51 |
// 00 = 1, 01 = 8, 10 = 64, 11 = 256
|
52 |
switch(prescaler){
|
53 |
case 0:T1CONbits.TCKPS = 0x00; |
54 |
break;
|
55 |
case 1:T1CONbits.TCKPS = 0x01; |
56 |
break;
|
57 |
case 2:T1CONbits.TCKPS = 0x02; |
58 |
break;
|
59 |
case 3: T1CONbits.TCKPS = 0x03; |
60 |
break;
|
61 |
default:T1CONbits.TCKPS = 0x00; |
62 |
break;
|
63 |
} |
64 |
|
65 |
|
66 |
return 0; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* O timer começa a contar.
|
71 |
*/
|
72 |
void start() {
|
73 |
T1CONbits.TSYNC = 0x0;
|
74 |
T1CONbits.ON = 0x1;
|
75 |
T1CONSET = 0x8000;
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* O timer pára de contar.
|
80 |
*/
|
81 |
void stop() {
|
82 |
T1CONbits.ON = 0x0;
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* Isto é para apagar...
|
87 |
* @param flag Activa a flag. (valor 0 ou 1)
|
88 |
* @param enable Activa interrupção. (valor 0 ou 1)
|
89 |
* @param priority Atribui prioridade.
|
90 |
* @param subpriority Atribui subprioridade.
|
91 |
* @return 0 = sucesso
|
92 |
*/
|
93 |
int setInterrupts(short flag, short enable, short priority, short subpriority) { |
94 |
IFS0bits.T1IF = flag; // bit 4
|
95 |
IEC0bits.T1IE = enable; // bit 4
|
96 |
IPC1bits.T1IP = priority; // bits 4:2
|
97 |
IPC1bits.T1IS = subpriority; // bits 1:0
|
98 |
return 0; |
99 |
} |
100 |
|
101 |
/**
|
102 |
*
|
103 |
* @param flag Activa a flag. (valor 0 ou 1)
|
104 |
* @param enable Activa interrupção. (valor 0 ou 1)
|
105 |
* @param priority Atribui prioridade.
|
106 |
* @param subpriority Atribui subprioridade.
|
107 |
*/
|
108 |
void __ISR()interrupt1(void) { |
109 |
/* Interrupt Service Routine code goes here */
|
110 |
|
111 |
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 |
115 |
|
116 |
IFS0CLR = 0x00000010; //Clear Timer1 interrupt flag |
117 |
} |