书上的代码
流水灯控制。当开关断开时流水灯(信号1)左移;当开关合上时流水灯(信号1)右移
左移间隔时间为1s,右移时间间隔为0.5s
源程序清单(项日一任务 2.c)
#include<stc8h.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar x=0x01;
sbit k1 = P3^2;
void delay(uint ms)
{
uint i,j;
for(j=0;j<ms;j++)
for(i=0;i<1210;i++);
}
void main(void)
{
while(1)
{
if(kl==1)
{
P1=x;
x=_crol_(x,1);
if(k1==1)
delay(1000);
}
else
{
P1=X;
x=_cror_(x,1);
delay(500);
}
}
}
STC大学计划实验箱(9.3)的 LED4、LED11~LED17 实现跑马灯控制,LED灯电路见附录4中的附图4.7所示。
(2)源程序清单(项目一任务 3.c)
#include<stc8h.h>
#include<intrins.h>
#include<gpio.h>
#define uchar unsigned char
#define uint unsigned int
uchar x=0xfe;
sbit SW18=P3^3;
sbit control= P4^0;
/*---1000ms延时函数,从STC-ISP在线编程软件延时器工具中获取(STC- Y6 指令集)----*/
void Delay1000ms()
{
unsigned char i,j,k;
_nop_();
_nop_();
i=122;
j=193;
k=128;
do
{
do
{
while(--k);
} while(--j);
} while(--i);
}
/* ---500ms延时函数,从STC-ISP在线编程软件延时器工具中获取(STC- Y6 指令集)---- */
void Delay500ms()
{
unsigned char i,j,k;
_nop_();
_nop_();
i=61;
j=225;
k=62;
do
{
do
{
while(--k);
} while(--j);
} while(--i);
}
/*-------主函数------*/
void main(void)
{
gpio(); //将1/0工作模式设置为准双向口模式
control= 0; //打开流水灯电源
while(1)
{
P6=x;
if(SW18==1)
{
x=_crol_(x,1);
Delay1000ms();
}
else
{
x=_cror_(x,1);
Delay500ms();
}
}
}
完成x与y的逻辑异或运算,运算结果可采用两种方法显示:一是直接用8位LED灯显示;二是采用LED数码管显示。本任务采用8位LED灯显示。
源程序清单(项目三任务 2.c)
#include<stc8h.h>
#include<intrins.h>
#include <gpio.h>
#define uint unsigned int
#define uchar unsigned char
#define x PO //x等效于PO口
#define y P3 //y等效于P3口
#define z P2 //z等效于P2口
void main(void)
{
gpio();
x=0xff; //P0口设置为输人状态
y=Oxff; //P3口设置为输人状态
while(1)
{
z=~(x^y); //PO口与P3口输入数据相异或并取反后,送P2口输出
}
}
用4只开关控制8只LED灯的显示,按K1键,P0端口位3、位4控制的LED灯亮;按K2键,P0端口位2、位5控制的LED灯亮;按K3键,P0端口位1、位6控制的LED灯亮;按K4键,P0端口位0、位7控制的LED灯亮;不按键,P0端口位2、位3、位4、位5控制的LED灯亮。
源程序清单(项目三任务 3.c)
#include <stc8h.h>
#include <intrins.h>
#include <gpio.h>
#define uint unsigned int
#define uchar unsigned char
#define x P0
#define y P3
sbit K1 = P3^0;//定义输入引脚
sbit K2 = P3^1;
sbit K3 = P3^2;
sbit K4 = P3^3;
void main(void)
{
gpio();
y=y|0x0f;
while(1)
{
if(!K1){x = 0xe7;} //按K1键,PO端口位3、位4控制的LED灯亮
else if(!K2){x = 0xdb;} //按K2键,PO端口位2、位5控制的LED灯亮
else if(!K3){x = 0xbd;} //按K3键,PO端口位1、位6控制的LED灯亮
else if(!K4){x = 0x7e;}//按K4键,PO端口位0、位7控制的LED灯亮
else{x = 0xc3;} //不按键,PO端口位2、位3、位4、位5控制的LED灯亮
}
}
8位LED数码管来回显示“12345678”与“87654321”,间隔时间约为800ms。
(2)显示函数源程序清单(LEDdisplay.h)。
#define font_PORT P6 //定义字形码输出端口
#define position_PORT P7 //定义位控制码输出端口
uchar code LED_SEG[] = (0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,
0xa1,0x86,0x8e,0xff,0x40,0x79,0x24,0x30,0x19,0x12d,0x02,0x78,0x00,0x10,0xbf};
//定义“0、1、2、3、4、5、6、7、8、9”“A、B、C、D、E、F”以及“灭”的字形码
//定义“0、1、2、3、4、5、6、7、8、9”(含小数点)的字符以及“-”的字形码
uchar code Scan_bit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //定义扫描位控制码
uchar data Dis_buf[]={16,16,16,16,16,16,16,0};//定义显示缓冲区,最低位显示“0”其他为“灭”
/*----------延时函数-----------*/
void Delaylms()
{
unsigned char i,j;
i=15;
j=90;
do
{
while(--j);
}while(--i);
}
/*----------显示函数------------*/
void LED_display(void)
{
uchar i;
for(i=0;i<8;i++)
{
position_PORT = 0xff;
font_PORT = LED_SEG[Dis_buf[i]];
position_PORT = Scan_bit[7-i];
Delaylms();
}
}
(3)主程序清单(项目三任务 4.c)
#include <stc8h.h> //包含支持STC8H8K64U单片机的头文件
#include <intrins.h>
#include <gpio.h>
#define uchar unsigned char
#define uint unsigned int
#include <LED_display.h>
void delayN(uint N)
{
uchar i;
for(i=0;i<N;i++)
{
LED_display(); //调显示函数,同时通过执行显示函数达到延时的目的
}
/*--------- 主函数(显示程序)---------*/
void main(void)
{
gpio();
while(1) //无限循环执行显示程序
{
Dis_buf[0]=1;Dis_buf[1]=2;Dis_buf[2]=3;Dis_buf[3]=4;
Dis_buf[4]=5;Dis_buf[5]=6;Dis_buf[6]=7;Dis_buf[7]=8;
delayN(100);
Dis_buf[0]=8;Dis_buf[1]=7;Dis_buf[2]=6;Dis_buf[3]=5;
Dis_buf[4]=4;Dis_buf[5]=3;Dis_buf[6]=2;Dis_buf[7]=1;
delayN(100);
}
}
完成z=(x+y)*C的运算。
源程序清单(项目四任务 1.c)
#include <stc8h.h>
#include <intrins.h>
#include <gpio.h>
#define uint unsigned int
#define uchar unsigned char
#define x Pl
#define y P3
#define outh P2
#define outl P0
uint data z;
uchar code C = 2;
void main(void)
{
gpio();
x = Oxff;
y = 0xff;
while(1)
{
z=(x+y)*C;
outl=z;
outh=z>>8;
}
}
5.1.1 用T1方式0实现定时,在P1.6引脚输出周期为10ms的方波。
C语言参考源程序如下:
#include <stc8h.h> //包含支持 STC8H8K64U单片机的头文件
#include <intrins.h>
#include <gpio.h> //I/0初始化文件
#define uchar unsigned char
#define uint unsigned int
void main(void)
{
gpio( ); //I/0初始化
TMOD = 0x00; //定时器初始化
TH1 = 0xec;
TL1 = 0x78;
TR1 = 1;//启动T1
while(1)
{
if(TF1==1) //判断 5ms定时是否到
{
TF1 = 0;
P16 = !P16; //5ms定时,取反输出
}
}
}
例5.1.2 用单片机定时器/计数器的定时功能,设计一个时间间隔为 1s 的流水灯电路。
源程序清单如下
#include <stc8h.h> //包含支持 STC8H8K64U 单片机的头文件
#include <intrins.h>
#include <gpio.h> //I/0初始化文件
#define uchar unsigned char
#define uint unsigned int
uchar cnt = 0;
uchar x = 0xfe;
void Timer0Init(void) //50ms@12.000MHz,从STC-ISP在线编程软件定时器/计数器工具中获得
{
AUXR & = 0x7F;//定时器时钟12T模式
TMOD & = OxF0;//设置定时器模式
TLO = 0xB0;//设置定时初值
THO = 0x3C;//设置定时初值
TFO = 0;//清除TEO标志
TRO = 1;//定时器0开始计时
}
void main(void)
{
gpio();
TimerOInit();
PO = X;
while(1)
{
if(TFO==1)
{
TFO=0;
cnt++;
if(cnt==20)
{
cnt=0;
x=_crol_(x,1);
PO=x;
}
}
}
}
STC
8X8点阵程序
#include <STC8H.H>
#include "delay.h"
#define OUTPUT P0 //阴极,列
unsigned char code string1_cat[]={0x7f,0x49,0x49,0x7f,0x49,0x49,0x7f,0x00}; //高电平点亮,汉字“田”
unsigned char code string2_cat[]={0x00,0x18,0x24,0x42,0x81,0x81,0x99,0x66}; //高电平点亮,图形“心”
unsigned char code string1_an[]={0x01,0x6d,0x6d,0x01,0x6d,0x6d,0x01,0xff}; //低电平点亮,汉字“田”
unsigned char code string2_an[]={0x8f,0x77,0x7b,0xbd,0xbd,0x7b,0x77,0x8f}; //低电平点亮,图形“心”
sbit HC595_SH_CP = P3^6; //移位寄存器时钟输入
sbit HC595_DS = P3^4; //串行输入端
sbit HC595_ST_CP = P3^5; //存储寄存器时钟输入
void SendByte_74HC595(unsigned char dt)
{
unsigned char i;
unsigned char temp = dt;
HC595_ST_CP = 0;
for(i=0;i<8;i++)
{
HC595_SH_CP = 0; //下降沿采样
HC595_DS = temp&0x80; //高位在前,低位在后
HC595_SH_CP = 1; //上升沿移出
temp <<= 1;
}
HC595_ST_CP = 1; //上升沿输出
}
/*8X8点阵显示*/
void LED_Dot_Matrix_Display(unsigned char *str)
{
unsigned char i;
unsigned char temp;
temp = 0x01;
for(i=0;i<8;i++)
{
SendByte_74HC595(temp); //扫描行row,即扫描阳极
OUTPUT = *str; //列column
delay_ms(1);
temp <<= 1;
*str++;
}
}
void main (void)
{
/*准双向口模式*/
P0M0 = 0x00;
P0M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x8f;
while(1)
{
LED_Dot_Matrix_Display(string2_an);
}
}
流水灯
#include "stc8a8k64d4.h"
void main()
{
unsigned int i=0;
unsigned char cnt=0;
P2M0=0x00; P2M1=0x00;
while(1)
{
P2=(~0x01<<cnt);//全亮
cnt++;
for(i=0;i<60000;i++);
if(cnt>8)
cnt=0;
// P2=~(0x01<<cnt);//单个
// cnt++;
// for(i=0;i<60000;i++);
// if(cnt>8)
// cnt=0;
}
}
定时器2&中断
现象嗯,led闪烁,1s一次哦
/*stc8h8k64u,T2定时器为固定的16位自动重装*/
#include "stc8h8k.h"
sbit led=P2^0;
void main()
{
P2M1=0x00; P2M0=0x00;
AUXR |= 0x04;//T2 1T模式
T2L = 0xCD;//波特率115200
T2H = 0xD4;
AUXR |= 0x10;// T2定时器开始计时
EA=1;
IE2=0x04;//T2中断
while(1);
}
void T2interrupt() interrupt 12
{
static unsigned int cnt=0;
cnt++;
if(cnt>1000)
{
cnt=0;
led=!led;
}
}
C51实现跑马灯
#include "stc8h.h" //用STC8H8K的头文件
#define Main_Fosc 1105920L //定义主时钟,11.0592MHz
typedef unsigned char u8; //定义unsigned char,unsigned int, unsigned long简写为u8,u16,u32
typedef unsigned int u16;
typedef unsigned long u32;
void delay_ms(u16 ms) //可控延时函数,最大可输入65535
{
u16 i;
do{
i = Main_Fosc / 10000;
while(--i); //10T per loop
}while(--ms);
}
void gpio() //gpio初始化为准双向口,刚开始除了P30,P31其他均为高阻态
{
P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
P1M1 = 0x00; P1M0 = 0x00; //设置为准双向口
P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
P5M1 = 0x00; P5M0 = 0x00; //设置为准双向口
P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
}
u8 str1[]={0x18,0x24,0x42,0x81}; //两种跑马灯的数组
u8 str2[]={0x81,0x42,0x24,0x18};
u8 i = 0,Key = 0; //定义数Key,选择不同跑马灯
void main()
{
gpio(); //调用gpio
P4 = 0; //先让P4口置零
P00 = 0;
while(1) //死循环,才能使灯一直亮
{
//注意:我在此处先让P00置0,然后又把P04、P05、P06置0是想用矩阵键盘的按键控制跑马灯模式
if(P04 == 0) Key = 1; //选择跑马灯模式
if(P05 == 0) Key = 2;
if(P06 == 0) Key = 3;
switch(Key)
{
case 1:P6 = ~str1[i]; //P6口整体赋值,再取反使LED亮
delay_ms(5000); //延迟
i++;
if(i == 4) i = 0;
break;
case 3:P6 = ~str2[i];
delay_ms(5000);
i++;
if(i == 4) i = 0;
break;
case 2:
P6 = ~(0x03<<i);
if(P6 == 0x7f) P6 = 0x7e;
else if(P6 == 0xfd) P6 = 0xfc;
delay_ms(5000);
i++;
if(i == 8) i = 0;
break;
}
}
}
60s倒计时
#include <STC8H.H>
#include "delay.h"
/*74HC245*/
#define OUTPUT P0
/*3——8译码器*/
sbit A0 = P2^2;
sbit A1 = P2^3;
sbit A2 = P2^4;
unsigned char second = 60; //秒计数
unsigned char count = 200; //中断200次为1秒
unsigned char code LED_7SEG_AN[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极数码管0~9
unsigned char code LED_7SEG_CAT[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0~9
void Timer0Init(void) //5毫秒@24.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xF0; //设置定时初始值
TH0 = 0xD8; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main(void)
{
/*准双向口模式*/
P0M0 = 0x00;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0xe3;
EA = 1; //总中断允许
ET0 = 1; //定时器T0中断允许
/*定时器初始化*/
Timer0Init();
while(1)
{
A0 = 1;
A1 = 0;
A2 = 0;
OUTPUT = LED_7SEG_CAT[second/10];
delay_ms(5);
A0 = 0;
A1 = 0;
A2 = 0;
OUTPUT = LED_7SEG_CAT[second%10];
delay_ms(5);
}
}
void Timer0() interrupt 1
{
count--;
if(count == 0)
{
count = 200;
second--;
if(second == 0)
{
second = 60;
}
}
}
LED灯闪烁
#include <STC8H.H>
sbit LED = P2^0;
unsigned char count = 50; //定时250ms
void Timer0Init(void) //5毫秒@24.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器16位重装载模式,系统时钟计数
TL0 = 0xF0; //设置定时初始值
TH0 = 0xD8; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main(void)
{
/*P2.0准双向口模式*/
P2M0 = 0x00;
P2M1 &= ~(0x01);
/*定时器T0初始化*/
Timer0Init();
EA = 1; //总中断允许
ET0 = 1; //定时器T0中断允许
LED = 0;
while(1);
}
void Timer0() interrupt 1
{
count--;
if(count == 0)
{
count = 50;
LED = !LED;
}
}
STC8H8K64U——点亮单个数码管
#include <STC8H.H>
#include "delay.h"
unsigned char code LED_7SEG_AN[10] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极数码管0~9
unsigned char code LED_7SEG_CAT[10] =
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0~9
/*74HC245
三态输出的八组总线收发器
OE低电平使能
DIR高电平由A到B输出
DIR低电平由B到A输出*/
#define OUT P0
/*3线——8线译码器*/
sbit A0 = P2^2;
sbit A1 = P2^3;
sbit A2 = P2^4;
void main(void)
{
/*准双向口模式*/
P0M0 = 0x00;
P0M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0xe3;
while(1)
{
unsigned char i;
A2 = 0; A1 = 0; A0 = 0;
for(i=0;i<10;i++)
{
OUT = LED_7SEG_CAT[i];
delay_ms(250);
}
}
}
按键按下加一
按键每按一下,Key_Flage 加一,使数码管显示的数字加一(从0到9循环)
#include <STC8H.H>
#include "delay.h"
#define OUTPUT P0
sbit KEY = P3^3;
unsigned char Key_Flage = 0; //按键按下标志
unsigned char code LED_7SEG_AN[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极数码管0~9
unsigned char code LED_7SEG_CAT[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0~9
void main(void)
{
/*P3.3 准双向口模式*/
P3M0 = 0x00;
P3M1 &= ~(1<<3);
/*P0 准双向口模式*/
P0M0 = 0x00;
P0M1 = 0x00;
while(1)
{
KEY = 1; //预先置一
if(KEY == 0)
{
delay_ms(20);
if(KEY == 0) //检测低电平
{
while(KEY == 0);//判断按键松开
delay_ms(20);
Key_Flage++;
}
}
if(Key_Flage == 10)
Key_Flage = 0;
OUTPUT = LED_7SEG_CAT[Key_Flage];
}
}
八个LED灯依次点亮
/*
频率:100MHz
Q0~Q7并行三态输出端
OE 输出使能端,高电平时,移位寄存器清零,Q0~Q7处于高阻态。
MR 主复位,低电平时,移位寄存器清零。
Q7' 串行出端
OE接低电平,MR接高电平。
*/
#include <STC8H.H>
#include "delay.h"
unsigned char code LED[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
sbit HC595_SH_CP = P3^6; //移位寄存器时钟输入
sbit HC595_DS = P3^4; //串行输入端
sbit HC595_ST_CP = P3^5; //存储寄存器时钟输入
void SendByte_74HC595(unsigned char dt)
{
unsigned char i;
unsigned char temp = dt;
HC595_SH_CP = 1;
for(i=0;i<8;i++)
{
HC595_SH_CP = 0; //下降沿采样
HC595_DS = temp&0x80; //高位在前,低位在后
HC595_SH_CP = 1; //上升沿移出
temp <<= 1;
}
HC595_ST_CP = 1; //上升沿输出
}
void main(void)
{
/*准双向口*/
P3M0 = 0x00;
P3M1 = 0x8f;
while(1)
{
unsigned char i;
for(i=0;i<8;i++)
{
SendByte_74HC595(~LED[i]); //灌电流,低电平点亮。
delay_ms(250);
}
}
}
独立键盘扫描
设8个独立按键,每个按键按下后,分别显示数字1~9。
按键松开后,仍显原数字。
#include <STC8H.H>
#define OUTPUT P0
#define INPUT P1
/*3——8译码器*/
sbit A0 = P2^2;
sbit A1 = P2^3;
sbit A2 = P2^4;
unsigned char Key_Flage = 0; //按键按下标志
unsigned char code LED_7SEG_AN[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极数码管0~9
unsigned char code LED_7SEG_CAT[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0~9
void key_scan (void)
{
unsigned char KeyValue;
do
{
INPUT = 0xff; //预先置一
KeyValue = INPUT; //读取P3口数据
KeyValue = ~KeyValue;
switch(KeyValue)
{
case 1: Key_Flage = 1; break;
case 2: Key_Flage = 2; break;
case 4: Key_Flage = 3; break;
case 8: Key_Flage = 4; break;
case 16: Key_Flage = 5; break;
case 32: Key_Flage = 6; break;
case 64: Key_Flage = 7; break;
case 128: Key_Flage = 8; break;
default: Key_Flage = 0; break;
}
}while(Key_Flage == 0);
}
void main (void)
{
/*P1 准双向口模式*/
P1M0 = 0x00;
P1M1 = 0x00;
/*P2 准双向口模式*/
P2M0 = 0x00;
P2M1 = 0x00;
/*P0 准双向口模式*/
P0M0 = 0x00;
P0M1 = 0x00;
A0 = 0;
A1 = 0;
A2 = 0;
while(1)
{
key_scan();
OUTPUT = LED_7SEG_CAT[Key_Flage];
}
}
点亮数码管
#include <STC8h.H>
#define uint unsigned int
#define uchar unsigned char
//数码管段选
unsigned char NixieTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x40};
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void GpioInit(void)
{
P0M0 = 0x00; P0M1 = 0x00;
P1M0 = 0x00; P1M1 = 0x00;
P2M0 = 0x00; P2M1 = 0x00;
P3M0 = 0x00; P3M1 = 0x00;
P4M0 = 0x00; P4M1 = 0x00;
P5M0 = 0x00; P5M1 = 0x00;
}
void Nixie(unsigned char Location,Number)
{
switch(Location)
{
case 1:P10=1;P11=0;P13=0;P14=0;break;
case 2:P10=0;P11=1;P13=0;P14=0;break;
case 3:P10=0;P11=0;P13=1;P14=0;break;
case 4:P10=0;P11=0;P13=0;P14=1;break;
}
P2=NixieTable[Number]; //段码输出
Delay(10); //显示一段时间
P2=0x00; //消影
}
void main()
{
GpioInit(); /*通用I/O口初始化*/
while(1)
{
Nixie(1,1); //在数码管的第1位置显示1
Nixie(2,2); //在数码管的第2位置显示2
Nixie(3,3); //在数码管的第3位置显示3
Nixie(4,4); //在数码管的第4位置显示4
}
}
其它代码
实例1:用单片机控制第一个灯亮
#include<reg51.h> //包含51单片机寄存器定义的头文件
voidmain(void)
{
P1=0xfe; //P1=1111 1110B,即P1.0输出低电平
}
LED闪烁
#include <reg51.h>sbit LED=P1^0;
void delay(int i)
{
while(i--);
}
void main(void)
{
LED = 1;
while(1)
{
delay(1000);
LED = !LED;
}
}
按键控制LED的亮灭
#include <reg51.h>sbit K1=P3^0;
sbit L1=P1^0;
void main(void)
{
while(1)
{
if(K1==0)
{
L1=0; //灯亮
}
else
{
L1=1; //灯灭
}
}
}
多按键控制多LED亮灭
#include <reg51.H>
unsigned char temp;
void main(void)
{
while(1)
{
temp=P1>>4;
temp=temp | 0xf0;
P1=temp;
}
}
流水灯
#include <reg51.H>unsigned char i;
unsigned char temp;
unsigned char a,b;
void delay(void)
{
unsigned char m,n,s;
for(m=20;m>0;m--)
for(n=20;n>0;n--)
for(s=248;s>0;s--);
}
void main(void)
{
while(1)
{
temp=0xfe;
P1=temp;
delay();
for(i=1;i<8;i++)
{
a=temp<<i;
b=temp>>(8-i);
P1=a|b;
delay();
}
for(i=1;i<8;i++)
{
a=temp>>i;
b=temp<<(8-i);
P1=a|b;
delay();
}
}
}
花样灯
#include <reg51.H>unsigned char code table[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f,
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe,
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe,
0x00,0xff,0x00,0xff,
0x01};
unsigned char i;
void delay(void)
{
unsigned char m,n,s;
for(m=20;m>0;m--)
for(n=20;n>0;n--)
for(s=248;s>0;s--);
}
void main(void)
{
while(1)
{
if(table[i]!=0x01)
{
P1=table[i];
i++;
delay();
}
else
{
i=0;
}
}
}
扬声器发声
#include <reg51.H>
#include <INTRINS.H>
bit flag;
sbit P1_7=P1^7;
sbit P1_0=P1^0;
unsigned char count;
void dely500(void)
{
unsigned char i;
for(i=250;i>0;i--)
{
_nop_();
}
}
void main(void)
{
while(1)
{
if(P1_7==0)
{
for(count=200;count>0;count--)
{
P1_0=~P1_0;
dely500();
}
for(count=200;count>0;count--)
{
P1_0=~P1_0;
dely500();
dely500();
}
}
}
}
静态数码管循环显示0-9
#include <reg51.H>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char dispcount;
void delay02s(void)
{
unsigned char i,j,k;
for(i=20;i>0;i--)
for(j=20;j>0;j--)
for(k=248;k>0;k--);
}
void main(void)
{
while(1)
{
for(dispcount=0;dispcount<10;dispcount++)
{
P0=table[dispcount];
delay02s();
}
}
}
键控花样灯(按一下改变一个状态)
#include <reg51.H>
sbit P3_7=P3^7;
unsigned char count;
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main(void)
{
while(1)
{
if(P3_7==0)
{
delay10ms();
if(P3_7==0)
{
count++;
if(count==16)
{
count=0;
}
P1=~count;
while(P3_7==0);
}
}
}
}
键控花样灯(2)
#include <reg51.H>
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P3_7=P3^7;
unsigned char ID;
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void delay02s(void)
{
unsigned char i;
for(i=20;i>0;i--)
{delay10ms();
}
}
void main(void)
{ while(1)
{ if(P3_7==0)
{delay10ms();
if(P3_7==0)
{
ID++;
if(ID==4)
{
ID=0;
}
while(P3_7==0);
}
}
switch(ID)
{ case 0:
P1_0=~P1_0;
delay02s();
break;
case 1:
P1_1=~P1_1;
delay02s();
break;
case 2:
P1_2=~P1_2;
delay02s();
break;
case 3:
P1_3=~P1_3;
delay02s();
break;
}
}
}
键控花样灯(3)
#include <reg51.H>
sbit P3_7=P3^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Count;
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main(void)
{
Count=0;
P0=table[Count/10];
P2=table[Count%10];
while(1)
{
if(P3_7==0)
{
delay10ms();
if(P3_7==0)
{
Count++;
if(Count==100)
{
Count=0;
}
P0=table[Count/10];
P2=table[Count%10];
while(P3_7==0);
}
}
}
}
静态数码管计数(0-99)
#include <reg51.H>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Second;
void delay1s(void)
{
unsigned char i,j,k;
for(k=100;k>0;k--)
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main(void)
{
Second=0;
P0=table[Second/10];
P2=table[Second%10];
while(1)
{
delay1s();
Second++;
if(Second==60)
{
Second=0;
}
P0=table[Second/10];
P2=table[Second%10];
}
}
增减计数器
#include <reg51.H>
sbit P3_6=P3^6;
sbit P3_7=P3^7;
unsigned char curcount;
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main(void)
{
curcount=P3 & 0x0f;
P1=~curcount;
while(1)
{
if(P3_6==0)
{
delay10ms();
if(P3_6==0)
{
if(curcount>=15)
{
curcount=15;
}
else
{
curcount++;
}
P1=~curcount;
while(P3_6==0);
}
}
if(P3_7==0)
{
delay10ms();
if(P3_7==0)
{
if(curcount<=0)
{
curcount=0;
}
else
{
curcount--;
}
P1=~curcount;
while(P3_7==0);
}
}
}
}
清风入人间
点亮第一个LED
#include <REGX52.H> //右键添加头文件
void main()
{ //2进制转16进制,前缀为 0x 87654321 8个灯序号对应进制
// 8个1分别为8个led,0则是负极点亮。最后一个0是D1,第一个是D8
P2 = 0xFE; //1111 1110 点亮第1个led
P2 = 0x7F; //0111 1111 点亮第8个led
P2 = 0x5F; //0101 1111 led 6,8亮
P2 = 0xAA; //1010 1010 led 1,3,5,7亮
P2 = 0x55; //0101 0101 led 2,4,6,8亮
while (1) //程序一直运行
{
}
}
LED闪烁
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz 500ms延迟函数
{
unsigned char i, j, k;
_nop_(); //去掉nop不用加头文件#include <INTRINS.H>
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2 = 0xFE;
Delay500ms(); //延迟500ms
P2 = 0xFF;
Delay500ms();
}
}
LED流水灯
#include <REGX52.H>
void Delay500ms() //@11.0592MHz 延迟500ms
{
unsigned char i, j, k;
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2 = 0xFE; //1111 1110
Delay500ms();
P2 = 0xFD; //1111 1101
Delay500ms();
P2 = 0xFB; //1111 1011
Delay500ms();
P2 = 0xF7; //1111 0111
Delay500ms();
P2 = 0xEF; //1110 1111
Delay500ms();
P2 = 0xDF; //1101 1111
Delay500ms();
P2 = 0xBF; //1011 1111
Delay500ms();
P2 = 0x7F; //0111 1111
Delay500ms();
}
}
LED流水灯Plus
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{ //xms 为延迟秒数
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
P2 = 0xFE; //1111 1110
Delay1ms(500); //延迟500ms
P2 = 0xFD; //1111 1101
Delay1ms(100); //100ms
P2 = 0xFB; //1111 1011
Delay1ms(1000); //1000ms
P2 = 0xF7; //1111 0111
Delay1ms(500);
P2 = 0xEF; //1110 1111
Delay1ms(200);
P2 = 0xDF; //1101 1111
Delay1ms(500);
P2 = 0xBF; //1011 1111
Delay1ms(900);
P2 = 0x7F; //0111 1111
Delay1ms(500);
}
Delay1ms(500);
}
独立按键控制LED灯灭
#include <REGX52.H>
//P2_0就是单片机上面的一个端口,这个端口就是链接右边第一个led灯的
//等于左边七个LED直接不给信号了,只给右边第一个一个亮的信号
void main()
{
//P2 = 0xFE;
P2_0 = 1;
while(1)
{ // 按下亮,松开灭
if(P3_1==0) //P3_1控制第一个按键,即P31
{
P2_0 = 0; //P2_0是第一个LED,0则亮
}
else
{
P2_0=1;
}
if (P3_0==0)
{
P2_1 = 0;
}
else
{
P2_1 = 1;
}
}
}
独立按键控制LED状态
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{ //按一次亮,再按一次灭
while(1)
{
if(P3_1==0)
{
Delay1ms(20); //按下时消抖
while(P3_1==0); //松开跳出while循环,不松开一直循环,所以灯不会有反应
Delay1ms(20); //松开时消抖
P2_0=~P2_0; //取反,灭变亮,亮变灭
}
}
}
独立按键控制LED显示二进制
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char LEDNum = 0;
while(1)
{
if(P3_1==0)
{
Delay1ms(20);
while(P3_1==0)
Delay1ms(20);
LEDNum++; //0000 0001
P2 = ~LEDNum; //取反, 1111 1110,第一个LED灯亮
}
}
}
独立按键控制LED移位
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
unsigned char LEDNum = 0;
P2 = ~0x01; //LED初始化,不然第一次led1不亮
while(1)
{
//按键1右移
if(P3_1==0) //按键1按下
{
Delay1ms(20);
while(P3_1==0);
Delay1ms(20);
LEDNum++; //右移
if (LEDNum>=8)
LEDNum=0; //重置
P2=~(0x01<<LEDNum);
}
//按键2左移
if(P3_0==0) //按键1按下
{
Delay1ms(20);
while(P3_0==0);
Delay1ms(20);
if(LEDNum==0)
LEDNum=7; //重置
else
LEDNum--; //左移
P2=~(0x01<<LEDNum);
}
}
}
静态数码管显示
#include <REGX52.H>
void main()
{
//100,即4,LED4亮
P2_4 = 1;
P2_3 = 0;
P2_2 = 0;
//P07->P00 0000 0111 ,即0x07,
//a、b、c为1,即a、b、c亮,显示为7
P0 = 0x07;
while(1)
{
}
}
模块化
#include <REGX52.H>
//显示数字的段码存储在数组中
//0~9 -> 0~9
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
void Nixie(unsigned char Location,Number)
{ //Location为LED灯序号,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(6,2); //第6个LED显示2
while(1)
{
}
}
动态数码管显示
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};
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]; //段选
Delay1ms(1);
P0=0x00; //清零
}
void main()
{
while(1)
{
Nixie(1,1);
//Delay1ms(500); //此延迟用于从第一个led1依次显示数字
Nixie(2,2);
//Delay1ms(500);
Nixie(3,3);
//Delay1ms(500);
Nixie(4,4);
// Delay1ms(500);
Nixie(5,5);
// Delay1ms(500);
Nixie(6,6);
// Delay1ms(500);
Nixie(7,7);
// Delay1ms(500);
Nixie(8,8);
// Delay1ms(500);
}
}