Anda di halaman 1dari 292

EC7010: INTRODUCTION TO

EMBEDDED CONTROLLERS

UNIT I: 8-Bit Microcontrollers


UNIT V: PERIPHERAL INTERFACING
WITH PIC MICROCONTROLLER
Presented By,
Dr. V. SATHIESH KUMAR
Assistant Professor
Department of Electronics Engineering
MIT-Anna University
1
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

UNIT I 8-BIT CONTROLLER


Microprocessors and Microcontrollers, Introducing PIC 16F877- Architecture, Memory
Technologies, Timing Circuits, Power-up and Reset, Parallel ports, ADC, Interrupt, Serial
Peripheral buses (UART, I2C, SPI), PWM, Counters and Timers, Instruction Set and
Assembly Language Programming.

UNIT V PERIPHERAL INTERFACING WITH PIC MICROCONTROLLER


Human and physical interfaces- Switches to Keyboard, LED display, Liquid Crystal
Display, Actuators and sensors, PWM, Serial communication protocols (UART, I2C, SPI),
Programming Interrupt, Timer and Counter.

Presentation Slides:

www.sathieshkumar.com/tutorials
2
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

1. D. E. Simon, An Embedded Software Primer, Addison-Wesley, 1999.


2. Kirk Zurell, "C programming for Embedded Systems", CRC Press, 2000.
3. Dogan Ibrahim, "Advanced PIC microcontroller projects in C", Newnes publication,
2012.
4. Tim Wilmshurst, "Designing Embedded Systems with PIC microcontrollers-Principles
and Applications", Newnes Publications, 2007.

1. Douglas V. Hall, "Microprocessor and Interfacing, Programming and Hardware",


Revised second Edition 2006, Eleventh Reprint 2011. Tata Mc Graw Hill.
2. Muhammad Ali Mazidi, Rolin McKinlay, Danny Causey, "PIC Microcontroller and
Embedded Systems: Using Assembly and C for PIC18", Prentice hall publications, 2007.
3. Julio Sanchez Maria P. Canton, Microcontroller Programming: The microchip PIC,
CRC Press, Taylor & Francis Group,2007.
4. Martin Bates, "Interfacing PIC microcontrollers-Embedded Design by Interactive
Simulation", Newnes Publication, 2006.

Presentation Slides:

www.sathieshkumar.com/tutorials
3
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

An Embedded system is a system designed to perform one or few dedicated


functions often with real time computing constraints. It is embedded as a part of
a complete device often including hardware (mechanical and electronic parts)
and software.

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

RISC Architecture (35 instructions)


All single cycle instructions except program branches which
are two cycle
Interrupt capability (up to 14 sources).
CMOS flash/EEPROM technology.
In circuit serial programming(ICSP).
Low power consumption(0.6 mA @ 3 V).
Watch dog timer with own RC oscillator
Parallel slave port (PSP) -8 bits wide
10 bit analog to digital convertors
Timer0 & 2 (8 bit), Timer1(16 bit)
Two PWM modules.
I2C(Master/Slave mode) & USART with 9 bit detection.

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

The Power-up Timer provides a nominal 72 millisecond delay after a Power


On Reset (POR), Brown Out Reset (BOR) or after MCLR pin reset is initiated.
The PWRT operates on a dedicated internal RC Oscillator.
The device is kept in reset as long as the PWRT has not timed out.
The PWRT is used to allow the Vdd voltage of the PIC MCU to rise to an
acceptable level before operating.
The PWRT delay may vary from device to device due to Vdd, temperature,
and process variations.
For most applications we recommend enabling the PWRT, especially when
the BOR is enabled, since the voltage level of brownout may be just below the
trigger point and the extra PWRT delay will guarantee a stable return from
reset.
9
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

When the External Crystal/Resonator is selected as the system clock on the


PIC MCU device the crystal or resonator drive circuit requires time to settle
before reliable code execution can occur.
For this reason, the Oscillator Start-up Timer (OST) is used to give the drive
circuitry time to settle.
The OST counts 1024 oscillations from the crystal/resonator drive circuit at
the OSC1 pin.
The count begins after a Power On Reset (POR) and if enabled, the Power-
Up Reset Timer (PWRT) has expired.
The OST ensures that the crystal oscillator or the ceramic resonator have
started and can provide a stable system clock.

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

The WDT is activated when its time-out period is exceeded.


