#include <xc.h>
#include <stdint.h>
#include <stdlib.h>
//#include <stdio.h>
#include <string.h>
#define _XTAL_FREQ 8000000
#include "xclcd.h"
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define MAX_FREQ_LENGTH 6 // largo de la frecuencia incluyendo el caracter nulo
uint16_t frecuencia;
uint8_t tiempoDeCuenta;
bit showDisplay;
void interrupt ISR()
{
if ((INTCONbits.INTF ==1) && (INTCONbits.INTE == 1))
{
if (T1CONbits.TMR1ON == 1) frecuencia++;
INTCONbits.INTF = 0;
}
if ((PIR1bits.TMR1IF == 1) && (PIE1bits.TMR1IE == 1))
{
TMR1 = 3068;
tiempoDeCuenta++;
if (tiempoDeCuenta >= 32)
{
T1CONbits.TMR1ON = 0;
tiempoDeCuenta = 0;
showDisplay = 1;
}
PIR1bits.TMR1IF = 0;
}
}
void main()
{
uint8_t frecuenciaTxt[MAX_FREQ_LENGTH];
//uint8_t frecuenciaParcialTxt[4];
// uint8_t * pFrecuencia;
int * estado;
uint8_t frecuenciaParteDecimal;
uint16_t frecuenciaParteEntera;
uint8_t cantidadDeEspacios;
uint8_t espacios;
CMCON = 0X07;
PORTB = 0x00;
PORTA = 0x00;
TRISB = 0x01;
TRISA = 0x00;
// pFrecuencia = frecuenciaTxt;
OPTION_REG = 0x80;
LCDInit();
LCDGotoxy(2,1);
LCDPuts("FW SoundSystem");
LCDGotoxy(14,2);
LCDPuts("Hz");
TMR1 = 3068;
/*
INTCONbits.INTF = 0;
PIR1bits.TMR1IF = 0;
PIE1bits.TMR1IE = 1;
INTCONbits.INTE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
*/
INTCON = 0x50;
PIR1bits.TMR1IF = 0;
PIE1bits.TMR1IE = 1;
INTCONbits.GIE = 1;
T1CONbits.TMR1ON = 1;
while(1)
{
if (showDisplay == 1)
{
//frecuenciaParteEntera = frecuencia / 2;
frecuenciaParteEntera = frecuencia >> 1;
frecuenciaParteDecimal = frecuencia % 2;
utoa(frecuenciaTxt, frecuenciaParteEntera, 10);
/*
if (frecuenciaParteDecimal != 0)
{
strcpy(frecuenciaParcialTxt, ".50");
}
else
strcpy(frecuenciaParcialTxt, ".00");
strcat(frecuenciaTxt, frecuenciaParcialTxt);
*/
cantidadDeEspacios
= MAX_FREQ_LENGTH
- strlen(frecuenciaTxt
);
LCDGotoxy(4,2);
for(espacios = cantidadDeEspacios; espacios > 0; espacios--)
{
LCDPutc(' ');
}
LCDPuts(frecuenciaTxt);
// agregado para la version 2.0
LCDPutc('.');
if (frecuenciaParteDecimal != 0) LCDPutc('5');
else
LCDPutc('0');
showDisplay = 0;
frecuencia = 0;
TMR1 = 3068;
T1CONbits.TMR1ON = 1;
NOP();
}
}
}