在前面一章的測試程序的基礎上,修改了另外一個pwm蜂鳴器驅動測試程序,這個相對更簡單。不多說,直接上程序。
1.編寫PWM蜂鳴器驅動的測試程序。文件名:pwm_test.c.
<pre class="html" name="code">#include <stdio.h> //必要的頭文件--輸入輸出函數定義
#include <stdlib.h> //標準函數庫定義
#include <unistd.h> //Unix 標準函數定義
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h> //定義了很多宏和open,fcntl函數原型
int main(int argc, char **argv)
{
int fd;
unsigned long temp =0;
int i;
fd = open("/dev/pwm", O_RDWR); //打開蜂鳴器的設備文件
if (fd < 0) //打開失敗
{
perror("open device pwm");
exit(1); //打開錯誤,則終止進程。退出參數爲1
}
printf("open device sucess\n");
for(i=0;i<2000;i++) //響應輸入的蜂鳴器頻率
{
scanf("%d",&temp);
printf("temp= %d \n",temp);
ioctl(fd, 1,temp);
if(temp == 0) //當輸入爲0是時退出
break;
}
ioctl(fd, 0); //停止蜂鳴器
close(fd); //關閉設備驅動程序文件
return 0;
}
2.在linux上交叉編譯測試應用程序。
# arm-linux-gcc -o pwm_test pwm_test.c -static
3.將生成的pwm_test複製到EM4412開發板的system目錄下,然後進入開發板的system目錄,給pwm_test權限。
#chmod 777 pwm_test
4.運行pwm_test。
# ./pwm_test
這時候你可以輸入從1到2000的任意值,蜂鳴器就響了,輸入0就關閉。