You are on page 1of 21

INTERRUPCIONES

ADC
COMUNICACIÓN
SERIAL
EN
C
INTERRUPCIONES
FUNCIONES:
ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
CLEAR_INTERRUPT(), INTERRUPT_ACTIVE(),
EXT_INT_EDGE()

Constants used in EXT_INT_EDGE() are:


#define L_TO_H 0x40
#define H_TO_L 0

DIRECTIVAS
GLOBAL 0xF2C0
INT_RTCC 0x00F220
INTERRUPCIONES INT_TIMER0 0x00F220
INT_TIMER1 0x009D01
Funciones: INT_TIMER2 0x009D02
enable_interrupts(nivel); INT_TIMER3 0x00A002
INT_EXT_L2H 0x5000F210
disable_interrupts(nivel); INT_EXT_H2L 0x6000F210
INT_EXT 0x00F210
INT_EXT1_L2H 0x5001F008
INT_EXT1_H2L 0x6001F008
INT_EXT1 0x00F008
INT_EXT2_L2H 0x5002F010
INT_EXT2_H2L 0x6002F010

nivel
INT_EXT2 0x00F010
INT_RB 0x00FFF208
INT_AD 0x009D40
INT_RDA 0x009D20
INT_TBE 0x009D10
INT_SSP 0x009D08
INT_CCP1 0x009D04
INT_CCP2 0x00A001
INT_BUSCOL 0x00A008
INT_LOWVOLT 0x00A004
INT_COMP 0x00A040
INT_EEPROM 0x00A010
INT_OSCF 0x00A080
INT_SPP 0x009D80
INT_USB 0x00A020
ADC_OFF 0 // ADC Off
ADC ADC_CLOCK_DIV_2 0x100
ADC_CLOCK_DIV_4 0x04
Function: SETUP_ADC(MODO); ADC_CLOCK_DIV_8 0x01
ADC_CLOCK_DIV_16 0x05
ADC_CLOCK_DIV_32 0x02
ADC_CLOCK_DIV_64 0x06
ADC_CLOCK_INTERNAL 0x07 // Internal 2-6us
ADC
SETUP_ADC_PORTS(VALOR);

#define NO_ANALOGS 0x0F // None


#define ALL_ANALOG 0x00 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4 B0
#define AN0_TO_AN11 0x03 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4
#define AN0_TO_AN10 0x04 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1
#define AN0_TO_AN9 0x05 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3
#define AN0_TO_AN8 0x06 // A0 A1 A2 A3 A5 E0 E1 E2 B2
#define AN0_TO_AN7 0x07 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_TO_AN6 0x08 // A0 A1 A2 A3 A5 E0 E1
#define AN0_TO_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_TO_AN4 0x0A // A0 A1 A2 A3 A5
#define AN0_TO_AN3 0x0B // A0 A1 A2 A3
#define AN0_TO_AN2 0x0C // A0 A1 A2
#define AN0_TO_AN1 0x0D // A0 A1
#define AN0 0x0E // A0
#define AN0_TO_AN11_ANALOG 0x03 //!old only provided for compatibility
#define AN0_TO_AN10_ANALOG 0x04 //!old only provided for compatibility
#define AN0_TO_AN9_ANALOG 0x05 //!old only provided for compatibility
#define AN0_TO_AN8_ANALOG 0x06 //!old only provided for compatibility
#define AN0_TO_AN7_ANALOG 0x07 //!old only provided for compatibility
#define AN0_TO_AN6_ANALOG 0x08 //!old only provided for compatibility
#define AN0_TO_AN5_ANALOG 0x09 //!old only provided for compatibility
#define AN0_TO_AN4_ANALOG 0x0A //!old only provided for compatibility
#define AN0_TO_AN3_ANALOG 0x0B //!old only provided for compatibility
#define AN0_TO_AN2_ANALOG 0x0C //!old only provided for compatibility
#define AN0_TO_AN1_ANALOG 0x0D //!old only provided for compatibility
#define AN0_ANALOG 0x0E //!old only provided for compatibility
ADC

SETUP_ADC_PORTS( VALOR);

// The following may be OR'ed in with the above using |


#define VSS_VDD 0x00 // Range 0-Vdd
#define VREF_VREF 0x30 // Range VrefL-VrefH
#define VREF_VDD 0x20 // Range VrefL-Vdd
#define VSS_VREF 0x10 // Range 0-VrefH
ADC
SET_ADC_CHANNEL(),

READ_ADC() #define ADC_START_AND_READ 7 // This is the default if nothing is specified


#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6

