linux蜂鸣器测试程序
2012-04-06 22:10
288 查看
必要的头文件#include<stdio.h>//必要的头文件 #include<termios.h>//POSIX终端控制定义 #include<unistd.h> #include<stdlib.h> #definePWM_IOCTL_SET_FREQ 1 #definePWM_IOCTL_STOP 0 #define ESC_KEY 0x1b//定义ESC_KEY为ESC按键的键值
staticintgetch(void)//从终端获得输入,并把输入转化为int返回
{
structtermiosoldt,newt;//终端结构体structtermios
intch;
if(!isatty(STDIN_FILENO)){//判断串口是否与标准输入相连,isatty(fd)判断fd是否是终端设备
fprintf(stderr,"thisproblemshouldberunataterminal\n");
exit(1);
}
//saveterminalsetting
if(tcgetattr(STDIN_FILENO,&oldt)<0){//过去fd的终端属性,并把它放入oldt中断结构体中,以为后面恢复使用
perror("savetheterminalsetting");
exit(1);
}
//setterminalasneed
newt=oldt;
newt.c_lflag&=~(ICANON|ECHO);//控制终端编辑功能参数ICANON表示使用标准输入模式;参数ECH0表示进行回送
if(tcsetattr(STDIN_FILENO,TCSANOW,&newt)<0){//表示更改立即生效
perror("setterminal");
exit(1);
}
ch=getchar();//输入数据
//restoretermialsetting
if(tcsetattr(STDIN_FILENO,TCSANOW,&oldt)<0){//恢复原来的终端属性
perror("restorethetermialsetting");
exit(1);
}
returnch;
}
staticintfd=-1;
staticvoidclose_buzzer(void);//关闭蜂鸣器
staticvoidopen_buzzer(void)//打开蜂鸣器
{
fd=open("/dev/pwm",0);//打开蜂鸣器的设备文件
if(fd<0){//打开失败
perror("openpwm_buzzerdevice");
exit(1);
}
//anyfunctionexitcallwillstopthebuzzer
atexit(close_buzzer);//注册清除函数
}
staticvoidclose_buzzer(void)//关闭蜂鸣器
{
if(fd>=0){
ioctl(fd,PWM_IOCTL_STOP);//停止蜂鸣器
close(fd);//关闭设备驱动程序文件
fd=-1;
}
}
staticvoidset_buzzer_freq(intfreq)
{
//thisIOCTLcommandisthekeytosetfrequency
intret=ioctl(fd,PWM_IOCTL_SET_FREQ,freq);//设置蜂鸣器的频率
if(ret<0){//如果设置出错
perror("setthefrequencyofthebuzzer");
exit(1);
}
}
staticvoidstop_buzzer(void)
{
intret=ioctl(fd,PWM_IOCTL_STOP);//关闭蜂鸣器
if(ret<0){//如果无法关闭蜂鸣器
perror("stopthebuzzer");
exit(1);
}
}
intmain(intargc,char**argv)
{
intfreq=1000;
open_buzzer();//打开蜂鸣器
//打印提示信息
printf("\nBUZZERTEST(PWMControl)\n");
printf("Press+/-toincrease/reducethefrequencyoftheBUZZER\n");
printf("Press'ESC'keytoExitthisprogram\n\n");
while(1)
{
intkey;
set_buzzer_freq(freq);//设置蜂鸣器的频率
printf("\tFreq=%d\n",freq);
key=getch();//从键盘获取数据
switch(key){//输入数据判断
case'+':
if(freq<20000)
freq+=10;
break;
case'-':
if(freq>11)
freq-=10;
break;
caseESC_KEY:
caseEOF:
stop_buzzer();//停止蜂鸣器
exit(0);
default:
break;
}
}
}
相关文章推荐
- linux下一致性网关测试程序qt开发环境搭建
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- 【2】在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- 嵌入式linux串口通信自发自收测试程序
- linux串口测试程序
- Linux下测试程序运行时间模板
- Linux环境下的opencv的配置和测试程序
- linux下通过编写测试程序的方式来确认arm-linux-gnueabihf-gcc-4.6lib和.h文件的安装位置,为复制libnl做准备
- 2.在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序(老罗学习笔记2)
- 【Xilinx-Petalinux学习】-05-OpenCV程序测试
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- Linux下的通信时延测试程序
- linux下测试程序中各函数执行时间工具
- hadoop 从客户端的hdfs测试程序连到linux 上的hadoop(hdfs)要调用的方法
- Linux串口测试工具简单程序代码解析
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- cb串口测试程序http://linux-sunxi.org/A20/PIO
- Linux移植随笔:对tslib库的ts_test测试程序代码的一点分析【转】
- linux下串口程序测试