#include <adc.h>
#include <usart.h>
#include <stdio.h>
#include "mylibs.h"
#pragma config WDT = OFF //WDT apagado
int result;
void main (void){
TRISA = 0xFF; TRISB = 0x00; PORTB = 0x00;
OpenADC(ADC_FOSC_RC & ADC_RIGHT_JUST,ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, ADC_1ANA);
SetChanADC(ADC_CH0);
OpenUSART(USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_LOW, 6); //8928 baudios
printf("Programa de prueba en C18 para PIC18F4550\n\r");
while(1){
result = Read_ADC();
PORTB = result;
printf("El valor del ADC = %u \n\r",result);
Set_baud(4000000,9000,0);
}
}#ifndef mylibs
#define mylibs
int Read_ADC (void);
//Macro para el calculo de baudios automatico
#define Set_baud (unsigned long XTAL, unsigned short long baud, char mode){ \
if((((XTAL/baud)/16)-1)<255){ \
return (unsigned int)(((XTAL/baud)/16)-1); \
} \
else if((((XTAL/baud)/64)-1)<255){ \
return (unsigned int)(((XTAL/baud)/64)-1); \
} \
else{ \
//#error No se pudo establecer un baud rate correcto \
} \
}
#endif/*Librerías varias*/
//Lectura completa del ADC, toma la conersion, espera y
//lee los registros
int Read_ADC (void){
ConvertADC(); //conversion
while(BusyADC()); //espero a que termine
return ReadADC(); //tomo el valor y lo retorno
}
//Macro para el calculo de baudios automatico
#define Set_baud (unsigned long XTAL, unsigned short long baud, char mode){ \
if((((XTAL/baud)/16)-1)<255){ \
return (unsigned int)(((XTAL/baud)/16)-1); \
} \
else if((((XTAL/baud)/64)-1)<255){ \
return (unsigned int)(((XTAL/baud)/64)-1); \
} \
else{ \
//#error No se pudo establecer un baud rate correcto \
} \
}OpenUSART(USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_LOW, 6); //8928 baudiosOpenUSART(USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_LOW, Set_baud(4000000,9000,0));#define Set_baud (XTAL, baud,mode) {....}#ifndef mylibs
#define mylibs
int Read_ADC (void);
#define XTAL 4000000
#define baud 9600
//Macro para el calculo de baudios automatico
#if((((XTAL/baud)/16)-1)<255)
#define SPEED_MODE USART_BRGH_HIGH
#define BAUDIOS (unsigned int)(((XTAL/baud)/16)-1)
#elif((((XTAL/baud)/64)-1)<255)
#define SPEED_MODE USART_BRGH_LOW
#define BAUDIOS (unsigned int)(((XTAL/baud)/64)-1)
#else
#error No se pudo establecer un baud rate correcto
#endif
#endif/*Librerías varias*/
//Lectura completa del ADC, toma la conersion, espera y
//lee los registros
int Read_ADC (void){
ConvertADC(); //conversion
while(BusyADC()); //espero a que termine
return ReadADC(); //tomo el valor y lo retorno
}