Created a PWM demo using the newly introduced defines to

manipulate PWM frequency.
This commit is contained in:
Bernd Porr 2012-05-02 17:58:15 +01:00
parent aaa4514804
commit 90ea57b2e9
3 changed files with 167 additions and 1 deletions

View file

@ -4,7 +4,7 @@ noinst_PROGRAMS = \
choose_routing cmd do_waveform dio eeprom_dump gpct_buffered_counting \
gpct_encoder gpct_pulse_generator \
gpct_simple_counting inp inpn insn ledclock \
mmap outp poll receiver select \
mmap outp poll receiver select pwm \
sender sigio sv tut1 tut2 tut3
noinst_HEADERS = examples.h
@ -101,6 +101,11 @@ poll_SOURCES = poll.c common.c
poll_CFLAGS = $(COMEDILIB_CFLAGS)
poll_LDADD = $(COMEDILIB_LIBS)
pwm_SOURCES = pwm.c common.c
pwm_CFLAGS = $(COMEDILIB_CFLAGS)
pwm_LDADD = $(COMEDILIB_LIBS)
receiver_SOURCES = receiver.c common.c
receiver_CFLAGS = $(COMEDILIB_CFLAGS)
receiver_LDADD = $(COMEDILIB_LIBS)

View file

@ -136,6 +136,11 @@ outp <value>:
Write one <value> to one channel of one subdevice. Requires
a digital or analog output subdevice.
pwm:
controls PWM devices. Use the option -N 0 and -N 1 to switch
it on / off respectively. Call with no arguments to get
a help screen.
receiver:
This demo is meant to be used in conjunction with the sender
demo. Receiver requires a digital input subdevice, and sender

156
demo/pwm.c Normal file
View file

@ -0,0 +1,156 @@
#include <stdio.h>
#include <comedilib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
/*
* A little pwm demo
* Part of Comedilib
*
* Copyright (c) 2012 Bernd Porr <berndporr@f2s.com>
*
* This file may be freely modified, distributed, and combined with
* other software, as long as proper attribution is given in the
* source code.
*/
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include "examples.h"
// the option -n is used to switch on/off the pwm
int main(int argc, char *argv[])
{
int ret,i;
comedi_insn insn;
lsampl_t d[5];
comedi_t *device;
int freq;
struct parsed_options options;
init_parsed_options(&options);
options.freq = -1;
// we hijack this option to switch it on or off
options.n_scan = -1;
options.value = -1;
parse_options(&options, argc, argv);
if ((options.value==-1)&&(options.n_scan==-1)&&(options.freq==-1)) {
fprintf(stderr,
"Usage: %s OPTIONS duty_cycle\n"
"options: \n"
" -N 0 switches PWM off\n"
" -N 1 switches PWM on\n"
" -N 2 enquires the max value for the duty cycle\n"
" -F FREQ sets the PWM frequency\n",
argv[0]);
}
device = comedi_open(options.filename);
if(!device){
comedi_perror(options.filename);
exit(-1);
}
options.subdevice = comedi_find_subdevice_by_type(device,COMEDI_SUBD_PWM,0);
if (options.verbose)
printf("PWM subdevice autodetection gave subdevice number %d\n",
options.subdevice);
if(options.n_scan==2) {
printf("%d\n",comedi_get_maxdata(device,options.subdevice,0));
close(device);
exit(0);
}
insn.insn=INSN_CONFIG;
insn.data=d;
insn.subdev=options.subdevice;
insn.chanspec=CR_PACK(0,0,0);
if(options.n_scan==1) {
d[0] = INSN_CONFIG_ARM;
d[1] = 0;
insn.n=2;
ret=comedi_do_insn(device,&insn);
if(ret < 0){
fprintf(stderr,"Could not switch on:%d\n",ret);
comedi_perror(options.filename);
exit(-1);
}
}
if(options.n_scan==0) {
d[0] = INSN_CONFIG_DISARM;
d[1] = 0;
insn.n=1;
ret=comedi_do_insn(device,&insn);
if(ret < 0){
fprintf(stderr,"Could not switch off:%d\n",ret);
comedi_perror(options.filename);
exit(-1);
}
}
if(options.freq>0) {
freq = options.freq;
d[0] = INSN_CONFIG_PWM_SET_PERIOD;
d[1] = 1E9/freq;
insn.n=2;
ret=comedi_do_insn(device,&insn);
if(ret < 0){
fprintf(stderr,"Could set frequ:%d\n",ret);
comedi_perror(options.filename);
exit(-1);
}
}
d[0] = INSN_CONFIG_GET_PWM_STATUS;
insn.n=2;
ret=comedi_do_insn(device,&insn);
if(ret < 0){
fprintf(stderr,"Could not get status:%d insn=%d\n",
ret,
d[0]);
comedi_perror(options.filename);
exit(-1);
}
if (options.verbose) {
if (d[1])
fprintf(stderr,
"PWM is on.\n");
else
fprintf(stderr,
"PWM is off.\n");
}
d[0] = INSN_CONFIG_PWM_GET_PERIOD;
insn.n=2;
ret=comedi_do_insn(device,&insn);
if(ret < 0){
fprintf(stderr,"Could get frequ:%d\n",ret);
comedi_perror(options.filename);
exit(-1);
}
freq = 1E9 / d[1];
if (options.verbose)
fprintf(stderr,"PWM frequency is %d\n", freq);
if (options.value>=0)
if(comedi_data_write(device,
options.subdevice,
options.channel,
0,
0,
options.value)<0)
{
fprintf(stderr,"error setting the pwm duty cycle on ");
comedi_perror(options.filename);
exit(1);
}
return 0;
}