You are on page 1of 3

//FRECUENCIMETRO

// CONEXIONES DE LCD
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// FIN DE CONEXIONES LCD
int CONTADOR=0;
void interrupt() //VECTOR DE INTERRUPCIONES
{
CONTADOR++;
PIR1.TMR1IF=0;
}
void main()
{
char MUESTRA[8];
int ALTO=25,BAJO=23, FRECUENCIA;
TRISC.B0=1; //HABILITACION DEL PORTC.B0 COMO ENT
RADA PARA CONTAR LOS PULSOS EN 1S
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
DELAY_MS(100);
INTCON=0B11000000; //INTERRUPCIONES GENERALES Y PERIFER
ICAS HABILITADAS
PIE1.TMR1IE=1; //HABILITACIN DEL LA INTERRUPCION DEL
TIMER1
PIR1.TMR1IF=0; //SE COLOCA A CERO LA BANDERA DE IND
ICACIN DEL DESBORDAMIENTO DEL TIMER1
T1CON=0B00000110; //CONFIGURACIN DEL TIMER1 COMO MODO C
ONTADOR ASINCRNICO Y EN ESTE MOMENTO APAGADO
while(1)
{
TMR1H=0; //REGISTRO DEL TIMER1 TMR1L Y TMR1H
SE COLOCAN A 0
TMR1L=0;
T1CON.TMR1ON=1; //SE ENCIENDE EL TIMER1
DELAY_MS(1000); //TIEMPO NECESARIO PARA QUE REGISTRA
R EL NUMERO DE PULSOS Y ASI CALCULAR LA FRECUENCIA
T1CON.TMR1ON=0;
ALTO=TMR1H; //SE GUARDAN EN VARIABLES LOS VALORE
S DE LOS REGISTROS TMR1H Y TRM1L
BAJO=TMR1L;
FRECUENCIA=((ALTO*256)+BAJO+CONTADOR*65536);
INTTOSTR(FRECUENCIA,MUESTRA);
Lcd_Out(1,1,MUESTRA);
Lcd_Out_Cp("Hz");
DELAY_MS(500);
CONTADOR=0;
}
}
// PWM
// Conexion LCD
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// Fin de conexion de LCD
void main()
{
TRISC=0;
TRISD=0XFF;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
DELAY_MS(100);
PWM1_Init(500);
PWM2_Init(500);
while(1)
{
PWM1_Start();
PWM2_Start();
if(PORTD==0)
{
PWM1_Stop();
PWM2_Stop();
Lcd_Out(1,1,"PWM APAGADO");
DELAY_MS(400);
}
if(PORTD==1)
{
PWM1_Set_Duty(63);
PWM2_Set_Duty(255);
Lcd_Out(1,3,"PWM1 LED 25");
Lcd_Out(2,3,"PWM2 MTR 100%");
DELAY_MS(400);
}
if(PORTD==2)
{
PWM1_Set_Duty(127);
PWM2_Set_Duty(191);
Lcd_Out(1,3,"PWM1 LED 50%");
Lcd_Out(2,3,"PWM2 MTR 75%");
DELAY_MS(400);
}
if(PORTD==4)
{
PWM1_Set_Duty(191);
PWM2_Set_Duty(127);
Lcd_Out(1,3,"PWM1 LED 75%");
Lcd_Out(2,3,"PWM2 MTR 50%");
DELAY_MS(400);
}
if(PORTD==8)
{
PWM1_Set_Duty(255);
PWM2_Set_Duty(63);
Lcd_Out(1,3,"PWM1 LED 100%");
Lcd_Out(2,3,"PWM2 MTR 25%");
DELAY_MS(400);
}
Lcd_Cmd(_LCD_CLEAR);
}
}
//nombre: frecuencimetro
//pic16f877 y crystal de 4mhz
//A travs del TMR1 entra una seal de tipo alterna, se hace uso del TMR1 donde
//se cuentan el nmero de pulsos recibidos en un segundo, para obtener la frec.
//y mostrar el resultado en una pantalla de LCD. La lectura se realiza cada
//segundo.
//Asignacin de Variables
unsigned Frecuencia;
char Frec[1];
char mensaje[12];
//Inicio del bloque principal
void main ()
{
LCD_Init(&PORTB); //Inicio del LCD en el puerto B
TRISA=0xFF; //Puerto A como entradas
TRISC=0xFF; //Puerto C como entradas-salidas
ADCON1=0x07; //Configura los pines como E/S Digitales
T1CON=0x0F; //Activa el TMR1
mensaje[12]="La Frec. es:"; //Guarda el mensaje en el arreglo
LCD_Cmd(LCD_Cursor_Off); //Desactiva el cursor
while (1)
{
LCD_Cmd(Lcd_First_Row); //Se coloca en la primera fila
//LCD_Out_Cp(mensaje); //Imprime lo contenido en mensaje
TMR1H=0; //Hace 0 a TMR1H
TMR1L=0; //Hace 0 a TMR1L
Delay_ms(997); //Retardo de 1 seg.
Frecuencia=TMR1H*256+TMR1L; //Obtiene la frecuencia
IntToStr(Frecuencia,Frec); //Convierte el valor a cadena
Lcd_Cmd(Lcd_Second_Row); //Se ubica en la segunda columna
LCD_Out(2,1,Frec); //Imprime el resultado en el LCD
LCD_Out_Cp(" Hz"); //Imprime "Hz" en el LCD
Delay_ms(1000); //Retardo de 1 seg.
}

You might also like