05 Communication Protocol
05 Communication Protocol
05 Communication Protocol
</ENT269>
Objectives
• Upon completion of this chapter, you will
be able to:
– Explain serial communication protocol
– Describe the serial communication features of
the PIC18
– Program the PIC18 serial port in C
What is a communication
protocol?
“A set of formal rules describing how to exchange data.”
Example of a serial interface, transmitting one bit every clock pulse. Just 2 wires
required!
Basics of Serial Communication
• Asynchronous
• Synchronous
Wired protocols:
SPI, I2C, CAN, UART(RS232, RS485), CAN, USB,
1-wire
Wireless protocols:
Zigbee, Bluetooth, RFID, LoRa, IoT (MQTT, HTTP,
CoAP), NFC
Wired Protocol
I2C - Inter-integrated Circuit: For on chip board communication between
ICs and micro controller, suitable for short distance communication on the
PCB circuit board.
SPI - Serial Peripheral Interface: Most relevant protocol for high speed
communication , For example SD card in mobile makes use of SPI for high
speed data transfer.
SPI used mainly for data communication at short distance but with high speed. It is
a synchronous serial communication link.
As shown in the figure one slave is connected with one master device. Clock is
generated by master device for synchronization of data transfer. It is also possible
to connected more than one slave device with single master for communication. SPI
interface operates in either half or full duplex mode.
SPI (Multiple Slaves)
1
- Need a separate SS line.
- To talk to a particular slave, you’ll
make that slave’s SS line low and
keep the rest of them high
I2C requires a mere two wires, like asynchronous serial, but those two wires can
support up to 1008 slave devices. Also, unlike SPI, I2C can support a multi-master
system, allowing more than one master to communicate with all devices on the bus
(although the master devices can’t talk to each other over the bus and must take turns
using the bus lines).
1-wire
1-Wire is a device communications bus system designed by Dallas
Semiconductor Corp. that provides low-speed data, signaling, and power
over a single conductor. 1-Wire is similar in concept to I2C, but with lower
data rates and longer range. It is typically used to communicate with small
inexpensive devices.
Universal
Asynchronous
Receiver-Transmitter
(UART)
UART
Rules of Serial:
Baud rates can be just about any value within reason. The only
requirement is that both devices operate at the same rate. One of the more
common baud rates, especially for simple stuff where speed isn’t critical, is
9600 bps. Other “standard” baud are 1200, 2400, 4800, 19200, 38400,
57600, and 115200.
The higher a baud rate goes, the faster data is sent/received, but there are
limits to how fast data can be transferred. You usually won’t see speeds
exceeding 115200 - that’s fast for most microcontrollers. Get too high, and
you’ll begin to see errors on the receiving end, as clocks and sampling
periods just can’t keep up.
Framing the data
Each block (usually a byte) of data transmitted is actually sent in a packet
or frame of bits. Frames are created by appending synchronization and
parity bits to our data.
A serial frame
The real ‘meat’ of every serial packet is the data it carries. The amount of
data in each packet can be set to anything from 5 to 9 bits. Certainly, the
standard data size is your basic 8-bit byte, but other sizes have their
uses.
The synchronization bits are two or three special bits transferred with
each chunk of data. They are the start bit and the stop bit(s). True to their
name, these bits mark the beginning and end of a packet. There’s always
only one start bit, but the number of stop bits is configurable to either one or
two (though it’s commonly left at one). The start bit is always indicated by an
idle data line going from 1 to 0, while the stop bit(s) will transition back to the
idle state by holding the line at 1.
A device transmitting the ASCII characters ‘O’ and ‘K’ would have to
create two packets of data. The ASCII value of O (that’s uppercase) is 79,
which breaks down into an 8-bit binary value of 01001111, while K’s
binary value is 01001011. All that’s left is appending sync bits.
It isn’t specifically stated, but it’s assumed that data is transferred least-
significant bit first. Notice how each of the two bytes is sent as it reads
from right-to-left.
Since we’re transferring at 9600 bps, the time spent holding each of those
bits high or low is 1/(9600 bps) or 104 µs per bit.
For every byte of data transmitted, there are actually 10 bits being sent: a
start bit, 8 data bits, and a stop bit. So, at 9600 bps, we’re actually
sending 9600 bits per second or 960 (9600/10) bytes per second.
Example
DB-25
25-Pin Connector
DB-9
9-Pin Connector
RS-232 Standards
• Standard for serial comm (COM port)
1: -3V to -25V;
0: +3V to +25V
– Reason: for long distance wired line
• Connectors
– Minimally, 3 wires: RxD, TxD, GND
– Could have 9-pin or 25-pin
Personal
Computer
PIC18 Connection to RS232
Microcontroller
UART Interfacing/Application
Selecting Baud Rates
• Baud rate in PIC18 is SPBRG
programmable Baud Rate
(Hex Value)
• The value loaded into
38400 3
the SPBRG decides
the baud rate 19200 7
9600 F
4800 20
2400 40
1200 81
*For XTAL = 10MHz only!
Baud Rate Formula
Desired Baud Rate = Fosc/(64(X+1))
X = (156250/1200) – 1
X = 129.21 = 129 = 81H
TXSTA (Transmit Status and
Control Register)
TXSTA (Transmit Status and
Control Register)
RCSTA (Receive Status and
Control Register)
RCSTA (Receive Status and
Control Register)
PIR: Peripheral Interrupt Register 1
Serial
Communication
Interface-Cable
Serial
Communication
Interface-USB
Wireless Devices
Serial
Communication
Interface-Wireless
Example1
Write a C program to transfer the letter ‘G’ serially at 9600,
continuously. Use 8-bit data and 1 stop bit. Assume XTAL = 10MHz.
Example 1 (cont’d)
Example2
Write a C program to transfer the message “YES” serially at 9600,
continuously. Use 8-bit data and 1 stop bit. Assume XTAL = 10MHz.
Example2 (cont’d)
Example 3
Write a C program to receive bytes of data serially and put them on
PORTB. Set the baud rate at 9600, 8-bit data and 1 stop bit.
Example 3 (cont’d)
RCSTA = 0x90;
Exercise
Select the suitable SPBRG value and TXSTA configuration for a PIC18
microcontroller to set the baud rate at 9600. Then, sketch a flowchart and
write a C18 program to monitor the status of a PIR sensor as shown in
Figure 2. If the PIR sensor (RC7) is activated, send a message “ALRT” to
the serial port continuously. But if the sensor is not activated, send a
message “SAFE” to the serial port. Assume the crystal oscillator frequency
is 10 MHz.
Solution
Exercise
Back
Front
Application: PIR
Sensor
1) Camping
2) Home Security
3) Etc…
Internet of things (IoT)
"Things", in the IoT sense, can refer to a wide variety of devices such
as heart monitoring implants, biochip transponders on farm animals,
cameras streaming live feeds of wild animals in coastal waters,[12]
automobiles with built-in sensors, DNA analysis devices for
environmental/food/pathogen monitoring,[13] or field operation devices
that assist firefighters in search and rescue operations.
-wikipedia
What is ESP8266
The ESP8266 is a System on a Chip (SoC), manufactured
by the Chinese company Espressif. It consists of a
Tensilica L106 32-bit microcontroller unit (MCU) and a WiFi
transceiver. It has 11 GPIO pins* (General Purpose
Input/Output pins), and an analog input as well. This means
that you can program it like any normal Arduino or other
microcontroller. And on top of that, you get WiFi
communication, so you can use it to connect to your WiFi
network, connect to the Internet, host a web server with
real web pages, let your smartphone connect to it, etc . The
possibilities are endless! It's no wonder that this chip has
become the most popular IOT device available.
Type of
ESP8266
Our Lab ESP8266 (ESP12F)
Main
Technical
Specification
Functional Block Diagram
ESP8266
AT
Instruction
Set
http://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/
Thingspeak (Cloud)
https://thingspeak.com/
Example
Continuously send “AT\r\n” command to ESP8266 (embedded in
ESP12 board) every 5 second. The ESP8266 should reply “OK”
if everything is good. Otherwise it will reply “ERROR”. If the
reply from ESP8266 is “OK”, you have to toggle RB1; otherwise,
toggle RB2.
Note: \r\n = Carriage Return (CR) + Line Feed (LF) //Used as a new line character in Windows
Schematic:
ESP12F Operating Voltage:
3.3V to 3.6V only!!!
3.3V
5V
In our case:
void hello_esp8266(void);
unsigned char receive_and_check(void);
void delay_in_sec(unsigned char second);
void send(unsigned char *p);
Void main()
{
ADCON0 = 0;
ADCON1 = 0X0F; // All Analog port are Digital
TXSTA = 0x20; //Select low baudrate, 8bit
SPBRG = 32; //9600bps, XTAL = 20Mhz
TRISCbits.TRISC6 = 0; //TX pin
TRISCbits.TRISC7 = 1; //RX pin
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB4 = 0;
TXSTAbits.TXEN = 1; //Transmit enable
RCSTAbits.CREN = 1; //Enable continuous receive
RCSTAbits.SPEN = 1; //Enable serial port
PORTBbits.RB1=0;
PORTBbits.RB2=0;
while(1)
{
hello_esp8266();
delay_in_sec(5);
}
}
//This code used for Received "OK" from ESP8266
void hello_esp8266()
{
while (1)
{
send(AT1_msg);
if (receive_and_check())
{
PORTBbits.RB6=1; //Green LED ON indicates no error
PORTBbits.RB7=0; //Red LED ON indicates error
break;
}
else
{
PORTBbits.RB7=1;
PORTBbits.RB6=0;
}
}
}
// To check either received "OK" or not from ESP8266
unsigned char receive_and_check()
{
unsigned char z;
DATA
AT Command
Exercise
Write a C program to access ESP8266 using AT
command and send the data to Thingspeak server.
Exercise
By using:
#include <stdio.h>
#include <stdlib.h>
//Random between 0 to 50
x = rand() % 50;
y = rand() % 100;
Thank You