Flight plan • In this lesson we will review a couple of communication peripherals available in all the general-purpose devices in the new PIC24 family.. • Asynchronous serial communica
Trang 1Hệ thống nhúng
Thạc sĩ Lê Mạnh Hải
Embedded Systems
Trang 2Lesson 6 :Communication
Motivation:
• Synchronous serial interfaces
• Asynchronous serial interfaces
• Parallel interfaces
• Synchronous communication using the SPI modules
• Testing the Read Status Register command
• Writing to the EEPROM
• Reading the memory contents
• A nonvolatile storage library
• Testing the new NVM library
Trang 3Flight plan
• In this lesson we will review a couple of
communication peripherals available in all the
general-purpose devices in the new PIC24 family
• Asynchronous serial communication interfaces
UART1 and UART2,
• Synchronous serial communication interfaces SPI1
and SPI2, comparing their relative strengths and limitations for use in embedded-control
applications.
Trang 4Preflight checklist
• MPLAB® IDE,
• MPLAB C30 compiler and the
• MPLAB SIM simulator,
• Explorer16 demonstration board and the
• MPLAB ICD2 In Circuit Debugger
Trang 5The flight
• The PIC24FJ128GA010 offers seven
communication peripherals that are designed to assist in all common embedded-control
applications.
– 2 × the universal asynchronous receiver and
transmitters (UARTs) – 2 × the SPI synchronous serial interfaces
– 2 × the I2C™ synchronous serial interfaces
Trang 6Synchronous serial interfaces
Trang 7Sharing an SPI bus among multiple masters is
theoretically possible but practically very rare
The main advantages of the SPI interface are truly
its simplicity and the speed that can be one order
of magnitude higher than that of the fastest I2C
bus (even without taking into consideration the
details of the protocol- specifi c overhead)
Trang 8Asynchronous serial interfaces
The synchronization between transmitter and
receiver is obtained by extracting timing information
from the data stream itself Start and stop bits are
added to the data and precise formatting (with a fi
xed baud rate) allow reliable data transfer
Trang 99
Trang 10Parallel interfaces
• The Parallel Master Port (PMP) completes the list of
basic communication interfaces of the PIC24.
• The PMP has the ability to transfer up to 8 bits of
information at a time while providing several address lines
– Interface directly to most LCD display modules commercially
available (alphanumeric and graphic modules with integrated controller)
– Compact Flash memory cards (or CF-I/O devices),
– printer ports and an almost infi nite number of other basic 8-bit
parallel devices available on the market and featuring the standard control signals: −CS, −RD, −WR.
Trang 11Synchronous communication using
the SPI modules
Trang 12• The SPI interface is essentially composed of
an 8-bit shift register: bits are
simultaneously shifted in (MSB fi rst) from the SDI line and shifted out from the SDO line in sync with the clock on pin SCK.
Trang 13SPI Programming
1 #define SPI_MASTER 0x0120 // select 8-bit master mode, CKE=1, CKP=0
2 #define SPI_ENABLE 0x8000 // enable SPI port, clear status
3 #define CSEE _RD12 // select line for Serial EEPROM
4 #define TCSEE _TRISD12 // tris control for CSEE pin
5 // 1 init the PIC24 SPI peripheral
6 TCSEE = 0; // make SSEE pin output
7 CSEE = 1; // de-select the Serial EEPROM (low power standby)
8 SPI2CON1 = SPI_MASTER; // select mode
9 SPI2STAT = SPI_ENABLE; // enable the peripheral
10 // send one byte of data and receive one back at the same time
11 int writeSPI2( int data)
12 {
13 SPI2BUF = data; // write to buffer for TX
14 while( !SPI2STATbits.SPIRBF); // wait for transfer to complete
15 return SPI2BUF; // read the received value
16 }//writeSPI2
Trang 1425LC256 Memory
1 // 25LC256 Serial EEPROM commands
2 #define SEE_WRSR 1 // write status register
3 #define SEE_WRITE 2 // write command
4 #define SEE_READ 3 // read command
5 #define SEE_WDI 4 // write disable
6 #define SEE_STAT 5 // read status register
7 #define SEE_WEN 6 // write enable
Trang 155 // I/O defi nitions
6 #define CSEE _RD12 // select line for Serial EEPROM
7 #define TCSEE _TRISD12 // tris control for CSEE pin
8 // peripheral confi gurations
9 #define SPI_MASTER 0x0120 // select 8-bit master mode, CKE=1, CKP=0
10 #define SPI_ENABLE 0x8000 // enable SPI port, clear status
11 // 25LC256 Serial EEPROM commands
12 #define SEE_WRSR 1 // write status register
13 #define SEE_WRITE 2 // write command
14 #define SEE_READ 3 // read command
15 #define SEE_WDI 4 // write disable
16 #define SEE_STAT 5 // read status register
17 #define SEE_WEN 6 // write enable
18 // send one byte of data and receive one back at the same time
19 int writeSPI2( int data)
20 {
21 SPI2BUF = data; // write to buffer for TX
22 while( !SPI2STATbits.SPIRBF); // wait for transfer to complete
23 return SPI2BUF; // read the received value
24 }//writeSPI2
Trang 161 main()
2 {
3 int i;
4 // 1 init the SPI peripheral
5 TCSEE = 0; // make SSEE pin output
6 CSEE = 1; // de-select the Serial EEPROM
7 SPI2CON1 = SPI_MASTER; // select mode
8 SPI2STAT = SPI_ENABLE; // enable the peripheral
9 // 2 Check the Serial EEPROM status
10.CSEE = 0; // select the Serial EEPROM
11.writeSPI2( SEE_STAT); // send a READ STATUS COMMAND
12.i = writeSPI2( 0); // send dummy, read data
13.CSEE = 1; // terminate command <-set brkpt here
14.} // main
Trang 17• // I/O defi nitions for PIC24 + Explorer16 demo board
• #define CSEE _RD12 // select line for Serial EEPROM
• #define TCSEE _TRISD12 // tris control for CSEE pin
• //
Trang 181 void InitNVM(void)
2 {
3 // init the SPI peripheral
4 TCSEE = 0; // make SSEE pin output
5 CSEE = 1; // de-select the Serial EEPROM
6 SPI2CON1 = SPI_MASTER; // select mode
7 SPI2STAT = SPI_ENABLE; // enable the peripheral
8 }//InitNVM
9 int writeSPI2( int data)
10 {// send one byte of data and receive one back at the same time
11 SPI2BUF = data; // write to buffer for TX
12 while( !SPI2STATbits.SPIRBF); // wait for transfer to complete
13 return SPI2BUF; // read the received value
14 }//WriteSPI2
15 int ReadSR( void)
16 {// Check the Serial EEPROM status register
17 int i;
18 CSEE = 0; // select the Serial EEPROM
19 WriteSPI2( SEE_STAT); // send a READ STATUS COMMAND
20 i = WriteSPI2( 0); // send/receive
21 CSEE = 1; // deselect to terminate command
22 return i;
23 } //ReadSR
Trang 19// increment current value
Nop(); // <-set brkpt here
Trang 20Post-flight briefing
In this lesson we have seen briefl y how to use the SPI peripheral module, in its simplest configuration, to gain access to a 25LC256 Serial EEPROM memory, one of the most common types of nonvolatile memory
peripherals used in embedded-control applications.
Trang 21Exercises
• Develop (circular) buffered versions of the
read and write functions.
• Enable the new SPI 16-bit mode to
accelerate basic read and write operation.
• Several functions in the library are
performing locking loops that could
reduce the overall application
performance By utilizing the SPI port
interrupts implement a non blocking
version of the library.
Trang 22What to read at home?
CHAPTER 8: Asynchronous communication (pg 104 – pg125)
– UART configuration
– Sending and receiving data
– Testing the serial communication routines
– Building a simple console library
– Testing a VT100 terminal
– Using the serial port as a debugging tool
– The matrix
Trang 2323
Trang 24Next: Glass bliss
• HD44780 controller compatibility
• The Parallel Master Port
• Configuring the PMP for LCD module
control
• A small library of functions to access an
LCD display
• Advanced LCD control