根据单片机100案例所做,适合初学者入门单片机的简单案例.
目录
-
-
- 单片机案例1
-
- 单片机案例2
-
- 单片机案例3
-
- 单片机案例4
-
- 单片机案例5
-
- 单片机案例6
-
- 单片机案例7
-
- 单片机案例8
-
- 单片机案例9
-
- 单片机案例10
-
单片机案例1
结合proteus和keil
1 闪烁的LED灯
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
sbit LED=P2^0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--);
for(t=0;t<120;t++);
}
void main()
{
while(1)
{
LED=~LED;
delay_ms(150);
}
}
单片机案例2
结合proteus和keil
2 双向流水灯
原理图
程序
#include
#include
#define INT8U unsigned char
#define INT16U unsigned int
void delsy_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void main()
{
INT8U i;
P2=0x01;
delsy_ms(150);
while(1)
{
for(i=0;i<7;i++)
{
P2=_crol_(P2,1);
delsy_ms(60);
}
for(i=0;i<7;i++)
{
P2=_cror_(P2,1);
delsy_ms(200);
}
}
}
单片机案例3
结合proteus和keil
3 变化多样的流水灯
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
code INT16U array[]=
{
0xFCFF,0xF9FF,0xF3FF,0xE7FF,0xCFFF,0x9FFF,0x3FFF,0x7FFE,0xFFFC,
0xFFF9,0xFFF3,0xFFE7,0xFFCF,0xFF9F,0xFF3F,0xFFFF,0xE7E7,0xDBDB,
0xBDBD,0x7E7E,0xBDBD,0xDBDB,0xE7E7,0xFFFF,0xE7E7,0xC3C3,0x8181,
0x0000,0x8181,0xC3C3,0xE7E7,0xFFFF,0xAAAA,0x5555,0x1818,0xFFFF,
0xF0F0,0x0F0F,0x0000,0xFFFF,0xF8F8,0xF1F1,0xE3E3,0xC7C7,0x8F8F,
0x1F1F,0x3F3F,0x7F7F,0x7F7F,0x3F3F,0x1F1F,0x8F8F,0xC7C7,0xE3E3,
0xF1F1,0xF8F8,0xFFFF,0x0000,0x0000,0xFFFF,0xFFFF,0x0F0F,0xF0F0,
0xFEFF,0xFDFF,0xFBFF,0xE7FF,0xEFFF,0xDFFF,0xBFFF,0x7FFF,0xFFFE,
0xFFFD,0xFFFB,0xFFF7,0xFFEF,0xFFDF,0xFFBF,0xFF7F,0xFF7F,0xFFBF,
0xFFDF,0xFFEF,0xFFF7,0xFFFB,0xFFFD,0xFFFE,0x7FFF,0xBFFF,0xDFFF,
0xEFFF,0xF7FF,0xFBFF,0xFDFF,0xFEFF,0xFEFF,0xFCFF,0xF8FF,0xF0FF,
0xE0FF,0xC0FF,0x80FF,0x00FF,0x00FE,0x00FC,0x00F8,0x00F0,0x00E0,
0x00C0,0x0080,0x0000,0x0000,0x0080,0x00C0,0x00E0,0x00F0,0x00F8,
0x00FC,0x00FE,0x00FF,0x80FF,0xC0FF,0xE0FF,0xF0FF,0xF8FF,0xFCFF,
0xFEFF,0x0000,0xFFFF,0x0000,0xFFFF,0x0000,0xFFFF,0x0000,0xFFFF
};
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void main()
{
INT8U i;
while(1)
{
for(i=0;i<136;i++)
{
P0=array[i]>>8;
P2=array[i];
delay_ms(60);
}
}
}
单片机案例4
结合proteus和keil
4 LED模拟交通信号灯
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
sbit RED_EW=P0^0;
sbit YELLOW_EW=P0^1;
sbit GREEN_EW=P0^2;
sbit RED_SN=P0^3;
sbit YELLOW_SN=P0^4;
sbit GREEN_SN=P0^5;
INT8U Flash_Count=0;
INT8U type=1;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void traffic_light()
{
switch(type)
{
case 1:
{
RED_EW=1;
YELLOW_EW=1;
GREEN_EW=0;
RED_SN=0;
YELLOW_SN=1;
GREEN_SN=1;
delay_ms(2000);
type=2;
break;
}
case 2:
{
delay_ms(500);
GREEN_EW=1;
YELLOW_EW=~YELLOW_EW;
if(++Flash_Count!=10)
return;
Flash_Count=0;
type=3;
break;
}
case 3:
{
RED_EW=0;
YELLOW_EW=1;
GREEN_EW=1;
RED_SN=1;
YELLOW_SN=1;
GREEN_SN=0;
delay_ms(2000);
type=4;
break;
}
case 4:
{
delay_ms(500);
YELLOW_SN=~YELLOW_SN;
GREEN_SN=1;
if(++Flash_Count!=10)
return;
Flash_Count=0;
type=1;
break;
}
}
}
void main()
{
while(1)
{
traffic_light();
}
}
单片机案例5
结合proteus和keil
5 单只数码管(共阴极)显示数字
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
code INT8U CC_Table[]=
{
0x3F,0x06,
0x5B,0x4F,
0x66,0x6D,
0x7D,0x07,
0x7F,0x6F
};
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void main()
{
INT8U i=0;
while(1)
{
P0=CC_Table[i];
i=(i+1)%10;
delay_ms(200);
}
}
单片机案例6
结合proteus和keil
6 集成式数码管动态扫描显示数字
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90
};
code INT8U array[]=
{
2,0,2,2,
0,2,
0,9
};
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void main()
{
INT8U i;
while(1)
{
for(i=0;i<8;i++)
{
P0=0xFF;
P2=1<<i;
P0=CA_Table[array[i]];
delay_ms(5);
}
}
}
单片机案例7
结合proteus和keil
7 按键控制数码管显示
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
sbit K1=P3^4;
sbit K2=P3^5;
sbit K3=P3^6;
sbit K4=P3^7;
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90
};
INT16U num=2022;
INT16U temp;
INT8U array[]={0,0,0,0};
INT8U isAdjust=0;
INT8U preKey=0xF0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void resolve(INT16U n)
{
array[0]=0;
while(n>=1000)
{
array[0]++;
n-=1000;
}
array[1]=0;
while(n>=100)
{
array[1]++;
n-=100;
}
array[2]=0;
while(n>=10)
{
array[2]++;
n-=10;
}
array[3]=n;
}
void key_handle()
{
P3|=0xF0;
if(preKey==(P3&0xF0))
return;
preKey=P3&0xF0;
if(!K1)
{
delay_ms(10);
if(!K1)
{
if(temp==0)
temp=num;
num++;
isAdjust=1;
}
}
if(!K2)
{
delay_ms(10);
if(!K2)
{
if(temp==0)
temp=num;
num--;
isAdjust=1;
}
}
if(!K3)
{
delay_ms(10);
if(!K3)
{
isAdjust=0;
temp=0;
}
}
if(!K4)
{
delay_ms(10);
if(!K4)
{
if(temp)
num=temp;
temp=0;
isAdjust=0;
}
}
}
void main()
{
INT8U i;
INT16U t;
while(1)
{
for(t=0;t<35;t++)
{
resolve(num);
for(i=0;i<4;i++)
{
P0=0xFF;
P3=(P3&0xF0)|(1<<i);
P0=CA_Table[array[i]];
delay_ms(2);
}
key_handle();
}
if(isAdjust)
{
P0=0xFF;
for(t=0;t<15;t++)
{
delay_ms(10);
key_handle();
}
}
}
}
单片机案例8
结合proteus和keil
8 4乘4按键显示
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90,
0x88,0x83,
0xC6,0xA1,
0x86,0x8E,
0xFF
};
sbit BEEP=P3^0;
INT8U preKey=0xFF;
INT8U curKey=0xFF;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void key_scan()
{
P1=0x0F;
delay_ms(1);
if(P1==0x0F)
{
curKey=0xFF;
return;
}
switch(P1)
{
case 0x0E:
{
curKey=0;
break;
}
case 0x0D:
{
curKey=1;
break;
}
case 0x0B:
{
curKey=2;
break;
}
case 0x07:
{
curKey=3;
break;
}
default:
{
curKey=0xFF;
return;
}
}
P1=0xF0;
delay_ms(1);
switch(P1)
{
case 0xE0:
{
curKey+=0;
break;
}
case 0xD0:
{
curKey+=4;
break;
}
case 0xB0:
{
curKey+=8;
break;
}
case 0x70:
{
curKey+=12;
break;
}
default:
{
curKey=0xFF;
}
}
}
void beep()
{
INT8U i;
for(i=0;i<100;i++)
{
delay_ms(1);
BEEP=~BEEP;
}
BEEP=1;
}
void main()
{
P0=0xFF;
while(1)
{
key_scan();
if(curKey==0xFF)
{
delay_ms(10);
continue;
}
P0=CA_Table[curKey];
beep();
while(curKey!=0xFF)
{
key_scan();
}
}
}
单片机案例9
结合proteus和keil
9 普通开关和拨码开关(报警器)
原理图
程序
#include
#include
#define INT8U unsigned char
#define INT16U unsigned int
code INT8U CC_Table[]=
{
0x3F,0x06,
0x5B,0x4F,
0x66,0x6D,
0x7D,0x07,
0x7F,0x6F
};
INT8U show_buff[]={0,0,0};
sbit BEEP=P3^3;
sbit SW1=P3^0;
sbit SW2=P3^6;
sbit CE=P2^0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void alarm(INT8U t)
{
INT8U i,j;
for(i=0;i<150;i++)
{
BEEP=~BEEP;
for(j=0;j<t;j++);
}
}
void main()
{
INT8U i;
CE=0;
while(1)
{
if(SW1)
{
CE=1;
}
else
{
CE=0;
show_buff[0]=P1/100;
show_buff[1]=P1/10%10;
show_buff[2]=P1%10;
for(i=0;i<3;i++)
{
P0=0x00;
P2=(~(0x20<<i))&0xF0;
P0=CC_Table[show_buff[i]];
delay_ms(5);
}
}
if(SW2)
{
alarm(100);
alarm(150);
}
}
}
单片机案例10
结合proteus和keil
10 继电器控制照明设备
原理图
程序
#include
#define INT8U unsigned char
#define INT16U unsigned int
sbit K1=P0^0;
sbit RELAY=P2^0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
| | |
|--|--|
| | |
void main()
{
RELAY=1;
while(1)
{
if(K1==0)
{
delay_ms(10);
if(K1==0)
{
while(K1==0);
RELAY=~RELAY;
}
}
}
}