0% found this document useful (0 votes)
112 views16 pages

MCU - LAB - 02 - RS232 Vs I2C Communication

The document provides instructions for a lab experiment on RS232 and I2C communication protocols using a PIC16LF15376 microcontroller. The lab contains two exercises: 1. Using an ADC sensor and RS232 to read light levels and transmit the values to a computer terminal. 2. Using a proximity sensor over I2C to detect nearby objects and turn on an LED. Details are given on sensor registers, initialization functions, and code examples to configure the communication protocols and sensors. The goal is for students to learn how to program a microcontroller to communicate with devices over serial interfaces and read signals from sensors. Devices used include a motherboard, daughterboard with sensors, and software tools.

Uploaded by

Quân Hà
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)
112 views16 pages

MCU - LAB - 02 - RS232 Vs I2C Communication

The document provides instructions for a lab experiment on RS232 and I2C communication protocols using a PIC16LF15376 microcontroller. The lab contains two exercises: 1. Using an ADC sensor and RS232 to read light levels and transmit the values to a computer terminal. 2. Using a proximity sensor over I2C to detect nearby objects and turn on an LED. Details are given on sensor registers, initialization functions, and code examples to configure the communication protocols and sensors. The goal is for students to learn how to program a microcontroller to communicate with devices over serial interfaces and read signals from sensors. Devices used include a motherboard, daughterboard with sensors, and software tools.

Uploaded by

Quân Hà
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/ 16

FL061

FACULTY OF MECHANICAL
ENGINEERING LAB 2: RS232 COMMUNICATION
DEPARTMENT OF MECHATRONICS
- I2C PROTOCOL - LIGHT
PROXIMITY SENSOR

1. INTRODUCTION
1.1. RS232 communication
The Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART)
module is a serial I/O communications peripheral. It contains all the clock generators, shift
registers and data buffers necessary to perform an input or output serial data transfer independent
of device program execution. The EUSART, also known as a Serial Communications Interface
(SCI), can be configured as a full-duplex asynchronous system or half-duplex synchronous
system.
A wiring diagram between MCU and equipment using RS232 communication is shown
in Figure 1. In this figure, the pin RC1 is UART_RX receive pin, the pin RC2 is UART_TX
transmit pin.

Figure 1: Wiring diagram of PIC16LF15376.

Figure 2: The pin of UNIBUS

Faculty of Mechanical Engineering @HCMUT

1
FL061

1.2. Introduction of I2C protocol


The Inter-Integrated Circuit bus (IIC or I2C) is a synchronous bus that operates on a
master-slave principle. It uses two single-ended wires SCL (Serial Clock Line) and SDA (Serial
Data Line) for half-duplex communication. The protocol has been developed by Philips and is
widely used for (short distance) communication between one or more controllers and peripheral
devices. One of the main advantages of the I2C bus is its easy extensibility. New devices can be
added to the bus by just connecting them. There is no specific limit on the number of devices
connected to the bus if the maximum bus capacitance of 400 pF is not exceeded.
A wiring diagram between MCU and equipment using I2C communication is shown in
Figure 1. In this figure, the pin RC6 is SCL pin, the pin RB2 is SDA pin.

1.3. Introduction of Proximity - Light Sensor (RPR-0521RS)


RPR-0521RS is a module with integrates optical proximity, digital ambient light sensor
IC and infrared LED (IrLED). The proximity sensor (PS) part detects the human or object
approaching by the reflection of IrLED light. The Ambient light sensor (ALS) part detects the
wide range of illumination, from the dark environment to the direct sunlight. The illuminant
intensity of LCD display and keypad can be adjusted by using RPR-0521RS. It enables lowering
current consumption and/or improving the visibility under the bright environment.

Figure 3: Block diagram and block explanation of RPR-0521RS sensor.

Figure 3 shows the block diagram and block explanation of the sensor. The pin number
and the explanation about the function of pins are shown in Figure 4.

Faculty of Mechanical Engineering @HCMUT

2
FL061

Figure 4: The symbol and description of PIN number.

1.4. Important registers and address of RPR-0521RS


To use and program for the RPR-0521RS, several important registers of the sensor need
to be considered.
a) I2C bus communication of RPR-0521RS
The I2C address of the sensor needs to be included. The MCU plays as a Master and the sensor
plays as a Slave.

#define RPR_0521RS_ADDRESS 0x38

b) Main write format

Function to write to the registers of the sensor


----------
void WritedataProximitySensor(uint8_t transmitt, uint8_t pointer_reg)
{
uint8_t send[2];
send[0]= pointer_reg;
send[1]= transmitt;
I2C1_WriteNBytes(RPR_0521RS_ADDRESS, send, 2);
}
----------
Faculty of Mechanical Engineering @HCMUT

3
FL061

c) Main read format

Function to read to the registers of the sensor


