Micro2440/Mini2440 Beep c program in linux

Source: Internet
Author: User
Tags usleep

Keywords: Micro2440, Mini2440, Beep, Beep

The buzzer support has been added to the default kernel of Micro2440/Mini2440 linux (see <micro2440 user manual -2010-6-9.pdf> P346). Therefore, you can directly operate the buzzer through the file ioctl.

(Ls/dev/pwm indicates that your Micro2440/Mini2440 has a buzzer function .)

The following code references <micro2440 user manual -2010-6-9.pdf>

# Include <stdio. h>
# Include <termios. h>
# Include <unistd. h>
# Include <stdlib. h>
 
# Define PWM_IOCTL_SET_FREQ 1
# Define PWM_IOCTL_STOP 0 // Note: <micro2440 user manual -2010-6-9.pdf>,
// PWM_IOCTL_STOP is defined as 2, which is incorrect;
// It should be consistent with the buzzer kernel code, which is 0
 
Static int fd =-1;/** <save the buzzer file handle */
 
/** Close the buzzer */
Static void close_buzzer (void)
{
If (fd> = 0)
{
Ioctl (fd, PWM_IOCTL_STOP );
Close (fd );
Fd =-1;
}
}
 
/** Open the buzzer */
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 );
}
 
/** Beep with freq */
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 );
}
}
 
/** Stop beep */
Static void stop_buzzer (void)
{
Int ret = ioctl (fd, PWM_IOCTL_STOP );
If (ret <0)
{
Perror ("stop the buzzer error. \ n ");
Exit (-1 );
}
}
 
/**
* The beep function.
* \ Param freq beep frequency
* \ Param t1 the duration of each beep. Unit: milliseconds
* \ Param t2 beep times
* \ Remark: after testing,./testBeep 3000 2000 3 as a normal condition, the beep effect is better (3 times at a frequency of 3 K, 2 seconds at each time)
*./TestBeep 3000 500 20 as the Failure Alarm beep effect is better (3 K frequency beep 20 times, every 0.5 seconds)
*/
Static void Beep (int freq, int t1, int t2)
{
Printf ("freq = % d, each_ms = % d, ntimes = % d \ n", freq, t1, t2 );
Open_buzzer ();
Int I = 0;
For (I = 0; I <t2; ++ I)
{
Set_buzzer_freq (freq );
Usleep (t1 * 100); // * 100 is an experience value. Why ~
Stop_buzzer ();
Usleep (t1 * 100); // * 100 is an experience value. Why ~
}
Close_buzzer ();
}
 
/** Test Program main */
Int main (int argc, char ** argv)
{
Int freq;
If (argc> = 2)
{
Freq = atoi (argv [1]);
If (freq <1000 | freq> 20000)
{
Freq = 10000;
}
}
 
Int each_ms = 500;
If (argc> = 3)
{
Each_ms = atoi (argv [2]);
If (each_ms <100 | each_ms> 5000)
{
Each_milliseconds = 500;
}
}
 
Int times = 0;
If (argc> = 4)
{
Times = atoi (argv [3]);
If (times <1 | times> 30)
{
Times = 5;
}
}
 
Beep (freq, each_ms, times );
 
Return 0;
}
 
// EasyVCR @ csdn, 2012.01.04

Compilation: arm-linux-gcc testBeep. c-o testBeep

From EasyVCR's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.