Tải bản đầy đủ (.pdf) (48 trang)

2023 1 embedded systems chapter 3 1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.85 MB, 48 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b>HỆ THỐNG NHÚNG</b>

<small>Dr. Thanh Nguyen</small>

<small>Email: : Room 606 - Building A4</small>

<small>Chương 3: LẬP TRÌNH NHÚNG VỚI RASPBERRY</small>

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

3.1 Raspberry GPIO

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

<small>▪ Có hai hệ thống đánh số cho các chân GPIO.</small>

<small>▪ Đá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.</small>

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

3.1 Raspberry GPIO

<small>▪ Chú ý:</small>

<small>▪ Tất cả các chân I/O hỗ trợ ngắt</small>

<small>▪ 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</small>

<small>▪ Lưu ý:</small>

<small>▪ Đoản mạch (kết nối </small><b><small>+ 3v hoặc +5 </small></b><small>từ một chân nguồn hoặc mộtchân đầu ra trực tiếp </small><b><small>xuống đất</small></b><small>) sẽ làm hỏng bo R.Pi</small>

<small>▪ Tất cả chân I/O là 3.3V. Gửi 5V trực tiếp đến một chân có thể làm hỏng bo R.Pi </small>

<small>▪ Dòng tối đa được phép rút ra từ các chân I/O là 50mA</small>

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

Truy cập phần cứng vs Linux

<small>▪ Giao tiếp giữa phần cứng và phần mềm là rất quan trọng.</small>

<small>▪ 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ạ </small>

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

Truy cập phần cứng vs Linux

▪There are many ways to implement hardware/software bridge

<b>▪Create a device node in /dev</b>

<b>▪Access device via virtual file system sysfs</b>

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

<b>Điều khiển GPIO dùng sysfs</b>

<b><small>▪ sysfs provide a virtual file system for device access at /sys/class </small></b>

<small>▪ Using sysfs, GPIO can be control in Shell language ▪ Functions: </small>

<small>▪ Export the particular GPIO pin for user control. echo 30 > /sys/class/gpio/export </small>

<small>▪ Change the GPIO pin direction to in/out</small>

<small>echo "out" > /sys/class/gpio/gpio30/direction echo “in" > /sys/class/gpio/gpio30/direction ▪ Change the value </small>

<small>echo 1 > /sys/class/gpio/gpio30/value echo 0 > /sys/class/gpio/gpio30/value ▪ Unexport the GPIO pin </small>

<small>echo 30 > /sys/class/gpio/unexport</small>

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

<b>Điều khiển GPIO dùng sysfs</b>

▪Example: Create a file name blink.sh

▪Run the code with command: ./blink.sh

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

<b>Thư viện WiringPi</b>

<b>▪WiringPi is a GPIO access library for Raspberry </b>

<small>▪ wiringPi is writte in C and preinstalled in raspbian▪ Website: </small>

<b><small>▪ Install: git clone

<small>▪ Support command-line GPIO control in terminal ▪ Support digital/analog reading and writing </small>

<small>▪ Support wired communication: SPI, I2C ▪ Provide software/hardware PWM </small>

<small>▪ Support external interrupts, delay.</small>

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

<b>Thư viện WiringPi</b>

<b>▪gpio –v : prints wiringPi version. </b>

<b>▪ gpio mode <pin> in/out/pwm/clock/up/down/tri :sets the </b>

mode for a pin

<b>▪gpio write <pin> 0/1 : sets an output pin to high (1) or low (0) </b>

<b>▪gpio read <pin> : Reads and prints the logic value of the given pin ▪gpio edge <pin> rising/falling/both/none : enables the given pin </b>

for edge interrupt triggering

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

<b>Thư viện WiringPi</b>

<b>▪gpio readall : reads all the normally accessible pins and prints a </b>

table of their numbers.

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

<b>Thư viện WiringPi</b>

<b>▪wiringPiSetupPhys () : initialize WiringPi library. </b>

<b>▪pinMode (int pin, int mode) : sets the mode of a pin</b>

<b>▪digitalWrite (int pin, int value) : Writes the value HIGH or LOW (1 </b>

or 0) to the given pin

<b>▪digitalRead (int pin) : returns the value read at the given pin ▪analogRead (int pin) : returns the value read on the supplied </b>

analog input pin

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

<b>Điều khiển GPIO dùng WiringPi</b>

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="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

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

KẾT NỐI MẠCH CÔNG SUẤT

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

Pulse Width Modulation

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

PWM

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