DIRECTIVA
#device ADC=8
while (true){
#include <18f4550.h> read_adc(ADC_START_ONLY);
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP while(adc_done()==0)
#fuses NODEBUG, PLL5, CPUDIV4, MCLR {}
#device ADC=8 valor_adc=read_adc(ADC_READ_ONLY);
#use delay (clock = 16000000) voltaje=valor_adc*resolucion;
#use fast_io(a) temperatura=voltaje*100;
#use fast_io(b) lcd_gotoxy(1,1);
#use fast_io(d) printf(lcd_putc,"%1.2f V, ", voltaje);
#define LCD_DATA_PORT lcd_gotoxy(9,1);
getenv("SFR:PORTB") printf(lcd_putc,"%3.1f C ",
#include <lcd.c> temperatura);

int8 valor_adc;
float resolucion=0.01969784; // 5/255 if((temperatura>80)&&(temperatura<120)){
float voltaje; output_high(PIN_D1);
float temperatura; lcd_gotoxy(1,2);
printf(lcd_putc,"VENTILADOR ON ");
void main (void) }
{ else{
set_tris_a(0xFF); // ENTRADAS output_low(PIN_D1);
//set_tris_b(0x00); // SALIDAS lcd_gotoxy(1,2);
set_tris_d(0x00); // SALIDAS printf(lcd_putc,"VENTILADOR OFF");
}
output_low(PIN_D1); if(temperatura>120){
output_low(PIN_D2); output_high(PIN_D1);
lcd_init(); output_high(PIN_D2);
setup_adc_ports (AN0|VSS_VDD); lcd_gotoxy(1,2);
setup_adc(ADC_CLOCK_DIV_64); printf(lcd_putc,"MUY CALIENTE!!!");
set_adc_channel(0); }
delay_us(50); else{
output_low(PIN_D2);
}
delay_ms(200);
}
}
COMUNICACIÓN SERIAL
Configuración genérica del USART: #use rs232(opciones)
COMUNICACIÓN SERIAL
COMUNICACIÓN SERIAL
PUTC()
PUTCHAR()

Estas funciones envían un carácter a la patilla XMIT del dispositivo RS232. Es preciso
utilizar la directiva #USE RS232 antes de la llamada a esta función para que el
compilador pueda determinar la velocidad de transmisión y la patilla utilizada. La
directiva #USE RS232 permanece efectiva hasta que se encuentre otra que anule la

PUTS(string)
Esta función envía cada carácter de string a la patilla XMIT del dispositivo RS232. Una
vez concluido el envío de todos los caracteres la función envía un retorno de carro CR
o RETURN (ASCII 13) y un avance de línea LF o LINE-FEED (ASCII 10).

Ejemplo:

puts( " ---------- " );


puts( " | HOLA |" );
puts( " ---------- " );
COMUNICACIÓN SERIAL
PRINTF([function], string, [values])
La función de impresión formateada PRINTF saca una cadena de caracteres al
estándar serie RS-232 o a una función especificada. El formato está relacionado con el
argumento que ponemos dentro de la cadena (string).

Cuando se usan variables, string debe ser una constante. El carácter % se pone dentro
de string para indicar un valor variable, seguido de uno o más caracteres que dan
formato al tipo de información a representar.

Si ponemos %% obtenemos a la salida un solo %. El formato tiene la forma genérica


%wt, donde w es optativo y puede ser 1,2,...,9. Esto es para especificar cuántos
carácteres son representados; si elegimos el formato 01,...,09 indicamos ceros a la
izquierda, o también 1.1 a 9.9 para representación en punto flotante.

t es el tipo de formato y puede ser uno de los siguientes:


COMUNICACIÓN SERIAL
C Carácter
U Entero sin signo
x Entero en Hex (en minúsculas)
X Entero en Hex (en mayúsculas)
D Entero con signo
%e Real en formato exponencial(notación científica)
%f Real (Float)
Lx Entero largo en Hex (en minúsculas)
LX Entero largo en Hex (en mayúsculas)
Lu Decimal largo sin signo
Ld Decimal largo con signo
% Simplemente un %
COMUNICACIÓN SERIAL
RECEPCION DE DATOS

c = GETC()
c = GETCH()
c = GETCHAR()

Estas funciones esperan un carácter por la patilla RCV del dispositivo RS232 y retorna
el carácter recibido.

Es preciso utilizar la directiva #USE RS232 antes de la llamada a esta función para que
el compilador pueda determinar la velocidad de transmisión y la patilla utilizada. La
directiva #USE RS232 permanece efectiva hasta que se encuentre otra que anule la
anterior.

KBHIT()

Esta función devuelve TRUE si el bit que se está enviando al pin RCV de un dispositivo
RS232, es el bit de inicio de un carácter. Es preciso utilizar la directiva #USE RS232
antes de la llamada a esta función para que el compilador pueda determinar la
velocidad en baudios y la patilla utilizada. La directiva #USE RS232 permanece efectiva
hasta que se encuentre otra que anule la anterior.
COMUNICACIÓN SERIAL
COMUNICACIÓN SERIAL
TRANSMISIÓN DE DATOS RECEPCIÓN DE DATOS

#include <16F876.h>
#FUSES XT,NOWDT
#use delay(clock=4000000)
#include <16F876.h>
#use rs232(baud=9600)
#FUSES XT,NOWDT
#include <LCD.C>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7)
int valor;
#include <LCD.C>
#int_RDA
void main() {
RDA_isr()
{
int valor;
valor=GETC();
}
lcd_init();
void main() {
while(1){
for (valor=0;valor<=10;valor++) {
lcd_init();
PUTC(valor);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
printf(lcd_putc,"\fenviandoooo=%1D",VALOR);
delay_ms(500);
for (;;) {
}
lcd_gotoxy(1,1);
}
printf(lcd_putc,"recibiendo=%1D",valor);
}
}
}
COMUNICACIÓN SERIAL
COMUNICACIÓN SERIAL

#include <16F877.h>
#device adc=10
#FUSES XT,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7, bits=8, parity=N)
#include <LCD.C>

void main() {
int16 q;
float p;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
lcd_init();
for (;;) {
set_adc_channel(0);
delay_us(10);
q = read_adc();
p = 5.0 * q / 1024.0;
printf(lcd_putc, "\fADC = %4ld", q);
printf(lcd_putc, "\nVoltage = %01.2fV", p);
printf("ADC = %4ld ", q);
printf("Voltage = %01.2fV\r", p); // El \r permite cambiar de línea.
delay_ms(100);
}
}

You might also like