EMBEDDED CONTROLLERS
Presentation Slides:
www.sathieshkumar.com/tutorials
2
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Presentation Slides:
www.sathieshkumar.com/tutorials
3
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
4
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Key Requirements:
1. Energy efficiency- low power, long battery
Human life.
2. High code density- less requirement of
Interface program memory.
Actuators
Processing Sensors
Unit
RTOS
5
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
6
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
7
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
8
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
10
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
During the 1024 oscillations the device is held in reset, the program counter
does not increment and program execution is suspended.
During this OST delay of 1024 oscillations, the system clock can be supplied
by the Internal Oscillator.
11
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
When a PIC MCU is first powered up, it will run through some defined
hardware functions internally to prepare the MCU for proper operation.
This can include the oscillator start-up and power stabilization.
The Power On Reset will hold the device in reset while these operations
happen.
It prevents the device from running any software until a minimum level Vdd
voltage threshold is met and the oscillator is stable.
POR also handles some internal settings for the hardware to operate properly.
12
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
BOR will hold a PIC MCU in reset when the Vdd drops below a brown out
threshold voltage.
Not all devices have BOR, but most do, and some have multiple voltage
thresholds to select from.
Between a BOR and Power On Reset the whole range of startup voltages can
be covered to protect for proper operation after a power drop at the Vdd line.
It is also recommended that the Power-Up Timer (PWRT) be enabled to
increase the delay in returning from a BOR event.
13
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
14
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
15
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
16
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
17
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Partitioned into multiple banks (Each bank is 128 bytes- Total of 512 bytes).
Data memory has 368 bytes of RAM and special function registers (SFR).
Bits RP1 (STATUS<6>) and RP0 (STATUS<5>) are used to select the banks.
STATUS register contains the arithmetic status of the ALU, the RESET status and the bank
select bits for data memory.
IRP: Register bank select bit (1: Bank 2,3 (100-1FF), 0: Bank 0,1 (00-FF))
18
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
TO: Time-out bit (0: A WDT time-out occurred, 1: After power-up, CLRWDT instruction or
SLEEP instruction)
CLRWDT: Clear Watchdog Timer (Syntax: CLRWDT)
SLEEP: Go into standby mode (Conserves power)- (Syntax: SLEEP)
PD: Power-down bit (0: By execution of the SLEEP instruction, 1: After power-up or by the
CLRWDT instruction)
Z: Zero bit (1: Result=0, 0:Result0)
DC: Digit carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)- 1: Carry out
from lower nibble (For borrow, the polarity is reversed)
C: Carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)- 1: Carry out from MSB
of the result (For borrow, the polarity is reversed)
19
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
20
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The Special function registers (SFR) are registers used by the CPU and peripheral modules
for controlling the desired operation of the device.
SFRs are implemented as static RAM.
Lower locations of each bank are reserved for Special function registers (SFR).
All General purpose registers (GPR), implemented as static RAM.
Few SFRs may be mirrored in another bank for code reduction and quicker access.
The register file can be accessed either directly or indirectly through the File select register
(FSR).
FSR is indirect data memory address pointer.
21
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
22
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
13-bits wide.
Low byte (PC<7:0>) comes from the PCL register, which is readable and writable.
The upper bits (PC<12:8>) are not readable, but are indirectly writable through the PCLATH
register.
PCLATH register is the write buffer for the upper 5-bits of the program counter.
On any RESET, the upper bits of the PC will be cleared.
CALL or GOTO instruction allows branching within any 2 K program memory page.
When doing a CALL or GOTO instruction, the user must ensure that the page select bits are
programmed so that the desired program memory page is addressed.
23
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Page 0/1/2/3
24
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
25
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
26
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
27
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
28
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
BOR- Brown-Out Reset (Voltage falls below 4 V for more than 100 s)
POR- Power-on Reset
29
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
PORT Pins:RB3:RB0
32
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
33
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
PORT Pins:RC4:RC3
34
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
35
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
36
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
37
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
38
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
39
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
40
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
42
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
43
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
44
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Compiler: Produces hex file which can be downloaded into the ROM of
Microcontroller.
i) PIC Microcontrollers- MikroC, MPLAB, CCS Compilers.
ii) ATMEL- Keil, MikroC, RIDE, Code vision AVR.
2. Burner: Burn the hex file in Microcontroller.
i) PIC:PICFLASH, Micropro Burn, Elnec.
ii) ATMEL- Proload, Keil, Code vision AVR.
3. Simulator: Software execution of codes
i) Proteus Lite.
ii) Real PIC Simulator.
4. Language:
i) Assembly ii) C iii)PASCAL iv) JAVA
45
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
47
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
void main()
{
TRISD=0X00;
while(1)
{
PORTD=0XAA;
delay_ms(1000);
PORTD=0X55;
delay_ms(1000);
}
}
48
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
49
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
50
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
To display letters and numbers, we send ASCII codes for the letters A-Z, a-z
and numbers 0-9 to these pins while making RS=1.
There are two ways to send characters (command/data):
1. use a delay before sending the next one
2. use the busy flag (D7 bit of command register, R/W=1, E=L to H pulse) to
see if the LCD is ready for the next one.
Code(Hex) Function Code(Hex) Function
53
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
54
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
55
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
56
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
57
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
58
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
void main(){
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,3,txt1); // Write text in first row
Lcd_Out(2,2,txt2); // Write text in second row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,3,"Embedded C"); // Write text in first row
Lcd_Out(2,2,"LCD Interface"); // Write text in second row
Delay_ms(2000);
61
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
A direct current (DC) motor is another widely used device that translates
electrical pulses into mechanical movement.
Polarity on the leads of DC motor determines the direction of rotation. Eg:
CPU Fan.
DC motor moves continuously.
Speed of the motor depends on three factors: a) load, b) voltage rating and c)
current rating.
DC motor has two rpm: no load and loaded. Manufacturer datasheet gives
the no-load rpm (few thousands to tens of thousands).
The current rating is the current consumption when the nominal voltage is
applied with no load, and can be from 25 mA to few amps.
Increase in voltage (1 to 150 V) will result in increase in rpm.
Pulse width Modulation (PWM): Wider the pulse, higher the speed. 62
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
As the load increases, the rpm is decreased, unless the current or voltage
provided to the motor is increased, which in turn increases the torque.
With a fixed voltage, as the load increases, the current (power) consumption
of a DC motor is increased.
If we overload the motor it will stall, and that can damage the motor due to
the heat generated by high current consumption.
63
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
With the help of relays or some specially designed chips we can change the
direction of the DC motor rotation.
H-Bridge control of DC motors.
64
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
When switches 1 and 4 are closed, current is allowed to pass through the
motor.
65
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
When switches 2 and 3 are closed, current is allowed to pass through the
motor in opposite direction.
66
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
67
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
68
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
69
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
70
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The speed of the motor depends on three factors: (a) Load, (b) Voltage and
(c) Current.
For a given load, steady speed can be maintained by 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.
72
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
73
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
74
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
void ServoActivate()
{
unsigned int i;
for(i=0;i<20;i++)
{
PORTA.F0=1;
delay_us(600); //Servo at -90 deg Operation Speed 200 ms @ 4.8 V
PORTA.F0=0;
delay_us(20000);
}
delay_ms(1000);
for(i=0;i<20;i++)
{
PORTA.F0=1;
delay_us(1500); //Servo at 0 deg
PORTA.F0=0;
delay_us(20000);
}
delay_ms(1000);
}
75
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
76
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
77
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
78
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
79
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
Step angle is determined by the internal construction of the motor, in particular the
number of teeth on the stator and the rotor.
The step angle is the minimum degree of rotation associated with a single step.
Steps per revolution is the total number of steps needed to rotate one complete
rotation or 360 degrees.
In a 4-step switching sequence, after four steps the same two windings will be ON.
How much movement is associated with these four steps?
After completing every four steps, the rotor moves only one tooth pitch.
Therefore, in a stepper motor with 200 steps per revolution, the rotor has 50 teeth
because 4x50=200 steps are needed to complete one revolution.
Minimum step angle is always a function of the number of teeth on the rotor.
In other words, the smaller the step angle, the more teeth the rotor passes.
Motor Speed: Measured in steps per second (steps/s), is a function of switching rate
(By changing the length of the time delay loop, we can achieve various rotation speeds).
Holding torque: With the motor shaft at standstill or zero rpm condition, the amount
of torque, from an external source, required to break away the shaft from its holding
position. This is measured with rated voltage and current applied to the motor. The unit
of torque is ounce-inch or kg-cm.
81
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Three common types of stepper motor interfacing: universal, unipolar and bipolar.
They can be identified by the number of connections to the motor.
A universal stepper motor has eight, while the unipolar has six and the bipolar has
four.
The universal stepper motor can be configured for all three modes, while the unipolar
can be either unipolar or bipolar. Obviously the bipolar cannot be configured for
universal nor unipolar mode.
82
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
83
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
85
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
86
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
87
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
88
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
void main()
{
ADCON1=0x87; // Configure PORTA as digital port
TRISA=0x01; //Switch is connected to PORTA/Pin0
TRISC=0x00; //Relay connected to PORTC/Pin0
while (1)
{
if (PORTA.F0==1) //if switch is pressed- Logic Low
{
PORTC.F0=0; //Switch the lamp on
delay_ms(2000);
}
else
{
PORTC.F0=1;
delay_ms(2000);
}
}
}
89
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
8 channels (AN0-AN7).
Successive approximation ADC.
Result is a 10-bit digital number.
Reference voltage input is software selectable to some combination of VDD, VSS,
RA2 or RA3.
The A/D convertor has a unique feature of being able to operate while the device is in
SLEEP mode.
To operate in SLEEP mode, the A/D clock must be derived from the A/Ds internal RC
oscillator.
The A/D module has four registers. These registers are:
1. A/D result high register (ADRESH)
2. A/D result low register (ADRESL)
3. A/D control register 0 (ADCON0)
4. A/D control register 1 (ADCON1)
90
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
91
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
92
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
93
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
94
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The ADRESH:ADRESL registers contain the 10-bit result of the A/D conversion.
When the A/D conversion is complete, the result is loaded into this A/D result register
pair, the GO/DONE bit (ADCON0<2>) is cleared and the A/D interrupt flag bit ADIF is
set.
These steps should be followed for doing an A/D Conversion:
1. Configure the A/D module:
Configure analog pins/voltage reference and digital I/O (ADCON1)
Select A/D input channel (ADCON0)
Select A/D conversion clock (ADCON0)
Turn on A/D module (ADCON0)
2. Configure A/D interrupt (if desired):
Clear ADIF bit
Set ADIE bit
Set PEIE & GIE bit
95
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
96
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
97
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
98
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The A/D conversion time per bit is defined as TAD (minimum of 1.6 s).
The A/D conversion requires a minimum 12TAD per 10-bit conversion.
The source of the A/D conversion clock is software selected.
The four possible options for TAD are:
1. 2TOSC
2. 8TOSC
3. 32TOSC
4. Internal A/D module RC oscillator (2-6 s)
99
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
100
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
101
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
{
temp_res = ADC_Read(1); // Read sensor output from channel 1
mV=temp_res*4.88; // ADC-1024 steps, Resolution is 4.88 mV (5000 mV/1024)
LongToStr(mV,op); // Convert data to string
}
Linear: 10 mV/ C
Measurement Range: -55 to +150 C
102
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
103
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
104
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
105
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
106
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
107
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
void main() {
void interrupt() TRISC=0x00;
{ TRISB=0x01;
if(INTCON.INTF==1) OPTION_REG.INTEDG=1;
{ INTCON=0x90;
INTCON=0x00; PORTB=0x00;
PORTC=0x55; PORTC=0x00;
delay_ms(1000); while(1)
PORTC=0xAA; { PORTC=0xFF;
delay_ms(1000); delay_ms(1000);
PORTC=0x00; PORTC=0x00;
INTCON=0x90; delay_ms(1000);
} }
} }
108
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
8-bit timer/counter
Readable and writable
8-bit software programmable prescalar
Every timer needs a clock pulse to tick
Internal or external clock select
If the clock source is internal, then 1/4th of the frequency of the crystal oscillator on the
OSC1 and OSC2 pins is fed into the timer.
By choosing the external clock option, we feed pulses through one of the PIC
microcontroller pins; this is called a counter.
Interrupt on overflow from FFh to 00h
Edge select for external clock
109
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Block diagram of the Timer0 module and the prescalar shared with the Watchdog
Timer (WDT).
110
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
111
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The prescaler is mutually exclusively shared between the Timer0 module and the
Watchdog Timer.
The prescaler is not readable or writable.
112
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
113
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h.
This overflow sets bit T0IF (INTCON<2>).
The interrupt can be masked by clearing bit T0IE (INTCON<5>).
Bit T0IF must be cleared in software by the Timer0 module Interrupt Service Routine
before re-enabling this interrupt.
The TMR0 interrupt cannot awaken the processor from SLEEP, since the timer is shut-
off during SLEEP.
114
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
115
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Prescalar
There is only one prescaler available, which is mutually exclusively shared between
the Timer0 module and the Watchdog Timer.
A prescaler assignment for the Timer0 module means that there is no prescaler for the
Watchdog Timer, and vice-versa. This prescaler is not readable or writable.
The PSA and PS2:PS0 bits (OPTION_REG<3:0>) determine the prescaler assignment
and pre-scale ratio.
When assigned to the Timer0 module, all instructions writing to the TMR0 register (e.g.
CLRF 1- Clear f register, MOVWF f- Move W to f, BSF f,b- Bit set f(b)....etc.) will clear
the prescaler.
When assigned to WDT, a CLRWDT instruction will clear the prescaler along with the
Watchdog Timer. The prescaler is not readable or writable.
116
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
117
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H
and TMR1L), which are readable and writable.
The TMR1 Register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls
over to 0000h.
The TMR1 Interrupt, if enabled, is generated on overflow, which is latched in interrupt
flag bit TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing
TMR1 interrupt enable bit TMR1IE (PIE1<0>).
Timer1 can operate in one of two modes:
1. As a timer
2. As a counter
118
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
119
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
120
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
121
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
122
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
123
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
124
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
125
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
126
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
127
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
129
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
130
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
131
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
132
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
133
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
134
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
135
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
136
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Capture mode, CCPR1H:CCPR1L captures the 16-bit value of the TMR1 register
when an event occurs on pin RC2/CCP1.
An event is defined as one of the following:
Every falling edge
Every rising edge
Every 4th rising edge
Every 16th rising edge
The type of event is configured by control bits CCP1M3:CCP1M0 (CCPxCON<3:0>).
When a capture is made, the interrupt request flag bit CCP1IF (PIR1<2>) is set.
The interrupt flag must be cleared in software.
If another capture occurs before the value in register CCPR1 is read, the old captured
value is overwritten by the new value.
137
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Capture mode, the RC2/CCP1 pin should be configured as an input by setting the
TRISC<2> bit.
CAPTURE MODE OPERATION BLOCK DIAGRAM
138
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
Timer1 must be running in Timer mode, or Synchronized Counter mode, for the CCP
module to use the capture feature.
In Asynchronous Counter mode, the capture operation may not work.
SOFTWARE INTERRUPT:
When the Capture mode is changed, a false capture interrupt may be generated.
The user should keep bit CCP1IE (PIE1<2>) clear to avoid false interrupts and should
clear the flag bit CCP1IF, following any such change in operating mode.
CCP PRESCALAR:
There are four prescalar settings, specified by bits CCP1M3:CCP1M0.
Whenever the CCP module is turned off, or the CCP module is not in Capture mode,
the prescalar counter is cleared.
Any RESET will clear the prescalar counter.
139
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Compare mode, the 16-bit CCPR1 register value is constantly compared against
the TMR1 register pair value.
When a match occurs, the RC2/CCP1 pin is:
Driven high
Driven low
Remains unchanged
The action on the pin is based on the value of control bits CCP1M3:CCP1M0
(CCP1CON<3:0>).
At the same time, interrupt flag bit CCP1IF is set.
The user must configure the RC2/CCP1 pin as an output by clearing the TRISC<2>
bit.
140
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
141
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Pulse Width Modulation mode, the CCPx pin produces up to a 10-bit resolution
PWM output.
Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must
be cleared to make the CCP1 pin an output.
Clearing the CCP1CON register will force the CCP1 PWM output latch to the default
low level. This is not the PORTC I/O data latch.
A PWM output has a time-base (period) and a time that the output stays high (duty
cycle). The frequency of the PWM is the inverse of the period (1/period).
143
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
144
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
PWM PERIOD
The PWM period is specified by writing to the PR2 register.
The PWM period can be calculated using the following formula:
PWM period = [(PR2) + 1] 4 TOSC (TMR2 prescale value)
PWM frequency is defined as 1 / [PWM period].
When TMR2 is equal to PR2, the following three events occur on the next increment
cycle:
1. TMR2 is cleared
2. The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be
set)
3. The PWM duty cycle is latched from CCPR1L into CCPR1H
145
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
146
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
FOSC
log( )
FPWM
Resolution= bits
log(2)
If the PWM duty cycle value is longer than the PWM period, the CCP1 pin will not be
cleared.
147
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
148
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
149
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
150
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
151
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
As can be seen from the general format of the instructions, the opcode portion of the
instruction word varies from 3-bits to 6-bits of information. This is what allows the
midrange instruction set to have 35 instructions.
152
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
153
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
154
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
155
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
156
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
157
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
158
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
159
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
160
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
161
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
162
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
163
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
164
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
165
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
166
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
167
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
168
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
169
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
170
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
171
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
172
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
173
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
174
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
175
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
176
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
177
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
178
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
179
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
180
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
181
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
182
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
183
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
184
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
185
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
186
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
187
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
188
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
189
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
190
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
191
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
192
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
193
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
194
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
195
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
196
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
197
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
198
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Create a square wave of 50% duty cycle on the PORTB.5 bit using Timer1.
BCF TRISB,5 //PB5 as an output
MOVLW 0xC0
MOVWF INTCON //Enable Interrupt
MOVLW 0x08 //No prescalar, Internal clock, OFF
MOVWF T1CON //Load T1CON reg
Here: MOVLW 0xFF
MOVWF TMR1H //TMR1H=0xFF
MOVLW 0xF2
MOVWF TMR1L //TMR1L=0xF2
BCF PIR1, TMR1IF //Clear timer interrupt flag bit
BTG PORTB,5 //Toggle PB5
BSF T1CON, TMR1ON //Start Timer1
199
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
200
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Calculate the amount of time delay generated by the timer. Assume that XTAL=10
MHz.
Solution:
Timer works with Fosc/4 clock [10 MHz/4=2.5 MHz]
Bit period T=1/f=1/2.5 MHz=0.4 us
The number of counts for the rollover is FFFFH-FFF2H=0DH(13)
Therefore, (13+1)x0.4 us=5.6 us for the half pulse. For the entire period
the time delay generated by the timer is T=11.2 us.
Note: 1 is added with rollover, because of the extra clock needed when it rolls over
from FFFF to 0.
201
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Toggle all the bits of PORTB. PORTC and PORTD every of a Second. Assume
that XTAL=4 MHz.
Solution:
Fosc/4 clock [4 MHz/4=1 MHz]
Bit period T=1/f=1/1 MHz=1 us
R1 equ 0x07
R2 equ 0x08
ORG 0
CLRF TRISB // PORTB an output port
CLRF TRISC //PORTC an output port
CLRF TRISD // PORTD an output port
MOVLW 0x55
202
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
MOVWF PORTB
MOVWF PORTC
MOVWF PORTD
L3: COMP PORTB,F // Toggle bits of PORTB
COMP PORTC,F
COMP PORTD,F
CALL QDELAY
BRA L3
203
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
QDELAY
MOVLW D200
MOVWF R1
D1: MOVLW D250
MOVWF R2
D2: NOP
NOP
DECF R2,F
BNZ D2
DECF R1,F
BNZ D1
RETURN
END
204
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
205
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
low cost.
easy to implement.
moderate speed (up to 100 KB/s for the standard bus and up to 400 KB/s for the
extended bus).
Two wire communication: Serial data line (SDL) for data and Serial clock line (SCL)
to indicate when valid data are on the data line.
208
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
209
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Other nodes may act as slaves that only respond to requests from masters.
210
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
No definite voltage levels is specified for logic levels high and low.
A pull-up resistor keeps the default state of the signal high, and the transistors are
used in each bus device to pull down the signal when a 0 is to be transmitted.
Open collector/open drain signaling allows several devices to simultaneously write the
bus without causing electrical damage.
The master is responsible for generating the SCL clock, but the slave can stretch the
low period of the clock (but not the high period) if necessary.
The I2C bus is designed as a multimaster- any one of several different devices may
act as the master at various times. As a result, there is no global master to generate the
clock signal on SCL. Master drives both SCL and SDL when it is sending data.
When the bus is idle, both SCL and SDL remain high.
211
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
When two devices try to drive either SCL or SDL to different values, the open
collector/open drain circuitry prevents errors, but each master device must listen to the
bus while transmitting to be sure that it is not interfering with another message.
The addresses are determined by system designer, usually as part of the program for
the I2C driver.
A device address is 7 bits in the standard I2C definition (the extended I2C allows 10-bit
addresses).
The addresses 0000000 is used to signal a generate call or bus broadcast, which can
be used to signal all devices simultaneously.
The address 11110XX is reserved for the extended 10-bit addressing scheme.
When a master wants to write a slave, it transmits the slaves address followed by the
data.
Since a slave cannot initiate a transfer, the master must send a read request with the
slaves address and let the slave transmit the data.
Therefore, an address transmission includes the 7-bit address and 1 bit for data
direction: 0 for writing from the master to the slave and 1 for reading from the slave to
the master.
213
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
A bus transaction is initiated by a start signal and completed with an end signal as
follows:
1. A start is signaled by leaving the SCL high and sending a 1 to 0 transition on SDL.
2. A stop is signaled by setting the SCL high and sending a 0 to 1 transition on SDL.
214
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
How a data byte is transmitted on the bus, including start and stop events.
215
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
After the start condition, the clock line is pulled low to initiate the data transfer.
At each bit, the clock line goes high while the data line assumes its proper value of 0
or 1.
For acknowledgment, the transmitter does not pull down the SDL, allowing the
receiver to set the SDL to 0 if it properly received the byte.
After acknowledgment, the SDL goes from low to high while the SCL is high, signaling
the stop condition.
When sending, devices listen to the bus as well. If a device is trying to send a logic 1
but hears a logic 0, it immediately stops transmitting and gives the other sender priority.
216
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The application code calls routines to send an address, send a data byte and so on.
The I2C device takes care of generating the clock and data.
217
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
218
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
while (1)
{
if (UART1_Data_Ready()) // If data is received,
{
uart_rd = UART1_Read(); // read the received data,
UART1_Write(uart_rd); // and send data via UART
}
}
219
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The USART can be configured as a full duplex asynchronous system that can
communicate with peripheral devices such as CRT terminals and personal computers, or
it can be configured as a half duplex synchronous system that can communicate with
peripheral devices such as A/D or D/A integrated circuits, serial EEPROMs etc.
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be set in order to configure pins
RC6/TX/CK and RC7/RX/DT as the Universal Synchronous Asynchronous Receiver
Transmitter.
220
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The USART module also has a multi-processor communication capability using 9-bit
address detection.
221
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
222
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
223
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
224
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The BRG supports both the Asynchronous and Synchronous modes of the USART.
The SPBRG register controls the period of a free running 8-bit timer.
In Asynchronous mode, bit BRGH (TXSTA<2>) also controls the baud rate.
Table 10-1 shows the formula for computation of the baud rate for different USART
modes which only apply in Master mode (internal clock).
Given the desired baud rate and FOSC, the nearest integer value for the SPBRG
register can be calculated using the formula in Table 10-1.
225
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
It may be advantageous to use the high baud rate (BRGH = 1), even for slower baud
clocks. This is because the FOSC/(16(X + 1)) equation can reduce the baud rate error in
some cases.
Writing a new value to the SPBRG register causes the BRG timer to be reset (or
cleared). This ensures the BRG does not wait for a timer overflow before outputting the
new baud rate.
The data on the RC7/RX/DT pin is sampled three times by a majority detect circuit to
determine if a high or a low level is present at the RX pin.
226
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
227
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
228
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
229
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In this mode, the USART uses standard non-return-to zero (NRZ) format (one START
bit, eight or nine data bits, and one STOP bit).
An on-chip, dedicated, 8-bit baud rate generator can be used to derive standard baud
rate frequencies from the oscillator.
The transmitter and receiver are functionally independent, but use the same data
format and baud rate.
The baud rate generator produces a clock, either x16 or x64 of the bit shift rate,
depending on bit BRGH (TXSTA<2>).
Parity is not supported by the hardware, but can be implemented in software (and
stored as the ninth data bit). Asynchronous mode is stopped during SLEEP.
2. Sampling Circuit
3. Asynchronous Transmitter
4. Asynchronous Receiver
231
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
232
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The heart of the transmitter is the transmit (serial) shift register (TSR).
The shift register obtains its data from the read/write transmit buffer, TXREG.
The TXREG register is loaded with data in software. The TSR register is not loaded
until the STOP bit has been transmitted from the previous load.
As soon as the STOP bit is transmitted, the TSR is loaded with new data from the
TXREG register (if available).
Once the TXREG register transfers the data to the TSR register (occurs in one TCY),
the TXREG register is empty and flag bit TXIF (PIR1<4>) is set.
Flag bit TXIF will be set, regardless of the state of enable bit TXIE and cannot be
cleared in software. It will reset only when new data is loaded into the TXREG register.
While flag bit TXIF indicates the status of the TXREG register, another bit TRMT
(TXSTA<1>) shows the status of the TSR register.
233
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
Status bit TRMT is a read only bit, which is set when the TSR register is empty.
No interrupt logic is tied to this bit, so the user has to poll this bit in order to determine
if the TSR register is empty.
The transmission can also be started by first loading the TXREG register and then
setting enable bit TXEN.
Normally, when transmission is first started, the TSR register is empty. At that point,
transfer to the TXREG register will result in an immediate transfer to TSR, resulting in an
empty TXREG. A back-to-back transfer is thus possible.
Clearing enable bit TXEN during a transmission will cause the transmission to be
aborted and will reset the transmitter. As a result, the RC6/TX/CK pin will revert to hi-
impedance. 234
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In order to select 9-bit transmission, transmit bit TX9(TXSTA<6>) should be set and
the ninth bit should be written to TX9D (TXSTA<0>).
The ninth bit must be written before writing the 8-bit data to the TXREG register. This
is because a data write to the TXREG register can result in an immediate transfer of the
data to the TSR register (if the TSR is empty).
In such a case, an incorrect ninth data bit may be loaded in the TSR register.
235
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
236
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
237
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud rate
is desired, set bit BRGH.
2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.
5. Enable the transmission by setting bit TXEN, which will also set bit TXIF.
6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.
8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are
set.
238
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
239
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
240
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The data is received on the RC7/RX/DT pin and drives the data recovery block.
The data recovery block is actually a high speed shifter, operating at x16 times the
baud rate; whereas, the main receive serial shifter operates at the bit rate or at FOSC.
The heart of the receiver is the receive (serial) shift register (RSR).
After sampling the STOP bit, the received data in the RSR is transferred to the
RCREG register (if it is empty).
If the transfer is complete, flag bit RCIF (PIR1<5>) is set. The actual interrupt can be
enabled/disabled by setting/clearing enable bit RCIE (PIE1<5>).
Flag bit RCIF is a read only bit, which is cleared by the hardware. It is cleared when
the RCREG register has been read and is empty.
It is possible for two bytes of data to be received and transferred to the RCREG FIFO
and a third byte to begin shifting to the RSR register.
On the detection of the STOP bit of the third byte, if the RCREG register is still full, the
overrun error bit OERR (RCSTA<1>) will be set. The word in the RSR will be lost.
The RCREG register can be read twice to retrieve the two bytes in the FIFO.
Overrun bit OERR has to be cleared in software. This is done by resetting the receive
logic (CREN is cleared and then set).
If bit OERR is set, transfers from the RSR register to the RCREG register are
inhibited, and no further data will be received. It is therefore, essential to clear error bit
OERR if it is set.
Framing error bit FERR (RCSTA<2>) is set if a STOP bit is detected as clear.
Bit FERR and the 9th receive bit are buffered the same way as the receive data.
242
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Reading the RCREG will load bits RX9D and FERR with new values, therefore, it is
essential for the user to read the RCSTA register before reading the RCREG register in
243
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud rate
is desired, set bit BRGH.
2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.
6. Flag bit RCIF will be set when reception is complete and an interrupt will be generated
if enable bit RCIE is set.
7. Read the RCSTA register to get the ninth bit (if enabled) and determine if any error
occurred during reception.
244
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
9. If any error occurred, clear the error by clearing enable bit CREN.
10. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register
are set.
245
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
246
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Initialize the SPBRG register for the appropriate baud rate. If a high speed baud rate
is desired, set bit BRGH.
2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.
247
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
7. Flag bit RCIF will be set when reception is complete, and an interrupt will be
generated if enable bit RCIE was set.
8. Read the RCSTA register to get the ninth bit and determine if any error occurred
during reception.
9. Read the 8-bit received data by reading the RCREG register, to determine if the
device is being addressed.
10. If any error occurred, clear the error by clearing enable bit CREN.
11. If the device has been addressed, clear the ADDEN bit to allow data bytes and
address bytes to be read into the receive buffer, and interrupt the CPU.
248
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
249
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
250
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
251
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In addition, enable bit SPEN (RCSTA<7>) is set in order to configure the RC6/TX/CK
and RC7/RX/DT I/O pins to CK (clock) and DT (data) lines, respectively.
The Master mode indicates that the processor transmits the master clock on the CK
line.
252
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The heart of the transmitter is the transmit (serial) shift register (TSR).
The shift register obtains its data from the read/write transmit buffer register TXREG.
The TSR register is not loaded until the last bit has been transmitted from the previous
load.
As soon as the last bit is transmitted, the TSR is loaded with new data from the
TXREG (if available).
Once the TXREG register transfers the data to the TSR register (occurs in one Tcycle),
the TXREG is empty and interrupt bit TXIF (PIR1<4>) is set.
Flag bit TXIF will be set, regardless of the state of enable bit TXIE and cannot be
cleared in software. It will reset only when new data is loaded into the TXREG register.
253
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
While flag bit TXIF indicates the status of the TXREG register, another bit TRMT
(TXSTA<1>) shows the status of the TSR register.
TRMT is a read only bit which is set when the TSR is empty.
No interrupt logic is tied to this bit, so the user has to poll this bit in order to determine
if the TSR register is empty.
254
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
255
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The actual transmission will not occur until the TXREG register has been loaded with
data.
The first data bit will be shifted out on the next available rising edge of the clock on the
CK line.
Data out is stable around the falling edge of the synchronous clock.
The transmission can also be started by first loading the TXREG register and then
setting bit TXEN.
This is advantageous when slow baud rates are selected, since the BRG is kept in
RESET when bits TXEN, CREN and SREN are clear.
256
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
257
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
2. Enable the synchronous master serial port by setting bits SYNC, SPEN and CSRC.
6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.
8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are
set.
258
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
259
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Once synchronous mode is selected, reception is enabled by setting either enable bit
SREN (RCSTA<5>), or enable bit CREN (RCSTA<4>).
Data is sampled on the RC7/RX/DT pin on the falling edge of the clock.
If enable bit CREN is set, the reception is continuous until CREN is cleared.
After clocking the last bit, the received data in the Receive Shift Register (RSR) is
transferred to the RCREG register (if it is empty).
When the transfer is complete, interrupt flag bit RCIF (PIR1<5>) is set.
Flag bit RCIF is a read only bit, which is reset by the hardware.
260
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In this case, it is reset when the RCREG register has been read and is empty.
It is possible for two bytes of data to be received and transferred to the RCREG FIFO
and a third byte to begin shifting into the RSR register.
On the clocking of the last bit of the third byte, if the RCREG register is still full, then
overrun error bit OERR (RCSTA<1>) is set.
The RCREG register can be read twice to retrieve the two bytes in the FIFO.
If bit OERR is set, transfers from the RSR to the RCREG are inhibited, so it is
essential to clear bit OERR if it is set.
261
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The ninth receive bit is buffered the same way as the receive data.
Reading the RCREG register will load bit RX9D with a new value, therefore, it is
essential for the user to read the RCSTA register before reading RCREG in order not to
lose the old RX9D information.
262
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
2. Enable the synchronous master serial port by setting bits SYNC, SPEN and CSRC.
6. If a single reception is required, set bit SREN. For continuous reception, set bit CREN.
7. Interrupt flag bit RCIF will be set when reception is complete and an interrupt will be
generated if enable bit RCIE was set.
263
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
8. Read the RCSTA register to get the ninth bit (if enabled) and determine if any error
occurred during reception.
10. If any error occurred, clear the error by clearing bit CREN.
11. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register
are set.
264
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
265
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
266
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
Synchronous Slave mode differs from the Master mode in the fact that the shift clock is
supplied externally at the RC6/TX/CK pin (instead of being supplied internally in Master
mode).
This allows the device to transfer or receive data while in SLEEP mode.
267
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The operation of the Synchronous Master and Slave modes is identical, except in the
case of the SLEEP mode.
If two words are written to the TXREG and then the SLEEP instruction is executed, the
following will occur:
a) The first word will immediately transfer to the TSR register and transmit.
d) When the first word has been shifted out of TSR, the TXREG register will transfer the
second word to the TSR and flag bit TXIF will now be set.
e) If enable bit TXIE is set, the interrupt will wake the chip from SLEEP and if the global
interrupt is enabled, the program will branch to the interrupt vector (0004h).
268
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Enable the synchronous slave serial port by setting bits SYNC and SPEN and
clearing bit CSRC.
6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.
8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are
set.
269
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
270
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The operation of the Synchronous Master and Slave modes is identical, except in the
case of the SLEEP mode.
If receive is enabled by setting bit CREN prior to the SLEEP instruction, then a word
may be received during SLEEP.
On completely receiving the word, the RSR register will transfer the data to the
RCREG register and if enable bit RCIE bit is set, the interrupt generated will wake the
chip from SLEEP.
If the global interrupt is enabled, the program will branch to the interrupt vector
(0004h).
271
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
1. Enable the synchronous master serial port by setting bits SYNC and SPEN and
clearing bit CSRC.
5. Flag bit RCIF will be set when reception is complete and an interrupt will be
generated, if enable bit RCIE was set.
6. Read the RCSTA register to get the ninth bit (if enabled) and determine if any error
occurred during reception.
8. If any error occurred, clear the error by clearing bit CREN. If using interrupts, ensure
that GIE and PEIE(bits 7 and 6) of the INTCON register are set. 272
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
273
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The Master Synchronous Serial Port (MSSP) module is a serial interface, useful for
communicating with other peripheral or microcontroller devices. These peripheral
devices may be serial EEPROMs, shift registers, display drivers, A/D converters, etc.
The SPI mode allows 8 bits of data to be synchronously transmitted and received
simultaneously.
Additionally, a fourth pin (Slave Select (SS)) is used in Slave mode. 274
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
When initializing the SPI, several options need to be specified. This is done by
programming the appropriate control bits (SSPCON<5:0> and SSPSTAT<7:6>).
275
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
276
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
277
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
278
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
279
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
280
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
281
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
282
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
283
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
To enable the serial port, MSSP Enable bit, SSPEN (SSPCON<5>) must be set.
To reset or reconfigure SPI mode, clear bit SSPEN, re-initialize the SSPCON registers,
and then set bit SSPEN.
This configures the SDI, SDO, SCK and SS pins as serial port pins.
For the pins to behave as the serial port function, some must have their data direction
bits (in the TRIS register) appropriately programmed. That is:
6. ADCON1 must be set in a way that pin RA5 is configured as a digital I/O
284
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
The master can initiate the data transfer at any time because it controls the SCK.
The master determines when the slave is to broadcast data by the software protocol.
If the SPI module is only going to receive, the SDO output could be disabled
(programmed as an input).
The SSPSR register will continue to shift in the signal present on the SDI pin at the
programmed clock rate.
As each byte is received, it will be loaded into the SSPBUF register as if a normal
received byte (interrupts and status bits appropriately set).
This then, would give waveforms for SPI communication as shown in Figure below,
where the MSB is transmitted first.
285
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Master mode, the SPI clock rate (bit rate) is user programmable to be one of the
following:
4. Timer2 output/2
This allows a maximum bit clock frequency (at 20 MHz) of 5.0 MHz.
Figure below shows the waveforms for Master mode. When CKE = 1, the SDO data is
valid before there is a clock edge on SCK.
The change of the input sample is shown based on the state of the SMP bit.
286
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
287
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
In Slave mode, the data is transmitted and received as the external clock pulses
appear on SCK.
When the last bit is latched, the interrupt flag bit SSPIF (PIR1<3>) is set.
While in Slave mode, the external clock is supplied by the external clock source on
the SCK pin.
This external clock must meet the minimum high and low times as specified in the
electrical specifications.
288
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
289
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
290
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
291
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV
I/O Ports
ADC
Interrupt
PWM
Instruction Set
292
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT