0% found this document useful (0 votes)
71 views47 pages

PIC16F877A I/O Ports and Functions

The document provides an overview of the input/output (I/O) capabilities of the PIC16F877A microcontroller, detailing its digital ports, pin configurations, and voltage levels for HIGH and LOW states. It discusses how to configure pins for input and output, including pull-up and pull-down resistors, as well as programming commands for reading and writing to the pins. Additionally, it covers interfacing with relays, motors, and EEPROM, along with examples of generating waveforms using digital-to-analog conversion.

Uploaded by

tn530751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views47 pages

PIC16F877A I/O Ports and Functions

The document provides an overview of the input/output (I/O) capabilities of the PIC16F877A microcontroller, detailing its digital ports, pin configurations, and voltage levels for HIGH and LOW states. It discusses how to configure pins for input and output, including pull-up and pull-down resistors, as well as programming commands for reading and writing to the pins. Additionally, it covers interfacing with relays, motors, and EEPROM, along with examples of generating waveforms using digital-to-analog conversion.

Uploaded by

tn530751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Input and Output Pin (I/O)

In our MCU there are 5 digital Input/output ports [A, B, C, D, and E]. It’s common in 8-Bit microcontrollers
to have I/O ports each of which has up to 8-pins. However, some I/O ports may have less than 8-pins as in
our PIC16F877A for examples. As you can see
• PORTA: has 6 pins [2 to 7]
• PORTB: has 8 pins [33 to 40]
• PORTC: has 8 pins
• PORTD: has 8 pins
• PORTE: has 3 pins [8 to 10]

Robotic & Vision Lab RoVis


Pin Voltages & Data Directionality
• Microcontrollers are fundamentally digital devices. For digital IO pins:
• Information is ‘coded’ in two discrete states:
• HIGH or LOW (logic: 1 or 0)
• Voltages
• TTL
• 5 V (for HIGH)
• 0 V (for LOW)
• 3.3 V CMOS
• 3.3 V (for HIGH)
• 0 V (for LOW)
• Input
• When you want to take information from
the external world (sensors) into the MCU
• Output
• When you want to change the state of
something outside the MCU (turn a motor
on or off, etc.)
• Pins default to input direction on power-up or
reset
• Your program can set or change the
directionality of a pin at any time

Robotic & Vision Lab RoVis


Input Pins Pull-Up & Pull-Down

Input Pins Pull-Up Input Pins Pull-Down


In this configuration, the pin is always pulled-up In this configuration, the pin is always pulled-down to
to High (logic 1) until an event occurs to drive it Low (logic 0) until an event occurs to drive it High (to 1).
Low (to 0). Mostly all you need to have is a Mostly all you need to have is a 10kohm resistor
10kohm resistor connected between the input connected between the input pin and the Vss (0v or Gd)
pin and the Vdd (+5v) as shown below. as shown below.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
PORTA

Robotic & Vision Lab RoVis


PORTA

Robotic & Vision Lab RoVis


Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register determine the
function of its pins

Robotic & Vision Lab RoVis


Output

Lệnh áp đặt chức năng của port hay chân


set_tris_X(value)
X: các port của PIC16F877
Value: 0: output (chân tương ứng sẽ là 0V)
1: input (chân tương ứng sẽ là 5V)
Vd: SET_TRIS_B( 0x0F );
// B7,B6,B5,B4 are outputs
// B3,B2,B1,B0 are inputs

Các lệnh xuất của I/O pin :


Truy xuất từng chân và thanh ghi
+Output_high(PIN_XXX): đặt giá trị tại chân tương ứng là 5V
+Output_low(PIN_XXX): đặt giá trị tại chân tương ứng là 0V
+Output_bit (PIN_XXX, value): value=0 hay là 1
PIN(XXX) : được định nghĩa sẵn trong file device.h

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Input

 Các lệnh đọc giá trị của cổng và thanh ghi


+ Đọc giá trị từ một chân của port
value=input(PIN_XXX);
value: trị trả về là true hay false
PIN_XXX: tên của chân

value=input_xx();
value: trị trả về là giá trị 8 bit trạng thái của port
xx: tên của port

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Phương pháp truy xuất trực tiếp địa chỉ thanh ghi
Khi kết hợp 2 tiền xử lý sau đây sẽ giúp người lập trình truy xuất đến từng thanh ghi
giống như lập trình trong ASM
#byte id=x
id: tên gợi nhớ
x: biến C hay là một hằng số, có thể coi x như địa chỉ thanh ghi
# bit id=x.y
id: tên gợi nhớ thường là tên của thanh ghi
x: là giá trị hằng số hay biến C, y: 0->7

