Sei sulla pagina 1di 6

// CONFIG1H

#pragma config OSC = XT // Oscillator Selection bits (XT oscillator)


#pragma config OSCS = OFF // Oscillator System Clock Switch Enable bit (Os
cillator system clock switch option is disabled (main oscillator is source))
// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = OFF // Brown-out Reset Enable bit (Brown-out Reset d
isabled)
#pragma config BORV = 20 // Brown-out Reset Voltage bits (VBOR set to 2.0
V)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (cont
rol is placed on the SWDTEN bit))
#pragma config WDTPS = 128 // Watchdog Timer Postscale Select bits (1:128)
// CONFIG3H
#pragma config CCP2MUX = ON // CCP2 Mux bit (CCP2 input/output is multiplexe
d with RC1)
// CONFIG4L
#pragma config STVR = ON // Stack Full/Underflow Reset Enable bit (Stack
Full/Underflow will cause RESET)
#pragma config LVP = OFF // Low Voltage ICSP Enable bit (Low Voltage ICSP
disabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000200-001FFFh)
not code protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh)
not code protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh)
not code protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh)
not code protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (0
00000-0001FFh) not code protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM
not code protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000200-001FFFh
) not write protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh
) not write protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh
) not write protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh
) not write protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (
Configuration registers (300000-3000FFh) not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (
000000-0001FFh) not write protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM
not write protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000200-00
1FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-00
3FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-00
5FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-00
7FFFh) not protected from Table Reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Bl
ock (000000-0001FFh) not protected from Table Reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h>
#include <string.h>
#define _XTAL_FREQ 4000000

unsigned char buffer[40];


unsigned char tam_buf=0;
unsigned int A=0,B=0,C=0,D=0,E=0,F=0,G=0,H=0;
signed int VARIABLE,S=0;
void CONF_PUERTOS(void);
void BORRAR_BASURA(void);
void CONF_LCD(void);
void CONF_USART(void);
void RECIBIR(void);
void CONTAR_DATOS(void);
void enable(void);
void dato_LCD(unsigned char dato);
void ins_LCD(unsigned char dato);
void write(unsigned char tam);
void mostrar (void);
void fin (void);
void main(void)
{
CONF_PUERTOS();
BORRAR_BASURA();
CONF_USART();
CONF_LCD();
ins_LCD(128);
sprintf(buffer, "212 ");
write(strlen(buffer));
sprintf(buffer, "213 ");
write(strlen(buffer));
sprintf(buffer, "214 ");
write(strlen(buffer));
sprintf(buffer, "215");
write(strlen(buffer));
ins_LCD(192);
sprintf(buffer, "216 ");
write(strlen(buffer));
sprintf(buffer, "217 ");
write(strlen(buffer));
sprintf(buffer, "218 ");
write(strlen(buffer));
sprintf(buffer, "219");
write(strlen(buffer));
A=0,B=0,C=0,D=0,E=0,F=0,G=0,H=0;
while (1){
if(PORTCbits.RC2==1)
{
__delay_ms(5);
S++;
PORTCbits.RC1=1;
RECIBIR();
CONTAR_DATOS();
}
fin();
}
}

void CONF_PUERTOS(void)
{
//76543210
TRISC = 0b10000100; // Configura los pines 7 Entrada RX
// y el pin 6 salida TX
TRISD = 0; // Bus de datos de la LCD
TRISA = 0x00; // puerto A de salida
ADCON1 = 0x06; //Puerto A digital
}
//----------------------------------
void BORRAR_BASURA(void)
{
LATD = LATE = LATA = 0;
PORTA = 0x00; // Limpia puerto A
}
//----------------------------------
void CONF_LCD(void)
{
__delay_ms(20); // delay 20 ms
PORTD=0x30; // VALOR DE INICIALIZACION
enable();
__delay_ms(5); // delay 5 ms
PORTD=0x30; // VALOR DE INICIALIZACION
enable();
__delay_ms(1); // delay 1 ms
PORTD=0x30; // VALOR DE INICIALIZACION
enable();
PORTD=0b00111000; // Function set 8 bits - 2 lineas - 5*8
enable();
PORTD=0b00001000; // display off
enable();
PORTD=0b00000001; // display clear
enable();
PORTD=0b00000110; // entry mode set - incrementa la posicion del cursor
enable();
PORTD=0b00001100; // display on/off control - LCD on - cursor on - blink o
ff
enable();

}
//...................enable....................................
void enable (void)
{
PORTAbits.RA2=1; // enable en alto
__delay_ms(1);
PORTAbits.RA2=0; // enable en bajo
}
//...................dato....................................
void dato_LCD(unsigned char dato)
{
PORTAbits.RA1=0; //R/W en 0
PORTAbits.RA0=1; //RS en 1 (envia dato)
PORTD=dato; //dato a publicar
enable();
__delay_ms(2); //tiempo de ejecucion
}
//...................instruccion..................................
void ins_LCD(unsigned char dato)
{
PORTAbits.RA1=0; //R/W en 0
PORTAbits.RA0=0; //RS en 0 (envia instruccion)
PORTD=dato; //dato a publicar
enable();
__delay_ms(2); //tiempo de ejecucion
}
void write(unsigned char tam){
unsigned char i;
for(i=0;i<tam;i++){
dato_LCD(buffer[i]);
}
}
//----------------------------------
void CONF_USART(void)
{
TXSTA = 0b00100110;
RCSTA = 0b10010000;
SPBRG = 12; // Tasa de Transmisin 19200 bps
}
//----------------------------------
void RECIBIR(void)
{
if(PIR1bits.RCIF ==1)
{
PORTCbits.RC0=1;
VARIABLE = RCREG;
PIR1bits.RCIF = 0;
}
}
//----------------------------------
void CONTAR_DATOS(void){
if(VARIABLE==212){
A++;
}
if(VARIABLE==213){
B++;
}
if(VARIABLE==214){
C++;
}
if(VARIABLE==215){
D++;
}
if(VARIABLE==216){
E++;
}
if(VARIABLE==217){
F++;
}
if(VARIABLE==218){
G++;
}
if(VARIABLE==219){
H++;
}
}
void mostrar (void){
ins_LCD(128);
sprintf(buffer, "%d",A);
write(strlen(buffer));
sprintf(buffer, " %d",B);
write(strlen(buffer));
sprintf(buffer, " %d",C);
write(strlen(buffer));
sprintf(buffer, " %d",D);
write(strlen(buffer));
ins_LCD(192);
sprintf(buffer, "%d",E);
write(strlen(buffer));
sprintf(buffer, " %d",F);
write(strlen(buffer));
sprintf(buffer, " %d",G);
write(strlen(buffer));
sprintf(buffer, " %d",H);
write(strlen(buffer));
}
void fin (void){
if(S>=1001){
PORTCbits.RC1=0;
PORTCbits.RC0=0;
S=0;
ins_LCD(1);
mostrar();
while(1){}
}
}

Potrebbero piacerti anche