前言:
本文中的所有代码都是测试过能正常实现功能的代码,并且进行了详细注释,如果大家在学习过程中遇到代码怎么改都无法实现功能,可以根据本文的代码对照哪里出现了问题,或者对哪句代码的理解有困难,也可以查看我标注的注释,基本是每一句都有。
2-1 点亮一个LED
#include <REGX52.H>
void main()
{
P2=0xFE;//1111 1110点亮第一个LED
while(1)
{
}
}
2-2 LED闪烁
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //添加延时函数
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;//点亮D1
Delay500ms();//延时500ms
P2=0XFF;//熄灭D1
Delay500ms();//延时500ms
}
}
2-3LED流水灯
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //添加延时函数
{
unsigned char i, j, k;
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0XFE;//1111 1110点亮D1
Delay500ms();//延时500ms
P2=0XFD;//1111 1101点亮D2
Delay500ms();
P2=0XFB;//1111 1011点亮D3
Delay500ms();
P2=0XF7;//1111 0111点亮D4
Delay500ms();
P2=0XEF;//1110 1111点亮D5
Delay500ms();
P2=0XDF;//1101 1111点亮D6
Delay500ms();
P2=0XBF;//1011 1111点亮D7
Delay500ms();
P2=0X7F;//0111 1111点亮D8
Delay500ms();
}
}
3-1独立按键控制LED亮灭
#include <REGX52.H>
void main()
{
while(1)
{
if(P3_1==0)//检测按键是否按下
{
P2_0=0;//若按下,则点亮D1
}
else
{
P2_0=1;//若未按下,则熄灭D1
}
}
}
3-2 独立按键控制LED状态
include <REGX52.H>
void Delay(unsigned int xms) //引入延时函数
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
if(P3_1==0)//检测K1是否按下
{
Delay(20);//延时20ms对按键消抖
while(P3_1==0);//消抖操作
Delay(20);//延时20ms对按键消抖
P2_0=~P2_0;//对P2_0取反(若本来是0则变成1,本来是1则变成0)
}
}
}
3-3独立按键控制LED显示二进制
#include <REGX52.H>
void Delay(xms) //引入延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void main()
{
while(1)
{
if(P3_1==0)//检测K2是否按下
{
Delay(20);//消抖操作
while(P3_1==0);//消抖操作
Delay(20);//消抖操作
P2--;//P2减一(退格操作)
}
if(P3_0==0)//检测K1是否按下
{
Delay(20);//消抖操作
while(P3_0==0);//消抖操作
Delay(20);//消抖操作
P2++;//P2加一
}
}
}
3-4 独立按键控制LED移位
#include <REGX52.H>
void Delay(xms) //引入延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
int LEDnum=0;//定义位数变量
void main()
{
P2=~0X01;//令P2=1111 1110点亮D1
while(1){
if(P3_1==0)//检测K1是否按下
{
Delay(20);//消抖操作
while(P3_1==0);//检测松手
Delay(20);//消抖操作
LEDnum++;//位数变量加一
if(LEDnum>=8)//检测位数变量是否超过或等于8(避免越界操作)
LEDnum=0;//将位数变量清零
P2=~(0x01<<LEDnum);//对P2左移LEDnum位
}
if(P3_0==0)//检测K2是否按下
{
Delay(20);//消抖操作
while(P3_0==0);//检测松手
Delay(20);//消抖操作
LEDnum--;//位数变量
if(LEDnum<=-8)//检测位数变量是否小于或等于-8(避免越界操作)
LEDnum=0;//将位数变量清零
P2=~(0x01<<LEDnum);//对P2右移LEDnum的绝对值位
}
}
}
4-1 静态数码管显示
#include <REGX52.H>
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0X07,0X7F,0X6F};//用数组表示数字
void Nixie(unsigned char Location, Number)//定义数码管函数,参数为位置和数字
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;//选中第一位数码管
case 2:P2_4=1;P2_3=1;P2_2=0;break;//选中第二位数码管
case 3:P2_4=1;P2_3=0;P2_2=1;break;//选中第三位数码管
case 4:P2_4=1;P2_3=0;P2_2=0;break;//选中第四位数码管
case 5:P2_4=0;P2_3=1;P2_2=1;break;//选中第五位数码管
case 6:P2_4=0;P2_3=1;P2_2=0;break;//选中第六位数码管
case 7:P2_4=0;P2_3=0;P2_2=1;break;//选中第七位数码管
case 8:P2_4=0;P2_3=0;P2_2=0;break;//选中第八位数码管
}
P0=NixieTable[Number];//确定数字
}
void main()
{
Nixie(2,3 );//在第二位数码管显示数字3
while(1)
{
}
}
4-2动态数码管显示
#include <REGX52.H>
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0X07,0X7F,0X6F};//引入数字数组用于显示数字
void Delay(unsigned int xms) //引入延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Nixie(unsigned char Location, Number)//引入数码管函数,参数为位置和数字
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;//选中第一位数码管
case 2:P2_4=1;P2_3=1;P2_2=0;break;//选中第二位数码管
case 3:P2_4=1;P2_3=0;P2_2=1;break;//选中第三位数码管
case 4:P2_4=1;P2_3=0;P2_2=0;break;//选中第四位数码管
case 5:P2_4=0;P2_3=1;P2_2=1;break;//选中第五位数码管
case 6:P2_4=0;P2_3=1;P2_2=0;break;//选中第六位数码管
case 7:P2_4=0;P2_3=0;P2_2=1;break;//选中第七位数码管
case 8:P2_4=0;P2_3=0;P2_2=0;break;//选中第八位数码管
}
P0=NixieTable[Number];//显示数字
Delay(1);//消除显影操作
P0=0x00;//消除显影操作
}
void main()
{
while(1)
{
Nixie(1,5);//在数码管第一位显示数字5
Nixie(2,2);//在数码管第二位显示数字2
Nixie(3,0);//在数码管第三位显示数字0
Nixie(4,1);//在数码管第四位显示数字1
Nixie(5,3);//在数码管第五位显示数字3
Nixie(6,1);//在数码管第六位显示数字1
Nixie(7,4);//在数码管第七位显示数字5
}
}
5-1模块化编程
main.c
#include <REGX52.H>
#include "Delay.h"//引入头文件Delay.h
#include "Nixie.h"//引入数码管头文件Nixie.h
void main()
{
while(1)
{
Nixie(1,5);//在第一位数码管显示数字5
Nixie(2,2);//在第二位数码管显示数字2
Nixie(3,0);//在第三位数码管显示数字0
Nixie(4,1);//在第四位数码管显示数字1
Nixie(5,3);//在第五位数码管显示数字3
Nixie(6,1);//在第六位数码管显示数字1
Nixie(7,4);//在第七位数码管显示数字4
}
}
Delay.h
#ifndef __DELAY_H__//如果未定义DELAY_H
#define __DELAY_H__//定义DELAY_H
void Delay(unsigned int xms);//引入Delay函数
#endif //结束预处理
Nixie.h
#ifndef __NIXIE_H__//如果未定义NIXIE_H
#define __NIXIE_H__//定义NIXIE_H
void Nixie(unsigned char Location, Number);//引入Nixie函数
#endif //结束预处理
Delay.c
void Delay(unsigned int xms) //延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
Nixie.c
#include <REGX52.H>
#include "Delay.h"
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0X07,0X7F,0X6F};//引入数字数组用于显示数字
void Delay(unsigned int xms) //引入延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Nixie(unsigned char Location, Number)//引入数码管函数,参数为位置和数字
{
switch(Location)
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;//选中第一位数码管
case 2:P2_4=1;P2_3=1;P2_2=0;break;//选中第二位数码管
case 3:P2_4=1;P2_3=0;P2_2=1;break;//选中第三位数码管
case 4:P2_4=1;P2_3=0;P2_2=0;break;//选中第四位数码管
case 5:P2_4=0;P2_3=1;P2_2=1;break;//选中第五位数码管
case 6:P2_4=0;P2_3=1;P2_2=0;break;//选中第六位数码管
case 7:P2_4=0;P2_3=0;P2_2=1;break;//选中第七位数码管
case 8:P2_4=0;P2_3=0;P2_2=0;break;//选中第八位数码管
}
P0=NixieTable[Number];//显示数字
Delay(1);//消除显影操作
P0=0x00;//消除显影操作
}
void main()
{
while(1)
{
Nixie(1,5);//在数码管第一位显示数字5
Nixie(2,2);//在数码管第二位显示数字2
Nixie(3,0);//在数码管第三位显示数字0
Nixie(4,1);//在数码管第四位显示数字1
Nixie(5,3);//在数码管第五位显示数字3
Nixie(6,1);//在数码管第六位显示数字1
Nixie(7,4);//在数码管第七位显示数字5
}
}
5-2 LCD1602调试工具
LCD1602.h和LCD102.c在文件夹课程及程序源码中,由于篇幅过长就不放出来了,找不到的朋友可以私信
main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
int Result;
void main()
{
LCD_Init();//初始化LCd
LCD_ShowString(1,3,"I miss you ");//在LCD第一行第三列显示字符串I miss you
while(1)
{
}
}