<b>Ngắt dùng WiringPi</b>

▪ Structure of an interrupt program

</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">

<b>Các chuẩn giao tiếp</b>

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

<b>Các chuẩn giao tiếp song song</b>

</div><span class="text_page_counter">Trang 28</span><div class="page_container" data-page="28">

<b>Giao tiếp nối tiếp</b>

</div><span class="text_page_counter">Trang 29</span><div class="page_container" data-page="29">

SPI - 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)

</div><span class="text_page_counter">Trang 30</span><div class="page_container" data-page="30">

SPI - 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”.

</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">

SPI connection

▪ Raspberry Pi is equipped with two SPI bus: SPI0 and SPI1

</div><span class="text_page_counter">Trang 32</span><div class="page_container" data-page="32">

SPI - Serial Peripheral Interface

</div><span class="text_page_counter">Trang 34</span><div class="page_container" data-page="34">

<small>34</small>

</div><span class="text_page_counter">Trang 36</span><div class="page_container" data-page="36">

SPI - 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

</div><span class="text_page_counter">Trang 37</span><div class="page_container" data-page="37">

SPI - Serial Peripheral Interface

▪ MCU tells a slave that it should wake up and

<b>receive/send data via Slave </b>

<b>select (SS) line </b>

▪ SS = 1 : disconnects the slave from the SPI bus.

▪ SS = 0 activates the slave

</div><span class="text_page_counter">Trang 38</span><div class="page_container" data-page="38">

SPI - Serial Peripheral Interface

▪ Simple protocol, 1 bit /1 clock ▪ High speed (<32MHz)

▪ Short distance (<1m), need

amplifier for long distance >10m

</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">

SPI Serial Frame Format

▪ SPI Serial Frame Format is selected based on connected devices ▪ The SPI interface format can be configured with

<small>▪ Clock Polarity – CPOL ▪ Clock Phase – CPHA </small>

▪ Clock Polarity Bit – CPOL

<small>▪ Clock Polarity = 0, the SCK line idle state is LOW. ▪ Clock Polarity = 1, the SCK line idle state is HIGH. </small>

▪ Clock Phase Bit – CPHA

<small>▪ 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.</small>

</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40">

SPI Serial Frame Format

▪ CPOL = 0, CPHA = 0

</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41">

SPI Serial Frame Format

▪ CPOL = 0, CPHA = 1

<small>▪ SEL signal must remain active until the last data transfer has completed.</small>

</div><span class="text_page_counter">Trang 42</span><div class="page_container" data-page="42">

SPI Serial Frame Format

▪ CPOL = 1, CPHA = 0

<small>▪ SEL signal must change to an inactive level between each data frame.</small>

</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43">

SPI Serial Frame Format

▪ CPOL = 1, CPHA = 1

<small>▪ SEL signal must remain active until the last data transfer has completed.</small>

</div><span class="text_page_counter">Trang 44</span><div class="page_container" data-page="44">

SPI Serial Frame Format

▪ nRF24L01<small>: Digital interface (SPI) speed 0-8 Mbps.</small>

</div><span class="text_page_counter">Trang 45</span><div class="page_container" data-page="45">

SPI connection

▪ nRF24L01

</div><span class="text_page_counter">Trang 46</span><div class="page_container" data-page="46">

SPI connection

▪ Raspberry Pi is equipped with two SPI bus: SPI0 and SPI1

</div><span class="text_page_counter">Trang 47</span><div class="page_container" data-page="47">

SPI connection

<b><small>▪ WiringPi includes a library for Raspberry Pi’s SPI interface </small></b>

<small>▪ To use SPI interface, follow these steps: </small>

<b><small>▪ Step 1: load the SPI drivers into the kernel with command: </small></b>

<small>gpio load spi</small>

<b><small>▪ Step 2: In C file, include the wiringPi SPI library with </small></b>

<small>#include <wiringPiSPI.h> </small>

<b><small>▪ Step 3: Initialize SPI with: </small></b>

<small>wiringPiSPISetup (int channel, int speed) </small>

<small>channel : 0 or 1; speed in range 500kHz through 32MHz </small>

<b><small>▪ Step 4: transfer data with : </small></b>

<small>wiringPiSPIDataRW (int channel, unsigned char *data, int len); </small>

<small>▪ Data that was in buffer is </small><b><small>overwritten by data returned </small></b><small>from the SPI bus</small>

</div><span class="text_page_counter">Trang 48</span><div class="page_container" data-page="48">

SPI Example

</div>

×