PIC16F877A-Ultrasonic-Configuration

by Marwen Maghrebi
#include <xc.h>
#include <stdint.h>
#include <stdio.h>

// Configuration bits
#pragma config FOSC = HS        // High-Speed Crystal
#pragma config WDTE = OFF       // Watchdog Timer disabled
#pragma config PWRTE = ON       // Power-up Timer enabled
#pragma config BOREN = ON       // Brown-out Reset enabled
#pragma config LVP = OFF        // Low Voltage Programming disabled
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection disabled
#pragma config WRT = OFF        // Flash Program Memory Write disabled
#pragma config CP = OFF         // Flash Program Memory Code Protection disabled

#define _XTAL_FREQ 20000000     // 20 MHz oscillator frequency

// Pin Definitions
#define TRIG_PIN RB0            // Trigger pin connected to RB0
#define ECHO_PIN RB1            // Echo pin connected to RB1
#define TRIG_PIN_DIR TRISB0     // Direction register for TRIG_PIN
#define ECHO_PIN_DIR TRISB1     // Direction register for ECHO_PIN

// Constants
#define US_ROUNDTRIP_CM 145      // Microseconds for sound to travel 1 cm (adjust for calibration)

// Function Prototypes
void initialize_uart(void);
void uart_send_string(const char* str);
void uart_send_byte(uint8_t data);
void initialize_pins(void);
uint16_t measure_distance(void);

// UART Buffer
char uart_buffer[32];
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00