記事ディレクトリ
1つは、Keilがプロジェクトを作成する
1. keilソフトウェアを開き、ツールバーの[プロジェクト]オプションをクリックして、新しいuVisionプロジェクトを選択し、新しいプロジェクトを作成して保存します以下の図に手順を示します。
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.ツールバーボタンを順番にクリックして、ターゲットファイルを生成します。
プログラムが正常に実行され、learning_002.hexファイルが相対パスのObjectフォルダーに生成されます
2.プロテウスが仮想シミュレーション回路を構築
3、LEDが点灯する
回路を構築した後、AT89C51メインコントロールをクリックし、C言語プログラムによって生成されたlearning_002.hexファイルを上記のkeilにインポートします。
ソフトウェアの右下隅にある実行ボタンをクリックすると、赤いLEDが点灯します
4番目に、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--); //延时
}
}
5、水ランプの実現
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--);
}
}
}