PIC Interface Design & Development Experiment 2 LED 5x7 DISPLAY Objective: 1. To use I/O of PIC Microcontroller to design a LED display 2. To develop a C code for Running the LED display We will design a 5x7 LED Display and PIC microcontroller will control each LED as shown below. Materials: x FlashPIC-Dev’ evaluation board, with PIC 16F877 or PIC 18F458 Microcontroller. x LEDs display (http://www.jameco.com) x 4017 chip (you can conduct the function of this 4017 using the “software” approach) x Other necessary equipment We use CD4017 counter chip to control only one column of LEDs turns on at a time. B0 is clock for 4017 and B1 is the reset. 4017 chip Output "0" goes HIGH on the rise of the first clock cycle. On the rise of the second clock cycle, 4017 chip output "0" goes LOW and output "1" goes HIGH. After each five cycle of clock, we can reset the counter to start over again. You may conduct the function of 4017 by the software. In which case, the 4017 chip can be omitted. 10/4/2006 17 of 61 PIC Interface Design & Development Seven of the lines of port D drive the LEDs via 470Ohms resistors. The output lines of the chip correspond to Port D and by turning off these lines, clocking the 4017 then turning on the outputs again, the second column of LEDs will be illuminated. This is repeated for the 3rd, 4th and 5th columns. When this is repeated at a rate above 50 times per second, the whole screen of LEDs appears to be ON at the same time. This is how a picture or effect is produced on the screen - it's called scanning. Test: x x Make sure all the LEDs are working. Test each LED display x Display each character of “EGR 592” 30 seconds such as x Running Sign The 5x7 Display can display running messages. Data can be loaded into 5 Ghost locations and this data is displayed on the screen. One byte is transferred at a time from the table and loaded into the Ghost section. A routine then outputs the five locations to the screen. The data in the Ghost section is then shifted one place to the left and a new byte loaded into the 5th location. This gives the effect of a message scrolling across the screen, from right to left. To 10/4/2006 18 of 61 PIC Interface Design & Development work out the value of each column of LEDs, the individual HEX values are added together. The program looks for "FF" and repeats the message. Reference: http://www4.tpg.com.au/users/talking/5x7%20Display%20Index.html An example of report and the corresponding code is listed below. 10/4/2006 19 of 61 PIC Interface Design & Development Experiment #2 LED 5x7 Display Sign Abstract Robert Quesada II California State University Pomona Pomona, CA 91768 In this experiment students will demonstrate displaying letters and numbers on a 5x7 LED matrix display. The program to drive the 5x7 LED matrix display will be written in C code. The 5x7 display will be connected to the Port D and Port B of the PIC microcontroller. 1. Introduction There are different steps to this experiment; the first part is to be able to light a single or a row of LED’s on the 5x7 display as shown below: 2. Background The 5x7 display is connected to Port D, but Port D only has 8 outputs total. In order to display all the output, the individual rows of LED’s are placed together. So when one say Pin D6 is set to turn on the whole row. Therefore in order to capture just one LED to turn on the columns are connected together. But this is not all that is need to display, what is also needed are transistors connected to the columns to hold or switch on the column, however the transistor can not hold the voltage for long, therefore the program must cycle the output to both Port D and Port B (controls the transistor indirectly). What controls the transistors is a 4017 chip, which is a simple divider; Port B controls the reset and the clock. The 4017 works like a shifter in the circuit. Figure 1 Test single column Using the knowledge gained from this first step the next step is to letters like the “E” “G” “R” “5” “9” “2” and display each letter for 30 seconds. 3. Equipment/Materials (1) PC with USB (1) PIC16F877 development board (Various) Resistors (35) LED’s (5) Transistors (1) 4017 chip (Various) Clip leads / wires 4. Circuit Description The circuit is shown below Figure 2 Displaying the letter “R” The final step is to make the letters move or what is called a “running sign”. Essentially the letter move across the LED display which a person is able to read. Figure 3 5x7 Circuit 10/4/2006 20 of 61 PIC Interface Design & Development Port D (D0-D6) is used for output to display rows and Port B (B0-B1) is used for the clock and reset on the 4017 chip to control the columns on the 5x7 display. The transistors are used as ighting-up control of the LEDs columns (to provide enough power). ~0x40,~0x40,~0x7F,~0x40,~0x40,~0x00,//T ~0x7F,~0x49,~0x49,~0x41,~0x41,~0x00,//E ~0x3F,~0x40,~0x40,~0x3F,~0x40,~0x40,~0x3F,~0x00, //M ~0x3E,~0x41,~0x41,~0x41,~0x3E,~0x00, //O ~0x70,~0x0E,~0x01,~0x0E,~0x70,~0x00, //V ~0x7F,~0x00,//I st The 1 step to display anything on the 5x7 display is to reset the 4017 chip. Next is to st send the desired output to Port D, the 1 column should be lit with the output of Port D because reset send high to pin 0 of the 4017.. Then to light the next column, clock the 4017 and turn on the desired output to nd Port D this will light up the 2 column. Do this for all 5 columns, after 5 the 4017 needs st to be reset to start over at 1 column. In order to see the image this needs to be cycled many times so it will appear the whole screen is on, this is called scanning. st To program this the 1 step is the header file: #include <16F877A.h> #fuses HS,NOWDT This allows us to use directives from the 16F877A.h header file, the NOWDT disables the watch dog timer which would cause the PIC to automatically reset. Next step is to set up the port definitions #byte PORTD = 0x08 /* output port D definition */ #byte PORTB = 0x06 /* output port B definition */ #bit clock = PORTB.0 /* clock*/ #bit reset = PORTB.1 /* reset*/ To store the columns a array is used This array is for the EGR592 display int displayEGR[35]={ ~0x7F,~0x49,~0x49,~0x41,~0x41,//E ~0x3E,~0x41,~0x49,~0x49,~0x2E,//G ~0x7F,~0x48,~0x4C,~0x4A,~0x31,//R ~0x7A,~0x49,~0x49,~0x49,~0x46,//5 ~0x32,~0x49,~0x49,~0x49,~0x3E,//9 ~0x27,~0x49,~0x49,~0x49,~0x31,//2 }; //array to store hex numbers for display This array is for the scrolling text int displaySCROLL[80]={ ~0x7F,~0x10,~0x0C,~0x02,~0x7F,~0x00,//N ~0x3E,~0x41,~0x45,~0x45,~0x36,~0x00, //G ~0x32,~0x49,~0x49,~0x49,~0x26,~0x00,//S ~0x7F,~0x00//I ~0x3E,~0x41,~0x45,~0x45,~0x36,~0x00,//G ~0x7F,~0x10,~0x0C,~0x02,~0x7F,~0x00,//N ~0x00,~0x00,~0x00,~0xFF}; //end of array Next setup the integers needed int y=0, column,columncounter; long int timecounter=0; y is what is used to control the amount of shifts of columns, to display for a letter at a time increment y by 5 and to scroll in a new column increment y by 1.column is used for the “for” loop this cycles through the columns 1 through 5. Columncounter is to scan Port D on the desired column over and over to eliminate the “ghosting” effect. And timecounter is used to display the columns for a desired amount of time. Two functions are declared, one is to clock the 4017 chip and the second is to reset the 4017 chip. void clockshift(void){ clock=0; clock = 1; } void resetchip(void){ reset=1; reset=0; } //reset 4017 Next is main part of the program void main(void){ set_tris_b(0x00); // port B is all output set_tris_d(0x00); // port D is all output resetchip(); This setups the ports for output and resets st the chip for the 1 column. ~0x00,~0x00,~0x00,~0x00,~0x00,~0x00,//blank space 10/4/2006 //clock the 4017 21 of 61 PIC Interface Design & Development The next section is only for displaying “E” “G” “R” “5” “9” “2” while(1){ for(timecounter=0;timecounter<3000;timecounter ++){//timing loop, adjust! for(column=0;column<=4;column++){ for(columncounter=0;columncounter<50;columnc ounter++) output_D(displayEGR[column+y]); clockshift(); } } y=y+5; //shift 5 to next "letter" if (y==30) //end of array restart y=0; } } The timecounter loop controls how long the letters stay on the LED display. The column count changes the columns 1 – 5, the columncounter is what prevents ghosting by display the column over and over many times. To display the next letter y needs to be shifted by 5 because each 5 columns represents one letter in the array. When y=30 or the end of the array reset the y value. 5. Testing Scenarios We will test our LEDs to see if they’re all working at first by lighting up all the columns of LEDs starting from column 1 and working our way to column 5. Then after we show that all the LEDs are working properly. We will start off by showing that we can hold the letter E, then G, then R, then 5, then 9 and lastly 2 for 30 seconds each. This will show we are capable of programming the LEDs accordingly. Then we will demo the scrolling message on the LED screen “TE MOVING SIGN.” 6. Conclusion The testing scenario was demonstrated to work correctly. The only difficulty in this lab was the running sign and EGR592 display in one program so two programs were needed because storing both arrays in one was too much memory for the PIC microcontroller to hold. This lab demonstarated how important the correctness of coding needs to be when connecting a PIC to some external circuit, in this case the 5x7 LED and 4017 chip. This next part is from the scrolling text the program is very similar. while(1){ for(timecounter=0;timecounter<200;timecounter+ +){//timing loop, adjust! for(column=0;column<=4;column++){ //output to columns 1-5 for(columncounter=0;columncounter<50;columnc ounter++) //prevent ghosting by scanning output_D(displaySCROLL[column+y]); //output to port D clockshift(); //next column } resetchip(); //restart afete 5 } y=y+1; if (displaySCROLL[5+y]==~0xFF) //reached end of usable array restart y=0; } } The only difference of the scrolling text is that the time displayed is much shorter and y is incremented by 1 to shift in next column. The terminating part of the array is ~FF 10/4/2006 22 of 61