Embedded Systems Interfacing PIC with external devices 7-Segment display Eng. Anis Nazer Second Semester 2017-2018
PIC interfacing In any embedded system, the microcontroller should be connected to other devices such as: LEDs Switches Keypad 7-Segment displays LCD Motor etc...
7-segment display
7-Segment display A device that has 7 LEDs used to display a single decimal digit Each LED has two pins Two types of 7-Segment displays: Common Anode: all anode pins are connected Common Cathode: all cathode pins are connected
7-Segment display
Example Assume you want to display a 3 on a common cathode 7- segment display: GND 1 1 1 1 0 0 1 0
7-Segment display Assume you connected a common cathode display as shown in the figure, what should be stored in PORTB to show the number 3 PIC RB0 RB1 a b RB6 g common
Example Write a program that will count through the numbers from 0 to 9, and display the numbers on a common cathode 7-segment display that is connected to PORTB PIC RB0 RB1 a b RB6 g common
7-Segment display Write a table showing the required inputs to display all digits from 0 to 9, assuming you are using a common cathode display
7-Segment display Basic structure of the program: #include "p16f84a.inc" ; variable definition ORG 0x000 ; main program ; initailization (input/output) ; lookup table ; delay subroutine END
7-Segment display To store the table you can use a lookup table table MOVF X, 0 ADDWF PCL, 1 RETLW B'00111111' RETLW B'00000110' RETLW B'01011011' RETLW B'01001111' RETLW B'01100110' RETLW B'01101101' RETLW B'01111101' RETLW B'00000111' RETLW B'01111111' RETLW B'01101111'
7-Segment display To count through the numbers, you use a loop CLRF X loop CALL table MOVWF PORTB CALL delay INCF X, 1 MOVLW D'10' ; make sure that x is less than 10 XORWF X, 0 ; if X == 10, then X = 0 BTFSC STATUS, Z CLRF X GOTO loop
7 Segment display Instead of a lookup table, you can use an external decoder (BCD to 7-segment) instead of using a lookup table PIC RB0 RB1 RB2 RB3 BCD to 7 segment decoder a b g
7 Segment display Program to count through the numbers 0 9 Much easier program, but you need to add a decoder CLRF X loop MOVF X, 0 MOVWF PORTB CALL delay INCF X, 1 MOVLW D'10' ; make sure that x is less than 10 XORWF X, 0 ; if X == 10, set X = 0 BTFSC STATUS, Z CLRF X GOTO loop
Exercise Assume that you have a push button and a common anode 7 segment display. The display starts at 0 and is incremented each time the button is pushed. Draw the circuit diagram in detail Write the flow chart of the program Write the program in assembly
Exercise Circuit diagram Circuit diagram:
Exercise Flow chart Start x=0 false true x = x+1 true x = 0 false display x
Exercise - Table Table: ; look up table for a common anode 7 segment display table ADDWF PCL, 1 RETLW 0x40 RETLW 0x79 RETLW 0x24 RETLW 0x30 RETLW 0x19 RETLW 0x12 RETLW 0x02 RETLW 0x78 RETLW 0x00 RETLW 0x10
Exercise - Code ; Include register definition file #include "p16f84a.inc" ; VARIABLES x EQU 0x0C ; input/output selection BSF STATUS, RP0 CLRF TRISB BSF TRISA, 0 BCF STATUS, RP0 CLRF x ; check if button is pushed loop BTFSS PORTA,0 CALL next_x ; put the value of x on ; the 7 segment display MOVF x, 0 CALL table MOVWF PORTB next_x INCF x, 1 ; increment x L1 ; if x == 10, then x = 0 MOVLW D'10' XORWF x, 0 BTFSC STATUS, Z CLRF x ; wait until RA0 = 1 BTFSS PORTA,0 GOTO L1 RETURN GOTO loop
Problem What if you need to connect 4 digits? directly, you need 4*7 = 28 pins using a decoder 4*4= 16 pins The number of pins is large Solutions: PIC with more outputs Use a Latch/register to store an output Use persistence of vision (POV)
Multiple 7-Segment displays Latch is a register that stores a value, when enabled, the input is transferred to the output when disabled, the output does not change, and stays as the previous output Input Latch Output Enable
Multiple 7-Segment displays PIC PORTB 8 Latch 8 RA1 RA0 Enable 8 Latch 8 Enable
Multiple 7-Segment displays How many outputs are used? 8 for a digit (PORTB) 2 for control (PORTA) Total: 10 outputs How many outputs if you want to connect 4 digits 8 for digit 4 for control Total: 12 outputs
Multiple 7-Segment displays Algorithm: Disable all latches Display digit 1 Enable latch 1 Disable latch 1 Display digit 2 Enable latch 2... repeat for remaining digits
Multiple 7-Segment displays Another method: Persistence of vision If an LED is turned on and off more than 25 times in a second, your eye will see it as always on. Same idea as the television: A frame is drawn 25 times per second, so you see the TV images as a moving picture (video)
Multiple 7-Segment displays Using the concept of Persistence of vision, circuit diagram: PIC PORTB 8 common
Multiple 7-Segment displays Algorithm to display a 4 digit number: Start: disable all digits (CLRF PORTA) put digit 1 on PORTB enable digit 1 (BSF PORTA, 0) call delay disable digit 1 (BCF PORTA, 0) put digit 2 on PORTB enable digit 2 (BSF PORTA, 1) call delay disable digit 2 (BCF PORTA, 1) repeat for digits 3 and 4... goto start
Problem Assembly programs: hard to write hard to debug user must be an expert in Assembly Most of you are not computer engineering students and don't need to be an expert in Assembly... So, Why use assembly?? efficient programs smaller program size complete control of the MCU
High level language A simpler option for non-computer engineers: write your program in a high level language C, Basic, Java, etc use a compiler to translate from C to PIC machine language some available compilers: mikroc, microchip C compiler,
Example Build a circuit that counts through the numbers from 0 to 9 and display the result on a 7 segment display, use a BCD to 7-segment decoder to connect the display BCD to 7-Segment decoder is connected to RB0, RB1, RB2, RB3 decoder output is connected to the 7-segment display
Example circuit diagram:
Example Program: void main() { int i = 0; // counter TRISB = 0; // set PORTB as output PORTB = 0; // output a zero while ( 1 ) { // loop to display numbers from 0 to 9 for ( i = 0; i <= 9; i++) { PORTB = i; // display the number Delay_ms(1000); // delay for 1 second } } }