Robotic & Vision Lab RoVis


#include<16F877.h>
#use delay(clock = 20000000)
#include <stdlib.h>
#include <string.h>
#byte porb=0x06
#bit bo=porb.0// hay là #bit bo=0x06.0
#bit trisbo=0x86.0
void main()
{
trisbo=0;
bo=0
delay_ms(1000);
bo=1
delay_ms(1000);
}

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
MPLAB programming

Xbits
Where X is the name of any specific register as found in the
datasheet. Which means that accessing any bit is easily done
by writing the following.
EX: PORTD
Xbits.
This will list down all the bits of this register (fields) for you to
choose from. Try it yourself!
EX: PORTDbits.RD4

Robotic & Vision Lab RoVis


MPLAB Programing

Robotic & Vision Lab RoVis


MPLAB Programing

Robotic & Vision Lab RoVis


Interfacing Relay with PIC Microcontroller

A relay is an electromagnetic switch which is used to switch High Voltage/Current using Low power circuits.
A relay should not be connected directly to a microcontroller due to following reasons.

• A microcontroller is not able to supply current required for the working of a relay. The maximum
current that a PIC Microcontroller can source or sink is 25mA while a relay needs about 50 – 100mA
current.
• A relay is activated by energizing its coil. Microcontroller may stop working by the negative voltages
produced in the relay due to its back emf.

Robotic & Vision Lab RoVis


KEY PAD 4x4

Robotic & Vision Lab RoVis


KEY PAD 4x4

Robotic & Vision Lab RoVis


KEY PAD 4x4

Robotic & Vision Lab RoVis


DC motor

• Forward
EN Pin High (En1 = 1 or En2 = 1)
Input 1 or Input 3 Pin High (In1 = 1 or In3=1)
Input 2 or Input 4 Pin Low (In2 = 0 or In4 = 0)
• Reverse
EN Pin High (En1 = 1 or En2 = 1)
Input 1 or Input 3 Pin Low (In1 = 0 or In3=0)
Input 2 or Input 4 Pin Low (In2 = 1 or In4 = 1)

Robotic & Vision Lab RoVis


Stepper motor

Unipolar

Bipolar

Robotic & Vision Lab RoVis


Unipolar Stepper motor

Robotic & Vision Lab RoVis


Bipolar Stepper motor

Robotic & Vision Lab RoVis


Bipolar Stepper motor A4988

Robotic & Vision Lab RoVis


Interrupt Service Routine (ISR)
As the name suggests, Interrupts are special events that require immediate attention, it
stops a microcontroller/microprocessor from the running task and serves a special task
known as Interrupt Service Routine (ISR) or Interrupt Handler.

PIC16F877A has the following 15 interrupt sources :


• USART Transmit
• External
• Synchronous Serial Port
• Timer 0
• CCP1 (Capture, Compare, PWM)
• Timer 1
• CCP2 (Capture, Compare, PWM)
• RB Port Change
• TMR2 to PR2 Match
• Parallel Slave Port Read/Write
• Comparator
• A/D Converter
• EEPROM Write Operation
• USART Receive
• Bus Collision
Registers Used for Interrupts
• INTCON
• OPTION_REG
• PIE1
• PIR1
• PIE2
• PIR2

Robotic & Vision Lab RoVis


Interrupt Service Routine (ISR)
From the diagram below you can see all the sources of the interrupts, when xxIF is an interrupt
flag and xxIE is an Interrupt Enable bit. Interrupt flag shows the result of an interrupt and
interrupt enable bit is used to enable the interrupt

Robotic & Vision Lab RoVis


The ISR (interrupt service routine) handler is a portion of your code that goes to a specific
chunk of ROM (program memory). In this routine, you’ll first have to check for the interrupt
source. Then, handle it respectively. Finally, don’t forget to clear the flag bit of this
interruption.

Robotic & Vision Lab RoVis


External Interrupt RB0

Robotic & Vision Lab RoVis


External Interrupt RB0

Robotic & Vision Lab RoVis


Control AC power Voltage

Triac firing with 4 ms delay Triac firing with 1.6 ms delay

- Một sóng sine đầy đủ hoàn thành một chu kỳ hết 360o, tương đương với 2
- Ta có ω = 2πf với tần số 50 Hz, như vậy ω = 100π. Vậy nếu ta kích với thời gian là 4 ms sau khi vượt
qua điểm 0 (zero crossing) thì α = 100 π * (4/1000) = 0.4 π. Lúc này điện áp đạt được:

RoboticTriac& firing
VisionwithLab RoVis
1.6 ms delay
Detect Zero point

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Count pulses and Detect Motor direction using incremental encoder

