To increment the value of a port in Embedded C, perform the following steps:
- Launch the Keil uVision5 IDE.
- Create a new project by clicking on the New Project | New uVision Project option.
- Specify the project name and folder location when prompted. Let's give the new project the name CounterApp; click on Save.
- 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].
- 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.
- 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.
- 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.
- 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.
- 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'.
- From the list box, select the C File (.c) option. Specify the filename as showcounter and then click on Add.
- 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++)
{
}
}
}
- 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.