按不同的按键出不同的响声,其中有一个按键因为参数设置的为65535所以基本听不到蜂鸣器所产生的声音,因为高电平的占空比趋近于0了。应用程序如下,请参考:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pwm_start 0x01
#define pwm_stop 0x02
int buttons_fd;
int leds_fd;
int eeprom_fd;
int pwm_fd;
char buttons[4] = {'0', '0', '0', '0',};
int value[512];
int main(void)
{
value[0]=0x12;
value[1]=0x23;
value[2]=0x34;
value[3]=0x45;
value[4]=0x56;
value[5]=0x67;
buttons_fd = open("/dev/keys_eint", O_RDWR);
leds_fd=open("/dev/LED_CONTROL",O_RDWR);
eeprom_fd=open("/dev/at24lc04",O_RDWR);
pwm_fd=open("/dev/pwm_micro2440_drv",O_RDWR);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
if(leds_fd<0)
{
perror("open device leds");
}
while(1) {
char current_buttons[4];
int count_of_changed_key;
int i;
if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
perror("read buttons:");
exit(1);
}
for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
if (buttons[i] != current_buttons[i]) {
buttons[i] = current_buttons[i];
printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");
switch(i)
{
case 0:
ioctl(leds_fd,0);
write(eeprom_fd,value,1);
//sleep(1);
value[0]=0;
read(eeprom_fd,value,1);
printf("\n write reg[0]data:%x to at24lc04\n",value[0]);
ioctl(pwm_fd,pwm_start,1);
break;
case 1:
ioctl(leds_fd,1);
//write(eeprom_fd,value+sizeof(value[0]),1);
write(eeprom_fd,value+1,1);
//sleep(1);
value[1]=0;
read(eeprom_fd,value+1,1);
printf("\n write reg[1]data:%x to at24lc04\n",value[1]);
ioctl(pwm_fd,pwm_start,2);
break;
case 2:
ioctl(leds_fd,2);
write(eeprom_fd,value+2,10);
//sleep(1);
value[2]=0;
read(eeprom_fd,value+2,1);
printf("\n write reg[2]data:%x to at24lc04\n",value[2]);
ioctl(pwm_fd,pwm_start,1);
break;
case 3:
ioctl(leds_fd,3);
write(eeprom_fd,value+3,1);
//sleep(1);
value[3]=0;
read(eeprom_fd,value+3,1);
printf("\n write reg[3]data:%x to at24lc04\n",value[3]);
ioctl(pwm_fd,pwm_start,65535);
break;
default:
break;
}
count_of_changed_key++;
}
}
if (count_of_changed_key) {
printf("\n");
}
}
close(eeprom_fd);
close(buttons_fd);
close(leds_fd);
close(pwm_fd);
return 0;
}
下一步搞ADC以及LCD控制器等
阅读(2035) | 评论(0) | 转发(0) |