#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <pthread.h>
实时检测button,当button0被按下后,启动ADC检测功能。当button1被按下后,关闭ADC检测功能。
若ADC检测功能被启用的情况下:
当ADC值超过1000的时候,闪烁LED2(1S闪烁1次),同时打开蜂鸣器(蜂鸣器设值1000)。当ADC值小于1000的时候,关掉LED2和蜂鸣器。
int beep_fd;
int led_flash_flag;
int adc_flash_flag;
void *thread_adc(void *data)
{
int adc_fd = *(int *)data;
char adc[6];
int i,ret;
int val;
while(adc_flash_flag)
{
ret = read(adc_fd, adc, 5);
printf("adc=%s\r\n",adc);//int a = atoi(adc); int vol = 3.3/4095*a
val = atoi(adc);
if(val >1000)
{
led_flash_flag = 1;
ioctl(beep_fd, 1, 100);
}
if(val < 1000)
{
led_flash_flag = 0;
ioctl(beep_fd, 0, 100);
}
sleep(1);
}
}
void *thread_led(void *data)
{
int led_fd = *(int *)data;
while(1)
{
while(led_flash_flag)
{
ioctl(led_fd, 1, 0);
sleep(1);
ioctl(led_fd, 0, 0);
sleep(1);
}
}
}
int main(void)
{
int led_fd, button_fd,adc_fd,ret,i;
char key[6];
pthread_t led_id,acd_id;
pthread_attr_t attr;
led_flash_flag = 0;
adc_flash_flag = 0;
pthread_attr_init( &attr );
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
led_fd = open("/dev/leds",O_RDWR);
if ( led_fd == -1 ) {
printf("open dev/mini2440_led fail=%d\n",led_fd);
return -1;
}
button_fd = open("/dev/buttons",O_RDWR);
if ( button_fd < 0 )
{
printf("open dev/button_fd fail=%d\n",button_fd);
return -1;
}
adc_fd = open("/dev/adc",O_RDWR);
if ( adc_fd == -1 )
{
printf("open dev/adc fail=%d\n",adc_fd);
return -1;
}
beep_fd = open("/dev/pwm",O_RDWR | O_NONBLOCK);
if ( beep_fd == -1 ) {
//printf("open dev/pwm fail=%d\n",dev_fd);
return -1;
}
pthread_create( &led_id, &attr, thread_led, &led_fd);
while(1)
{
ret = read(button_fd, key, 4);
if(key[0] == '1')
{
if(adc_flash_flag == 0)
{
adc_flash_flag = 1;
pthread_create( &acd_id, &attr, thread_adc, &adc_fd);
}
}
if(key[1] == '1')
{
adc_flash_flag = 0;
led_flash_flag = 0;
ioctl(beep_fd, 0, 100);
}
}
close(led_fd);
close(button_fd);
close(adc_fd);
close(beep_fd);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <pthread.h>
实时检测button,当button0被按下后,启动ADC检测功能。当button1被按下后,关闭ADC检测功能。
若ADC检测功能被启用的情况下:
当ADC值超过1000的时候,闪烁LED2(1S闪烁1次),同时打开蜂鸣器(蜂鸣器设值1000)。当ADC值小于1000的时候,关掉LED2和蜂鸣器。
int beep_fd;
int led_flash_flag;
int adc_flash_flag;
void *thread_adc(void *data)
{
int adc_fd = *(int *)data;
char adc[6];
int i,ret;
int val;
while(adc_flash_flag)
{
ret = read(adc_fd, adc, 5);
printf("adc=%s\r\n",adc);//int a = atoi(adc); int vol = 3.3/4095*a
val = atoi(adc);
if(val >1000)
{
led_flash_flag = 1;
ioctl(beep_fd, 1, 100);
}
if(val < 1000)
{
led_flash_flag = 0;
ioctl(beep_fd, 0, 100);
}
sleep(1);
}
}
void *thread_led(void *data)
{
int led_fd = *(int *)data;
while(1)
{
while(led_flash_flag)
{
ioctl(led_fd, 1, 0);
sleep(1);
ioctl(led_fd, 0, 0);
sleep(1);
}
}
}
int main(void)
{
int led_fd, button_fd,adc_fd,ret,i;
char key[6];
pthread_t led_id,acd_id;
pthread_attr_t attr;
led_flash_flag = 0;
adc_flash_flag = 0;
pthread_attr_init( &attr );
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
led_fd = open("/dev/leds",O_RDWR);
if ( led_fd == -1 ) {
printf("open dev/mini2440_led fail=%d\n",led_fd);
return -1;
}
button_fd = open("/dev/buttons",O_RDWR);
if ( button_fd < 0 )
{
printf("open dev/button_fd fail=%d\n",button_fd);
return -1;
}
adc_fd = open("/dev/adc",O_RDWR);
if ( adc_fd == -1 )
{
printf("open dev/adc fail=%d\n",adc_fd);
return -1;
}
beep_fd = open("/dev/pwm",O_RDWR | O_NONBLOCK);
if ( beep_fd == -1 ) {
//printf("open dev/pwm fail=%d\n",dev_fd);
return -1;
}
pthread_create( &led_id, &attr, thread_led, &led_fd);
while(1)
{
ret = read(button_fd, key, 4);
if(key[0] == '1')
{
if(adc_flash_flag == 0)
{
adc_flash_flag = 1;
pthread_create( &acd_id, &attr, thread_adc, &adc_fd);
}
}
if(key[1] == '1')
{
adc_flash_flag = 0;
led_flash_flag = 0;
ioctl(beep_fd, 0, 100);
}
}
close(led_fd);
close(button_fd);
close(adc_fd);
close(beep_fd);
return 0;
}