3.1 Raspberry GPIO▪ Chú ý:▪ Tất cả các chân I/O hỗ trợ ngắt▪ Tất cả các chân đều có điện trở kéo lên và kéo xuống bên trong▪ Một số chân có thể chia sẻ chức năng▪ Lưu ý:▪ Đoản mạch kết n
Trang 1HỆ THỐNG NHÚNG
Dr Thanh Nguyen
Email: thanh.nguyenngoc@phenikaa-uni.edu.vn Office: Room 606 - Building A4
Chương 3: LẬP TRÌNH NHÚNG VỚI RASPBERRY
Trang 33.1 Raspberry GPIO
Trang 5▪ Có hai hệ thống đánh số cho các chân GPIO.
▪ Đánh số vật lý (dễ hiểu) cho biết vị trí để chân GPIO trên board R.Pi
▪ Đánh số theo chân bộ xử lý BCM2837.
Trang 63.1 Raspberry GPIO
▪ Chú ý:
▪ Tất cả các chân I/O hỗ trợ ngắt
▪ Tất cả các chân đều có điện trở kéo lên và kéo xuống bên trong
▪ Một số chân có thể chia sẻ chức năng
Trang 7Truy cập phần cứng vs Linux
▪ Giao tiếp giữa phần cứng và phần mềm là
rất quan trọng.
▪ Giao tiếp được xử lý bằng cách sử dụng
thanh ghi thiết bị phần cứng được ánh xạ
Trang 8Truy cập phần cứng vs Linux
▪There are many ways to implement hardware/software bridge
▪Create a device node in /dev
▪Access device via virtual file system sysfs
Trang 9Điều khiển GPIO dùng sysfs
▪ sysfs provide a virtual file system for device access at /sys/class
▪ Using sysfs, GPIO can be control in Shell language
▪ Functions:
▪ Export the particular GPIO pin for user control
echo 30 > /sys/class/gpio/export
▪ Change the GPIO pin direction to in/out
echo "out" > /sys/class/gpio/gpio30/direction
echo “in" > /sys/class/gpio/gpio30/direction
▪ Change the value
echo 1 > /sys/class/gpio/gpio30/value
echo 0 > /sys/class/gpio/gpio30/value
▪ Unexport the GPIO pin
echo 30 > /sys/class/gpio/unexport
Trang 10Điều khiển GPIO dùng sysfs
▪Example: Create a file name blink.sh
▪Run the code with command: /blink.sh
Trang 11Thư viện WiringPi
▪WiringPi is a GPIO access library for Raspberry
▪ wiringPi is writte in C and preinstalled in raspbian
▪ Website: https://wiringpi.com
▪ Install: git clone https://github.com/WiringPi/WiringPi.git
▪Features:
▪ Support command-line GPIO control in terminal
▪ Support digital/analog reading and writing
▪ Support wired communication: SPI, I2C
▪ Provide software/hardware PWM
▪ Support external interrupts, delay.
Trang 12Thư viện WiringPi
▪gpio –v : prints wiringPi version
▪ gpio mode <pin> in/out/pwm/clock/up/down/tri :sets the
mode for a pin
▪gpio write <pin> 0/1 : sets an output pin to high (1) or low (0)
▪gpio read <pin> : Reads and prints the logic value of the given pin
▪gpio edge <pin> rising/falling/both/none : enables the given pin
for edge interrupt triggering
Trang 13Thư viện WiringPi
▪gpio readall : reads all the normally accessible pins and prints a
table of their numbers
Trang 14Thư viện WiringPi
▪wiringPiSetupPhys () : initialize WiringPi library
▪pinMode (int pin, int mode) : sets the mode of a pin
▪digitalWrite (int pin, int value) : Writes the value HIGH or LOW (1
or 0) to the given pin
▪digitalRead (int pin) : returns the value read at the given pin
▪analogRead (int pin) : returns the value read on the supplied
analog input pin
Trang 15Điều khiển GPIO dùng WiringPi
Trang 16▪ Compile and run the blink program
gcc –Wall –o blink blink.c –lwiringPi
./blink
▪ The program will run forever, stop it with Ctrl-C
Trang 17KẾT NỐI MẠCH CÔNG SUẤT
Trang 19Pulse Width Modulation
Trang 20PWM
Trang 25Ngắt dùng WiringPi
▪ Structure of an interrupt program
Trang 26Các chuẩn giao tiếp
Trang 27Các chuẩn giao tiếp song song
Trang 28Giao tiếp nối tiếp
Trang 29SPI - Serial Peripheral Interface
▪ SPI uses separate lines for
data and a “clock” that keeps
both sides in perfect sync
▪ Only one way direction bus
▪ SDO/MOSI (Signal Data
Out/Master Out Slave In)
▪ SDI/MISO (Signal Data
In/Master In Slave Out)
▪ SCK (System Clock)
Trang 30SPI - Serial Peripheral Interface
▪ In SPI, only one side generates the clock signal (usually called
CLK or SCK for Serial Clock)
▪ The side that generates the clock is called the “master”, and the
other side is called the “slave”
Trang 31SPI connection
▪ Raspberry Pi is equipped with two SPI bus: SPI0 and SPI1
Trang 32SPI - Serial Peripheral Interface
Trang 3434
Trang 36SPI - Serial Peripheral Interface
▪ How do you send data back
from slaves to master?
▪ Master always generates the
clock signal
▪ MCU must know in advance
when a slave needs to return
data and how much data will
be returned
Trang 37SPI - Serial Peripheral Interface
▪ MCU tells a slave that it
should wake up and
receive/send data via Slave
select (SS) line
▪ SS = 1 : disconnects the slave
from the SPI bus
▪ SS = 0 activates the slave
Trang 38SPI - Serial Peripheral Interface
▪ Simple protocol, 1 bit /1 clock
▪ High speed (<32MHz)
▪ Short distance (<1m), need
amplifier for long distance >10m
Trang 39SPI Serial Frame Format
▪ SPI Serial Frame Format is selected based on connected devices
▪ The SPI interface format can be configured with
▪ Clock Polarity – CPOL
▪ Clock Phase – CPHA
▪ Clock Polarity Bit – CPOL
▪ Clock Polarity = 0, the SCK line idle state is LOW
▪ Clock Polarity = 1, the SCK line idle state is HIGH
▪ Clock Phase Bit – CPHA
▪ Clock Phase = 0, the data is sampled on the first SCK clock transition
▪ Clock Phase = 1, the data is sampled on the second SCK clock transition.
Trang 40SPI Serial Frame Format
▪ CPOL = 0, CPHA = 0
Trang 41SPI Serial Frame Format
▪ CPOL = 0, CPHA = 1
▪ SEL signal must remain active until the last data transfer has completed.
Trang 42SPI Serial Frame Format
▪ CPOL = 1, CPHA = 0
▪ SEL signal must change to an inactive level between each data frame.
Trang 43SPI Serial Frame Format
▪ CPOL = 1, CPHA = 1
▪ SEL signal must remain active until the last data transfer has completed.
Trang 44SPI Serial Frame Format
▪ nRF24L01: Digital interface (SPI) speed 0-8 Mbps.
Trang 45SPI connection
▪ nRF24L01
Trang 46SPI connection
▪ Raspberry Pi is equipped with two SPI bus: SPI0 and SPI1
Trang 47SPI connection
▪ WiringPi includes a library for Raspberry Pi’s SPI interface
▪ To use SPI interface, follow these steps:
▪ Step 1: load the SPI drivers into the kernel with command:
gpio load spi
▪ Step 2: In C file, include the wiringPi SPI library with
#include <wiringPiSPI.h>
▪ Step 3: Initialize SPI with:
wiringPiSPISetup (int channel, int speed) channel : 0 or 1; speed in range 500kHz through 32MHz
▪ Step 4: transfer data with :
wiringPiSPIDataRW (int channel, unsigned char *data, int len);
▪ Data that was in buffer is overwritten by data returned from the SPI bus
Trang 48SPI Example