mini2440 模擬實現安防的代碼 linuc -c語言

#include
#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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章