Professional Documents
Culture Documents
Chapter 6 - Hardware Interfacing
Chapter 6 - Hardware Interfacing
HARDWARE INTERFACING
Chapter Objectives
Parallel
- 8 or more wire line used to transfer data. E.g.
Printers & hard disk
2.
Serial
- data sent one bit at a time. E.g. data communication
between 2 computer.
Serial
Expensive
Cheaper
Simplex transmission
- Data is transmitted from the sender to receiver only
- The communication can only take place in one direction
and it is not possible for the receiver to send data back.
Half-Duplex
&
Full-Duplex
RS232 standard
RS232 standard using DB25
connector
RS232 standards
most widely used serial I/O interfacing standard
input and output voltage levels are not TTL compatible
Null Modem
Connection
MAX232
Serial Programming in C
Programming in C programming
a. To transfer data serially
b. To receive data serially
c. To transmit and receive connected to pc com port
Interrupt Programming
Interrupts are mechanisms which enable instant
response to events such as counter overflow, pin
change, data received, etc.
In normal mode, microcontroller executes the main
program as long as there are no occurrences that
would cause an interrupt.
Upon interrupt, microcontroller stops the execution of
main program and commences the special part of the
program(ISR) which will analyze and handle the
interrupt.
Interrupt
Devices get the attention of the CPU only when it
needs a service
Can service many devices with different level of
priorities
Timers
A/D Converter
Serial I/O
CCP (compare capture pulse-width-modulation)
INTCON
External interrupt sources
Valid interrupt
Interrupt request bit (flag)
Interrupt enable bit
Priority bit
INTERUPT
LABEL
REGISTER
CODE
INTERRUPT
SOURCE
INT_EXT
0x0B10
INT_RB
0x0B08
INT_RTCC
0x0B20
INT_TIMER0
0x0B20
INT_TIMER1
0x8C10
INT_CCP1
0x8C04
INT_TIMER2
0x8C02
INT_CCP2
0x8D01
INT_AD
0x8C40
INT_SSP
0x8C08
INT_PSP
0x8C80
INT_EEPROM
0x8D10
INTCON ( interrupt
control register)
Interrupt priorities
High-priority interrupt vector 000008H
Low-priority interrupt vector 000018H
A high-priority interrupt can interrupt a low-priority
interrupt in progress.
Interrupt priority enable
Bit7 (IPEN) in RCON register
RCON
Flag Bit
Register
Timer0
TMR0IF
INTCON
TMR0IE
INTCON
Timer1
TMR1IF
PIR1
TMR1IE
PIE1
Timer2
TMR2IF
PIR1
TMR3IE
PIE1
Timer3
TMR3IF
PIR3
TMR3IE
PIE2
ORG
GOTO
0000H
MAIN
ORG
MAIN
ORG
0008H
BTFSS INTCON,TMR0IF
RETFIE
GOTO T0_ISR
Timer0 Interrupt
OVER
00100H
T0_ISR
BCF TRISB,5
ORG 200H
CLRF TRISD
MOVLW 0xFF
SETF TRISC
MOVWF TMR0H
MOVLW 0x08
MOVLW 0xF2
MOVWF T0CON
MOVWF TMR0L
MOVLW 0xFF
BTG
PORTB,5
MOVWF TMR0H
BCF
MOVLW 0xF2
INTCON,TMR0IF
MOVWF TMR0L
RETFIE
BCF INTCON,TMR0IF
END
BSF T0CON,TMR0ON
BSF INTCON,TMR0IE
BSF INTCON,GIE
MOVFF PORTC,PORTD
BRA OVER
INTCON3 Register
ORG 0000H
GOTO MAIN
ORG 0008H
BTFSS INTCON,INT0IF
RETFIE
GOTO INT0_ISR
ORG 00100H
MAIN
OVER
BCF TRISB,7
BSF TRISB,INT0
CLRF TRISD
SETF TRISC
BSF INTCON,INT0IE
BSF INTCON,GIE
MOVFF PORTC,PORTD
BRA OVER
INT0_ISR
ORG 200H
BTG PORTB,7
BCF INTCON,INT0IF
RETFIE
END
Flag Bit
Register
TXIF
(Transmit)
TXIF
PIR1
TXIE
PIE1
RCIF
(Receive)
RCIF
PIR1
RCIE
PIE1
8 bit switch is connected to port.D. the PIC18 reads data from PORTD and
writes it to TXREG.
ORG
00100H
MAIN SETF TRISD
ORG
0000H
MOVLW 0x20
GOTO MAIN
MOVWF TXSTA
ORG
0008H
BTFSC PIR1,TXIF
BRA TX_ISR
RETFIE
ORG 0040H
MOVLW D'15'
MOVWF SPBRG
TX_ISR
MOVWFF
PORTD,TXREG
RETFIE
BCF TRISC, TX
BSF RCSTA, SPEN
BSF PIE1,TXIE
BSF INTCON,PEIE
BSF INTCON,GIE
OVER
BRA OVER
END
Enable peripheral Interrupt
LCD Interfacing
Driver HD44780
8-bit data bus (RD7-RD0)
Three control signals
RS Register Select
(RA3)
R/W Read/Write
(RA2)
E Enable (RA1)
Three power connections
Power, ground, and
variable resistor to
control brightness
Interfacing LCD
Can be interfaced either in 8-bit mode or 4-bit mode
In 8-bit mode, all eight data lines are connected
In 4-bit mode, only four data lines are connected
Two transfers per character (or instruction) are needed
LCD Operation
When the MPU writes an instruction to IR or data to
DR, the controller:
Sets DB7 high indicating that the controller is busy
Sets DB7 low after the completion of the operation
Software
To write into the LCD
Send the initial instructions to set up the LCD
4-bit or 8-bit mode
Software
To recognize and encode the key pressed,
the program should:
Ground all the columns by sending zeros.
Check each key in a row for logic zero.
Ground one column at a time and check all the
rows in that column.
Once a key is identified, it is encoded based on its
position in the column.
Analog
Digital
Digital System
Conversion time
Define as the time it takes the ADC to convert the analog input to
digital (binary) number.
Dictated by:
Clock source connected to the ADC
Technology used in the fabrication of the ADC chips (CMOS or
TTL)
Electromchanical sensor
A simple electromagnetic relay consists of a coil of wire wrapped
around a soft iron core, an iron yoke which provides a low
reluctance path for magnetic flux, a movable iron armature, and one
or more sets of contacts .
Other relays may have more or fewer sets of contacts depending on
their function.
Driving a relay
circuit to drive relay from I/O port using transistor and ULN2803
Optoisolator
also called an optocoupler, photocoupler, or optical isolator
"an electronic device designed to transfer electrical signals by
utilizing light waves to provide coupling with electrical isolation
between its input and output".
The main purpose of an opto-isolator is "to prevent high voltages or
rapidly changing voltages on one side of the circuit from damaging
components or distorting transmissions on the other side.
Commercially available opto-isolators withstand input-to-output
voltages up to 10 kV and voltage transients with speeds up to
10 kV/s.
Stepper Motor
A stepper motor (or step motor) is a brushless DC electric motor
that divides a full rotation into a number of equal steps.
The motor's position can then be commanded to move and hold at
one of these steps without any feedback sensor (an open-loop
controller), as long as the motor is carefully sized to the application.
There are many kind of stepper motors. Unipolar type, Bipolar type,
Single-phase type, Multi-phase type...
* 2-phase unipolar PM type stepper motor
DC Motor
Motors come in many sizes and types, but their basic function is the
same. Motors of all types serve to convert electrical energy into
mechanical energy.
They can be found in VCR's, elevators, CD players, toys, robots,
watches, automobiles, subway trains, fans, space ships, air
conditioners, refrigerators, and many other places.
D.C. motors are motors that run on Direct Current from a battery or
D.C. power supply.
PWM comparison
Program code