if(tcsetattr(STDIN_FILENO,TCSANOW, &newt) < 0) {
perror('set terminal');
exit(1);
}
ch = getchar();
// restore termial setting
if(tcsetattr(STDIN_FILENO,TCSANOW,&oldt) < 0) {
perror('restore the termial setting');
exit(1);
}
return ch;
}
static int fd = -1;
static void close_buzzer(void);
static void open_buzzer(void)
{
fd = open('/dev/pwm', 0);
if (fd < 0) {
perror('open pwm_buzzer device');
exit(1);
}
// any function exit call will stop the buzzer
atexit(close_buzzer);
}
static void close_buzzer(void)
{
if (fd >= 0) {
ioctl(fd, PWM_IOCTL_STOP);
close(fd);
fd = -1;
}
}
static void set_buzzer_freq(int freq)
{
// this IOCTL command is the key to set frequency
int ret = ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
if(ret < 0) {
perror('set the frequency of the buzzer');
exit(1);
}
}
static void stop_buzzer(void)
{
int ret = ioctl(fd, PWM_IOCTL_STOP);
if(ret < 0) {
perror('stop the buzzer');
exit(1);
}
}
int main(int argc, char **argv)
{
int freq = 1000 ;
open_buzzer();
printf( 'nBUZZER TEST ( PWM Control )n' );
printf( 'Press +/- to increase/reduce the frequency of the BUZZERn' ) ;
printf( 'Press 'ESC' key to Exit this programnn' );
while( 1 )
{
int key;
set_buzzer_freq(freq);
printf( 'tFreq = %dn', freq );
key = getch();
switch(key) {
case '+':
if( freq < 20000 )
freq += 10;
break;
case '-':
if( freq > 11 )
freq -= 10 ;
break;
case ESC_KEY:
case EOF:
stop_buzzer();
exit(0);
default:
break;
}
}
}
通过调用ioctl函数实现控制PWM的频率以及关闭PWM.
上一篇:linux-2.6.32在mini2440开发板上移植 按键驱动程序移植
下一篇:mini2440 Norflash驱动移植过程
推荐阅读最新更新时间:2024-11-09 09:17