It is a free running timer independent of the main system clock.
If the operation of the code in the device is affected by an external source that
causes the software to deviate from its expected path, the WDT may be able to
reset the device automatically depending how the reset is used in the design.
When WDT is activated, it will hold a PIC MCU in reset and place the
Program Counter back to the beginning of the program software (org 0).
The software designer can place instructions to clear the WDT counter and
prevent a WDT reset from occurring.

14
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Three memory blocks:


1. Program memory space (8Kx14)
2. Data Memory (368 Bytes of RAM + Special function registers (SFR))
3. EEPROM

15
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Program counter (PC)- 13 bit (Stores the address of the next


instruction to be executed by the CPU).
Each memory space or instruction size is of 14 bits.
RESET vector is at 0000H.
Interrupt vector is at 0004H.
Memory is divided into four pages- Page 0/1/2/3 (Each of 2K
in size).
It constitutes a 8-level deep x 13-bit wide hardware stack.
It acts like a circular buffer.
Stack is not a part of program/data memory.
Stack pointer is not readable or writable.
PCLATH is not affected by a PUSH or POP operation.

16
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

CALL: Call subroutine.


Syntax: CALL k
RETURN: Return from subroutine.
Syntax: RETURN
RETFIE: Return from interrupt.
Syntax: RETFIE
RETLW: Return with literal in W.
Syntax: RETLW k
W: Working register (Similar to Accumulator).
Literal: k (8-bit constant).

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

ADDWF: Add W and f (File register or RAM)


Syntax: ADDWF f, d (d: Destination (0: W, 1: File register))
ADDLW: Add literal and W
Syntax: ADDLW k
SUBLW: Subtract W from literal
Syntax: SUBLW k
SUBWF: Subtract W from f (File register)
Syntax: SUBWF f, d (d: Destination (0: W, 1: File register))

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

PORTA (RA5:RA0)- 6 Pins (Bi-directional port)


Block diagram of Port pins RA3:RA0 and RA5
Each PORT is associated with two SFRs, namely
TRISA and PORTA registers.
TRISA is a data direction register
(0: OUTPUT, 1: INPUT)
PORTA is an output or an input buffer register
PORTA (RA5, RA3:RA0) has alternate functions
(ADC inputs- AN4-AN0) and analog VREF input.
On a Power-on Reset, these pins are configured
as analog inputs and read as 0.

26
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

PORTA (RA4) is multiplexed with the


Timer0 module clock input to become
the RA4/T0CK1 pin.
The RA4/T0CK1 pin is a Schmitt Trigger
input and an open drain output (External
pull-up resistor is needed to used it as a
output pin).

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

PORTB is an 8-bit wide, bi-directional port.


TRISB=0x00 // OUTPUT MODE
TRISB=0xFF // INPUT MODE
Three pins of PORTB are multiplexed with
the Low voltage programming function:
RB3/PGM, RB6/PGC and RB7/PGD.
Each of PORTB pins has a weak internal
pull-up.
A single control bit to turn on all the
pull-ups RBPU (OPTION_REG<7>).
In OUTPUT MODE and POR, weak pull-up
is turned off.
30
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V
PORT Pins:RB7:RB4

RB7:RB4 have an interrupt on change feature.


Only pins configured as INPUT can cause this
interrupt to occur.
Input pins of RB7:RB4 are compared with the
old value latched on the last read of PORTB.
The mismatched outputs of RB7:RB4 are ORed
together to generate the RB Port Change
Interrupt with flag bit RBIF (INTCON<0>).
Interrupt on change feature is recommended
for wake-up on key depression operations.
RB0/INT is an external interrupt pin and is
configured using the INTEDG bit (OPTION_REG<6>).
31
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

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

PORTC is an 8-bit wide, bi-directional port.


TRISC=0x00 // OUTPUT MODE
TRISC=0xFF // INPUT MODE
When the I2C module is enabled, the
PORTC<4:3> pins can be configured with normal
I2C levels (HIGH=3 V, LOW=1.5 V) or with SMBus
levels (HIGH=2.1 V, LOW=0.8 V) by using CKE bit
(SSPSTAT<6>).
SSPSTAT: Sync serial port status register
CKE: SPI Clock edge select
In I2C mode, CKE=0 (I2C spec) and CKE=1 (SMBus)

34
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

PORT Pins:RC<2:0>, RC<7:5>

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

