Last active
December 29, 2015 00:59
-
-
Save jerivas/7589719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Manda datos del ADC por el EUSART al presionar un boton | |
// Boton pull down en RB0 | |
// Entrada analogica en RA0 | |
// Carlos Zelada | |
// Eduardo Rivas | |
#include "xc.h" | |
#include "stdlib.h" | |
#include "p18f4550.h" | |
//Directivas para activar los bits de configuracion | |
#pragma config FOSC = INTOSCIO_EC //Oscilador Interno, Puerto A RA6 activo, | |
#pragma config WDT = OFF //Watchdog timer apagado | |
#pragma config PBADEN = OFF //Parte baja del puerto B digitales | |
#pragma config MCLRE = ON //MCLRE Disponible | |
#pragma config DEBUG = ON //Modo de depuracion disponible | |
#pragma config LVP = OFF //Fuente de ISCP apagada | |
#define _XTAL_FREQ 8e6 //Definir la frecuencia del cristal para delays | |
//Variables globales | |
unsigned char ascii_buffer[5] = 0x30; //Representación ASCII de un entero | |
//Configuracion | |
void config() { | |
// Configurar OSCCON F=8MHz | |
IRCF0 = 1; | |
IRCF1 = 1; | |
IRCF2 = 1; | |
//Inicializar el ADC | |
ADCON0 = 0; //ADC en canal 0, apagado | |
ADCON1 = 0b00001110; //RA0 como entrada angalógica | |
ADCON2 = 0b10000000; //Tiempo de adquisición 0TAD y velocidad de Fosc/2 | |
ADIE = 0; //Deshabilitar interrupción del ADC | |
ADON = 1; //Encender ADC | |
//Configurar interrupciones por botones | |
INTEDG0 = 0; //Interrupción en PORTB0 en flanco descendente | |
INT0IE = 1; //Habilitar interrupción en PORTB0 | |
//Configurar EUSART | |
SPBRGH = 0; //FOSC/[16 (n + 1)] | |
SPBRG = 51; //((8000000/9600)/16)-1 = (833.33/16)-1 = 51 | |
TXSTA = 0x2C; | |
RCSTA = 0x90; | |
BAUDCON = 0x00; | |
//Inicializar los puertos | |
PORTA = 0; //Limpiar PORTA | |
PORTB = 0; //Limpiar PORTB | |
LATC = 0; //Limpiar PORTC | |
TRISA0 = 1; //PORTA0 como entrada (analogico) | |
TRISB0 = 1; //PORTB0 como entrada (digital) | |
TRISC6 = 0; //Tx como salida | |
TRISC7 = 1; //Rx como entrada | |
//Habilitar interrupciones | |
PEIE = 1; //Habilitar interrupciones periféricas | |
GIE = 1; //Habilitar interrupcioes globalmente | |
} | |
//Programa principal | |
void main() { | |
config(); //Ejecutar configuración | |
while(1); //No hacer nada, todo se hace por interrupcion | |
} | |
//Rutinas de interrupcion | |
//Lee el ADC y manda el resultado por EUSART al presionar el boton | |
void interrupt service(void) { | |
GIE = 0; //Deshabilitar interrupciones y evitar anidamiento | |
if(INT0IF == 1) { //Se presionó el botón (hubo flanco ascendente)? | |
GO = 1; //Leer la entrada analógica | |
while(GO); //Esperar que termine la conversion ADC | |
itoa(ascii_buffer, ADRES, 10); //Convertir el resultado del ADC a ASCII | |
char i = 0; //Inicializar contador | |
while(ascii_buffer[i]){ //Ejecutar hasta hallar 0x00 (fin de cadena) | |
TXREG = ascii_buffer[i]; //Mandar byte por EUSART | |
while(!TRMT); //Esperar que termine la transmision | |
i++; //Pasar al siguient caracter | |
} | |
TXREG = 0x0D; //Mandar retorno de linea | |
while(!TRMT); //Esperar fin de transmision | |
TXREG = 0x0A; //Mandar nueva linea | |
while(!TRMT); //Esperar fin de transmision | |
__delay_ms(98); | |
__delay_ms(98); //Dos retardos para evitar rebote, 196ms | |
INT0IF = 0; //Bajar la bandera del boton | |
} | |
GIE = 1; //Rehabilitar interrupciones | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment