|
最近本人用cubiebaord的开发板做了毕业设计,搞了很多东西,
整个设计的硬件:
Cubiebaord2及DVK扩展板 MQ-2 烟雾传感器 HC-SR501人体热释红外传感器
18B20温度传感器 数码管模块 蜂鸣器 led灯
设计系统的整体
各个模块设计
HC-SR501红外模块
功能:感应到人在检测范围内就会报警,usb摄像头也会对现场进行拍照
代码- #!/bin/bash
- videodev="/dev/video0"
- redled=4
- sensor=5
- #second(s) between each check
- waitperiod=1
- photorepeattimes=3
- resolution="800x600"
- directory="/tmp"
- init_led ()
- {
- for i in $redled
- do
- if [ ! -d /sys/class/gpio/gpio4_pd0 ]
- then
- echo $i > /sys/class/gpio/export
- fi
- echo "out" > /sys/class/gpio/gpio4_pd0/direction
- echo 0 > /sys/class/gpio/gpio4_pd0/value
- done
- }
- init_sensor ()
- {
- for i in $sensor
- do
- if [ ! -d /sys/class/gpio/gpio5_pd1 ]
- then
- echo $i > /sys/class/gpio/export
- fi
- echo "in" > /sys/class/gpio/gpio5_pd1/direction
- done
- }
- init_led_sensor ()
- {
- init_led
- init_sensor
- }
- cleanup ()
- {
- init_led_sensor
- for i in $redled $sensor
- do
- if [ -d /sys/class/gpio/gpio$i ]
- then
- echo $i > /sys/class/gpio/unexport
- fi
- done
- exit 0
- }
- capture_photo()
- {
- for (( c=0; c<$photorepeattimes; c++ ))
- do
- filename=$directory/$(date -u +"%d%m%Y_%H%M-%S").jpg
- fswebcam -d $videodev --timestamp "%Y-%m-%d %H:%M:%S (%Z)" -r $r
- esolution $filename
- sleep 1
- done
- }
- check_activity ()
- {
- sensor_status=`cat /sys/class/gpio/gpio5_pd1/value`
- echo $sensor_status > /sys/class/gpio/gpio4_pd0/value
- if [ "$sensor_status" -eq 1 ]
- then
- capture_photo
- fi
- }
- init_led_sensor
- trap cleanup INT TERM EXIT
- while :
- do
- check_activity
- sleep $waitperiod
- done
复制代码
18b20温度传感器
功能:实时检测当前温度,超过预设的温度就蜂鸣器响来报警
代码:- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <signal.h>
- #include <pthread.h>
- #include "gpio_lib.h"
- #define LSBFIRST 0
- #define MSBFIRST 1
- #define DISPBUF_LEN 8
- typedef unsigned char byte;
- change it in your case.
- const char* DS18B20_DEVICE="/sys/bus/w1/devices/28-00000626fc86/w1_slave";
- /*
- * 74HC595 relative stuff
- */
- unsigned int latchPin = SUNXI_GPB(11);
- unsigned int clockPin = SUNXI_GPB(13);
- unsigned int dataPin = SUNXI_GPH(14);
- unsigned int zzzzPin = SUNXI_GPG(3);
- /*
- * Display relative stuff
- */
- unsigned int digit_tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
- 0x82, 0xf8, 0x80, 0x90};
- unsigned int digitdot_tab[] = { 0xc0&0x7f, 0xf9&0x7f, 0xa4&0x7f,
- 0xb0&0x7f, 0x99&0x7f, 0x92&0x7f, 0x82&0x7f,
- 0xf8&0x7f, 0x80&0x7f, 0x90&0x7f};
- unsigned int symbol_tab[]={ 0xff, 0x7f, 0xbf};
- static char dispbuf[DISPBUF_LEN];
- /**
- * Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
- */
- void pinMode(unsigned int pin, unsigned int io_mode)
- {
- if (SETUP_OK != sunxi_gpio_set_cfgpin(pin, io_mode))
- {
- printf("Failed to config GPIO pin\n");
- }
- }
- /**
- * Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
- */
- void digitalWrite(int pin, int hl)
- {
- if (sunxi_gpio_output(pin, hl))
- {
- printf("Failed to set GPIO pin value\n");
- }
- }
- void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
- {
- byte i;
- for (i = 0; i < 8; i++)
- {
- if (bitOrder == LSBFIRST)
- digitalWrite(dataPin, ! !(val & (1 << i)));
- else
- digitalWrite(dataPin, ! !(val & (1 << (7 - i))));
- digitalWrite(clockPin, HIGH);
- digitalWrite(clockPin, LOW);
- }
- }
- /**
- * Initialize the GPIO & relative pins
- */
- void init_gpio()
- {
- if (SETUP_OK != sunxi_gpio_init())
- {
- printf("Failed to initialize GPIO\n");
- }
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(zzzzPin, OUTPUT);
- }
- /**
- * Get current temperature from the w1-thermal device
- */
- void get_temperature(char* tempbuf, int len)
- {
- FILE* fp=fopen(DS18B20_DEVICE,"r");
- char* line=NULL;
- char* temperature_tok=NULL;
- int temperature=0;
-
- int n;
- if(!fp){
- fprintf(stderr,"Failed to open device(%s) file!\n", DS18B20_DEVICE);
- return;
- }
- // skip the first line
- getline(&line, &n, fp);
- free(line);
- line=NULL;
-
- getline(&line, &n, fp);
- strtok(line,"=");
- temperature_tok=strtok(NULL,"\n");
-
- strncpy(tempbuf, temperature_tok, len);
- free(line);
- fclose(fp);
- }
- /**
- * Thread of filling the time infomation into display buffer
- */
- void* time_to_dispbuf()
- {
- time_t timep;
- struct tm *p;
- char timebuf[4];
- int interval=1; // in seconds
- while(1){
- // get localtime
- time(&timep);
- p = localtime(&timep);
- sprintf(timebuf, "%02d%02d", p->tm_hour, p->tm_min);
- dispbuf[0]=digit_tab[timebuf[0] - '0'];
- dispbuf[1]=digitdot_tab[timebuf[1] - '0'];
- dispbuf[2]=digit_tab[timebuf[2] - '0'];
- dispbuf[3]=digit_tab[timebuf[3] - '0'];
- dispbuf[4]=symbol_tab[2]; // '-'
- sleep(interval);
- }
- }
- /**
- * Thread of filling the temperature into display buffer
- */
- void* temp_to_dispbuf()
- {
- char tempbuf[3];
- int interval=5;
- float tmp = 0;
-
- while(1){
- get_temperature(tempbuf, sizeof tempbuf);
- dispbuf[5]=digit_tab[tempbuf[0]-'0'];
- dispbuf[6]=digitdot_tab[tempbuf[1]-'0'];
- dispbuf[7]=digit_tab[tempbuf[2]-'0'];
- tmp = (tempbuf[0]-'0')*10 + (tempbuf[1]-'0');
- printf("temperature = %f -------------\n", tmp);
- if (tmp > 28)
- {
- printf("-----FIRE----FIRE----FIRE----\n");
- digitalWrite(zzzzPin, LOW);
- }
- else
- {
- digitalWrite(zzzzPin, HIGH);
- }
- sleep(interval);
- }
-
- }
- int main(int argc, char **argv)
- {
- pthread_t get_time_thread, get_temp_thread;
- void * thread_ret;
- init_gpio();
- pthread_create( &get_time_thread, NULL, time_to_dispbuf, NULL);
- pthread_create( &get_temp_thread, NULL, temp_to_dispbuf, NULL);
-
- while (1)
- {
- int i;
- for(i=0;i<DISPBUF_LEN;i++){
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);
- shiftOut(dataPin, clockPin, MSBFIRST, dispbuf[i]);
- digitalWrite(latchPin, 1);
- usleep(2000);
- }
-
- }
- pthread_join(get_time_thread,&thread_ret);
- pthread_join(get_temp_thread,&thread_ret);
- return 0;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|