8051 Development Board
/*
* Project name:
8051 Development Board
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include<reg51.h>
unsigned int portb_index,portb_array[4],digit, COUNT=0;
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
void timer0(void) interrupt 1 {
IE=0;
P1 = 0XFF; // Turn off all 7seg displays
P2= portb_array[portb_index]; // bring appropriate value to PORTB
if(portb_index==0)
P1 = 0XF7;
else if(portb_index==1) // turn on appropriate 7seg. display
P1 = 0XFB;
else if(portb_index==2) // turn on appropriate 7seg. display
P1 = 0XFD;
else if(portb_index==3) // turn on appropriate 7seg. display
P1 = 0XFE;
portb_index++ ;
if (portb_index > 4)
portb_index = 0; // turn on 1st, turn off 2nd 7seg.
TH0=0xA4; //TH0=-92
IE=0x82;
// Clear T0IF
}
void display(unsigned int number)
{
digit = number / 1000u ; // extract thousands digit
portb_array[3] = mask(digit); // and store it to PORTB array
digit = (number / 100u) % 10u; // extract hundreds digit
portb_array[2] = mask(digit); // and store it to PORTB array
digit = (number / 10u) % 10u; // extract tens digit
portb_array[1] = mask(digit); // and store it to PORTB array
digit = number % 10u; // extract ones digit
portb_array[0] = mask(digit); // and store it to PORTB array
}
void DELAY()
{
unsigned int X=6000000;
while(X--);
}
void main() {
// Enable GIE, T0IE
TMOD=0x02;
TH0=0xA4; //TH0=-92
TR0=1;
IE=0x82;
P1 = 0xFF;
DELAY();
COUNT=0;
while(1)
{
display(COUNT);
DELAY();
COUNT=COUNT+5;
if(COUNT>2000)
COUNT=0;
}
}