Unit Vi
Unit Vi
Unit Vi
D7 D6 D5 D4 D3 D2 D1 D0
SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
SPEN: Serial Port Enable bit RX9: 9-Bit Receive Enable bit
SREN: Single Receive Enable bit CREN: Continuous Receive Enable bit
ADDEN: Address Detect Enable bit FERR: Framing Error bit
OERR: Overrun Error bit RX9D: 9th bit of Received Data
Transmission Block Diagram
Serial Transmission and associated SFRs
The information we want to transmit is loaded into register TXREG (8
bits size). In case the transmitted data is 9 bits long, the 9th bit is placed
TX9D.
At the same time, the information above is being loaded into the
register TSR, which is used as a temporary buffer before that
information is transmitted.
TXIF – is set then TXREG is empty/free and ready to be loaded with a new
information.
TXIE – is enabling the interrupt in the case TXREG is loaded/filled and
TXIF = 1.
SPBRG – sets the desired baud rate in the system.
TXEN – is enabling the SPBRG.
Reception Block Diagram
Baud rates supported by PIC 18
1200, 2400,4800,9600,19200,38400,57600,115200
The BRG is a dedicated, 8-bit or 16-bit generator that
supports both the Asynchronous and Synchronous modes of
the EUSART.
By default, the BRG operates in 8-bit mode; setting the
BRG16 bit (BAUDCON<3>) selects 16-bit mode.
The SPBRGH:SPBRG register pair controls the period of a
free-running timer.
In Asynchronous mode, bits, BRGH (TXSTA<2>) and
BRG16 (BAUDCON<3>), also control the baud rate.
Configuration bits BRG mode Baud rate formula
SYNC =0(Asynchronous mode)
BRG16 BRGH N=SPBRGH:SPBRG
0 0 8bit BR= FOSC/[64 (n + 1)]
0 1 8bit BR=FOSC/[16 (n + 1)]
1 0 16bit BR=FOSC/[16 (n + 1)]
1 1 16bit BR=FOSC/[4 (n + 1)]
➢ Examples
SFRs associated with Serial
communication
TXREG: Transfer register
8 bit register used for serial communication in PIC18
For a byte of data to be transferred via Tx pin, it must be
placed in TXREG.
The moment the byte is written into TXREG it is fetched
into TSR(transmit shift register)
The TSR frames the 8 bit data with the start and stop bits.
The 10 bit data is transferred serially via the Tx pin.
TSR is not accessible and is strictly for internal use.
RCREG: Receive register
When the bits are received serially via the Rx pin, the PIC18
de frames them by eliminating the stop bit and start bits.
The value for generation of baud rate is loaded in
SPBRGH:SPBRG SFRs.
Algorithm for serial transmission
1. Initialize RxD line as i/p and TxD line as o/p
2. Calculate the count value for desired baud rate and load
SPBRGH:SPBRG using one of the formulae from the table.
3. Load a value in TXSTA, indicating asynchronous mode and 8
bit data frame, baud rate speed (high/low)
4. Select BRG mode=16 bit by setting BRG16 in BAUDCON
SFR.
5. Enable serial port by setting SPEN bit in RCSTA
6. Load TXREG with byte to be transmitted.
7. Monitor TXIF flag in PIR1 register to make sure UART is
ready for next transmission.
8. Repeat step 6 until all bytes are transmitted.
Enable the reception by setting bit CREN bit in RCSTA
Flag bit, RCIF, will be set when reception is complete and an
interrupt will be generated if enable bit, RCIE, was set.
Read the 8-bit received data by reading the RCREG register.
If any error occurred, clear the error by clearing enable bit
CREN.
To check the received data either display it on LCD or on PC
by transmitting it through EUSART.
Serial Communication
Registers associated with the EUSART:
TXREG(Transfer register)
RCREG(Receive register)
BRG(Serial port Baud rate Generator)
TXSTA(Transmit Status and control register)
RCSTA(Receive status and control register)
BAUDCON (Baud Rate Control) [BRG16]
PIR1 (Peripheral Interrupt request 1) [Flags TXIF(4) & RCIF(5)]
PIE1(Peripheral Interrupt enable 1) [TXIE(4) & RCIE(5)]
“BITS” TO REMEMBER
SYNC
BRGH
TXEN
TXIF
SPEN
BRG16
SPBRGH: SPBRG
Serial Communication interfacing
diagram
ADC INTERFACING
ADC is used to convert analog signals into digital signal.
The physical quantities are all analog (continuous). These
physical quantities must be first converted into electrical
signals through transducers or sensors.
While interfacing a microcontroller with the real world
quantities such converters are quite essential.
Characteristics of ADC are:
Resolution
Conversion time
Reference voltage
10bit A/D module of PIC18
The Analog-to-Digital (A/D) Converter module 13 inputs for the
40/44-pin devices.
This module allows conversion of an analog input signal to a
corresponding 10-bit digital number.
The module has five registers:
SFR Function
A/D Result High Register To store the 10 bit result after conversion
(ADRESH)
A/D Result Low Register(ADRESL)
A/D Control Register 0 (ADCON0) .control the A/D module, SOC and EOC
A/D Control Register1 (ADCON1) configures the functions of the port pins
A/D Control Register 2 (ADCON2) configures the A/D clock source, programme acquisition time
and justification.
Pin
Signal
description
Pin No. Function
Channel 0 (AN0) 2 RA0/AN0
Channel 1 (AN1) 3 RA1
Channel 2 (AN2) 4 RA2
Channel 3 (AN3) 5 RA3
Channel 4 (AN4) 7 RA5
Channel 5 (AN5) 8 RE0
Channel 6 (AN6) 9 RE1
Channel 7 (AN7) 10 RE2
Channel 8 (AN8) 35 RB2
Channel 9 (AN9) 36 RB3
Channel 10 (AN10) 34 RB1
Channel 11(AN11) 37 RB4
Channel 12 (AN12) 33 RB0
ADCON0 (A/D CONTROL 0 )
D7 D6 D5 D4 D3 D2 D1 D0
-- -- CHS3 CHS2 CHS1 CHS0 GO/*DONE ADON
SMP: Sample bit BF: Buffer Full Status bit (Receive mode
SPI Master mode: only)
1 = Input data sampled at end of data output time 1 = Receive complete, SSPBUF is full
0 = Input data sampled at middle of data output time 0 = Receive not complete, SSPBUF is empty
CKE: SPI Clock Select bit
1 = Transmit occurs on transition from active to Idle
clock state
0 = Transmit occurs on transition from Idle to active
clock state
SSPCON1
D7 D6 D5 D4 D3 D2 D1 d0