[ Foro de C ]

Tengo uncodigo que no me compila

14-Aug-2025 22:43
Invitado (Tiago)
0 Respuestas

Tengo problemas con el codigo no deja correr y me bota error
Nose si hay algun error dentro del codigo

#include <built_in.h>

// ==================== CONFIGURACIÓN DE PINES ====================
sbit A at PORTB.B0;  // Válvula A
sbit B at PORTB.B1;  // Válvula B
sbit C at PORTB.B2;  // Válvula C
sbit D at PORTB.B3;  // Válvula D

// Canales ADC
#define LEVEL_CH 0  // AN0
#define TEMP_CH  1  // AN1

// ==================== VARIABLES ====================
unsigned int level_pct = 0;
unsigned int temp_C = 0;
unsigned long heat_elapsed_ms = 0;
const unsigned int RECIPE_TIME_MS = 120000; // 2 min

typedef enum { ST_INIT, ST_FILL_A, ST_FILL_B, ST_FILL_C, ST_HEAT, ST_DRAIN } State;
State state = ST_INIT;

// ==================== FUNCIONES ====================
// Apagar todas las válvulas
void all_valves_off() {
   A = B = C = D = 0;
}

// Leer nivel en %
unsigned int read_level_percent() {
   unsigned int raw = ADC_Read(LEVEL_CH);
   unsigned int lvl = (raw * 100UL) / 1023UL;
   if (lvl > 100) lvl = 100;
   return lvl;
}

// Leer temperatura en °C (LM35 con Vref=5V)
unsigned int read_temp_c() {
   unsigned int raw = ADC_Read(TEMP_CH);
   return (raw * 500UL) / 1023UL;
}

// Convertir % a duty cycle de 10 bits
unsigned int percentToDuty(unsigned int percent) {
   if (percent > 100) percent = 100;
   return (percent * 1023UL) / 100;
}

// Actualizar LCD (sin sprintf para ahorrar memoria)
void update_lcd(unsigned int temp, unsigned int level) {
   char txt[6];

   Lcd_Cmd(_LCD_CLEAR);

   // Línea 1
   Lcd_Out(1, 1, "Temp:");
   WordToStr(temp, txt); Lcd_Out_CP(txt); Lcd_Chr_CP('C');
   Lcd_Out_CP(" Lvl:");
   WordToStr(level, txt); Lcd_Out_CP(txt);

   // Línea 2
   Lcd_Out(2, 1, "A:");
   ByteToStr(A, txt); Lcd_Out_CP(txt);
   Lcd_Out_CP(" B:"); ByteToStr(B, txt); Lcd_Out_CP(txt);
   Lcd_Out_CP(" C:"); ByteToStr(C, txt); Lcd_Out_CP(txt);
   Lcd_Out_CP(" D:"); ByteToStr(D, txt); Lcd_Out_CP(txt);
}

// ==================== PROGRAMA PRINCIPAL ====================
void main() {
   // Config ADC
   ADC_Init();
   ADCON1 = 0x0D; // AN0 y AN1 como analógicos, resto digitales

   // Configurar pines como salida
   TRISB.B0 = 0;
   TRISB.B1 = 0;
   TRISB.B2 = 0;
   TRISB.B3 = 0;

   // Estado inicial
   all_valves_off();

   // Config LCD
   Lcd_Init();
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Cmd(_LCD_CURSOR_OFF);

   // Config PWM
   PWM1_Init(5000); // Resistencia
   PWM2_Init(5000); // Motor
   PWM1_Start();
   PWM2_Start();
   PWM1_Set_Duty(0);
   PWM2_Set_Duty(0);

   while (1) {
       // Leer sensores
       level_pct = read_level_percent();
       temp_C = read_temp_c();

       // Actualizar LCD
       update_lcd(temp_C, level_pct);

       // Máquina de estados
       switch (state) {
           case ST_INIT:
               all_valves_off();
               A = 1; // Llenar desde A
               PWM1_Set_Duty(0);
               PWM2_Set_Duty(0);
               state = ST_FILL_A;
               break;

           case ST_FILL_A:
               if (level_pct >= 30) { all_valves_off(); B = 1; state = ST_FILL_B; }
               break;

           case ST_FILL_B:
               if (level_pct >= 60) { all_valves_off(); C = 1; state = ST_FILL_C; }
               break;

           case ST_FILL_C:
               if (level_pct >= 100) {
                   all_valves_off();
                   PWM1_Set_Duty(percentToDuty(100)); // Calentar
                   PWM2_Set_Duty(percentToDuty(50));  // Motor
                   heat_elapsed_ms = 0;
                   state = ST_HEAT;
               }
               break;

           case ST_HEAT:
               if (temp_C < 70)       PWM1_Set_Duty(percentToDuty(100));
               else if (temp_C < 80)  PWM1_Set_Duty(percentToDuty(20));
               else                   PWM1_Set_Duty(percentToDuty(0));

               delay_ms(250);
               heat_elapsed_ms += 250;

               if (heat_elapsed_ms >= RECIPE_TIME_MS) {
                   PWM1_Set_Duty(0);
                   PWM2_Set_Duty(percentToDuty(50));
                   D = 1;
                   state = ST_DRAIN;
               }
               break;

           case ST_DRAIN:
               if (level_pct <= 5) {
                   all_valves_off();
                   PWM1_Set_Duty(0);
                   PWM2_Set_Duty(0);
                   state = ST_INIT;
               }
               break;
       }

       delay_ms(50); // Pequeña pausa
   }
}




Si ya eres usuario del sistema, puedes contestar desde tu cuenta y así ganar prestigio.

Si sólo eres un visitante, puedes optar por...