Código y Correcciones para RPM
1:54
lcd_command() (además de funciones auxiliares para inicializar el LCD, enviar caracteres, limpiar la pantalla y posicionar el cursor) para operar el LCD en modo de 4 bits:C
#include #fuses XT, NOWDT, NOPUT, NOMCLR, NOPROTECT, NOCPD, NOBROWNOUT, NOIESO, NOFCMEN, NOLVP #use delay(clock=4000000) #use fast_io(B) // Puerto B como entradas (para la interrupción en RB0) #use fast_io(D) // Puerto D como salidas (para el LCD) // Definiciones de pines del LCD en modo 4 bits #define LCD_DB4 PIN_D4 #define LCD_DB5 PIN_D5 #define LCD_DB6 PIN_D6 #define LCD_DB7 PIN_D7 #define LCD_RS PIN_D2 #define LCD_E PIN_D3 volatile long contador = 0; // Contador de pulsos float RPM; // Velocidad en RPM int pulsos_rev = 5; // Pulsos por revolución #int_ext void interrupcion_RB0() { contador++; // Incrementa el contador en cada pulso } // Función para enviar un comando al LCD en modo 4 bits void lcd_command(unsigned char cmd) { // Modo comando: RS = 0 output_low(LCD_RS); // Envía el nibble superior output_bit(LCD_DB4, (cmd & 0x10) ? 1 : 0); output_bit(LCD_DB5, (cmd & 0x20) ? 1 : 0); output_bit(LCD_DB6, (cmd & 0x40) ? 1 : 0); output_bit(LCD_DB7, (cmd & 0x80) ? 1 : 0); output_high(LCD_E); delay_us(1); output_low(LCD_E); // Envía el nibble inferior output_bit(LCD_DB4, (cmd & 0x01) ? 1 : 0); output_bit(LCD_DB5, (cmd & 0x02) ? 1 : 0); output_bit(LCD_DB6, (cmd & 0x04) ? 1 : 0); output_bit(LCD_DB7, (cmd & 0x08) ? 1 : 0); output_high(LCD_E); delay_us(1); output_low(LCD_E); delay_ms(2); // Tiempo para que el LCD procese el comando } // Función para limpiar el LCD void lcd_clear(void) { lcd_command(0x01); // Comando para limpiar pantalla delay_ms(2); } // Función para enviar un carácter (dato) al LCD // Se utiliza el modo de datos: RS = 1 void lcd_putc(char data) { output_high(LCD_RS); // Modo dato // Envía el nibble superior output_bit(LCD_DB4, (data & 0x10) ? 1 : 0); output_bit(LCD_DB5, (data & 0x20) ? 1 : 0); output_bit(LCD_DB6, (data & 0x40) ? 1 : 0); output_bit(LCD_DB7, (data & 0x80) ? 1 : 0); output_high(LCD_E); delay_us(1); output_low(LCD_E); // Envía el nibble inferior output_bit(LCD_DB4, (data & 0x01) ? 1 : 0); output_bit(LCD_DB5, (data & 0x02) ? 1 : 0); output_bit(LCD_DB6, (data & 0x04) ? 1 : 0); output_bit(LCD_DB7, (data & 0x08) ? 1 : 0); output_high(LCD_E); delay_us(1); output_low(LCD_E); delay_ms(2); } // Función para posicionar el cursor en el LCD en coordenadas (x, y) // Se asume que se utiliza un LCD de 2 líneas (línea 1: dirección 0x00, línea 2: dirección 0x40) void lcd_gotoxy(int x, int y) { int addr; if (y == 1) { addr = x - 1; } else { addr = 0x40 + x - 1; } lcd_command(0x80 | addr); } // Función para inicializar el LCD en modo 4 bits void lcd_init(void) { // Espera después de encender la alimentación delay_ms(20); output_low(LCD_RS); output_low(LCD_E); // Secuencia de inicialización: // Se envía el mismo nibble (0x03) tres veces para configurar el LCD en modo de 8 bits temporalmente output_high(LCD_DB4); // 0x03 = 0b0011 (enviamos solo el nibble superior) output_high(LCD_DB5); output_low(LCD_DB6); output_low(LCD_DB7); output_high(LCD_E); delay_ms(5); output_low(LCD_E); delay_ms(1); output_high(LCD_E); delay_ms(5); output_low(LCD_E); delay_ms(1); output_high(LCD_E); delay_ms(5); output_low(LCD_E); delay_ms(1); // Forzar el LCD a entrar en modo 4 bits enviando 0x02 lcd_command(0x02); // Configurar función: 4 bits, 2 líneas, 5x8 puntos (comando 0x28) lcd_command(0x28); // Encender la pantalla, sin cursor, sin parpadeo (0x0C) lcd_command(0x0C); // Establecer modo de entrada: incrementar el cursor, sin shift (0x06) lcd_command(0x06); lcd_clear(); } void main(void) { set_tris_B(0xFF); // Configurar el puerto B como entradas (INT_EXT en RB0) set_tris_D(0x00); // Configurar el puerto D como salidas (para el LCD) output_d(0x00); lcd_init(); // Inicializa el LCD // Configuración de la interrupción externa INT_EXT en RB0 enable_interrupts(INT_EXT); ext_int_edge(L_TO_H); enable_interrupts(GLOBAL); while(TRUE) { delay_ms(400); // Intervalo de muestreo de 400 ms RPM = ((float)contador * (1000.0 / 400.0) * 60.0) / pulsos_rev; contador = 0; lcd_clear(); // Limpia el LCD lcd_gotoxy(1, 1); printf(lcd_putc, "Velocidad motor"); lcd_gotoxy(1, 2); printf(lcd_putc, "RPM: %0.2f", RPM); } }
lcd_command(0x01) para limpiar la pantalla y refrescar el LCD.Ready.
Reviews
Colossal Azure Dejarik
Properties
Properties describe core aspects of the project.
Pricing & Availability
Distributor | Qty 1 |
|---|
Assets
No assets added.
Controls
Welcome 👋
Flux helps you build PCBs faster with an AI teammate!
Create your account to collaborate, stay updated, fork your own version, and get instant answers from our AI agent.