Professional Documents
Culture Documents
Cuman
MICROCONTROLADORES II
INTERRUPCIONES Y
TEMPORIZADORES
TEMA 2
PROF. LUIS ZURITA
IUT
Cuman
REGISTRO INTCON
INTERRUCCIONES PIC16F87X
CLSICAS PIC16F84
IUT
Cuman
NUEVAS
GIE PEIETOIEINTERBIETOIFINTFRBIF
PIE1
PSPIE
ADIE
RCIE
TXIE
PIR1
SSPIE
CCP1IE
PIE2
---
---
EEIE
TMR2IE
TMR1IE
PIR2
BCLIE
--
--
CCP2IE
REGISTRO PIE1
IUT
Cuman
modelos de 40 pines.
1 = Habilita la interrupcin por lectura/escritura en el
PSP
0= inhabilita la interrupcin por lectura/escritura en el
PSP
bit 6: ADIF: bit de habilitacin de interrupcin por
finalizacin de la conversin A/D.
1 = Habilita la interrupcin del convertidor A/D
0 = Inhabilita la interrupcin del convertidor A/D
bit 5: RCIE: bit de habilitacin de interrupcin en
recepcin por el USART, cuando se llena el buffer.
1 Zurita
= Habilita interrupcin por recepcin en el USART
Prof. Luis
REGISTRO PIE1
IUT
Cuman
REGISTRO PIE2
U
---
0 R/W 0 U
0
---
0 R/W 0 R/W 0 U
EEIE BCLI -E como 0
bit 7: No implementado: se lee
bit 6
Bit
7 Reservado, Mantiene este bits a cero
IUT
Cuman
0U
--
0 R/W
CCP2
IE
Bit 0
PC<12:0>
IUT
Cuman
CALL, RETURN
RETFIE, RETLW
Nivel 1 de la Pila
1
3
Nivel 2 de la Pila
Nivel 8 de la Pila
Vector de Reset
Vector de
Interrupcin
Pgina 0
Pgina 1
Pgina 2
Pgina 3
0000h
0004h
0005h
07FFh
0800h
0FFFh
1000h
17FFh
1FFh
INTERRUPCIONES EN LENGUAJE C
IUT
Cuman
#INT_RTCC
#INT_RB
#INT_EXT
#INT_AD
#INT_TBE
#INT_RDA
#INT_TIMER
1
#INT_TIMER
2
#INT_CCP1
#INT_CCP2
#INT_SSP
#INT_PSP
TOIF
RBIF
INTF
ADIF
TXIF
RCIF
TMR1IF
TMR2IF
Captura/Comparacin en CCP1
Captura/Comparacin en CCP2
Envio/Recepcin de dato serie
sncrono
Dato entrante en puerto esclavo
CCP1IF
CCP2IF
SSPIF
PSPIF
INTERRUPCIONES EN LENGUAJE
C
IUT
Cuman
La directiva #INT_DEFAULT
Indica que la funcin que viene a continuacin
ser llamada si se dispara una interrupcin y
ninguno de los flags est activo.
La directiva #INT_GLOBAL
Indica que la funcin que va a continuacin
sustituye todas las acciones que inserta el
compilador al aceptarse una interrupcin. Slo se
ejecuta lo que vaya en dicha funcin.
GLOBAL equivale a GIE=PEIE=1 y debe activarse
de forma independiente. El resto activarn la
mscara correspondiente.
Prof. Luis Zurita
IUT
Cuman
enable_interrupts (nivel);
nivel es una constante definida en el 16F877.h y
genera el cdigo necesario para activar las
mscaras necesarias.
Etiquetas de nivel definidas para el 16F877:
GLOBAL
INT_RTCC
INT_RB
INT_EXT
INT_AD
INT_TBE
INT_RDA
INT_TIMER1
INT_TIMER2
INT_CCP1
INT_CCP2
INT_SSP
INT_PSP
INT_BUSCOL
INT_EEPROM
La mscara global (hace a GIE=1) debe activarse
de manera independiente. Las otras activan la
mscara particular y el PEIE si es necesario.
disable_interrupts(nivel);
IUT
Cuman
ext_int_edge(H_TO_L);
La interrupcin es activada por flanco de
bajada (antiva el flag INTF).
ext_int_edge(L_TO_H);
La interrupcin es activada por flanco de
subida (activa el flag INTF).
Prof. Luis Zurita
IUT
Cuman
IUT
Cuman
Continuacin Ejemplo 2.
void main(){
set_tris_b(0x01);
set_tris_d(0);
set_tris_a(0x01);
output_d(0);
port_b_pullups(true);
enable_interrupts(int_ext);
ext_int_edge(H_to_L);
enable_interrupts(global);
while(TRUE){
output_high(PIN_D0);
output_low(PIN_D1);
delay_ms(1000);
output_low(PIN_D0);
output_high(PIN_D1);
delay_ms(1000);}
}
Prof. Luis Zurita
IUT
Cuman
TIMER 0
IUT
Cuman
TMR0:
Registro
de
8
bits
de
lectura/escritura
OPTION: Configura al TMR0 para que
trabaje como temporizador contador y
asigna el valor al prescaler
INTCON: Da informacin mediante el bit
TOIF cuando el TMR0 se ha desbordado.
TRISA (PUERTO A): Permite el ingreso de
Prof. Luispulsos
Zurita
cuando el TMR0 est configurado
IUT
Cuman
QU ES EL PRESCALER?
IUT
Cuman
IUT
Cuman
00H
FFH
El
TMR0
exclusivamente
ascendente,
descendente.
de
cuenta
forma
nunca
00H
(28 N10)
00H
FFH
Valor cargado
En el TMR0
00H
IUT
Cuman
OTROS EJEMPLOS
IUT
Cuman
10
Prof. Luis Zurita
= 256 - (3750/RgoDivisor)
IUT
Cuman
TIMER0 EN LENGUAJE C
La funcin para configurar el TIMER0 es:
setup_timer_0 (modo);
Donde modo est definido en el fichero de
setup_timer_0
OPTION_REG
cabecera
y afecta a los bits
5:0 del OPTION_REG:
IUT
Cuman
(modo);
(81h)
RTCC_INTERNAL
00000000
RTCC_EXT_L_TO_H
00100000
RTCC_EXT_H_TO_L
00110000
RTCC_DIV_1
00001000
RTCC_DIV_2
00000000
RTCC_DIV_4
00000001
RTCC_DIV_8
00000010
RTCC_DIV_16
00000011
RTCC_DIV_32
00000100
RTCC_DIV_64
00000101
RTCC_DIV_128
00000110
RTCC_DIV_256
00000111
TIMER0 EN LENGUAJE C
IUT
Cuman
IUT
Cuman
IUT
Cuman
#INCLUDE <16F877.h>
#FUSES
XT,NOWDT,NOPROTECT,NOLVP
#USE
DELAY(CLOCK=4000000)
#USE
fast_io(B)
NOTA: El compilador se
#INT_RTCC
encarga al entrar en la
RTCC_isr(){
interrupcin de inhabilitar
output_toggle(PIN_B7);
las interrupciones y al
set_timer0(198);}
salir de borrar los flags,
void main(){
por
lo
que
no
es
set_tris_B(0x00);
necesario hacerlo por
output_low(PIN_B7);
programa
setup_timer_0(RTCC_DIV_8);
set_timer0(198);
enable_interrupts(INT_RTCC);
Prof. Luis Zurita
enable_interrupts(GLOBAL);
TIMER 1
IUT
Cuman
Caractersticas bsicas:
1. Es de 16 bits.
2. Puede actuar como temporizador o como
contador bit (TMR1CS).
3. Se puede leer y escribir en los registros TMR1H,
TMR1L.
4. Puede pararse o habilitarse mediante el bit
TMR1ON.
5. Tiene un pre-divisor programable por software.
6. El oscilador de bajo consumo est situado entre
los pines T1OSI (entrada) y T1OSO (salida).
Prof. Luis Zurita
TIMER 1
IUT
Cuman
IUT
Cuman
IUT
Cuman
IUT
Cuman
T1CON
Bit 3: T1OSCEN: bit de habilitacin del oscilador del TIMER1. Cuando se emplea un
oscilador externo, hay que poner este bit a 1. El TMR1 puede trabajar a una
frecuencia totalmente independiente de la del sistema.
1 = Habilita el oscilador
0 = Deshabilita el oscilador
Nota: El oscilador y la resistencia se desconectan para reducir el consumo
IUT
Cuman
(TMR1H:TMR1L))
(TMR1H:TMR1L) = Valor a cargar en el
TIMER1
Ejercicio:
Cul es el mximo valor de temporizacin
que se puede alcanzar con el TIMER 1?
Prof. Luis Zurita
IUT
Cuman
RSI
Configurar
TIMER 1
Deshabilitar GIE
Cargar TIMER 1
Limpiar
sealizador
Habilitar TIMER
1
Habilitar GIE
SLEEP
FIN
RETFIE
TIMER1 EN LENGUAJE C
La funcin para configurar el TIMER0 es:
setup_timer_1 (modo);
Donde modo est definido en el fichero de
cabecera
y afecta a los bitsT1CON
5:0 del (10h)
T1CON:
setup_timer_0
(modo);
T1_DISABLED
00000000
T1_INTERNAL
10000101
T1_EXTERNAL
10000111
T1_EXTERNAL_SYNC 10000011
T1_CLK_OUT
00001000
T1_DIV_BY_1
00000000
T1_DIV_BY_2
00010000
T1_DIV_BY_4
00100000
00110000
Prof. Luis Zurita T1_DIV_BY_8
IUT
Cuman
TIMER1 EN LENGUAJE C
IUT
Cuman
IUT
Cuman
IUT
Cuman
IUT
Cuman
TIMER 2
IUT
Cuman
Caractersticas bsicas:
1. Es de 8 bits.
2. Se puede leer y escribir en los registros
TMR2.
3. Puede pararse o habilitarse mediante el
bit TMR2ON.
4. Tiene un pre-divisor y un post-divisor
programable por software
5. Slo tiene modo temporizador.
6. Posee un registro (PR2) que ajusta el
momento de desborde.
7. PR2(Registro de 8 bits) que puede leerse
y escribirse PR2 con el cual compara su
valor:
Prof. Luis Zurita
TIMER 2
IUT
Cuman
IUT
Cuman
IUT
Cuman
T2CON
IUT
Cuman
CLCULOS TIMER2
IUT
Cuman
TIMER2 EN LENGUAJE C
IUT
Cuman
TIMER2 EN LENGUAJE C
setup_timer_2(modo , periodo ,
postcaler);
T2_DISABLED
T2_DIV_BY_1
T2_DIV_BY_4
T2_DIV_BY_16
T2CON(12h
)
00000000
00000100
00000101
00000110
set_timer2(valor);
valor : es un entero de 16 bits.
Para leer el valor actual del registro:
Prof. Luis Zurita
IUT
Cuman
IUT
Cuman
IUT
Cuman
#INCLUDE <16F877.h>
#FUSES XT,NOWDT,NOPROTECT,NOLVP
#USE
DELAY(CLOCK=4000000)
#USE
fast_io(B)
int1
cont=0;
#INT_TIMER2
void temp2_isr(void){
output_toggle(PIN_B0);
set_timer2(11);}
void main(){
set_tris_B(0x00);
output_low(PIN_B0);
Prof. Luis Zurita
setup_timer_2(T2_DIV_BY_4 , 124 , 1);