0% found this document useful (0 votes)
49 views32 pages

PBL3 Đồ Án Môn Học

The document discusses the design of a system to sort and count products by color. It describes the components of the system including a microcontroller, sensors, motors and display. It provides details on the power supply, microcontroller, sensor and motor blocks, and the software and circuit design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views32 pages

PBL3 Đồ Án Môn Học

The document discusses the design of a system to sort and count products by color. It describes the components of the system including a microcontroller, sensors, motors and display. It provides details on the power supply, microcontroller, sensor and motor blocks, and the software and circuit design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

The University of Danang

University of Science and Technology


FACULTY OF MECHANICAL ENGINEERING

PBL3: DESIGN OF
MICROCONTROLLER AND SENSOR
APPLICATION DEVICES
TOPIC: SORTING AND COUNTING PRODUCTS

Lecture: Ph.D DANG PHUOC VINH


Students: NGUYEN THI MY LINH
VAN THI KIM THUY
Group: N20.04
Class: 20CDT

Da Nang, April/2022
INDEX

PREFACE ..................................................................................................... 3
CHAPTER 1: PRODUCT OVERVIEW ................................................... 4
1.1. PRODUCT INTRODUCTION ............................................................... 4
1.2. PRODUCT REQUIREMENTS............................................................... 5
CHAPTER 2: SYSTEM DESIGN .............................................................. 6
2.1. SYSTEM OVERVIEW .............................................................................. 6
2.2. COMPONENTS OF THE SYSTEM ........................................................ 6
2.2.1. Power block ......................................................................................... 6
2.2.2. Microcontroller block ........................................................................ 7
2.2.3. Sensor block ...................................................................................... 10
2.2.4. Servo Motor block ............................................................................ 14
2.2.5. Conveyor Motor Block ..................................................................... 15
2.2.6. Screen Block ...................................................................................... 15
2.2.7. Push button block:............................................................................ 16
2.3. Circuit design .......................................................................................... 17
2.3.1. Circuit design on Proteus 8.9 professional software ..................... 17
2.3.2. Practical external circuit design...................................................... 19
2.4. SOFTWARE DESIGN OF THE SYSTEM ............................................ 20
2.4.1. Algorithmic flowchart ...................................................................... 20
2.4.2. Program code on CCS software ...................................................... 23
CHAPTER 3: USER MANUALS AND SOFTWARE ........................... 31
3.1. INSTRUCTIONS FOR USE OF THE PRODUCT ............................... 31
BIBLIOGRAPHY....................................................................................... 32

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 2
PREFACE
Along with the rapid development of modern technological machines today,
people have to improve their knowledge to keep up with the times. Knowledge is
something of particular importance to society in general and Mechatronics
students in particular. Each student needs to equip themselves with the best
specialized knowledge to be able to create the highest quality and at the same time
the most useful products.

"PBL3: Design of microcontrollers and sensors" is one of the important


specialized projects for Mechatronics students to consolidate as well as deepen
their understanding of relevant subjects learned in class. After completing PBL3
with the topic "Classifying and counting products based on color", we have better
understood the knowledge we have learned and know how to apply them for
different purposes, and thereby learned more methods to create a finished product
with the highest applicability.

Spending a semester with PBL3 under the enthusiastic guidance of Mr. Dang
Phuoc Vinh has helped us not only know more useful knowledge but also know how
to work, how to divide the best time to complete the product, know how to work in
a team, The way to support each other and above all is to take responsibility for
your tasks so as not to affect your companions.

Sincerely thanks Mr. Dang Phuoc Vinh for guiding and accompanying us during
the past semester so that we can complete PBL3 in the best way.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 3
CHAPTER 1: PRODUCT OVERVIEW
1.1. PRODUCT INTRODUCTION
Currently, our country is in the process of industrialization and modernization.
Therefore, there are many industrial parks that have been formed with many modern
operating equipment lines along with investment and capital contribution from other
countries around the world.
In recent decades, electronic devices have been widely used around the world. The
diversity and development of industries are constantly changing. The combination of
electrical – electronics and mechanical engineering is an important step in the
development of automation in industry.
Currently, our country is in the process of development and integration, so the
manufactured products not only meet quality standards, but also require high accuracy
in shape, size, weight ... Therefore, from there, industrial parks are formed with many
modern lines of equipment and machinery to coordinate with production needs, to
create higher productivity in the production process. In factories, products
manufactured before being shipped must go through several stages of product
inspection.
Depending on the product produced, it must be checked through different stages, such
as checking for quality, size, shape, or weight ... In this topic, we would like to do:
"Sort and count products by color". This topic will help us better understand how the
equipment lines used in product classification work, and the application of PIC to
control them, as well as conveyor belt systems.