PORTD is an 8-bit wide, bi-directional port.


TRISD=0x00 // OUTPUT MODE
TRISD=0xFF // INPUT MODE
PORTD can be configured as an 8-bit
wide microprocessor port (Parallel
slave port) by setting control bit
PSPMODE (TRISE<4>).
In this mode , the input buffers are TTL.

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

PORTE has three pins (RE0/RD/AN5,


RE1/WR/AN6, RE2/CS/AN7)
TRISE=0x00 // OUTPUT MODE
TRISE=0xFF // INPUT MODE
ADCON1 register is configured for
digital I/O
PORTE pins are multiplexed with analog
inputs.
When selected for analog input, these
pins will read as 0s.
On a POR, these pins are configured as
analog inputs, and read as 0.
41
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

Size of hex file is a concern for programmers because microcontrollers


have
limited on chip ROM.
Hex file produced by assembly is small when compared to high level
language
programming. But programming in assembly is tedious than high level
language and time consuming too.
Reasons for writing programs in C:
i) It is easier and less time consuming.
ii) C is easier to modify and update.
iii) C can use codes available in function libraries.
iv) C code is portable to other microcontrollers with little or no
modification. 46
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

LCD replacing LEDs (Seven-segment LEDs or other multi-segment LEDs)


due to following reasons:
1. Declining prices of LCDs
2. The ability to display numbers, characters and graphics. This is in contrast
to LEDs, which are limited to numbers and few characters.
3. Incorporation of a refreshing controller into the LCD, thereby relieving the
CPU of the task of refreshing the LCD. IN contrast, the LED must be
refreshed by the CPU (or in some other way) to keep displaying the data.
4. Ease of programming for characters and graphics.

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

Vcc and Vss provide +5V and ground respectively.


VEE is used for controlling the LCD contrast.
Register Select (RS) is used to select the command code register or data
register (RS=0: command code register, RS=1: data register).
Read/Write (R/W) input allows the user to write information to the LCD or
read information from it (R/W=0: Write, R/W=1: Read).
Enable (E) pin is used by the LCD to latch information presented to its data
pin. When data is supplied to data pins, a high-to-low pulse must be applied
to the E pin in order for the LCD to latch in the data present at the data pins.
This pulse must be a minimum of 450 ns wide.
D0-D7 is 8-bit data pins are used to send information to the LCD or read the
contents of the LCDs internal registers.
51
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV

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

1 Clear display screen C Display on, Cursor off


LCD command codes
4 Decrement Cursor E (or) F Display on, Cursor Blinking

6 Increment Cursor 80 Beginning of 1st line

5 Shift Display Right C0 Beginning of 2nd line

7 Shift Display Left 90 Beginning of 3rd line

8 Display off, Cursor off D0 Beginning of 4th line

A Display off, Cursor on 2 Return Home


52
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

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

void lcdInit (void)


{
cmd_to_lcd(0x3a); // 16X2 LCD
delay_ms(500);
cmd_to_lcd(0xc); // Display On, Cursor Off
cmd_to_lcd(0x1); // Display Clear
cmd_to_lcd(0x6); // Increment Cursor
cmd_to_lcd(0x80); // Beginning of 1st line
}

57
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV

void cmd_to_lcd(char p) void data_to_lcd(char p)


{ {
PORTD=p; PORTD=p;
RS=0; RS=1;
EN=1; EN=1;
delay_ms(500); delay_ms(500);
EN=0; EN=0;
} }

58
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

// LCD module connections


sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;


sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
char txt1[] = "PIC Micro";
char txt2[] = "Programming";
char i; // Loop variable
59
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);

while(1) { // Endless loop


for(i=0; i<8; i++) { // Move text to the left 7 times
Lcd_Cmd(_LCD_SHIFT_LEFT);
delay_ms(500);
}

for(i=0; i<8; i++) { // Move text to the right 7 times


Lcd_Cmd(_LCD_SHIFT_RIGHT);
delay_ms(500);
}
}
} 60
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

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

Invalid configuration: When switches 1, 2,3 and 4 are closed.

67
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

H-Bridge logic configurations:

68
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Quadruple high-current half-H drivers.


It provides a bidirectional drive currents of up to 600 mA at voltages from 4.5
V to 36 V.
Peak output current of 1.2 A.
All inputs are TTL compatible
Each output is a complete totem-pole drive circuit, with a Darlington transistor
sink and a pseudo-Darlington source.

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

