Article Directory
One, Keil creates the project
1. Open the keil software, click on the Project option in the toolbar and select new uVision Project to create a new project and save it. The steps are shown in the figure below:
2. Create a new file, press the shortcut key "Ctrl+S" to name it led.c and save it, the steps are as follows:
3. Write C language program in .c file
#include "reg51.h"
sbit led=P2^0;
void main()
{
while(1)
{
led=0;
}
}
4. Click "output" in the "Option for target" selection box in the toolbar and check "Create Hex file" to ensure that the source program you write is converted to a .hex file for subsequent operations
5. Click the toolbar buttons in turn to generate the target file
The program runs successfully and the learning_002.hex file will be generated in the Object folder of the relative path
2. Proteus builds virtual simulation circuit
Three, the LED lights up
After building the circuit, click the AT89C51 main control and import the learning_002.hex file generated by the C language program in keil above
Click the run button in the lower right corner of the software, the red LED will be lit
Fourth, the LED flashes
The C language code is changed as follows:
#include "reg51.h"
unsigned int x;
sbit led=P2^0;
void main()
{
x=50000;
while(1)
{
led=0; //亮
while(x--); //延时
led=1; //灭
while(x--); //延时
}
}
Five, water lamp realization
1. Water lamp (library function method)
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp;
int x;
void main()
{
x=50000;
temp = 0x01;
P1 = temp;
while(x--); //延时
while(1)
{
temp = _crol_(temp,1); //调用库函数
P1=temp;
while(x--);
}
}
2. Flowing water lamp (left shift method)
#include <reg51.h>
unsigned int x;
//shift to the left water lamp
void main()
{
x=50000;
P1=0x01;
while(1)
{
while(x--); //delay time
P1=P1<<1; //左移
if(P1==0x00)
P1=0x01; //回到起始位置
}
}
3. Flowing water lamp (right shift method)
#include <reg51.h>
unsigned int x;
//shift to the right water lamp
void main()
{
x=50000;
P1=0x80;
while(1)
{
while(x--);
P1=P1>>1;
if(P1==0x00)
P1=0x80;
}
}
4. Running lights (array index method)
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar table[]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar p;
int x;
void main()
{
x=50000;
while(1)
{
for(p=0;p<8;p++)
{
P1=table[p];
while(x--);
}
for(p=6;p>=1;p--)
{
P1=table[p];
while(x--);
}
}
}