75 lines
1.8 KiB
C
75 lines
1.8 KiB
C
/*
|
|
Code pour lire la data du I2C decibel meter
|
|
Pour compiler le code: gcc -o sound_meter sound_meter.c
|
|
Connexion I2C:
|
|
SDA to Pi Pico GPIO 2
|
|
SCL to Pi Pico GPIO 3
|
|
|
|
Device connected on addr 0x48
|
|
|
|
0x0A : Decibel register (Latest sound intensity value in decibels, averaged over the last Tavg time period.)
|
|
0x0B : Min register (Minimum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
|
0x0C : Max register (Maximum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
|
|
|
#Le Tavg est programmé sur 2 octets: le high byte et le low byte.
|
|
valeur = (Tavg_high x 256) + Tavg_low
|
|
0x07 #Averaging time (high byte) in milliseconds for calculating sound intensity levels. (default 0x03)
|
|
0x08 #Averaging time (low byte) in milliseconds for calculating sound intensity levels. (default 0xE8)
|
|
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <linux/i2c-dev.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
int main()
|
|
{
|
|
int file;
|
|
char data;
|
|
|
|
// Open I2C1 for reading the sound meter module registers
|
|
if ((file = open("/dev/i2c-1", O_RDWR)) < 0)
|
|
{
|
|
perror("Failed to open I2C1!");
|
|
exit(1);
|
|
}
|
|
|
|
// 0x48 is the decibel meter I2C address
|
|
if (ioctl (file, I2C_SLAVE, 0x48) < 0)
|
|
{
|
|
perror ("db Meter module is not connected/recognized at I2C addr = 0x48");
|
|
close (file);
|
|
exit (1);
|
|
}
|
|
|
|
while (1)
|
|
{
|
|
// Decibel value is stored in register 0x0A
|
|
data = 0x0A;
|
|
|
|
//write: send 1 byte from data (memory address pointer) to file
|
|
if (write (file, &data, 1) != 1)
|
|
{
|
|
perror ("Failed to write register 0x0A");
|
|
close (file);
|
|
exit (1);
|
|
}
|
|
|
|
if (read (file, &data, 1) != 1)
|
|
{
|
|
perror ("Failed to read register 0x0A");
|
|
close (file);
|
|
exit (1);
|
|
}
|
|
|
|
printf ("Sound level: %d dB SPL\r\n\r\n", data);
|
|
sleep (1);
|
|
}
|
|
|
|
close (file);
|
|
return 0;
|
|
} |