void forward () void reverse () void stop ()


{ { {
PORTC.F0=1; PORTC.F0=0; PORTC.F0=0;
PORTC.F1=0; PORTC.F1=1; PORTC.F1=0;
} } } 71
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

Wider the pulse, higher the speed.


The ability to control the speed of the DC motor using PWM is one reason
that DC motors are preferable over AC motors.
AC motor speed is dictated by the AC frequency of the voltage applied to the
motor and the frequency is generally fixed. Hence we cannot control the speed
of the AC motor.

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

Translates electrical pulses into mechanical movement.


Applications: Disk drives, Dot matrix printers, robotics
Shaft is a permanent magnet rotor, surrounded by a four stator windings that are
paired with a centre-tapped common. This type of motors are termed as four phase or
unipolar stepper motors.
The center tap allows a change of current direction in each of two coils when a
winding is grounded, thereby resulting in a polarity change of the stator.

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.

rpm Steps per revolution


Steps per second=
60
80
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
VE7104: INTRODUCTION TO EMBEDDED CONTROLLERS
EC7010: UNIT I
II&&VIV

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

Bipolar stepper requires H-Bridge circuitry.


Bipolar stepper motors require a higher operational current than the unipolar; the
advantage of this is a higher holding torque.

83
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Micro stepping method: Excitation current is varied gradually


Small step angles can be achieved

4 Step- 2 Phase on Sequence

void Stepper_Clock() void Stepper_AntiClock()


{ {
PORTC=0x06; //0 PORTC=0x06; //0
delay_ms(1000); delay_ms(1000);
PORTC=0x03; //90 PORTC=0x0C; //270
delay_ms(1000); delay_ms(1000);
PORTC=0x09; //180 PORTC=0x09; //180
delay_ms(1000); delay_ms(1000);
PORTC=0x0C; //270 PORTC=0x03; //90
delay_ms(1000); delay_ms(1000);
PORTC=0x06; //0 PORTC=0x06; //0
delay_ms(1000); delay_ms(1000);
} }
84
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

Electrically controllable switch


Applications: Industrial controls, automobiles and appliances
Types: SPST (Single pole, single throw), SPDT (Single pole, double throw),
DPST (Double pole, single throw) and DPDT (Double pole, double throw)

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

The ADCON0 register controls the operation of the A/D module.


The ADCON1 register configures the functions of the port pins. The port pins can be
configured as analog inputs (RA3 can also be the voltage reference) or as digital I/O.

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

3. Wait the required acquisition time.


4. Start conversion:
Set GO/DONE bit (ADCON0)
5. Wait for A/D conversion to complete, by either:
Polling for the GO/DONE bit to be cleared (with interrupts enabled); OR
Waiting for the A/D interrupt
6. Read A/D result register pair (ADRESH:ADRESL), clear bit ADIF if required.
7. For the next conversion, go to step 1 or step 2, as required.

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

A/D Conversion TAD cycles:

100
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Registers/Bits associated with A/D:

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

Timer mode is selected by clearing bit T0CS (OPTION_REG<5>).


In Timer mode, the Timer0 module will increment every instruction cycle (without
prescaler).
If the TMR0 register is written, the increment is inhibited for the following two
instruction cycles.
The user can work around this by writing an adjusted value to the TMR0 register.
Counter mode is selected by setting bit T0CS (OPTION_REG<5>).
In Counter mode, Timer0 will increment either on every rising, or falling edge of pin
RA4/T0CKI.
The incrementing edge is determined by the Timer0 Source Edge Select bit, T0SE
(OPTION_REG<4>).
Clearing bit T0SE selects the rising edge.

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

Using Timer0 with an External Clock


When no prescaler is used, the external clock input is the same as the prescaler
output.
The synchronization of T0CKI with the internal phase clocks is accomplished by
sampling the prescaler output on the Q2 and Q4 cycles of the internal phase clocks.
Therefore, it is necessary for T0CKI to be high for at least 2Tosc (and a small RC delay
of 20 ns) and low for at least 2Tosc (and a small RC delay of 20 ns).

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

Registers associated with Timer0

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

In Timer mode, Timer1 increments every instruction cycle.