----------
void GetdataProximitySensor(uint8_t receive[], uint8_t pointer_reg , uint8_t length)
{
uint8_t send;
send= pointer_reg;
I2C1_WriteNBytes(RPR_0521RS_ADDRESS, &send, 1);
I2C1_ReadNBytes(RPR_0521RS_ADDRESS, receive, length);
}
----------

d) MODE_CONTROL Register (41h)

Function to enable the sensor


----------
void Proximity_Sensor_Init (void)
{
Faculty of Mechanical Engineering @HCMUT

4
FL061

WritedataProximitySensor(0x52, 0x41);
WritedataProximitySensor(0x00, 0x4A);
}
----------

e) INTERRUPT Register (4Ah)

Function to enable the pin INT


----------
void Proximity_Interupt_Init (uint8_t reg_value)
{
WritedataProximitySensor(reg_value, 0x4A);
}
----------

f) THRESHOLD Registers of the Proximity Sensor (PS)

Function to set the PS Threshold High


-----------
void Set_PS_ThresholdHigh(uint16_t value)

Faculty of Mechanical Engineering @HCMUT

5
FL061

{
WritedataProximitySensor(value & 0xFF, 0x4B);
WritedataProximitySensor(value >>8, 0x4C);
}
----------

Function to set the PS Threshold Low


----------
void Set_PS_ThresholdLow(uint16_t value)
{
WritedataProximitySensor(value & 0xFF, 0x4D);
WritedataProximitySensor(value >>8, 0x4E);
}
----------

1.5. Goal of LAB


After learning of this LAB, the students should have the following abilities:
- Know how to program for the microcontroller to communicate with other device via
RS232 Communication.
- Know how to program for the microcontroller to read the signal from the sensor via I2C
Communication Protocol.
- Know how to use the Proximity Sensor.
- Know how to use the Ambient Light Sensor.
- Know how to use the ADC Sensor.

2. EXPERIMENTAL DEVICES
2.1 Devices
• Hardware (Experimental Kit for Microcontroller LAB): Motherboard 15376,
Daughterboard Light vs Proximity Sensor, Phototransistor RPM-075PTT86 (Q5),
Pickit3, Power.
• Software: MPLAB X, XC8 Compiler, Advanced Serial Port Terminal.

Faculty of Mechanical Engineering @HCMUT

6
FL061

Figure 5: The Light and Proximity Sensor daughterboard.

Figure 6: Schematic diagram of the ADC sensor.

Figure 7: Phototransistor RPM-075PTT86 (Q5) on board.

2.2 Specification of Devices


• Voltage level: 3.6V
• PIC16LF15376 microcontroller.

Faculty of Mechanical Engineering @HCMUT

7
FL061

3. CONTENT OF LAB
3.1 Time: 5 hours for each student group.

3.2 Experiments

Exercise 1: Using the ADC sensor vs RS232 Communication.


Program to read the signal from the Phototransistor RPM-075PTT86 Sensor, when the light hits
the phototransistor larger, the smaller the ADC reading value will be and vice versa. Program the
MCU as follows:
- The ADC value is displayed on the Advanced Serial Port Terminal every 200ms over the
COM port communication with the computer.
- When ADC value is less than 100, print to Terminal “Detect light”.
Instruction
The necessary modules.

Figure 8: The necessary modules for the exercise 1.

Configuration for Pin manager.

Figure 9: Configuration for Pin manager of the exercise 1.


Faculty of Mechanical Engineering @HCMUT

8
FL061

Configuration for Pin Module.

Figure 10: Configuration for Pin Module of the exercise 1.

Configuration for ADC Module.

Figure 11: Configuration for ADC Module of the exercise 1.

Configuration for EUSART module.

Figure 12: Configuration for EUSART Module of the exercise 1.


Faculty of Mechanical Engineering @HCMUT

9
FL061

Main.c
#include "mcc_generated_files/mcc.h"
uint16_t result_ADC;

void main(void)
{
SYSTEM_Initialize();

while (1)
{
CLRWDT();
result_ADC = ADC_GetConversion(channel_ANB4); // Read ADC
printf("%u\n",result_ADC); // Send the value of ADC sensor via EUSART module
if(result_ADC <= 100 )
{
printf("Detect light\n");

}
__delay_ms(200);
}
}

Instruction of How to use Advanced Serial Port Terminal


Choose “New Session” or “File -> New Session”

Choose: COM Port to connect to the kit board, Baudrate, Data bits, Parity, Stop bits, Flow
control.

Faculty of Mechanical Engineering @HCMUT

10
FL061

Click “Open” to open the COM Port.

The monitor shows the received results.

Send the data by the window “Send”.

Faculty of Mechanical Engineering @HCMUT

11
FL061

Exercise 2: Using the Proximity Sensor vs I2C Communication Protocol.


Program to read the signal from the Proximity Sensor (PS) as follows:
- Normally the LED1 is OFF.
- When the Proximity Sensor detects the object near the sensor, LED1 is ON.
Instruction
Choose necessary modules.

Figure 13: The used modules in exercise 2.

Configuration for Pin Manager.

Figure 14: The Pin Manager in exercise 2.

Configuration for Pin Module

Figure 15: The Pin Module in exercise 2.

Faculty of Mechanical Engineering @HCMUT

12
FL061

Configuration for Module EUSART1

Figure 16: The Module EUSART1 in exercise 2.

Cấu hình Module MSSP1

Figure 17: The Module MSSP1 in exercise 2.

Main.c

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#define RPR_0521RS_ADDRESS 0x38
uint8_t getdata[10];

void GetdataProximitySensor(uint8_t receive[], uint8_t pointer_reg , uint8_t length)


{
uint8_t send;
send= pointer_reg;
I2C1_WriteNBytes(RPR_0521RS_ADDRESS, &send, 1);
I2C1_ReadNBytes(RPR_0521RS_ADDRESS, receive, length);
}

Faculty of Mechanical Engineering @HCMUT

13
FL061

void WritedataProximitySensor(uint8_t transmitt, uint8_t pointer_reg)


{
uint8_t send[2];
send[0]= pointer_reg;
send[1]= transmitt;
I2C1_WriteNBytes(RPR_0521RS_ADDRESS, send, 2);
}

void Proximity_Sensor_Init (void)


{
WritedataProximitySensor(0x52, 0x41);
WritedataProximitySensor(0x00, 0x4A);
}

void Proximity_Interupt_Init (uint8_t reg_value)


{
WritedataProximitySensor(reg_value, 0x4A);
}

void Set_PS_ThresholdHigh(uint16_t value)


{
WritedataProximitySensor(value & 0xFF, 0x4B);
WritedataProximitySensor(value >>8, 0x4C);
}

void Set_PS_ThresholdLow(uint16_t value)


{
WritedataProximitySensor(value & 0xFF, 0x4D);
WritedataProximitySensor(value >>8, 0x4E);
}

void main(void)
{

// initialize the device


SYSTEM_Initialize();

printf("Hello\n");
Proximity_Sensor_Init();
Proximity_Interupt_Init (0x05);
Set_PS_ThresholdHigh(1500);
__delay_ms(300);
while (1)
{
CLRWDT();

if(INT_GetValue() == 0)
{
LED1_SetHigh();
}
Faculty of Mechanical Engineering @HCMUT

14
FL061

else
{
LED1_SetLow();
}

__delay_ms(300);
}
}

Exercise 3: Using the Ambient Light Sensor.


Program to read the signal from the Ambient Light Sensor (ALS) as follows:
- Normally the LED2 is OFF.
- When the light intensity is smaller than a threshold value, LED2 is ON.
Instruction
- Configure to use the Ambient Light Sensor.

Exercise 4: Reading the Light Intensity from the Ambient Light Sensor.
Program to read the Light Intensity from the Ambient Light Sensor (ALS) as follows:
- Normally all LED1, LED2, LED3 are OFF.
- When the light intensity is smaller than the first threshold value, LED1 is ON.
- When the light intensity is smaller than a second threshold value, LED1 vs LED2 are
ON.
- When the light intensity is smaller than a third threshold value, LED1, LED2 vs LED3
are ON.
Instruction
- Read the ADC signal from the Ambient Light Sensor.

4. REFERENCES
[1] Datasheet of RPR-0521RS sensor:
https://fscdn.rohm.com/en/products/databook/datasheet/opto/optical_sensor/opto_module
/rpr-0521rs-e.pdf

[2] Datasheet of MCU PIC16LF15376.

Faculty of Mechanical Engineering @HCMUT

15
FL061

5. EXPERIMENTAL RESULTS

Experiment 1:
Weak Fair Good
Comment: .........................................................................................................................................
...........................................................................................................................................................
...........................................................................................................................................................

Experiment 2:
Weak Fair Good
Comment: .........................................................................................................................................
...........................................................................................................................................................
...........................................................................................................................................................

Experiment 3:
Weak Fair Good
Comment: .........................................................................................................................................
...........................................................................................................................................................
...........................................................................................................................................................

Experiment 4:
Weak Fair Good
Comment: .........................................................................................................................................
...........................................................................................................................................................
...........................................................................................................................................................

Name:…………………………………………….Student ID:……………………Group: .............


Name:…………………………………………….Student ID:……………………Group: .............
Name:…………………………………………….Student ID:……………………Group: .............
Name:…………………………………………….Student ID:……………………Group: .............
Name:…………………………………………….Student ID:……………………Group: .............
Name:…………………………………………….Student ID:……………………Group: .............

Faculty of Mechanical Engineering @HCMUT

16

You might also like