Tut 3 : 7-segment LED and Keypad

A program to press keypad and display the numbers on 7-segment LED (common-anode).

7-segment LED display
 
It is composed of 8 LEDs- 7 segments are arranged as a rectangle for symbol displaying and there is an additional segment for decimal point displaying. In order to simplify connection, anodes or cathodes of all diodes are connected to the common pin so that there are common anode displays and common cathode displays, respectively. Segments are marked with the letters from a to g, plus decimal point (dp), as shown in figure below. On connecting, each diode is treated separately, which means that each must have its own current limiting resistor.



In addition to digits from 0 to 9, there are some letters- A, C, E, J, F, U, H, L, b, c, d, o, r, t  that can be also displayed by means of the appropriate masking.

Keypad configuration
 
PBP Program

' Example program to read keypad numbers and display with 7-segment

DEFINE OSC 20                          ' using 20MHz clock oscillator
@ device hs_osc

        row1 VAR PORTD.6    'declare ports
        row2 VAR PORTD.5
        row3 VAR PORTD.4
        row4 VAR PORTD.3
        col1 VAR PORTD.2
        col2 VAR PORTD.1
        col3 VAR PORTD.0
                    
        TRISD=%11111000 'assign ports
        TRISB=0
             
     loop:
    
     Low col1:High col2:High col3          'column 1

     IF (row1==0) Then                 'if key 1 is pressed
     PORTB=%1111001
     Pause 1000
     Else
     PORTB=%1111111
     EndIF
    
     IF (row3==0) Then                     'key 7
     PORTB=%1111000
     Pause 1000
     Else
     PORTB=%1111111
     EndIF
    
     IF (row4==0) Then                     'key *
     PORTB=%0111111
     Pause 1000
     Else
     PORTB=%1111111
     EndIF
    
     High col1:Low col2:High col3            'column 2

     IF (row1==0) Then                    'key 2
     PORTB=%0100100
     Pause 1000
     Else
     PORTB=%1111111
     EndIF
          
     IF (row4==0) Then                       'key 0
     PORTB=%1000000
     Pause 1000
     Else
     PORTB=%1111111
     EndIF

     High col1:High col2:Low col3           'column 3

     IF (row1==0) Then                    'key 3
     PORTB=%0110000
     Pause 1000
     Else
     PORTB=%1111111
     EndIF
    
     GoTo loop
     End
    
You should try to write your own program for numbers "4", "5", "6" "8", "9" and symbol "#"......


 

No comments:

Post a Comment