Figure 1.1. Actual product model

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 4
1.2. PRODUCT REQUIREMENTS
➢ The problem in practice:
Research and control product classification system using PIC 16F877A, Servo
motor and some other necessary equipment. Research on this topic aims to apply
advanced scientific and technical technologies in the production process in order to
reduce human labor and improve productivity in industrial production.
➢ Request:
• Classification of green and red tomatoes .
• Count the number of classified green and red products.
➢ Solution:
• Use the TCS3200 color sensor to distinguish colors
• Using Servo Motors for Classification
• Use optical sensors to detect products
➢ Idea:
• Use the TCS3200 to distinguish blue and red based on frequency.
• Use geared motors to control the conveyor to run.
• Use the push button to control system operation.
• Use the LCD screen to display distinguishable colors and countable number of
products.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 5
CHAPTER 2: SYSTEM DESIGN

2.1. SYSTEM OVERVIEW


• The system consists of blocks as shown in Figure 2.1.
• Power block: power supply for the entire operation of the circuit.
o 5 VDC power
o 12 VDC power
o 24VDC Power
• Microcontroller block: PIC16F877A
• Sensor block: TCS3200 color sensor and infrared optical sensor.
• 5 VDC Servo Motor Block: After the product is color recognized, the microprocessor
block will check the color and control the engine to classify the product according to the
recognized color.
• 24 VDC conveyor motor block: Use the motor speed control circuit to adjust the running
speed of the conveyor in accordance with the processing time and reading speed of the sensors.
• LCD Display Block(16x2)
• Reset block
• Crystal Block
• Push button block:
• Start/Stop button

Servo motor
Motor block
block

Sensor block

Figure 2.1. System overview

2.2. COMPONENTS OF THE SYSTEM


2.2.1. Power block
• The schematic and layout of the power circuit are shown in Figure 2.2 and
Figure 2.3.
• Input voltage: Vin = 12 VDC
• Output voltage: Vout = 5 VDC

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 6
Figure 2.2. Schematic diagram of the power circuit

Figure 2.3. Layout and 3D of the power circuit

2.2.2. Microcontroller block


A microcontroller is a computer built into a chip, which is often used to control
electronic devices. Microcontroller, in essence, is a system consisting of a
microprocessor with sufficient performance and low cost (unlike general-purpose
microprocessors used in computers) combined with peripheral blocks such as
memory, input/output modules, digital to analog and analog to digital modules, ...
In computers, modules are usually built by chips and external circuits.
The actual image of PIC16F877A is shown in Figure 2.4.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 7
Figure 2.4. PIC 16F877A microcontroller (Source: Internet)

PIC 16F877A pin

PIN NAME FUNCTION