In Counter mode, it increments on every rising edge of the external clock input.
Timer1 can be enabled/disabled by setting/clearing control bit TMR1ON (T1CON<0>).
Timer1 also has an internal RESET input. This RESET can be generated by either of
the two CCP modules.
When the Timer1 oscillator is enabled (T1OSCEN is set), the RC1/T1OSI/CCP2 and
RC0/T1OSO/T1CKI pins become inputs. That is, the TRISC<1:0> value is ignored, and
these pins read as 0.

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

Timer1 operation in Timer mode


Timer mode is selected by clearing the TMR1CS (T1CON<1>) bit. In this mode, the
input clock to the timer is FOSC/4.
The synchronize control bit T1SYNC(T1CON<2>) has no effect, since the internal
clock is always in sync.

121
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Timer1 operation in Counter mode


Timer1 may operate in either a Synchronous, or an Asynchronous mode, depending
on the setting of the TMR1CS bit.
When Timer1 is being incremented via an external source, increments occur on a
rising edge.
After Timer1 is enabled in Counter mode, the module must first have a falling edge
before the counter begins to increment.

122
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Timer1 operation in Synchronized Counter mode


Counter mode is selected by setting bit TMR1CS.
In this mode, the timer increments on every rising edge of clock input on pin
RC1/T1OSI/CCP2, when bit T1OSCEN is set, or on pin RC0/T1OSO/T1CKI, when bit
T1OSCEN is cleared.
If T1SYNC is cleared, then the external clock input is synchronized with internal phase
clocks.
The synchronization is done after the prescaler stage. The prescaler stage is an
asynchronous ripple-counter.
In this configuration, during SLEEP mode, Timer1 will not increment even if the
external clock is present, since the synchronization circuit is shut-off. The prescaler,
however, will continue to increment.

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

Timer1 operation in Asynchronous Counter mode


If control bit T1SYNC (T1CON<2>) is set, the external clock input is not synchronized.
The timer continues to increment asynchronous to the internal phase clocks.
The timer will continue to run during SLEEP and can generate an interrupt-on-
overflow, which will wake-up the processor.
Timer1 cannot be used as a time-base for capture or compare operations.

125
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Reading and writing Timer1 in Asynchronous Counter mode


Reading TMR1H or TMR1L while the timer is running from an external asynchronous
clock, will guarantee a valid read (taken care of in hardware).
However, the user should keep in mind that reading the 16-bit timer in two 8-bit values
itself, poses certain problems, since the timer may overflow between the reads.
For writes, it is recommended that the user simply stop the timer and write the desired
values.
A write contention may occur by writing to the timer registers, while the register is
incrementing. This may produce an unpredictable value in the timer register.

126
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Registers associated with Timer1 as a Timer/Counter

127
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Timer2 is an 8-bit timer with a prescaler and a postscaler.


It can be used as the PWM time-base for the PWM mode of the CCP module(s).
The TMR2 register is readable and writable, and is cleared on any device RESET.
The input clock (FOSC/4) has a prescale option of 1:1,1:4, or 1:16, selected by control
bits T2CKPS1:T2CKPS0 (T2CON<1:0>).
The Timer2 module has an 8-bit period register, PR2.
Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next
increment cycle.
PR2 is a readable and writable register. The PR2 register is initialized to FFh upon
RESET.
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16
scaling inclusive) to generate a TMR2 interrupt (latched in flag bit TMR2IF, (PIR1<1>)).
Timer2 can be shut-off by clearing control bit TMR2ON (T2CON<2>), to minimize
power consumption.
128
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

Timer2 Prescalar and Postscalar


The prescaler and Postscalar counters are cleared when any of the following occurs:
1. a write to the TMR2 register
2. a write to the T2CON register
3. any device RESET (POR, MCLR Reset, WDTReset, or BOR)
TMR2 is not cleared when T2CON is written.

131
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Registers associated with Timer2 as a Timer/Counter

132
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Each Capture/Compare/PWM (CCP) module contains a 16-bit register which can


operate as a:
16-bit Capture register
16-bit Compare register
PWM Master/Slave Duty Cycle register
Both the CCP1 and CCP2 modules are identical in operation, with the exception being
the operation of the special event trigger.

133
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Capture/Compare/PWM Register1 (CCPR1) is comprised of two 8-bit registers:


CCPR1L (low byte) and CCPR1H (high byte).
The CCP1CON register controls the operation of CCP1.
The special event trigger is generated by a compare match and will reset Timer1.

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

