Categories

8051 7 Segment display program written in Basic

While browsing through the mikroelectronika website and looking at available downloads, I noticed their Basic compilers for various microcontroller architectures. So after downloading the 8051 version and installing lets look at an example converted from the C version.

The code is below, some key differences as you can see looking at the 2 code examples.

The comments are different, in Basic its While … Wend and you do not need a ; after every statement. In general though its simple to convert across, so if Basic is your preferred programming language then you could do worse than try this out, there is a free code size limited trial

C Code

void main()
{
do
{

P0 = 0xFF;
//activate segments 1 at a time
P0 = 0xF9;
Delay_ms(1000);
//number 2
P0 = 0xA4;
Delay_ms(1000);
//number 3
P0 = 0xB0;
Delay_ms(1000);
//number 4
P0 = 0x99;
Delay_ms(1000);
//number 5
P0 = 0x92;
Delay_ms(1000);
//number 6
P0 = 0x82;
Delay_ms(1000);
//number 7
P0 = 0xF8;
Delay_ms(1000);
//number 8
P0 = 0x80;
Delay_ms(1000);
//number 9
P0 = 0x98;
Delay_ms(1000);
//number 0
P0 = 0xC0;
Delay_ms(1000);
P0 = 0xff;

} while(1); // Endless loop
}

Basic Code

program LED_Blinking

main:
while TRUE
P0 = 0xFF
‘activate segments 1 at a time
P0 = 0xF9
Delay_ms(1000)
‘number 2
P0 = 0xA4
Delay_ms(1000)
‘number 3
P0 = 0xB0
Delay_ms(1000)
‘number 4
P0 = 0x99
Delay_ms(1000)
‘number 5
P0 = 0x92
Delay_ms(1000)
‘number 6
P0 = 0x82
Delay_ms(1000)
‘number 7
P0 = 0xF8
Delay_ms(1000)
‘number 8
P0 = 0x80
Delay_ms(1000)
‘number 9
P0 = 0x98
Delay_ms(1000)
‘number 0
P0 = 0xC0
Delay_ms(1000)
P0 = 0xff
wend ‘ Endless loop
end.

 

Links

for 8051

8051 and 7 segment displays

Its fairly straightforward to connect  a 7 segment display to your 8051 processor, in this example we will do this and show a code example which cycles through the numbers 0 to 9.

First of all a 7 segment display is basically a collection of LED’s, 1 LED per segment and a decimal point.

Here is the pinout of a fairly typical display, you should always refer to the datasheet of your display to verify the pinout.

7 segment

7 segment

 

7 segment display

7 segment display

Displaying a number

Displaying a number on a 7 segment display is as simple as sending a logic low or high (depending on the display type) to a sequence of pin(s) to light up the segment. The table below shows the segments that would be required to be enabled for a common cathode, in that case a 1 will light a segment. Invert the numbers in columns a – g if you have a common anode 7 segment display

So to display a 1 we need to activate segments b and c.

Digit a b c d e f g
0 1 1 1 1 1 1 0
1 0 1 1 0 0 0 0
2 1 1 0 1 1 0 1
3 1 1 1 1 0 0 1
4 0 1 1 0 0 1 1
5 1 0 1 1 0 1 1
6 1 0 1 1 1 1 1
7 1 1 1 0 0 0 0
8 1 1 1 1 1 1 1
9 1 1 1 1 0 1 1

Schematic

This is the basic 8051 connected up to the 7 segment display, other circuitry such as crystal and capacitors and power is not shown here.

The display is a common anode type

 

8051 and 7 segment

8051 and 7 segment

Code

This code is for a common anode 7 segment display, the code was written using the mikroC PRO for 8051 compiler

void main()
{
do
{

P0 = 0xFF;
//activate segments 1 at a time. Number 1 first
P0 = 0xF9;
Delay_ms(1000);
//number 2
P0 = 0xA4;
Delay_ms(1000);
//number 3
P0 = 0xB0;
Delay_ms(1000);
//number 4
P0 = 0x99;
Delay_ms(1000);
//number 5
P0 = 0x92;
Delay_ms(1000);
//number 6
P0 = 0x82;
Delay_ms(1000);
//number 7
P0 = 0xF8;
Delay_ms(1000);
//number 8
P0 = 0x80;
Delay_ms(1000);
//number 9
P0 = 0x98;
Delay_ms(1000);
//number 0
P0 = 0xC0;
Delay_ms(1000);
P0 = 0xff;

} while(1); // Endless loop
}

8051 and RGB LED example

In this example we connect an RGB LED to our 8051. In this case we used an RGB LED breakout, this was a common anode device. The schematic below shows the connection to the RGB LED breakout.

We will switch on the main colours individually in the example

 

Schematic

8051 RGB LED

8051 RGB LED

Code

The code was written using mikro C PRO for 8051

void main()
{
do
{
P2 = 0xFF; // Turn OFF
Delay_ms(1000); // 1 second delay
P2 = 0xFE;
Delay_ms(1000);
P2 = 0xFD;
Delay_ms(1000);
P2 = 0xFB;
Delay_ms(1000);
P2 = 0xF8;
Delay_ms(1000);
P2 = 0xFF; // Turn OFF diodes on PORT2
Delay_ms(1000); // 1 second delay

} while(1); // Endless loop
}

 

Links

 


RGB LED Module for Arduino (Works with Official Arduino Boards) – $2.23

from: DealExtreme

8051 switch input example

A very simple example that toggles a set of 8 LEDs based on whether a switch is pressed or not

The switch is connected to Port 3.0, the LEDs are connected to Port 2.0 to 7.

The code was written using Keil uVision2

Code

#include<reg52.h>

sbit SW1 = P3^0;

void main (void)
{
SW1 = 1;
while (1)
{
if(!SW1)
{
P2 = 0xAA;
}
else
{
P2 = 0x55;
}
}
}

8051 rotating LEDs examples

Again using the ARm + 8051 development board

Example 1

#include<reg52.h>
void Delay(unsigned int t);
void main (void)
{

unsigned char i;
Delay(50000);

P2=0x7f;
while (1)
{
for(i=0;i<8;i++)
{
Delay(50000);
P2>>=1;
P2=P2|0x80;
}
P2=0x7f;
}
}
void Delay(unsigned int t)
{
while(–t);
}

 

 

Example 2

 

#include<reg52.h>
void Delay(unsigned int t);
void main (void)
{

unsigned char i;
Delay(50000);

P2=0x7f;
while (1)
{
for(i=0;i<8;i++)
{
Delay(50000);
P2<<=1;
P2=P2|0x80;
}
P2=0x7f;
}
}
void Delay(unsigned int t)
{
while(–t);
}