Friday, March 1, 2013
Ejemplo del uso del lcd 2x16 con ADC C18
// USO DEL LCD
#include
#include
#include
#include
#include
#define LCD_4X20 //usamos un LCD4x20
//Si usamos un LCD 16x2 quitamos el #define
//Fuses para trabajar con 8Mhz/////////
#pragma config FOSC = INTOSCIO_EC //usamos 8Mhz internos
#pragma config PLLDIV = 5 //PLL DIV 20Mhz/5=4Mhz
#pragma config CPUDIV = OSC1_PLL2 //CPUDIV1 96Mhz/2=48Mhz
#pragma config USBDIV = 1 //tRABAJAMOS CON USB CLOCK divido en 1
#pragma config VREGEN = OFF //Trabajamos sin regulador interno 3.3v para usb
#pragma config FCMEN = OFF, IESO = OFF
#pragma config PWRT = ON, BOR = OFF,BORV = 0
#pragma config WDT = OFF,WDTPS = 32768
#pragma config MCLRE = ON, LPT1OSC = OFF,PBADEN = OFF,CCP2MX = ON
#pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF
#pragma config CP0 = OFF,CP1 = OFF,CP2 = OFF
#pragma config CPB = ON, CPD = ON
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF
#pragma config WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF
#pragma config EBTRB = OFF
//Funciones prototipos ////////
void cmdXLCD(unsigned char cd);
void gotoXYLCD(unsigned char x,unsigned char y);
////////////////////////////////////////////////
///Variables globales ////////
////////////////////////////////////////////////
char buf[15];
char buf2[15];
const char adc[15]={"valor de ADC:"};
const char yo[20]={"Hecho en Mexico"};
char email[20]={"george.manson.69"};
unsigned int value1,value2,ch;
/////////////////////////////////////////////////
////Funciones necesarias para uso del LCD ///////
/////////////////////////////////////////////////
void DelayFor18TCY(void){
Delay10TCYx(4);
}
void DelayPORXLCD (void){
Delay1KTCYx(30); // Delay of 15ms
return; // Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 8MHz) / 4
// Cycles = 30,000
}
void DelayXLCD (void){
Delay1KTCYx(10); // Delay of 5ms
return; // Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 8MHz) / 4
// Cycles = 10,000
}
///////////////////////////////////
////Funcion de configuracion /////
///////////////////////////////////
void config(void){
TRISA=0x03;//RA0=e,RA1=e
OpenXLCD(FOUR_BIT & //4-bit
LINES_5X7); //
//Configuramos ADC
OpenADC(ADC_FOSC_RC & //Clock Interno
ADC_RIGHT_JUST & //10bit
ADC_20_TAD , //20TAD
ADC_CH0 & //CANAL0
ADC_CH1 & //CANAL1
ADC_INT_OFF & //INTERRUPCIONES OFF
ADC_REF_VDD_VSS , //+5,GND
ADC_2ANA); //canal 0,1 analogo, resto digital
}
////Funcion de comandos para lcd/////
/// cd=0x01 clear screen /////
/// cd=0x0c ON creen /////
void cmdXLCD(unsigned char cd){
while(BusyXLCD());
WriteCmdXLCD(cd);
}
///Funcion de posicionamiento /////
/// x=renglon /////
/// y=columna /////
///Si queremos usar un lcd4x20 /////
///definimos despues del /////
///#include
///#define LCD_4X20 /////
#ifndef LCD_4X20
void gotoXYLCD(unsigned char x,unsigned char y){
unsigned char adr;
if(y!=1) adr=0x40;
else adr=0;
adr+=x-1;
cmdXLCD(0x80|adr);
}
#else
void gotoXYLCD(unsigned char x,unsigned char y){
unsigned char adr;
switch(y) {
case 1 : adr=0x80;break;
case 2 : adr=0xc0;break;
case 3 : adr=0x94;break;
case 4 : adr=0xd4;break;
}
adr+=x-1;
cmdXLCD(adr);
}
#endif
void main(void){
/*Calibramos el oscilador Interno del PIC*/
OSCCON=0x70;
/*Llamamos la funcion de configuracion*/
config();
cmdXLCD(0x0c); //
cmdXLCD(0x01); //
gotoXYLCD(1,1);
putsXLCD(adc);
gotoXYLCD(1,3);
putsXLCD(yo);
gotoXYLCD(1,4);
putsXLCD(email);
while(1){
Delay100TCYx(0);
SetChanADC(ADC_CH0);//canal ch empieza la conversion
ConvertADC(); //start convert
while(BusyADC()); //Ha terminado?
value1=ReadADC();
Delay100TCYx(0);
SetChanADC(ADC_CH1);//canal ch empieza la conversion
ConvertADC(); //start convert
while(BusyADC()); //Ha terminado?
value2=ReadADC();
sprintf(buf,"VAL=%i ",value1); //string"buf"="VALUE=value"
sprintf(buf2,"VAL2=%i ",value2); //string"buf2"="VALUE=value2"
gotoXYLCD(1,2); //segunda linea
putsXLCD(buf); //imprime
gotoXYLCD(10,2); //segunda linea
putsXLCD(buf2); //imprime
}
}
Currently have 0 comentarios: