MCU - LAB - 02 - RS232 Vs I2C Communication
MCU - LAB - 02 - RS232 Vs I2C Communication
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.
1
FL061
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.
2
FL061
3
FL061
4
FL061
WritedataProximitySensor(0x52, 0x41);
WritedataProximitySensor(0x00, 0x4A);
}
----------
5
FL061
{
WritedataProximitySensor(value & 0xFF, 0x4B);
WritedataProximitySensor(value >>8, 0x4C);
}
----------
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.
6
FL061
7
FL061
3. CONTENT OF LAB
3.1 Time: 5 hours for each student group.
3.2 Experiments
8
FL061
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);
}
}
Choose: COM Port to connect to the kit board, Baudrate, Data bits, Parity, Stop bits, Flow
control.
10
FL061
11
FL061
12
FL061
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];
13
FL061
void main(void)
{
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 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
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: .........................................................................................................................................
...........................................................................................................................................................
...........................................................................................................................................................
16