Robotic & Vision Lab RoVis


Multiple External Interrupts

Robotic & Vision Lab RoVis


Internal EEPROM

EEPROM, pronounced as Double-E-PROM,


stands for Electrically Erasable Programmable
Read-Only Memory. This kind of memory
devices is re-programmable by the application
of electrical voltage and can be addressed to
write/read each specific memory location.
The PIC Microcontroller has the following
EEPROM features:

 Up-to 256 x 8 Bytes of EEPROM Data


Memory
 1,000,000 Erase/Write Cycles EEPROM
 Low-Power, High-Speed EEPROM
Memory

Robotic & Vision Lab RoVis


Steps For Writing To EEPROM and Reading from EEPROM

Steps For Writing To EEPROM


1. If step 10 is not implemented, check the WR bit
to see if a write is in progress.
2. Write the address to EEADR. Make sure that
the address is not larger than the memory size
of the device.
3. Write the 8-bit data value to be programmed in
the EEDATA register.
4. Clear the EEPGD bit to point to EEPROM data
memory.
5. Set the WREN bit to enable program
operations. Steps For Reading From EEPROM
6. Disable interrupts (if enabled). 1. Write the address to EEADR. Make sure that the
7. Execute the special five instruction sequence: address is not larger than the memory size of the
• 1,2) Write 55h to EECON2 in two steps device.
(first to W, then to EECON2) 2. Clear the EEPGD bit to point to EEPROM data memory.
• 3,4) Write AAh to EECON2 in two steps 3. Set the RD bit to start the read operation.
(first to W, then to EECON2) 4. Read the data from the EEDATA register.
• 5) Set the WR bit
8. Enable interrupts (if using interrupts).
9. Clear the WREN bit to disable program
operations.
10. At the completion of the write cycle, the WR bit
is cleared and the EEIF interrupt flag bit is set. (EEIF
must be cleared by firmware.) If step 1 is not
implemented, then firmware should check for EEIF
to be set, or WR to clear, to indicate the end of the
program cycle.
Robotic & Vision Lab RoVis
Internal EEPROM

Robotic & Vision Lab RoVis


Internal EEPROM Using CCSC

write_eeprom (address, value)


address: 8 bit địa chỉ
value: giá trị 8 bit
VD: ghi giá trị 1 vào địa chỉ 10
#define LAST_VOLUME 10 // Location in EEPROM
int volume=1;
write_eeprom(LAST_VOLUME,volume);

+ value = read_eeprom (address) //Address: địa chỉ


cần đọc
VD: đọc giá trị từ địa chỉ 10
#define LAST_VOLUME 10
volume = read_EEPROM (LAST_VOLUME);

Robotic & Vision Lab RoVis


PIC16F877 I/O Example

Robotic & Vision Lab RoVis


PIC16F877 I/O Example: DAC: Digital to Analog Conversion

• R-2R ladder
Here is the schematic diagram for the R-2R ladder
(assuming 4-bit DAC). You can continue connecting
resistors in the same manner to get 10-Bit or 12-
Bit DAC

Robotic & Vision Lab RoVis


DAC: Digital to Analog Conversion Using R-2R Ladder

Robotic & Vision Lab RoVis


DAC: Sawtooth waveform swinging
• In order to get a ramp-up in output voltage, the digital output going to the DAC should go
from 0 up to 255 which corresponds to a voltage ramp from 0 to 5v.
• So, let’s now settle for 256 levels (sample points) in each cycle of the waveform. So now
we’ve Ns = 256
• For the desired Fout = 10Hz, the period of each cycle is T = 100ms. The only unknown
now is the sampling period Ts. And using the formula shown above, it ends up being
Ts = 100ms/256 = 390μsec

Robotic & Vision Lab RoVis


Generating Triangular Waveform

In this example, Ns = 256 (ramp-up) + 255 (ramp-down) = 511 sample points. So, the Ts =
100ms/511 = 196μsec

Robotic & Vision Lab RoVis


Generating Sine Wave With MCU & DAC

For this tutorial, I’ll only need 32 sample points. The maximum amplitude for the sine
wave is 5v which corresponds to 255 as a digital input for the DAC. My DAC is 8-Bit. If
you’re using a 10-Bit DAC, this value would be 1023 and so on
F=50Hz so T = 20ms => Therefore, Ts = 20/32= 625μsec

https://deepbluembedded.com/sine-lookup-table-generator-calculator/

Robotic & Vision Lab RoVis


Generating Sine Wave With MCU & DAC
F=50Hz so T = 20ms => Therefore, Ts = 20/32= 625μsec

Robotic & Vision Lab RoVis

You might also like