Hello Everyone,
I am new to beagle bone black and Linux. so please help me.
I want to Calculate Pulse width in microseconds. I want to measure pulse width approx 100 us. so how can I do that if anyone has a C code for that please help. I designed one code but it does not show proper output for microseconds of range. when I give it greater than 1 ms of pulse it shows fine output.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/time.h>
#include <poll.h>
#define GPIO_PATH "/sys/class/gpio"
#define P8_9 "69" // Change this to the GPIO pin you want to control
// Global variable for value_fd
int value_fd;
// Function declarations
void cleanup();
// Function to check if GPIO is already exported
int is_gpio_exported(const char *gpio_pin) {
char path[255];
sprintf(path, GPIO_PATH "/gpio%s", gpio_pin);
return access(path, F_OK) != -1;
}
// Function to export a GPIO pin if not already exported
void export_gpio(const char *gpio_pin) {
if (!is_gpio_exported(gpio_pin)) {
FILE *export_file = fopen(GPIO_PATH "/export", "w");
if (export_file == NULL) {
perror("Error exporting GPIO");
exit(EXIT_FAILURE);
}
fprintf(export_file, "%s", gpio_pin);
fclose(export_file);
}
}
// Function to set the direction of a GPIO pin
void set_gpio_direction(const char *gpio_pin, const char *direction) {
char path[255];
sprintf(path, GPIO_PATH "/gpio%s/direction", gpio_pin);
FILE *direction_file = fopen(path, "w");
if (direction_file == NULL) {
perror("Error setting GPIO direction");
exit(EXIT_FAILURE);
}
fprintf(direction_file, "%s", direction);
fclose(direction_file);
}
// Function to set a GPIO pin high
void set_gpio_high(const char *gpio_pin) {
char path[255];
sprintf(path, GPIO_PATH "/gpio%s/value", gpio_pin);
FILE *value_file = fopen(path, "w");
if (value_file == NULL) {
perror("Error setting GPIO high");
exit(EXIT_FAILURE);
}
fprintf(value_file, "1");
fclose(value_file);
}
// Function to set a GPIO pin low
void set_gpio_low(const char *gpio_pin) {
char path[255];
sprintf(path, GPIO_PATH "/gpio%s/value", gpio_pin);
FILE *value_file = fopen(path, "w");
if (value_file == NULL) {
perror("Error setting GPIO low");
exit(EXIT_FAILURE);
}
fprintf(value_file, "0");
fclose(value_file);
}
// Function to unexport a GPIO pin
void unexport_gpio(const char *gpio_pin) {
FILE *unexport_file = fopen(GPIO_PATH "/unexport", "w");
if (unexport_file == NULL) {
perror("Error unexporting GPIO");
exit(EXIT_FAILURE);
}
fprintf(unexport_file, "%s", gpio_pin);
fclose(unexport_file);
}
// Signal handler for SIGINT (Ctrl+C)
void signal_handler(int signo) {
if (signo == SIGINT) {
printf("\nReceived SIGINT. Cleaning up...\n");
cleanup();
exit(EXIT_SUCCESS);
}
}
// Function to set GPIO low and unexport the pin
void cleanup() {
set_gpio_low(P8_9);
unexport_gpio(P8_9);
}
// Function to read the value of a GPIO pin
int read_gpio_value(const char *gpio_pin) {
char path[255];
sprintf(path, GPIO_PATH "/gpio%s/value", gpio_pin);
FILE *value_file = fopen(path, "r");
if (value_file == NULL) {
perror("Error reading GPIO value");
exit(EXIT_FAILURE);
}
int value;
fscanf(value_file, "%d", &value);
fclose(value_file);
return value;
}
void measure_low_pulse_width(const char *gpio_pin) {
struct timeval last_edge_time, current_time;
// Open value file descriptor
int value_fd = open(GPIO_PATH "/gpio" P8_9 "/value", O_RDONLY);
if (value_fd == -1) {
perror("Error opening value file");
exit(EXIT_FAILURE);
}
// Register cleanup function
if (atexit(cleanup) != 0) {
perror("Error registering cleanup function");
exit(EXIT_FAILURE);
}
while (1) {
// Wait for low signal
while (read_gpio_value(gpio_pin) == 1) {}
// Measure low pulse width
gettimeofday(&last_edge_time, NULL);
// Wait for high signal
while (read_gpio_value(gpio_pin) == 0) {}
gettimeofday(¤t_time, NULL);
// Calculate and print pulse width in seconds
double pulse_width = (current_time.tv_sec - last_edge_time.tv_sec) +
(current_time.tv_usec - last_edge_time.tv_usec) / 1.0e6;
// Print only low pulse widths
//if (pulse_width >= 0.5) {
// printf("Low Pulse Width: %f seconds\n", pulse_width);
printf("Low Pulse Width: %lf milliseconds\n", pulse_width * 1000);
// printf("Low Pulse Width: %lf microseconds\n", pulse_width * 1.0e6);
// }
}
}
int main() {
// Register the signal handler
if (signal(SIGINT, signal_handler) == SIG_ERR) {
perror("Error registering signal handler");
exit(EXIT_FAILURE);
}
// Export the GPIO pin
export_gpio(P8_9);
// Set the GPIO direction to out
set_gpio_direction(P8_9, "in");
// Measure low pulse width
measure_low_pulse_width(P8_9);
return 0;
}