How to do it…

To increment the value of a port in Embedded C, perform the following steps:

  1. Launch the Keil uVision5 IDE.
  2. Create a new project by clicking on the New Project | New uVision Project option.
  3. Specify the project name and folder location when prompted. Let's give the new project the name CounterApp; click on Save.
  4. The device selection window will open and you will be prompted to select a device. From the Device combo box, select Legacy Device Database [no RTE].
  5. You will get the list of devices in the lower-left pane. Click on the Microchip node to expand it and display the list of devices in it.
  6. From the Microchip node, select the AT89C51 device. The description of the selected device will appear in the description pane on the right. Click on OK to move further.
  1. You will be asked whether you want to copy the STARTUP.A51 file to the project folder. Click on Yes to add the file and move further.
  2. The IDE will open showing three windows: The Project Workspace on the left, the Editing Window on the right, and the Output Window at the bottom. You will see Target1 created under the Project space.
  3. Add a C file by right-clicking on Source Group1 under the Target1 node and then click on Add New item to Group 'Source Group 1'.
  4. From the list box, select the C File (.c) option. Specify the filename as showcounter and then click on Add.
  5. The showcounter.c file will be added to Source Group 1. Enter the following code in the editor window:
#include<stdio.h>
#include<reg52.h>
void delay(void);
void main()
{
unsigned char i;
i=0x00;
while(++i)
{
P3=i;
delay();
}
}

void delay(void)
{
int j;
int i;
for(i=0;i<1000;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
  1. After entering the code, click on the SAVE icon in the toolbar to save the showcounter.c file.

Now, let's go behind the scenes to understand the steps better.