你好,游客 登录 注册 搜索
背景:
阅读新闻

Mini2440开发板PWM驱动与测试程序简要分析

[日期:2013-01-23] 来源:Linux社区  作者:ssdsafsdsd [字体:大 中 小]

分析:static void PWM_Stop(void)

static void PWM_Stop(void)

{

        s3c2410_gpio_cfgpin(S3C2410_GPB(0), S3C2410_GPIO_OUTPUT);//配置管脚为输出状态

        s3c2410_gpio_setpin(S3C2410_GPB(0), 0);                  //设置管脚为低电平输出

}

 

以下是测试程序

#include <stdio.h>

#include <termios.h>

#include <unistd.h>

#include <stdlib.h>

 

#define PWM_IOCTL_SET_FREQ              1

#define PWM_IOCTL_STOP                        0

 

#define    ESC_KEY            0x1b

 

static int getch(void)

{

        struct termios oldt,newt;

        int ch;

 

        if (!isatty(STDIN_FILENO)) {

                  fprintf(stderr, "this problem should be run at a terminal\n");

                  exit(1);

        }

        // save terminal setting

        if(tcgetattr(STDIN_FILENO, &oldt) < 0) {

                  perror("save the terminal setting");

                  exit(1);

        }

 

        // set terminal as need

        newt = oldt;

        newt.c_lflag &= ~( ICANON | ECHO );

        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 BUZZER\n" ) ;

        printf( "Press 'ESC' key to Exit this program\n\n" );

       

       

        while( 1 )

        {

                  int key;

 

                  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;

 

                  case ESC_KEY:

                  case EOF:

                            stop_buzzer();

                            exit(0);

 

                  default:

                            break;

                  }

        }

}

通过调用ioctl函数实现控制PWM的频率以及关闭PWM.

linux
相关资讯       mini2440  Mini2440开发板  PWM驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款