MCLR: Low level reset operation
1 /MCLR/VPP
– VPP: programmable pressure input
– RA0: output / import number
2 RA0/AN0
– AN0: analog input
– RA1: number export/import
3 RA1/AN1
– AN1 : analog input
– RA2: number export/import
RA2/AN2/V REF-
4 – AN2: analog input
/CVREF
– VREF -: standard voltage input (low) of the A/D unit
– RA3: number export/import
5 RA3/AN3/VREF+ – AN3 : analog input
– VREF+: standard (high) voltage input of the A/D unit
– RA4: Export/import numbers
6 RA4/TOCKI/C1OUT – TOCKI: external clock pulse input for timer0
– C1 OUT: Comparator 1 output
– RA5: number export/import
RA5/AN4//SS – AN4 : analog input 4
7
/C2OUT – SS: secondary SPI selection input
– C2 OUT: comparator output 2

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 8
– RE0: import and export numbers
8 RE0//RD/AN5 – RD: controls the reading at the parallel branch port
– AN5: analog input
– RE1: number export/import
9 RE1//WR/AN6 – WR: controls the recording in the parallel branch port
– AN6 : analog input
– RE2: number export/import
10 RE2//CS/AN7 – CS: The chip selects control at the parallel branch port
– AN7: analog input
11 VDD Power pin of PIC
12 VSS Ground pins
Input to quartz oscillations or external clock pulses.
– OSC1: input to quartz oscillations or external clock pulses. Schmit
13 OSC1/CLKI trigger input when configured in RC mode; another way of CMOS.
– CLKI: external pulse source input. Always combined with OSC1
function.
Quartz oscillating input or clock pulse
– OSC2: Quartz oscillation output. Connect to quartz or resonator.
OSC2/CLKO
14 CLKO : in RC mode, the output of OSC2, equal to the frequency of
OSC1 and indicates the speed of the instruction cycle.
– RC0: digital export/import
15 RC0/T1 OCO/T1CKI – T1OCO: Timer oscillator input 1
– T1CKI: external clock pulse input Timer 1
– RC1: digital export/import
16 RC1/T1OSI/CCP2 – T1OSI: timer oscillator input 1
- CCP2: Capture 2 input, compare 2 output, PWM2 output
– RC2: digital export/import
17 RC2/CCP1
- CCP1: Capture input 1, compare output 1, PWM1 output
– RC3: digital export/import
18 RC3/SCK/SCL – SCK: synchronous serial clock clock input / output of SPI mode
– SCL: synchronous serial clock pulse input / output of I2C mode
– RD0: digital export/import
19 RD0/PSP0
– PSP0: parallel branch port data
– RD1: digital export/import
20 RD1/PSP1
– PSP1: parallel branch port data
– RD2: digital export/import
21 RD2/PSP2
– PSP2: parallel branch port data
– RD3: digital export/import
22 RD3/PSP3
– PSP3: parallel branch port data
– RC4: digital export/import
23 RC4/SDI/SDA – SDI: data into SPI
– SDA: export/import data into I2C
– RC5: digital export/import
24 RC5/SDO
– SDO: data out SPI

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 9
– RC6: digital export/import
25 RC6/TX/CK – TX: USART asynchronous transmission
– CK : USART synchronous pulse
– RC7: digital export/import
26 RC7/RX/DT - RX: received USART disagreement
– DT: USART synchronous data
– RD4: digital export/import
27 RD4/PSP
– PSP4: parallel branch port data
– RD5: digital export/import
28 RD5/PSP5
– PSP5: parallel branch port data
– RD6: digital export/import
29 RD6/PSP6
– PSP6: parallel branch port data
– RD7: digital export/import
30 RD7/PSP7
– PSP7: parallel branch port data
31 VSS Ground pins
32 VDD Power pin of PIC
– RB0: number export/import
33 RB0/INT
– INT: external interrupt
34 RB1 Export/import numbers
35 RB2 Export/import numbers
– RB3: number export/import
36 RB3
– ICPS low voltage programmable pins
– Export/import numbers
37 RB4
- PortB interrupt
– Export/import numbers
38 RB5
- PortB interrupt
– RB6: number export/import
39 RB6/PGC – PGC: ICSP programmable differential circuit and clock pulse
- PortB interrupt
– RB7: number export/import
40 RB7/PGD – PGD: differential circuits and ICSP programmable data
- PortB interrupt

2.2.3. Sensor block


a. TCS3200 color sensor
• The pinout and actual image of the TCS3200 sensor are shown as Figure 2.5.
• Specifications:
• Operating range:
• Supply voltage: (2.7V đến 5.5V)
• Convert from light intensity to frequency with high resolution
• Programmatically select different color filters and output frequency
patterns.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 10
• Low power consumption. Communicate directly with the
microcontroller
• Output signal: Digital

Figure 2.5. TCS3200 color sensor (Source: Internet)


➢ The principle of operation of the color sensor of TCS3200 electronic components:
+ TCS3200 sensor structure consists of 2 blocks as shown below

+ The first block is an 8x8 matrix array consisting of photodiodes. These include
16 photodiodes that can filter blue, 16 photodiodes that can filter red, 16
photodiodes that can filter green and 16 photodiodes that do not filter white. All
photodiodes of the same color are connected parallel to each other, and are placed
interspersed for anti-interference purposes.
+ The nature of the above 4 types of photodiodes as light filters of different colors.
This means that it only receives light of the same color as the corresponding
photodiode and does not receive light of a different color.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 11
• The selection of these 4 types of photodiodes is through 2 input pins S2,S3:

• The 2nd block is a converter of current from the output of the first block into a
frequency :

• The output frequency of TCS3200 electronic components is in the range of


