PIC16F877-04I/P
Controls
Properties
Assets
id=Footprint_V5
id=DIP-40_W15.24mm
id=3D
id=default
Simulation
while (true) {
if (input(PIN_PIR) == 1) { //IF IS TRUE ACTIVE THE SENSOR LM35
float temperatura = leerTemperaturaLM35();
if (temperatura > 25.0) {
controlarVentilador(1);
delay_ms(5000);
} else {
controlarVentilador(0);//IF THE CONDITION IS FALSE NO ACTIVE THE SENSOR LM35
}
}
delay_ms(100);
}
temperatura = (adc_value * 5.0) / 1023.0;
temperatura = temperatura * 100.0;
return temperatura;
#define PIN_LM35 PIN_B2 ERROR (ES UN PIN ANALOGICO AN0/RC0))
. Indeed, PIN_B2 cannot be used for LM35 because it's a digital I/O pin, and for LM35 temperature sensor, an analog input pin is required. Given you're using set_adc_channel(0);
, you are correctly planning to use AN0 which is associated with RC0
on the PIC16F877A, not RA0
. So ensure your hardware connection matches this pin for the LM35 sensor.#fuses HS
, ensure you have a High-Speed crystal oscillator attached to your OSC1/CLKI
and OSC2/CLKO
pins for the system clock. Assuming your comment #use delay(clock=20000000)
means you intend to use a 20MHz crystal.AN0
, which corresponds to RA0
on the PIC16F877A. Please note that in your description, AN0/RA0
was listed for pin number 2 (this may require connection adjustment based on actual hardware layout).RB0
(INT/RB0
) on the PIC16F877A for the digital reading.PIN_FAN
, which corresponds to RB3
(PGM/RB3
). Ensure your fan control circuitry or relay is compatible with the output current and voltage levels of the PIC16F877A.Plaintext
- Connect LM35 output to AN0/RA0 (Ensure this is correct based on your hardware setup, as your code suggests using AN0) - Connect PIR sensor output to RB0 (INT/RB0) - Connect fan control mechanism to RB3 (PGM/RB3)
while (true) {
if (input(PIN_PIR) == 1) { //IF IS TRUE ACTIVE THE SENSOR LM35
float temperatura = leerTemperaturaLM35();
if (temperatura > 25.0) {
controlarVentilador(1);
delay_ms(5000);
} else {
controlarVentilador(0);//IF THE CONDITION IS FALSE NO ACTIVE THE SENSOR LM35
}
}
delay_ms(100);
}
temperatura = (adc_value * 5.0) / 1023.0;
temperatura = temperatura * 100.0;
return temperatura;
C
#include <xc.h> #define _XTAL_FREQ 20000000 //20MHz Crystal Oscillator void UART_init(void){ SPBRG = 129; // Set baud rate as 9600 for Fosc = 20MHz TXSTA = 0x20; // Enable transmitter RCSTA = 0x90; // Enable receiver } void UART_send_char(char ch){ while(!TRMT); // Wait until transmitter is empty TXREG = ch; } void main(void){ UART_init(); while(1){ UART_send_char('A'); __delay_ms(1000); } }
C
#include <xc.h> #define _XTAL_FREQ 20000000 //20MHz Crystal Oscillator void UART_init(void){ SPBRG = 129; // Baud rate=9600 for 20MHz TXSTA = 0x20; // Enable Transmitter RCSTA = 0x90; // Enable Receiver } char UART_get_char(void){ if(OERR){ // check for an overrun error CREN = 0; // Reset CREN (receiver) CREN = 1; // enable receiver again } while(!RCIF); // Wait until the data is ready to be read return RCREG; // return the received data } void main(void){ UART_init(); TRISB = 0; // Set PORTB as output PORTB = 0; // Initialize PORTB as 0 char received_data; while(1){ received_data = UART_get_char(); if(received_data == 'A'){ // if received data is 'A' PORTB = 0xFF; // Switch ON all LEDs connected to PORTB __delay_ms(1000); // Delay of 1 second PORTB = 0x00; // Switch OFF all LEDs connected to PORTB } } }
C
#include <xc.h> #define _XTAL_FREQ 4000000 // Asumiendo que usas un cristal de 4MHz // Configuración de bits de configuración #pragma config FOSC = HS // Oscilador de alta velocidad #pragma config WDTE = OFF // Watchdog Timer desactivado #pragma config PWRTE = OFF // Power-up Timer desactivado #pragma config BOREN = ON // Brown-out Reset habilitado #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming desactivado #pragma config CPD = OFF // Data code protection desactivado #pragma config WRT = OFF // Flash Program Memory write protection desactivado #pragma config CP = OFF // Code protection desactivado void main(void) { TRISB = 0x00; // Configura el puerto B como salida int contador = 0; // Inicializa el contador while(1) { PORTB = contador; // Muestra el valor del contador en PORTB __delay_ms(1000); // Espera un segundo contador++; // Incrementa el contador // Si el contador llega a 17, reinícialo a 0 if(contador == 17) { contador = 0; } } }
_XTAL_FREQ
en consecuencia. La configuración de bits de configuración también puede necesitar ajustes según tus necesidades específicas (por ejemplo, si estás utilizando otro método de reloj).while (true) {
if (input(PIN_PIR) == 1) {
float temperatura = leerTemperaturaLM35();
if (temperatura > 25.0) {
controlarVentilador(1);
delay_ms(5000);
} else {
controlarVentilador(0);
}
}
delay_ms(100);
}
temperatura = (adc_value * 5.0) / 1023.0;
temperatura = temperatura * 100.0;
return temperatura;
PIN_LM35
en el PIN_B2, en realidad para leer el sensor LM35 se utiliza el canal analógico, especificado en set_adc_channel(0);
. El canal 0 analógico es AN0
, que corresponde a RC0
o pin 15 en el PIC16F877A, no PIN_B2
. Es importante aclarar este detalle para evitar confusión. En tu código, el nombre PIN_LM35
no se utiliza realmente en la función leerTemperaturaLM35()
. En su lugar, especificas directamente el canal analógico 0 con set_adc_channel(0);
, que es el correcto enfoque para leer el sensor LM35 conectado a RC0/AN0
.leerTemperaturaLM35()
, haces correctamente la llamada a set_adc_channel(0);
para leer valores analógicos del sensor LM35 conectado al RC0/AN0
, no PIN_B2
. Recuerda, RC0/AN0
es el canal analógico 0 y corresponde al pin 15 en el PIC16F877A.controlarVentilador(int encendido)
, hay un pequeño error lógico en la comparación: $if (encendido >1) {$, debería ser $if (encendido ==1) {$, para encender el ventilador si la variable $encendido$ es 1, y apagarlo si es 0.Fuente de alimentación (V+) --- Ventilador+
Ventilador- --- Colector (2N2222)
Emisor (2N2222) --- GND
Base (2N2222) --- Resistor (1kΩ) --- PIN_B3 del PIC16F877A
Diodo de rueda libre (1N4007) entre V+ y colector del 2N2222 (cátodo en V+, ánodo en colector)
Plaintext
PIC16F877A LED Pin RB0 -----> Anodo ------ + Pin RB1 -----> Anodo ------ + Vss -----> Cátodo (a través de resistencia de 330 ohm)
C
#include <xc.h> #define _XTAL_FREQ 4000000 // Esto se utiliza por la macro __delay_ms void main() { TRISB0 = 0; // Configura el pin RB0 como salida TRISB1 = 0; // Configura el pin RB1 como salida while(1) { RB0 = 1; // Enciende el LED en el pin RB0 RB1 = 0; // Apaga el LED en el pin RB1 __delay_ms(500); // Espera medio segundo RB0 = 0; // Apaga el LED en el pin RB0 RB1 = 1; // Enciende el LED en el pin RB1 __delay_ms(500); // Espera medio segundo } }
_XTAL_FREQ
al valor de tu cristal para que la función __delay_ms()
funcione correctamente. PIC16F877-04I/P Pin RB0 ----------(A) LED (K)------- Resistor 220 Ω ------- GND
C
#include <xc.h> // CONFIG #pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = ON #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF #define _XTAL_FREQ 4000000 int main() { TRISB0 = 0; // Configura el pin RB0 como salida RB0 = 1; // Enciende el LED en RB0 while(1); }
PIC16F877A Pin RB0 <---------- GP2Y0A21YK0F Sensor Output
PIC16F877A Vcc ----------> GP2Y0A21YK0F Vcc
PIC16F877A GND ----------> GP2Y0A21YK0F GND
C
#include <xc.h> #define _XTAL_FREQ 4000000 // Asegúrate de ajustar esto a la frecuencia de tu oscilador int main() { TRISB0 = 1; // Configura el pin RB0 como entrada while(1) { if (RB0 == 1) { // El sensor detectó un objeto, aquí puedes poner el código a realizar en este caso } else { // El sensor no detectó un objeto, aquí puedes poner el código a realizar en este caso } } }
Welcome 👋
Flux helps you build PCBs faster with an AI teammate!
To use this component and get started with Flux sign up now.