Capture/Compare/PWM Register2 (CCPR2) is comprised of two 8-bit registers:


CCPR2L (low byte) and CCPR2H (high byte).
The CCP2CON register controls the operation of CCP2.
The special event trigger is generated by a compare match and will reset Timer1 and
start an A/D conversion (if the A/D module is enabled).

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

COMPARE MODE OPERATION BLOCK DIAGRAM

141
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

TIMER1 MODE SELECTION


Timer1 must be running in Timer mode, or Synchronized Counter mode, for the CCP
module to use the COMPARE feature.
In Asynchronous Counter mode, the compare operation may not work.
SOFTWARE INTERRUPT MODE:
When Generate Software Interrupt mode is chosen, the CCP1 pin is not affected.
The CCPIF bit is set, causing a CCP interrupt (if enabled)
SPECIAL EVENT TRIGGER:
In this mode, an internal hardware trigger is generated, which may be used to initiate
an action.
The special event trigger output of CCP1 resets the TMR1 register pair. This allows the
CCPR1 register to effectively be a 16-bit programmable period register for Timer1.
The special event trigger output of CCP2 resets the TMR1 register pair and starts an
A/D conversion (if the A/D module is enabled).
142
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

PWM DUTY CYCLE


The PWM duty cycle is specified by writing to the CCPR1L register and to the
CCP1CON<5:4> bits.
Up to 10-bit resolution is available. The CCPR1L contains the eight MSbs and the
CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by
CCPR1L:CCP1CON<5:4>.
The following equation is used to calculate the PWM duty cycle in time:
PWM duty cycle =(CCPR1L:CCP1CON<5:4>) TOSC (TMR2 prescale value)
CCPR1L and CCP1CON<5:4> can be written to at any time, but the duty cycle value is
not latched into CCPR1H until after a match between PR2 and TMR2 occurs (i.e., the
period is complete).
In PWM mode, CCPR1H is a read-only register.

146
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

PWM DUTY CYCLE


The CCPR1H register and a 2-bit internal latch are used to double buffer the PWM
duty cycle. This double buffering is essential for glitch-free PWM operation.
When the CCPR1H and 2-bit latch match TMR2, concatenated with an internal 2-bit Q
clock, or 2 bits of the TMR2 prescalar, the CCP1 pin is cleared.
The maximum PWM resolution (bits) for a given PWM frequency is given by the
formula:

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

SETUP FOR PWM OPERATION


The following steps should be taken when configuring the CCP module for PWM
operation:
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.
3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
5. Configure the CCP1 module for PWM operation.

148
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

EXAMPLE PWM FREQUENCIES AND RESOLUTIONS AT 20 MHz

149
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

REGISTERS ASSOCIATED WITH CAPTURE, COMPARE AND TIMER1

150
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

REGISTERS ASSOCIATED WITH PWM AND TIMER2

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

Each instruction cycle (Tcy) is comprised of four Q cycles (Q1-Q4).


The Q cycle is the same as the device oscillator cycle (TOSC).
The Q cycles provide the timing/designation for the Decode, Read, Process Data,
Write etc., of each instruction cycle.
The four Q cycles that make up an instruction cycle (Tcy) can be generalized as:
1. Q1: Instruction Decode Cycle or forced No Operation
2. Q2: Instruction Read Cycle or No Operation
3. Q3: Process the Data
4. Q4: Instruction Write Cycle or No Operation

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

Again: BTFSS PIR1, TMR1IF //Monitor Timer1 flag until overflow


BRA Again
BCF T1CON, TMR1ON //Stop Timer1
BRA Here

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

Fosc/4 clock [4 MHz/4=1 MHz]


Execution period T=1/f=1/1 MHz=1 us
Except Branch instruction{2 MC} all the other instruction requires 1 machine cycle
(MC) to execute.
Approximate delay= 250x200x5 MCx 1 us= 250000 us.

205
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Write a program to perform the following:


(a) Keep monitoring the RB2 bit until it becomes HIGH
(b) When RB2 becomes HIGH, write value 45H to PORTC, and also send a HIGH
to LOW pulse to RD3.
Solution:
BSF TRISB,2 //RB2 an input port
CLRF TRISC //PORTC an output port
BCF PORTD,3 //RD3 an output port
MOVLW 0x45
Again: BTFSS PORTB,2 //Bit test RB2 for HIGH
BRA Again
MOVWF PORTC
BSF PORTD,3
BCF PORTD,3 206
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