2HZ~500KHZ.
• The output frequency is in the form of square pulses with different frequencies where
the color is different and the luminous intensity is different. We can choose the output
frequency ratio at different levels as shown in the table above to suit the frequency
measurement hardware.
➢ Đấu dây

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 12
➢ Foot function:
• S0,S1 : Input selects output frequency ratio.
• S2,S3 : Input selects photodiode type.
• OE : The input allows frequency output in the OUT pin.
• OUT : The output is a variable frequency depending on intensity and
color.

b. Infrared optical sensor


Used for obstruction detection or color detection.
A diffuse reflector optical sensor is a type of sensor with a common receiver and
transmitter. Diffuse reflective optical sensors are widely used in machine parts or on
production lines for product counting or product classification.

➢ Operating principle:
– When there are no obstructions: light does not reflect to the collecting position or
the surface of the object does not reflect to the collecting position.
– When there are obstacles: the sensor emits light continuously from the emitter.
When encountering an obstruction, the light is reflected back to the receiver position
on the sensor. Characteristics: easily affected by surface, color and maximum
distance of 2m.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 13
Figure 2.6. Optical sensor
➢ Specifications:

- Shell material: Plastic


- Voltage: 6~36VDC
- Output: NPN is usually open
- Detecting distance: 3~30cm
- The detection distance can be adjusted by the rheostat behind the sensor
- Output current: <100mA (Not using direct relay will cause damage to the sensor, the
user isolates via transistor or uses the relay module)
➢ Wiring
- Brown wire: VCC
- Blue wire: GND
- Black wire: Output
2.2.4. Servo Motor block
Servo is a special form of electric motor. Unlike conventional motors that plug in and
rotate continuously, servos rotate only when driven (by PPM pulse) with rotation
angles ranging from 00 to 1800. Each type of servo has different sizes, masses and
structures.
➢ Specifications:

- Tên: Servo MSG90


- Điện áp hoạt động: 4.8V ~ 6V DC
- Tốc độ quay: 0.12 giây/60° (4.8V) , 0.1 giây/60° (6V)
- Mômen xoắn: 1,8kg/cm (4,8V), 2,5kg/cm (6V)
- Góc quay: 180° - Bánh răng: nhựa
- Kích thước: 22.5 * 11.8 * 30 mm

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 14
- Chiều dài dây điện: 175mm
- Trọng lượng: 9g
- Nhiệt độ hoạt động: 0°C ~ 55°C
➢ Wiring
+ Orange wire: Pulse
+ Red wire: Vcc (4.8V ~ 6V)
+ Black wire: GND / 0V

Figure 2.7. Servo structure Figure 2.8. Servo SG90

(Source: Internet)

2.2.5. Conveyor Motor Block


• Realistic image of the engine.

Figure 2.9. 24v motor


• Function: Conveyor operation
2.2.6. Screen Block
• The pinout and actual image of the LCD are shown as Figure 2.10

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 15
Figure 2.10. LCD 16x2 display
(Source: Internet)
➢ The specifications of LCD 16X2 are discussed below.
• The operating voltage of this display ranges from 4.7V to 5.3V
• The display bezel is 72 x 25mm
• The operating current is 1mA without a backlight
• PCB size of the module is 80L x 36W x 10H mm
• HD47780 controller
• LED color for backlight is green or blue
• Number of columns – 16
• Number of rows – 2
• Number of LCD pins – 16
• Characters – 32
• It works in 4-bit and 8-bit modes
• Pixel box of each character is 5×8 pixel
• Font size of character is 0.125Width x 0.200height

2.2.7. Push button block:


The actual image of the 2-pin push button is shown as shown in Figure 2.12

Figure 2.12. 2-pin push button (Source: Internet)

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 16
2.3. CIRCUIT DESIGN
2.3.1. Circuit design on Proteus 8.9 professional software
a) Schematic capture
• The schematic diagram of the main circuit is shown as Figure 2.16.
• The main circuit consists of 10 blocks
• Power block: Power supply to the main circuit.
• Microcontroller block: Transmitting and receiving signals, controlling
circuits.
• Reset block
• Test Code Block
• Crystal Block
• PIC Loading Block
• Manual Control Block
• LCD Block
• Computer communication block

Figure 2.16. Schematic diagram of the main circuit

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 17
b) PCB layout
• The layout of the main circuit and LCD circuit is shown as shown in Figure 2.17
and 2.18.

Figure 2.17. Layout of the main circuit

c) 3D visualizer
The 3D visualizer of the main circuit and LCD circuit is shown as shown in Figure 2.19.

