根据单片机100案例所做,适合初学者进一步学习单片机的案例.
单片机案例11
结合proteus和keil
11 INT0中断计数器
原理图
程序
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90,
0xFF
};
INT8U show_buffer[]={0,0,0};
INT16U count=0;
sbit clear_key=P3^6;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void refresh_show()
{
show_buffer[0]=count/100;
show_buffer[1]=count%100/10;
show_buffer[2]=count%10;
if(show_buffer[0]==0)
{
show_buffer[0]=10;
if(show_buffer[1]==0)
show_buffer[1]=10;
}
P0=CA_Table[show_buffer[0]];
P1=CA_Table[show_buffer[1]];
P2=CA_Table[show_buffer[2]];
}
void main()
{
P0=0xFF;
P1=0xFF;
P2=0xFF;
IE=0x81;
IT0=1;
while(1)
{
if(clear_key==0)
count=0;
refresh_show();
}
}
void ex_int0() interrupt 0
{
EA=0;
delay_ms(10);
count++;
EA=1;
}
单片机案例12
结合proteus和keil
12 INT0与INT1中断计数
原理图
程序
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
sbit K3=P3^4;
sbit K4=P3^5;
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90,
0xFF
};
code INT8U scan_bits[]=
{
0x20,0x10,
0x08,0x04,
0x02,0x01
};
INT8U show_buffer[]=
{
0,0,0,
0,0,0
};
INT16U count_a=0;
INT16U count_b=0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void refresh_show()
{
INT8U i;
show_buffer[5]=count_a/100;
show_buffer[4]=count_a%100/10;
show_buffer[3]=count_a%10;
if(show_buffer[5]==0)
{
show_buffer[5]=10;
if(show_buffer[4]==0)
show_buffer[4]=10;
}
show_buffer[2]=count_b/100;
show_buffer[1]=count_b%100/10;
show_buffer[0]=count_b%10;
if(show_buffer[2]==0)
{
show_buffer[2]=10;
if(show_buffer[1]==0)
show_buffer[1]=10;
}
for(i=0;i<6;i++)
{
P0=0xFF;
P2=scan_bits[i];
P0=CA_Table[show_buffer[i]];
delay_ms(1);
}
}
void main()
{
IT0=1;
IT1=1;
PX0=1;
IE=0x85;
while(1)
{
if(!K3)
count_a=0;
if(!K4)
count_b=0;
refresh_show();
}
}
void ex_int0() interrupt 0
{
EA=0;
delay_ms(10);
count_a++;
EA=1;
}
void ex_int1() interrupt 2
{
EA=0;
delay_ms(10);
count_b++;
EA=1;
}
单片机案例13
结合proteus和keil
13 TIMER0控制单只LED闪烁
原理图
程序
#include<reg51.h>
#define INT8U unsigned char
#define INT16U unsigned int
INT16U count=0;
void main()
{
TMOD=0x00;
TH0=(8192-5000)>>5;
TL0=(8192-5000)&0x1F;
IE=0x82;
TR0=1;
while(1);
}
void led_flash() interrupt 1
{
TH0=(8192-5000)>>5;
TL0=(8192-5000)&0x1F;
if(++count==50)
{
P0=~P0;
count=0;
}
}
单片机案例14
结合proteus和keil
14 定时器控制数码管显示
原理图
程序
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
#define METHOD1 1
code INT8U CA_Table[]=
{
0xC0,0xF9,
0xA4,0xB0,
0x99,0x92,
0x82,0xF8,
0x80,0x90,
0xBF
};
code INT8U show_date[][8]=
{
{2,2,10,0,2,10,1,4},
{2,3,10,5,9,10,5,9}
};
INT8U i=0;
INT8U j=0;
INT16U t=0;
#if METHOD1
void main()
{
TMOD=0x01;
TH0=(65536-5000)>>8;
TL0=(65536-5000)&0xFF;
IE=0x82;
TR0=1;
while(1);
}
#else
void main()
{
TMOD=0x01;
TH0=(65536-5000)>>8;
TL0=(65536-5000)&0xFF;
IE=0x82;
TR0=1;
while(1)
{
if(TF0)
{
TF0=0;
TH0=(65536-5000)>>8;
TL0=(65536-5000)&0xFF;
P0=0xFF;
P2=~(1<<j);
P0=CA_Table[show_date[i][j]];
j=(j+1)%8;
if(++t!=350)
continue;
t=0;
i=(i+1)%2;
}
}
}
#endif
void seg_show() interrupt 1
{
TH0=(65536-5000)>>8;
TL0=(65536-5000)&0xFF;
P0=0xFF;
P2=~(1<<j);
P0=CA_Table[show_date[i][j]];
j=(j+1)%8;
if(++t!=350)
return;
t=0;
i=(i+1)%2;
}
单片机案例15
结合proteus和keil
15 定时器控制8乘8点阵显示
原理图
程序
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
INT8U code DotMatrix[] =
{
0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00,
0x00,0x00,0x00,0x21,0x7F,0x01,0x00,0x00,
0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00,
0x00,0x22,0x49,0x49,0x49,0x36,0x00,0x00,
0x00,0x0C,0x14,0x24,0x7F,0x04,0x00,0x00,
0x00,0x72,0x51,0x51,0x51,0x4E,0x00,0x00,
0x00,0x3E,0x49,0x49,0x49,0x26,0x00,0x00,
0x00,0x40,0x40,0x40,0x4F,0x70,0x00,0x00,
0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00,
0x00,0x32,0x49,0x49,0x49,0x3E,0x00,0x00
};
void main()
{
TMOD=0x00;
TH0=(8192-2000)>>5;
TL0=(8192-2000)&0x1F;
TR0=1;
IE=0x82;
while(1);
}
void seg_show() interrupt 1
{
static INT8U i=0;
static INT8U num_idx=0;
static INT8U t=0;
TH0=(8192-2000)>>5;
TL0=(8192-2000)&0x1F;
P2=0xFF;
P3=1<<i;
P2=~DotMatrix[num_idx*8+i];
if(++i==8)
i=0;
if(++t==200)
{
t=0x00;
if(++num_idx==10)
num_idx=0;
}
}
单片机案例16
结合proteus和keil
16 TIMER0控制蜂鸣器发出门铃声
原理图
程序
#include<reg51.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
sbit key=P1^7;
sbit doorBell=P3^0;
INT16U p=0;
void delay_ms(INT16U x)
{
INT8U t;
while(x--);
for(t=0;t<120;t++);
}
void main()
{
IE=0x82;
TMOD=0x00;
TH0=(8192-1000)/32;
TL0=(8192-1000)%32;
while(1)
{
if(key==0)
{
delay_ms(10);
if(key==0)
{
while(!key);
TR0=1;
}
}
}
while(1);
}
void timer0() interrupt 1
{
doorBell=~doorBell;
p++;
if(p<400)
{
TH0=(8192-700)/32;
TL0=(8192-700)%32;
}
else if(p<800)
{
TH0=(8192-1000)/32;
TL0=(8192-1000)%32;
}
else
{
TR0=0;
p=0;
}
}
单片机案例17
结合proteus和keil
17 TIMER0控制交通指示灯
原理图
程序
#include<reg52.h>
#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 Time_Count=0;
INT8U Flash_Count=0;
INT8U type=1;
void Timer0() interrupt 1
{
TH0=-50000/256;
TL0=-50000%256;
switch(type)
{
case 1:
{
RED_EW=0;
YELLOW_EW=0;
GREEN_EW=1;
RED_SN=1;
YELLOW_SN=0;
GREEN_SN=0;
if(++Time_Count!=100)
return;
Time_Count=0;
type=2;
break;
}
case 2:
{
if(++Time_Count!=8)
return;
Time_Count=0;
GREEN_EW=0;
YELLOW_EW=~YELLOW_EW;
if(++Flash_Count!=10)
return;
Flash_Count=0;
type=3;
break;
}
case 3:
{
RED_EW=1;
YELLOW_EW=0;
GREEN_EW=0;
RED_SN=0;
YELLOW_SN=0;
GREEN_SN=1;
if(++Time_Count!=100)
return;
Time_Count=0;
type=4;
break;
}
case 4:
{
if(++Time_Count!=8)
return;
Time_Count=0;
YELLOW_SN=~YELLOW_SN;
GREEN_SN=0;
if(++Flash_Count!=10)
return;
Flash_Count=0;
type=1;
}
}
}
void main()
{
TMOD=0x01;
IE=0x82;
TR0=1;
while(1);
}
单片机案例18
结合proteus和keil
18 TIMER1控制音阶演奏
原理图
程序
#include<reg52.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
INT8U i=0;
sbit SPK=P2^0;
sbit K1=P3^6;
code INT8U hight_list[]=
{
0,226,229,232,234,
236,238,240,241,243,
244,245,246,247,248
};
code INT8U lower_list[]=
{
0,4,12,9,20,
2,8,6,2,22,
5,26,1,4,2
};
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void Timer1() interrupt 3
{
SPK=!SPK;
TH1=hight_list[i];
TL1=lower_list[i];
}
void main()
{
IE=0x88;
TMOD=0x00;
while(1)
{
if(!K1)
{
delay_ms(10);
if(!K1)
{
for(i=1;i<15;i++)
{
TR1=1;
delay_ms(500);
TR1=0;
delay_ms(50);
}
}
}
}
}
单片机案例19
结合proteus和keil
19 T0T1T2实现外部计数二进制显示
原理图
程序
#include<reg52.h>
sfr T2MOD =0xC9;
sbit SW1=P1^5;
sbit SW2=P1^6;
sbit SW3=P1^7;
void main()
{
TMOD=0x55;
T2MOD=0x00;
C_T2=1;
RCAP2H=0x00;
RCAP2L=0x00;
TH0=0x00;
TL0=0x00;
TH1=0x00;
TL1=0x00;
TH2=0x00;
TL2=0x00;
TR0=1;
TR1=1;
TR2=1;
while(1)
{
if(!SW1)
{
P2=~TH0;
P0=~TL0;
}
else if(!SW2)
{
P2=~TH1;
P0=~TL1;
}
else if(!SW3)
{
P2=~TH2;
P0=~TL2;
}
}
}
单片机案例20
结合proteus和keil
20 T0T1INT0中断控制报警器与旋转灯
原理图
程序
#include<reg52.h>
#include<intrins.h>
#define INT8U unsigned char
#define INT16U unsigned int
sbit SPK=P3^7;
INT8U FRQ=0x00;
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void main()
{
P2=0x00;
TMOD=0x11;
TH0=0xFE;
TL0=0x00;
IT0=1;
IE=0x8B;
IP=0x01;
TR0=0;
TR1=0;
while(1)
{
FRQ++;
delay_ms(1);
}
}
void ex0_int() interrupt 0
{
TR0=!TR0;
TR1=!TR1;
if(P2==0x00)
P2=0xE0;
else
P2=0x00;
}
void Timer0() interrupt 1
{
TH0=0xFE;
TL0=FRQ;
SPK=~SPK;
}
void Timer1() interrupt 3
{
TH1=-45000>>8;
TL1=-45000&0x1F;
P2=_crol_(P2,1);
}
如果觉得对你有帮助的话,不妨点个赞呗^_^
|