Write a program to perform the following:


(a) Keep monitoring the RB2 bit until it becomes HIGH send the
(b) Letter Y to PORTD, else send the Letter N to PORTD
Solution:
BSF TRISB,2 //RB2 an input port
CLRF TRISD //PORTD an output port
Again: BTFSS PORTB,2 //Bit test RB2 for HIGH
BRA Over
MOVLW AY //WREG=Y, ASCII Letter
MOVWF PORTD
GOTO Again
Over: MOVLW AN
MOVWF PORTD
GOTO Again
207
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

The I2C bus is used in microcontroller based systems.

Used in command interface (setup information was transmitted to on-chip controller)


in an MPEG-2 video chip; while a separate bus was used for high-speed video data.

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.

Every node in the network is connected to both SDL and SCL.

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

Some nodes may be able to act as a bus master.

Bus may have more than one master.

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.

Either bipolar or MOS circuits can be connected to the bus.

Both bus signals use open collector/open drain circuits.

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.

Every I2C device has an address.

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.

A bus transaction comprised a series of 1-byte transmissions and an address


followed by one or more data bytes. I2C encourages a data-push programming style.
212
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

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.

However, starts and stops must be paired.

Formats of some typical complete bus transactions are:

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.

An acknowledgment is sent at the end of every 8-bit transmission, whether it is an


address or data.

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

Hex value for J


0x4A-0100 1010

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

USART is also known as a Serial Communications Interface or SCI.

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.

The USART can be configured in the following modes:

1. Asynchronous (full duplex)

2. Synchronous - Master (half duplex)

3. Synchronous - Slave (half duplex)

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.

It is a dedicated 8-bit baud rate generator.

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.

In Synchronous mode, bit BRGH is ignored.

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).

The most common data format is 8-bits.

An on-chip, dedicated, 8-bit baud rate generator can be used to derive standard baud
rate frequencies from the oscillator.

The USART transmits and receives the LSB first.

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.

Asynchronous mode is selected by clearing bit SYNC(TXSTA<4>).


230
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

The USART Asynchronous module consists of the following important elements:

1. Baud Rate Generator

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.

This interrupt can be enabled/disabled by setting/clearing enable bit TXIE ( PIE1<4>).

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.

Transmission is enabled by setting enable bit TXEN (TXSTA<5>).The actual


transmission will not occur until the TXREG register has been loaded with data and the
baud rate generator (BRG) has produced a shift clock.

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

When setting up an Asynchronous Transmission, follow these steps:

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.

3. If interrupts are desired, then set enable bit TXIE.

4. If 9-bit transmission is desired, then set transmit bit TX9.

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.

7. Load data to the TXREG register (starts transmission).

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.

Once Asynchronous mode is selected, reception is enabled by setting bit CREN


(RCSTA<4>).

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.

The RCREG is a double buffered register (i.e., it is a two deep FIFO).


241
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

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

order not to lose the old FERR and RX9D information.

243
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

When setting up an Asynchronous Reception, follow these steps:

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.

3. If interrupts are desired, then set enable bit RCIE.

4. If 9-bit reception is desired, then set bit RX9.

5. Enable the reception by setting bit CREN.

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.

8. Read the 8-bit received data by reading the RCREG register.

244
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

When setting up an Asynchronous Reception, follow these steps:

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

When setting up an Asynchronous Reception with Address Detect Enabled:

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.

3. If interrupts are desired, then set enable bit RCIE.

4. Set bit RX9 to enable 9-bit reception.

5. Set ADDEN to enable address detect.

6. Enable the reception by setting enable bit CREN.

247
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT
EC7010: INTRODUCTION TO EMBEDDED CONTROLLERS UNIT I & V

When setting up an Asynchronous Reception with Address Detect Enabled:

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 Synchronous Master mode, the data is transmitted in a half-duplex manner (i.e.,


transmission and reception do not occur at the same time).

When transmitting data, the reception is inhibited and vice versa.

Synchronous mode is entered by setting bit SYNC (TXSTA<4>).

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.

The Master mode is entered by setting bit CSRC (TXSTA<7>).

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 TXREG register is loaded with data in software.

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.

The interrupt can be enabled/disabled by setting/clearing enable bit TXIE (PIE1<4>).

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

Transmission is enabled by setting enable bit TXEN (TXSTA<5>).

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

