Chapter 6 Final
Chapter 6 Final
MSSP Features
SPI connections:-
• SDO: This line is used to send data to a slave. It can be read as master-out salve-in
• SDI: This line is used to receive data from a slave. It is an input line for the master
and output line for a slave.
• SS: In case of a single master and multiple slave configuration, this wire is used to
select particular slave device.
• SPI bus interface connection is used in many devices like ADC, DAC, and EEPROM.
• The microcontroller acts as the master while the SPI device acts as a slave.
• The microcontroller generates the SCLK, which is fed to the SCLK pin of the SPI device.
• The SPI protocol uses SCLK to synchronize the transfer of information one bit at a time.
The most-significant bit (MSB) goes in first.
• During the transfer, the CE must stay HIGH.
• The information (address and data) is transferred between the microcontroller and the
SPI device in groups of 8 bits, where the address byte is followed immediately by the
data byte.
• To distinguish between the read and write, the D7 bit of the address is used
D7=1 for write,
D7=0 for read.
Steps for writing data to an SPI
device
• Single-byte write:-
• Make CE = 1 to begin writing.
• The 8-bit address is shifted in one bit at a time, with each edge of
SCLK.
• First 8 bits of the address are sent by microcontroller and then the
SPI device receives the data belonging to that address location.
• The 8-bit data is shifted in one bit at a time, with each edge of the
SCLK.
• Make CE = 0 to indicate the end of the write cycle.
Multibyte burst write
• In burst mode, the address of the first location is sent, followed by the data for that location.
• while CE = 1, consecutive bytes are written to consecutive memory locations. In this mode,
the SPI device internally increments the address location as long as CE is HIGH.
Steps to be followed:-
• Make CE = 1 to begin writing.
• The 8-bit address of the first location is provided and shifted in one bit at a time, with each
edge of SCLK.
• The 8-bit data for the first location is provided and shifted in one bit at a time, with each edge
of the SCLK.
• After that we simply provide consecutive bytes of data to be placed in consecutive memory
locations.
• Make CE = 0 to end writing.
Single-byte read from slave device
• Make CE = 1 to begin writing.
• The 8-bit address is shifted in one bit at a time, with each edge of SCLK.
• Make A 7 = 0 for the read operation.
• After all 8 bits of the address are sent in, the SPI device sends out data belonging
to that location.
• The 8-bit data is shifted out one bit at a time, with each edge of the SCLK.
• Make CE = 0 to indicate the end of the read cycle.
Multibyte burst read
• In burst mode, we provide the address of the first location only. After that while CE =
I, consecutive bytes are brought out from con- secutive memory locations. In this
mode, the SPI device internally increments the address location as long as CE is
HIGH.
Steps to be followed:-
• Make CE = 1 to begin reading.
• The 8-bit address of the first location is provided and shifted in one bit at a time, with
each edge of SCLK. A 7 = 0 for the read operation.
• The 8-bit data for the first location is shifted out one bit at a time, with each edge of
the SCLK.
• After that we simply keep getting consecutive bytes of data belonging to consecutive
memory locations.
• Make CE = 0 to end reading
I2C protocol
• The I2C (Inter-Integrated Circuit) protocol is a m ulti-master, multi-slave, packet-switched, single-
ended, serial communication bus widely used for short-distance communication.
• It operates over two wires: SDA (Serial Data) and SCL (Serial Clock). In PIC microcontrollers,
the MSSP (Master Synchronous Serial Port) module supports the I2C protocol, providing an
efficient way to interface with other I2C-compatible devices like sensors, EEPROMs, and
displays.
Key Features of I2C Protocol
• Two-wire Communication: Utilizes SDA and SCL lines.
• Multi-master Capability: Multiple master devices can control the bus.
• 7-bit and 10-bit Addressing
• Bidirectional Data Transfer: Allows both read and write operations.
• Clock Stretching: Slaves can hold the clock line low to delay the master.
• Start and Stop Conditions: Specific signals to indicate the beginning and end of communication.
• I2C makes use of SDA and SCL, which are 2 bi-directional open-drain
lines for data communication.
• Serial Data (SDA) – Data transfer takes place via this pin.
Serial Clock (SCL) – Clock signal is carried by this pin.
I2C Operations in PIC Microcontroller
I2C Initialization
• Before any I2C communication can begin, the MSSP module must be
initialized. This involves configuring the control registers and setting the I2C
clock frequency.
Start Condition
• The start condition is generated by making the SDA line low while the SCL
line remains high. This indicates the beginning of an I2C communication.
• Stop Condition
• The stop condition is generated by making the SDA line high while the SCL
line is high. This indicates the end of an I2C communication.
Repeated Start Condition
• A repeated start condition is similar to the start condition but occurs without
a preceding stop condition. It is used when the master needs to
communicate with another device without releasing the bus.
Write Data
• Data is written to the SSPBUF register, and the module handles the
transmission over the I2C bus. The master device generates clock pulses for
each bit of data.
Read Data
• Data is read from the SSPBUF register. The master device generates clock
pulses, and the slave device places data on the SDA line.
Stepper motor control using PIC
• Stepper motors A stepper motor is a widely used device that translates
electrical pulses into mechanical movement. In applications such as disk
drives, dot matrix printers, and robotics, the stepper motor is used for
position control.
• Normal Four-Step Sequence
Stepper Motor Step Angles:-
The step angle is the minimum degree of rotation associated with a
single step. Various motors have different step angles.
Half step sequence
Dc motor control using PWM
• The speed of the motor depends on three factors: (a) load, (b) voltage, and (c) current.
• For a given fixed load we can maintain a steady speed by using a method called pulse width
modulation (PWM).
• By changing the width of the pulse applied to the DC motor we can increase or decrease the
amount of power provided to the motor, thereby increasing or decreasing the motor speed.
• The voltage has a fixed amplitude, it has a variable duty cycle. That means the wider the
pulse, the higher the speed.
• PWM is so widely used in DC motor control. In such a process load the proper registers
with the values of the high and low portions of the desired pulse, and the rest is taken care of
by the microcontroller.
Write a C program to monitor the status of SW and perform
the following:
(a) If SW = 0,
The DC motor moves with 50% duty cycle pulse.
(b) If SW = 1,
The DC motor moves with 25% duty cycle pulse.
ADC devices
Steps in programming the AID converter:-
1. Turn on the ADC module of the PICI8 using "BSF ADCONO, ADON" instruction.
2. Make the selected ADC channel pin as an input pin.
3. Select voltage reference and AC input channels using ADCONO and ADCONI.
4. Select the conversion speed using registers ADCONO and ADCONI.
5. Wait for the required acquisition time.
6. Activate the start conversion (GO/DOWN) bit.
7. Wait for the conversion to be completed by polling the end-of-conversion
(GO/DOWN) bit.
8. After the End of conversion (GO/DOWN) bit has gone LOW, read the ADRESL and
ADRESH registers to get the digital data output.
9. Go back to step 5
• ADC features
• (a) It is a 10-bit ADC.
• (b) It can have 5 to 15 channels of analog input channels, depending
on the family member.
• In PIC18452/458, pins RAO--RA7 of PORTA are used for the 8 analog
channels.
• The converted output binary data is held by two special function
registers called ADRESL (AID Result Low) and ADRESH (AID Result
High).
• Because the ADRESH:ADRESL registers give us 16 bits and the ADC data out is
only 10 bits wide, 6 bits of the 16 are unused.
• The voltage source of the PICI8 chip itself, as the Vref or connecting it to an
external voltage source for the Vref.
• The conversion time is calculated by the F osc of crystal frequency.
• It allows the implementation of the differential Vref voltage using the Vref(+) and
Vref(-) pins, where Vref= Vref(+) - Vref(-).
• Above features can be programmed by way of ADCON-0 and
ADCON-1