Since any PORTB pin is able to source a max. of 20mA per pin with PORTB capable of sourcing a max- imum total current of 100mA, hence we can use PORTB to drive LED display directly. As shown in the circuit, 8 LEDs are connected to PORTB with each 120 Ohm current limiting resistor
Example to drive 8 LEDS
/************************************************ * * * COPYRIGHT (c) Blitzlogic Sdn. Bhd. * * Author : Abraham Wong 21/1/2000 * * * * example of using WHILE loop construct * * to drive 8 LEDS connected to port B * * * ************************************************/ #include <16c84.h> #USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */ #FUSES XT,NOWDT,NOPROTECT,NOPUT /* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */ #byte port_b=6 /* define the location of register port_b */
main(){ byte cnt; value; set_tris_b(0); /* set port_b to be outputs */ port_b = 0; /* initialize All port_b outp/uts to be zero */ value = 0x01; while( TRUE ) { /* forever loop using WHILE construct */ cnt = 0; while ( cnt<8 ) { port_b = value; DELAY_MS(1000); value = value << 1; /* shift left will put 0x01, 0x02, 0x04, 0x08, 0x10 */ cnt++; /* 0x20, 0x40, 0x80 to port_b */ } } }
EXERCISES Modify the above program to display the following sequence : LED1 and LED8 turn on, 1 sec delay; LED2 and LED7 turn on, 1 sec delay; LED3 and LED6 turn on, 1 sec delay; LED4 and LED5 turn on, 1 sec delay; Loop back & Repeat again forever
TWO 7 SEGMENT LED & 2 SWITCHES
Since we can source a maximum of 20mA per pin for PORTB and total maximum source current of PORTB is 100mA, hence we can use PORTB to drive LED display directly. As shown in the circuit, 2 common cathode 7-segment LEDs are connected to PORTB with 120 Ohms current limit- ing resistor. Each segment of two LED are tied in parallel to P1. Q1 and Q2 are activated by logic high of PA0 and PA1 which switch on ground to the respective common cathode pins. PA2 and PA3 pin are use to read the inputs S1 or S2 switches. The following code, 7-SEG.C demonstrate how to drive two 7-segment LEDs. The program first display digits on the Left LEDs and counts from 0 to 9 with a delay of 1 second between each digit. Then it switches to the Right LED and repeats the same tasks and then the whole sequence is repeated all over again from the beginning. Example to drive two 7-Segment LEDs
/**************************************** * * COPYRIGHT (c) Blitzlogic Sdn. Bhd. * Author : Abraham Wong 21/1/2000 * * example of using FOR loop to drive * two 7-Segment LEDs
****************************************/ #include <16c84.h> #USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */ #FUSES XT,NOWDT,NOPROTECT,NOPUT /* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */ #byte port_b=6 /* define the location of register port_b */ #byte port_a=5 /* define the location of register port_b */ byte CONST LED_MAP[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; main(){ byte cnt, right,num ; set_tris_b(0); /* set port_b as outputs */ set_tris_a(0); /* set port_a as output */ port_b = 0; /* ZERO port_a & port_b */ port_a = 0; for( ;; ){ for (right=1;right<3;right++){ port_a = right; for (cnt=0;cnt<10;cnt++){ port_b = LED_MAP[cnt]; DELAY_MS(1000); /* one second delay */ } } } }
EXERCISES Assignment 1 : The two 7-segment LED now is made to display 00 simultaneously and starting counting up automatically by incrementingby one with a one second delay between each increment. When count reach 99, it should reset itself to 00 and repeat all over again Assignment 2 : This is a modification to Asignment 1 where the increment by one is executed only when S1 switch is pressed. When the switch is released, the increment stops. When S2 switch is pressed, it initiate a decrement by one. When count is 99, and S1 is pressed, it becomes 00. When the count is 00, and S1 is pressed, it will becomes 99.