Steps to follow when setting up a Synchronous Master Transmission:

1. Initialize the SPBRG register for the appropriate baud rate.

2. Enable the synchronous master serial port by setting bits SYNC, SPEN and CSRC.

3. If interrupts are desired, set enable bit TXIE.

4. If 9-bit transmission is desired, set bit TX9.

5. Enable the transmission by setting bit TXEN.

6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.

7. Start transmission by loading data to the TXREG register.

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 SREN is set, then only a single word is received.

If enable bit CREN is set, the reception is continuous until CREN is cleared.

If both bits are set, CREN takes precedence.

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.

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 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.

The RCREG is a double buffered register (i.e., it is a two deep FIFO).

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 word in the RSR will be lost.

The RCREG register can be read twice to retrieve the two bytes in the FIFO.

Bit OERR has to be cleared in software (by clearing bit CREN).

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

When setting up a Synchronous Master Reception:

1. Initialize the SPBRG register for the appropriate baud rate.

2. Enable the synchronous master serial port by setting bits SYNC, SPEN and CSRC.

3. Ensure bits CREN and SREN are clear.

4. If interrupts are desired, then set enable bit RCIE.

5. If 9-bit reception is desired, then set bit RX9.

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

When setting up a Synchronous Master Reception:

8. Read the RCSTA register to get the ninth bit (if enabled) and determine if any error
occurred during reception.

9. Read the 8-bit received data by reading the RCREG register.

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.

Slave mode is entered by clearing bit CSRC (TXSTA<7>).

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.

b) The second word will remain in TXREG register.

c) Flag bit TXIF will not be set.

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

When setting up a Synchronous Slave Transmission, follow these steps:

1. Enable the synchronous slave serial port by setting bits SYNC and SPEN and
clearing bit CSRC.

2. Clear bits CREN and SREN.

3. If interrupts are desired, then set enable bit TXIE.

4. If 9-bit transmission is desired, then set bit TX9.

5. Enable the transmission by setting enable bit TXEN.

6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.

7. Start transmission by loading data to the TXREG register.

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.

Bit SREN is a don't care in Slave 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

When setting up a Synchronous Slave Reception, follow these steps:

1. Enable the synchronous master serial port by setting bits SYNC and SPEN and
clearing bit CSRC.

2. If interrupts are desired, set enable bit RCIE.

3. If 9-bit reception is desired, set bit RX9.

4. To enable reception, set enable bit CREN.

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.

7. Read the 8-bit received data by reading the RCREG register.

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 MSSP module can operate in one of two modes:

1. Serial Peripheral Interface (SPI)

2. Inter-Integrated Circuit (I2C)

The SPI mode allows 8 bits of data to be synchronously transmitted and received
simultaneously.

To accomplish communication, typically three pins are used:

1. Serial Data Out (SDO)

2. Serial Data In (SDI)

3. Serial Clock (SCK)

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>).

These control bits allow the following to be specified,

1. Master mode (SCK is the clock output)

2. Slave mode (SCK is the clock input)

3. Clock Polarity (Idle state of SCK)

4. Data input sample phase (middle or end of data output time)

5. Clock edge (output data on rising/falling edge of SCK)

6. Clock Rate (Master mode only)

7. Slave Select mode (Slave mode only)

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:

1. SDI is automatically controlled by the SPI module

2. SDO must have TRISC<5> cleared

3. SCK (Master mode) must have TRISC<3> cleared

4. SCK (Slave mode) must have TRISC<3> set

5. SS must have TRISA<5> set and register

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.

In Master mode, the data is transmitted/received as soon as the SSPBUF register is


written to.

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).

The clock polarity is selected by appropriately programming bit CKP (SSPCON<4>).

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:

1. FOSC/4 (or TCY)

2. FOSC/16 (or 4 TCY)

3. FOSC/64 (or 16 TCY)

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.

While in SLEEP mode, the slave can transmit/receive data.

When a byte is received, the device will wake-up from SLEEP.

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

PIC 16F877 Architecture, Memory Map

I/O Ports

Basics of Embedded C Programming

ADC

Interrupt

Timers and Counters

PWM

Serial Communication Protocols (USART, I2C, SPI)

Instruction Set

Assembly Language Programming

292
Dr. V. SATHIESH KUMAR Department of Electronics Engineering, MIT

Anda mungkin juga menyukai