# **嵌入式系統 第16周** ###### tags: `嵌入式系統` `考前題目講解` ## 第一題 ![](https://i.imgur.com/YfuHMZc.png) ``` python import numpy as np import cv2 import RPi.GPIO as GPIO import time CONTROL_PIN1 = 17 CONTROL_PIN2 = 18 PWM_FREQ = 50 STEP=15 GPIO.setmode(GPIO.BCM) GPIO.setup(CONTROL_PIN1, GPIO.OUT) GPIO.setup(CONTROL_PIN2, GPIO.OUT) pwm = GPIO.PWM(CONTROL_PIN1, PWM_FREQ) pwm.start(0) faceCascade = cv2.CascadeClassifier('/home/pi/haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) cap.set(3,640) cap.set(4,480) while True: ret,img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(20, 20)) print(faces) if len(faces): pwm.ChangeDutyCycle(20) else: pwm.ChangeDutyCycle(0) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] cv2.imshow('video',img) k = cv2.waitKey(30) & 0xff if k == 27: break cap.release() cv2.destroyAllWindows() ``` ## 第二題 ![](https://i.imgur.com/Vsxryyg.png) ``` cmd sudo python3 -m pip install sudo pip3 install dht11 ``` ``` python import RPi.GPIO as GPIO import dht11 import time from RPLCD import CharLCD # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() GPIO.setup(26,GPIO.OUT) GPIO.setup(12,GPIO.OUT) GPIO.setup(16,GPIO.OUT) GPIO.setup(20,GPIO.OUT) chan_list = (26,12,16,20) CONTROL_PIN1 = 17 CONTROL_PIN2 = 18 PWM_FREQ = 50 STEP=15 GPIO.setup(CONTROL_PIN1, GPIO.OUT) GPIO.setup(CONTROL_PIN2, GPIO.OUT) pwm = GPIO.PWM(CONTROL_PIN1, PWM_FREQ) pwm.start(0) LCD = CharLCD(numbering_mode = GPIO.BCM,cols=16 ,rows = 2,pin_rs=22,pin_e=11,pins_data=[23,10,9,25]) countLCD = 0 # read data using pin 4 instance = dht11.DHT11(pin = 4) while True: result = instance.read() if result.is_valid(): print("溫度: %-3.1f C" % result.temperature) print("濕度: %-3.1f %%" % result.humidity) LCD.cursor_pos=(0,0) LCD.write_string("Temperature:%-3.1fC" % result.temperature) if int(result.temperature) >= 25 and int(result.temperature) < 27: pwm.ChangeDutyCycle(7) GPIO.output(chan_list,(GPIO.HIGH,GPIO. LOW,GPIO.LOW,GPIO.LOW)) elif int(result.temperature) >= 27 and int(result.temperature) < 29: pwm.ChangeDutyCycle(15) GPIO.output(chan_list,(GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW)) elif int(result.temperature) >= 29 : pwm.ChangeDutyCycle(30) GPIO.output(chan_list,(GPIO.HIGH,GPIO.HIGH,GPIO.HIGH,GPIO.LOW)) time.sleep(1) ``` ## 第二題 ### 可變電阻空這LCD阻值 ![](https://i.imgur.com/MrDnInO.png) ``` python import smbus import time import RPi.GPIO as GPIO from RPLCD import CharLCD address = 0x48 LCD=CharLCD(numbering_mode=GPIO.BCM,cols=16,rows=2,pin_rs=22,pin_e=11,pins_data=[23,10,9,25]) bus = smbus.SMBus(1) while True: bus.write_byte(address,0x40) value = bus.read_byte(address) print("AOUT:%1.3f " %(value*3.3/255)) number1 = str("%1.3f"%(value*3.3/255)) LCD.cursor_pos = (0,0) LCD.write_string(number1) time.sleep(0.1) LCD.clear() ``` ## 可變電阻以前做的作業程式碼 ![](https://i.imgur.com/DTlhPM8.jpg) 偵測幾個人臉 在LCD上顯是