文章目錄
- 一、Keil創建專案
- 二、Proteus搭建虛擬仿真電路
- 三、LED點亮
- 四、LED閃爍
- 五、流水燈實作
- 1. 流水燈(庫函式法)
- 2. 流水燈(左移法)
- 3. 流水燈(右移法)
- 4. 流水燈(陣列索引法)
一、Keil創建專案
1. 打開keil軟體,在工具列點擊Project選項選擇new uVision Project創建新的工程并保存,步驟如下圖所示:



2. 創建新的檔案,按快捷鍵“Ctrl+S”命名為led.c并保存,步驟如下:



3. 在.c檔案中撰寫C語言程式
#include "reg51.h"
sbit led=P2^0;
void main()
{
while(1)
{
led=0;
}
}

4. 依次點擊工具列中的"Option for target"選擇框中的"output ",勾選“Create Hex file ”,確保自己撰寫的源程式轉換為.hex檔案,為后續操作使用

5. 依次點擊工具列按鈕,生成目標檔案

程式運行成功,將在相對路徑Object檔案夾中生成learning_002.hex檔案
二、Proteus搭建虛擬仿真電路

三、LED點亮
搭建好電路后,點擊AT89C51主控,匯入上文用keil中C語言程式生成的learning_002.hex檔案

點擊軟體右下角的運行按鈕,紅色的發光二級管被點亮

四、LED閃爍
C語言代碼改為如下:
#include "reg51.h"
unsigned int x;
sbit led=P2^0;
void main()
{
x=50000;
while(1)
{
led=0; //亮
while(x--); //延時
led=1; //滅
while(x--); //延時
}
}

五、流水燈實作
1. 流水燈(庫函式法)
#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. 流水燈(左移法)
#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. 流水燈(右移法)
#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. 流水燈(陣列索引法)
#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--);
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57502.html
標籤:其他