Figure 2.19. 3D visualizer

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 18
2.3.2. Practical external circuit design
• The image of the circuit after ironing, soldering and assembling components is
shown as Figure 2.21.
Hình 2.21. Hoàn thiện mạch chính

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 19
2.4. SOFTWARE DESIGN OF THE SYSTEM
2.4.1. Algorithmic flowchart
2.4.1.1. Algorithmic flowchart of the main program
• The main program flowchart of the system is shown in Figure 2.22.

Figure 2.22. Algorithmic flowchart of the main program

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 20
2.4.1.2. Algorithmic flowchart of subroutines

Figure 2.23. Subroutine algorithm flowchart Do_tanso and Ngat_ngoai

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 21
Figure 2.23. Subroutine algorithm flowchart Quaysangphai and Quayvelai

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 22
2.4.2. Program code on CCS software

CCS Program Code


1
#include <codetonghop.h>
2
#FUSES HS
3
4 #include <16F887.h>
5 #device ADC=16
6
7 #FUSES NOWDT //No Watch Dog Timer
8
#FUSES NOBROWNOUT //No brownout reset
9
10 #FUSES NOLVP //No low voltage prgming, B3(PIC16) or
11 B5(PIC18) used for I/O
12
13 #use delay(crystal=4000000)
14
15
#define LCD_ENABLE_PIN PIN_D1
16
#define LCD_RS_PIN PIN_C3
17
18 #define LCD_RW_PIN PIN_D0
19 #define LCD_DATA4 PIN_D2
20 #define LCD_DATA5 PIN_D3
21 #define LCD_DATA6 PIN_C4
22
#define LCD_DATA7 PIN_C5
23
24
25 #include <lcd.c>
26 #define S0 PIN_D4
27 #define S1 PIN_D5
28
#define S2 PIN_D6
29
#define S3 PIN_D7
30
31 #define LED PIN_B1
32
33
34 #define CBQ1 PIN_B0
35
#define CBQ2 PIN_A3
36
37 #define BANGTAI PIN_E0

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 23
38 float tanso=0,F_R=0,F_B=0,F_G=0;
39 int16 R=0,B=0,G=0;
40
unsigned int i;
41
42 float a=0;
43 int spD=0;
44 int spX=0;
45 int bien=0;
46
char m;
47
#define servo PIN_A2
48
49
50 //================================//
51 // Tien Hanh nhan tan so tu TCS3200 output
52 // Doc tung phan tan so anh sang Red, sau do la Blue cuoi cung la
53 Green
54
void do_tanso()
55
56 {
57 output_high(LED);
58 output_low(S2);
59 output_low(S3);
60
61
delay_ms(5);
62
63 tanso=get_timer1(); // 100
64 F_R=tanso;
65 set_timer1(0);
66
67
output_low(S2);
68
output_high(S3);
69
70 delay_ms(5);
71
72 tanso=get_timer1();
73 F_B=tanso;
74
set_timer1(0);
75
76
77

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 24
78 output_high(S2);
79 output_high(S3);
80
delay_ms(5);
81
82 tanso=get_timer1();
83 F_G=tanso;
84 set_timer1(0);
85
86
87
DELAY_MS(100);
88
89 r=f_r;
90 g=f_g;
91 b=f_b;
92 }
93
void quayvelai()
94
95 {
96 for (i=0;i<20;i++)
97 {
98 output_high(servo);
99 delay_us(3500);
100
output_low(servo);
101
102 delay_us(1000);
103 }
104 }
105
106
void quaysangphai()
107
{
108
109 for (i=0;i<20;i++)
110 {
111 output_high(servo);
112 delay_us(1500);
113
output_low(servo);
114
115 delay_us(7000);
116 }
117 }

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 25
118
119 //=============================================
120 =====//
121
//Hien thi Tan so do duoc de xac dinh khoang tan so
122
123 void hienthitanso()
124 {
125 lcd_gotoxy(1,1);
126 lcd_putc("R=");
127
lcd_putc(r/1000%10+48); // tach chuoi tung so sau do +48 de
128
chuyen sang bang ma ASCII
129
lcd_putc(r/100%10+48);
130
131 lcd_putc(r/10%10+48);
132 lcd_putc(r%10+48);
133
134 lcd_gotoxy(11,1);
135
lcd_putc("G=");
136
137 lcd_putc(g/1000%10+48);
138 lcd_putc(g/100%10+48);
139 lcd_putc(g/10%10+48);
140 lcd_putc(g%10+48);
141
142
lcd_gotoxy(1,2);
143
144 lcd_putc("B=");
145 lcd_putc(b/1000%10+48);
146 lcd_putc(b/100%10+48);
147 lcd_putc(b/10%10+48);
148
lcd_putc(b%10+48);
149
150 }
151 //=============================================
152 ==//
153 #int_TIMER0
154 void Ngat_Timer0() {
155
156
do_tanso();
157

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 26
158
159 set_timer0(0);
160
}
161
162 //=============================================
163 =====//
164 #INT_EXT
165 void isr()
166 {
167
delay_ms(5);
168
do_tanso();
169
170 if((R<120))
171 {
172 a=a+1;
173 lcd_putc(" \f ") ;
174
lcd_gotoxy(4,1);
175
176 lcd_putc(" Mau do ");
177 delay_ms(10);
178 spD=spD+1;
179 }
180 if(R>140)
181
{
182
183
184 lcd_putc(" \f ") ;
185 lcd_gotoxy(4,1);
186 lcd_putc(" Mau xanh ");
187
delay_ms(10);
188
spX=spX+1;
189
190 }
191 }
192
193 void main()
194
{
195
196 //=============================================
197 ==//

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 27
198
199 set_tris_b(0b00001101);
200
set_tris_c(0b0000011);
201
202 set_tris_d(0);
203 set_tris_a(0b00001000);
204 set_tris_e(1);
205 port_b_pullups(1);
206
207
//=============================================
208
=//
209
210 // chon tan so nhan ve la 2% tan so mac dinh
211 output_low(S1);
212 output_high(S0);
213 a=0;
214
215
216 //==========================================//
217 // Khoi tao timer 1 ngat ngoai ngat timer
218 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
219 set_timer1(0);
220 setup_timer_0(T1_INTERNAL|T0_DIV_256);
221
set_timer0(0);
222
223 enable_interrupts(INT_TIMER0);
224 ext_int_edge(h_to_l);
225 enable_interrupts(int_ext);
226 enable_interrupts(GLOBAL);
227
//=======================//
228
// Dieu khien toc do bang tai
229
230 setup_timer_2(T2_DIV_BY_4,255,1);
231 setup_CCP1(CCP_PWM);
232 output_low(PIN_A1);// IN2
233 output_high(PIN_A0);// IN1
234
set_pwm1_duty(0);
235
236
237 //=========================================//

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 28
238 //Khoi tao LCD
239 lcd_init();
240
delay_ms(200);
241
242 lcd_gotoxy(1,1);
243 lcd_putc("phan loai sp");
244 lcd_gotoxy(1,2);
245 lcd_putc("My Linh_Kim Thuy");
246
delay_ms(2000);
247
lcd_putc("\f");
248
249 //=========================//
250 a=0;
251 spD=0;
252 while(true)
253
{
254
255 if(input(BANGTAI)==0){bien++;delay_ms(50);}
256 if(bien%2==1){
257 set_pwm1_duty(255);
258 }
259 if(bien%2==0){
260
set_pwm1_duty(0);
261
262 }
263 if(input(CBQ2)==0&&a>0)
264 {
265 a=a-1;
266
quaysangphai();
267
quayvelai();
268
269
270 }
271 delay_ms(10);
272 lcd_gotoxy(1,2);
273
printf(lcd_putc,"spX=%d",spX);
274
275 lcd_gotoxy(10,2);
276 printf(lcd_putc,"spD=%d",spD);
277 }

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 29
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 30
CHAPTER 3: USER MANUALS AND SOFTWARE
3.1. INSTRUCTIONS FOR USE OF THE PRODUCT
• The actual image of the product is shown in Figure 3.1.

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 31
BIBLIOGRAPHY

[1] Đặng Phước Vinh, Võ Như Thành, “Giáo trình kỹ thuật vi điều khiển PIC”, NXB Xây
dựng, 2022.
[2] Đặng Phước Vinh, Trần Quang Khải, Đoàn Lê Anh, Võ Như Thành, Phạm Anh Đức,
“Lập trình vi điều khiển PIC và thiết bị ngoại vi”, NXB Khoa học và kỹ thuật, 2022.
[3] Datasheet of PIC16F877A, truy cập gần nhất ngày 28/3/2023.
https://ww1.microchip.com/downloads/en/devicedoc/39582b.pdf

SVTH: Văn Thị Kim Thùy – Nguyễn Thị Mỹ Linh


Trang 32

You might also like