我正在使用mini2440主板。我想为pwm开发一个可以增加和减少信号频率的gui。它有四个按钮 - 开始,停止,最大和最小 - 但按下最大按钮不会改变频率。通过使用启动和停止gui程序终止但仍然pwm打开,我的意思是蜂鸣器开启。
这是代码
#include "hello.h"
#include <qlabel.h>
#include <qpushbutton.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <qobject.h>
#include <fstream>
#include <linux/i2c-dev.h>
#include <string.h>
#include <stdio.h>
#include <linux/fs.h>
#include <termios.h> //Headers
#include <stdint.h>
#include <getopt.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
int freq=1000;
int fd;
HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl)
:HelloBaseForm(parent, name, fl)
{
connect(PushButton33,SIGNAL(clicked()),this,SLOT(start()));
connect(PushButton30,SIGNAL(clicked()),this,SLOT(stop()));
connect(PushButton31,SIGNAL(clicked()),this,SLOT(plus()));
connect(PushButton32,SIGNAL(clicked()),this,SLOT(minus()));
}
HelloForm::~HelloForm()
{
}
// this is start button
void HelloForm::start()
{
int fd = open("/dev/pwm", 0);
if (fd < 0) {
status->setText("open pwm_buzzer device");
}
else {
status->setText("start");
}
ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
pwm->setText(QString::number(freq));
//close(fd);
}
//stop button
void HelloForm::stop()
{
close(fd);
int fd=open("/dev/pwm",0); //File handler
ioctl(fd, PWM_IOCTL_STOP); // stop the ioctl
close(fd);
}
// increase the pwm
void HelloForm::plus()
{
if(freq<20000)
{
freq+=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq); //freq set
pwm->setText(QString::number(freq));
}
}
void HelloForm::minus()
{
if(freq>11)
{
freq-=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq);
pwm->setText(QString::number(freq));
}
}