Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
/************************************************
* *
* 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>
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */
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
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.
/****************************************
*
* 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>
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */
main(){
byte cnt, right,num ;
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.