The PIC16F887 microcontroller has several independent serial communication modules and each of them can be configured to operate in several different modes, which make them irreplaceable in many situations. Remember what we advised you about the CCP modules as the same applies here. Don’t burden yourself with details on the operation of all of them, just select one and learn only what you really need.The USART is one of the oldest serial communication systems. The modern versions of this system are upgraded and called somewhat differently - EUSART.
' In this example, internal EUSART module is initialized and set to send back the ' message immediately after receiving it. Baud rate is set to 9600 bps. The pro 'gram uses UART library routines UART1_init(), UART1_Write_Text(), UART1_Data_Ready(), 'UART1_Write() and UART1_Read(). dim uart_rd as byte main: ANSEL,ANSELH = 0 ' Configure AN pins as digital CM1CON0.C1ON = 0 ' Disable comparators CM2CON0.C2ON = 0 UART1_Init(9600) ' Initialize UART module at 9600 bps Delay_ms(100) ' Wait for UART module to become stable UART1_Write_Text("Start") while 1 ' Endless loop if (UART1_Data_Ready() ' If data is received, uart_rd = UART1_Read() ' read it, end if UART1_Write(uart_rd) ' and send it back via UART wend ...
' In this example, PIC microcontroller (master) sends data byte to peripheral chip ' (slave) via SPI. Program uses SPI library functions SPI1_init() and SPI1_Write. dim Chip_Select as sbit at PORTC.RC0 ' Peripheral chip_select line is connected to RC0 dim Chip_Select_Direction as sbit at TRISC.0 ' TRISC0 bit defines RC0 pin to be input or output dim value as unsigned int ' Data to be sent (value) is of unsigned int type main: ANSEL, ANSELH = 0 ' All I/O pins are digital TRISB.0 = 1 TRISB.1 = 1 ' Configure RB0, RB1 pins as inputs Chip_Select = 0 ' Select peripheral chip Chip_Select_Direction = 0 ' Configure the CS# pin as an output SPI1_Init() ' Initialize SPI module SPI1_Write(value) ' Send value to peripheral chip ...
|0||0||0||0||SPI master mode, clock = Fosc/4|
|0||0||0||1||SPI master mode, clock = Fosc/16|
|0||0||1||0||SPI master mode, clock = Fosc/64|
|0||0||1||1||SPI master mode, clock = (output TMR)/2|
|0||1||0||0||SPI slave mode, SS pin control enabled|
|0||1||0||1||SPI slave mode, SS pin control disabled, SS can be used as I/O pin|
|0||1||1||0||I²C slave mode, 7-bit address used|
|0||1||1||1||I²C slave mode, 10-bit address used|
|1||0||0||0||I²C master mode, clock = Fosc / [4(SSPAD+1)]|
|1||0||0||1||Mask used in I²C slave mode|
|1||0||1||1||I²C controlled master mode|
|1||1||1||0||I²C slave mode, 7-bit address used,START and STOP bits enable interrupt|
|1||1||1||1||I²C slave mode, 10-bit address used,START and STOP bits enable interrupt|
' In this example, PIC MCU is connected to 24C02 EEPROM via SCL and SDA pins. The program ' sends one byte of data to the EEPROM address 2. Then, it reads data via I2C from EEPROM ' and sends it to PORTB in order to check if the data has been successfully written. main: ANSEL = ANSELH = PORTB = TRISB = 0 ' All pins are digital. PORTB pins are outputs. I2C1_Init(100000) ' Initialize I2C with desired clock I2C1_Start() ' I2C start signal I2C1_Wr(0xA2) ' Send byte via I2C (device address + W) I2C1_Wr(2) ' Send byte (address of EEPROM location) I2C1_Wr(0xF0) ' Send data to be written I2C1_Stop() ' I2C stop signal Delay_100ms() I2C1_Start() ' I2C start signal I2C1_Wr(0xA2) ' Send byte via I2C (device address + W) I2C1_Wr(2) ' Send byte (data address) I2C1_Repeated_Start() ' Issue I2C signal repeated start I2C1_Wr(0xA3) ' Send byte (device address + R) PORTB = I2C1_Rd(0u) ' Read the data (NO acknowledge) I2C1_Stop() ' I2C stop signal