ELF1 板载蜂鸣器为无源蜂鸣器,由 PWM 进行控制。
将 PWM 设置为输出:
echo 0 > /sys/class/pwm/pwmchip1/export
设置 PWM 周期,单位为 ns:
echo 1000000 > /sys/class/pwm/pwmchip1/pwm0/period
设置 PWM 占空比为 50%:
echo 500000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle
使能 PWM 功能:
echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
此时可以听到蜂鸣器响起. 关闭蜂鸣器,失能 PWM 功能:
echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
ELF1 主板上集成了温湿度传感器 AHT20 。在终端执行 elf1_cmd_aht20
程序:
elf1_cmd_aht20
[16:27:07:228] hum = 42.20 temp = 25.00
[16:27:07:819] hum = 42.30 temp = 25.00
[16:27:08:410] hum = 42.30 temp = 25.00
经过测试蜂鸣器和温湿度传感器都是正常的,那我们就可以用这 2 个元件,实现一个简易的报警装置。 代码实现: tempAlarm.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#define AHT20_DEV "/dev/aht20"
#define BUZZER_PWM_PATH "/sys/class/pwm/pwmchip1"
#define BUZZER_EXPORT BUZZER_PWM_PATH "/export"
#define BUZZER_PERIOD BUZZER_PWM_PATH "/pwm0/period"
#define BUZZER_DUTY_CYCLE BUZZER_PWM_PATH "/pwm0/duty_cycle"
#define BUZZER_ENABLE BUZZER_PWM_PATH "/pwm0/enable"
#define LED1_PATH "/sys/class/leds/led1/brightness"
#define TEMP_THRESHOLD 30.0
#define HUM_THRESHOLD 55.0
void init_buzzer() {
if (access(BUZZER_ENABLE, F_OK) != 0) {
int export_fd = open(BUZZER_EXPORT, O_WRONLY);
if (export_fd < 0) {
perror("Error opening export file for buzzer");
exit(EXIT_FAILURE);
}
write(export_fd, "0", 1);
close(export_fd);
}
int period_fd = open(BUZZER_PERIOD, O_RDWR);
if (period_fd < 0) {
perror("Error opening period file for buzzer");
exit(EXIT_FAILURE);
}
write(period_fd, "1000000", 7);
close(period_fd);
int duty_cycle_fd = open(BUZZER_DUTY_CYCLE, O_RDWR);
if (duty_cycle_fd < 0) {
perror("Error opening duty_cycle file for buzzer");
exit(EXIT_FAILURE);
}
write(duty_cycle_fd, "500000", 6);
close(duty_cycle_fd);
}
void enable_buzzer() {
int enable_fd = open(BUZZER_ENABLE, O_RDWR);
if (enable_fd < 0) {
perror("Error opening enable file for buzzer");
exit(EXIT_FAILURE);
}
write(enable_fd, "1", 1);
close(enable_fd);
int brightness_fd = open(LED1_PATH, O_RDWR);
if (brightness_fd < 0) {
perror("Error opening brightness file for led1");
exit(EXIT_FAILURE);
}
write(brightness_fd, "1", 1);
close(brightness_fd);
}
void disable_buzzer() {
int enable_fd = open(BUZZER_ENABLE, O_RDWR);
if (enable_fd < 0) {
perror("Error opening enable file for buzzer");
exit(EXIT_FAILURE);
}
write(enable_fd, "0", 1);
close(enable_fd);
int brightness_fd = open(LED1_PATH, O_RDWR);
if (brightness_fd < 0) {
perror("Error opening brightness file for led1");
exit(EXIT_FAILURE);
}
write(brightness_fd, "0", 1);
close(brightness_fd);
}
void check_temperature_humidity() {
int fd;
unsigned int databuf[2];
int c1,t1;
float hum,temp;
int ret = 0;
fd = open(AHT20_DEV, O_RDWR);
if(fd < 0) {
perror("Error opening AHT20 device file");
exit(EXIT_FAILURE);
}
while(1){
ret = read(fd, databuf, sizeof(databuf));
if(ret==0){
c1 = databuf[0] * 1000 / 1024 / 1024;
t1 = databuf[1] * 200 * 10 / 1024 / 1024 - 500;
temp = (float)t1 / 10.0;
hum = (float)c1 / 10.0;
printf("hum = %0.2f temp = %0.2f \r\n",hum,temp);
if (temp > TEMP_THRESHOLD) {
printf("Temperature exceeded threshold! Current temperature: %.2f°C\n", temp);
enable_buzzer();
} else if (hum > HUM_THRESHOLD) {
printf("Humidity exceeded threshold! Current humidity: %.2f%%\n", hum);
enable_buzzer();
} else {
disable_buzzer();
}
}
sleep(1);
}
}
int main() {
init_buzzer();
check_temperature_humidity();
return 0;
}
此外,除了蜂鸣器会报警外,红色LED也会点亮。
首先在 Ubuntu 主机下新建终端,执行设置环境命令
. /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
将上面的代码编译成二进制文件
$CC tempAlarm.c -o tempAlarm
查看二进制文件属性
file tempAlarm
tempAlarm: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=07f5d61a1aa831d3fac7f8fbc5b65b2c88065754, not stripped
因为我的路由器 LAN 口 IP 地址是 192.168.124.1
,所以每次开发板插电开机,都需要重新设置下动态获取 IP 地址。首先通过 CuteCOM 串口通信的办法,连上开发板。执行
udhcpc -i eth0
获取到新的 IP 地址
[01:29:10:581] udhcpc (v1.24.1) started
[01:29:10:715] Sending discover...
[01:29:11:043] Sending select for 192.168.124.22...
[01:29:11:164] Lease of 192.168.124.22 obtained, lease time 86400
[01:29:11:279] /etc/udhcpc.d/50default: Adding DNS 192.168.124.1
如果你不记得密码了,可以重置
passwd root
[01:35:34:091] Changing password for root␍␊
[01:35:34:091] Enter the new password (minimum of 5 characters)␍␊
[01:35:34:097] Please use a combination of upper and lower case letters and numbers.␍␊
[01:35:34:116] New password: ␍␊
[01:35:47:475] Bad password: too simple. ␍␊
[01:35:47:475] Warning: weak password (enter it again to use it anyway).␍␊
[01:35:47:495] New password: ␍␊
[01:35:50:776] Re-enter new password: ␍␊
[01:35:53:773] passwd: password changed.␍␊
如果是弱密码,需要重复输入3次,终端不会显示出密码,实际上是已经输入进去了的。 此时,Ubuntu 实机新开一个终端,执行
ftp 192.168.124.22
Connected to 192.168.124.22.
220 (vsFTPd 3.0.3)
Name (192.168.124.22:clark): root
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
这里输入刚才重置过的用户名和密码,就登录上去了。接着执行
ftp> put tempAlarm tempAlarm
拷贝二进制文件到开发板
ftp> put tempAlarm tempAlarm
local: tempAlarm remote: tempAlarm
229 Entering Extended Passive Mode (|||39594|)
150 Ok to send data.
100% |**********************************************************************************************************************************************************************| 11860 31.41 MiB/s 00:00 ETA
226 Transfer complete.
11860 bytes sent in 00:00 (943.46 KiB/s)
ftp>
回到 CuteCOM ,给执行权限
chmod +x tempAlarm
执行代码
./tempAlarm
[01:44:10:220] hum = 27.10 temp = 24.40 ␍␍␊
[01:44:11:310] hum = 27.10 temp = 24.40 ␍␍␊
[01:44:12:400] hum = 27.30 temp = 24.40 ␍␍␊
[01:44:13:490] hum = 27.80 temp = 24.40 ␍␍␊
[01:44:14:581] hum = 29.20 temp = 26.20 ␍␍␊
[01:44:15:670] hum = 31.00 temp = 28.10 ␍␍␊
[01:44:16:760] hum = 32.50 temp = 28.90 ␍␍␊
[01:44:17:850] hum = 34.00 temp = 29.50 ␍␍␊
[01:44:18:940] hum = 35.20 temp = 29.90 ␍␍␊
[01:44:20:029] hum = 36.40 temp = 30.10 ␍␍␊
[01:44:20:029] Temperature exceeded threshold! Current temperature: 30.10<0xc2><0xb0>C␍␊
[01:44:21:118] hum = 37.50 temp = 30.30 ␍␍␊
[01:44:21:118] Temperature exceeded threshold! Current temperature: 30.30<0xc2><0xb0>C␍␊
[01:44:22:209] hum = 38.50 temp = 30.50 ␍␍␊
[01:44:22:209] Temperature exceeded threshold! Current temperature: 30.50<0xc2><0xb0>C␍␊
[01:44:23:298] hum = 39.50 temp = 30.60 ␍␍␊
[01:44:23:298] Temperature exceeded threshold! Current temperature: 30.60<0xc2><0xb0>C␍␊
[01:44:24:389] hum = 40.30 temp = 30.60 ␍␍␊
[01:44:24:389] Temperature exceeded threshold! Current temperature: 30.60<0xc2><0xb0>C␍␊
[01:44:25:479] hum = 41.40 temp = 30.50 ␍␍␊
[01:44:25:479] Temperature exceeded threshold! Current temperature: 30.50<0xc2><0xb0>C␍␊
[01:44:26:570] hum = 37.30 temp = 29.60 ␍␍␊
[01:44:27:660] hum = 34.30 temp = 28.90 ␍␍␊
[01:44:28:750] hum = 32.00 temp = 28.30 ␍␍␊
[01:44:29:840] hum = 30.30 temp = 28.00 ␍␍␊
用大拇指轻轻按压 AHT20 温湿度传感器,当温度高于 30 摄氏度时,红色LED点亮,蜂鸣器报警。