8051单片机Proteus仿真实例3(原理图+C程序)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void main()
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0xB0;
EA = 1;
ET0 = 1;
TR0 = 1;
led=1;
while(1);
}
void timer_0() interrupt 1
{
TH0 = 0x3C;
TL0 = 0xB0;
count++;
if(count==10)
case 1: transmit('B');break;
case 2: transmit('C');break;
}
delay(10);
}
}
void serial() interrupt 4
{
if(RI==1)
{
RI=0;
if(SBUF>=0&&SBUF<=9)
P0=led_code[SBUF];
else P0=0x00;
{
led=!led;
count=0;
}
}
5.定时器中断控制流水灯
原理图:
程序:
#include <reg51.h>
#include <intrins.h>
unsigned char count=0;
void main()
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0xB0;
EA = 1;
buffer[5]=10;
buffer[6]=sec/10;
buffer[7]=sec%10;
}
void read_key()
{
unsigned char x;
P3=0xf0;
x=P3|0x0f;
if(x!=0xff)
{
delay(1000);
P3=0xf0;
x=P3|0x0f;
if(x!=0xff)
{
delay(3000);
read_key();
if(key_press==1)
{
key_press=0;
switch(key_code)
{
case 0x77:hour++;
if(hour>23)
hour=0;
break;
case 0x7b:hour--;
if(hour<0)
hour=23;
break;
TH1=0xfd;
TL1=0xfd;
TR1=1;
RI=0;
IE=0x90;
while(1)
{
key=1;
if(key==0)
{
while(key==0);
operation=(operation+1)%3;
}
switch(operation)
{
case 0: transmit('A');break;
sbit K1=P1^0;
sbit RELAY=P2^4;
void delay(unsigned int x)
{
while(x--);
}
void main()
{
RELAY=1;
while(1)
{
K1=1;
if(K1==0)
{
while(K1==0);
RELAY=~RELAY;
delay(20);
}
}
delay(100);
}
c=(c+1)%26;
if(c%10==0)
{
transmit_string("\r\n");
delay(100);
}
}
}
3.可校时数字钟
原理图:
程序:
#include <reg51.h>
unsigned char code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(unsigned int ms)
{
unsigned char t;
while(ms--)
for(t=0;t<120;t++);
}
void playmusic()
{
unsigned int i=0,j,k;
while(song_tone[i]!=0||song_long[i]!=0)
while(key==0);
}
}
}
void timer_0() interrupt 1
{
doorbell=~doorbell;
p++;
if(p<400)
{
TH0=(8192-700)/32;
1.单片机之间通过串口进行通信
原理图:
程序:
#include <reg51.h>
sbit key=P1^0;
unsigned char code led_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
void delay(long dly)
sbHale Waihona Puke Baidut key=P1^7;
sbit doorbell=P3^7;
unsigned int p=0;
void main()
{
EA=1;
ET0=1;
TMOD=0x00;
TH0=(8192-700)/32;
TL0=(8192-700)%32;
while(1)
{
key=1;
if(key==0)
{
TR0=1;
{
P3=x;
key_code=P3;
key_press=1;
}
}
}
void timer_0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ms++;
if(ms>1000)
{
ms=0;
clock();
}
get_clock();
P0=0x00;
void traffic_light() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
switch(operation_type)
{
case 1: red_a=0;yellow_a=0;green_a=1;
red_b=1;yellow_b=0;green_b=0;
unsigned char code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char buffer[]={0,0,0,0,0,0,0,0};
signed char hour=23,min=59,sec=59;
}
}
2.单片机与主机通信
原理图:
程序:
#include <reg51.h>
void delay(long dly)
{
while(dly--);
}
void transmit(unsigned char c)
{
TI=0;
SBUF=c;
while(TI==0);
TI=1;
}
void transmit_string(unsigned char *s)
ET0 = 1;
TR0 = 1;
P2=0x01;
while(1);
}
void timer_0() interrupt 1
{
TH0 = 0x3C;
TL0 = 0xB0;
count++;
if(count==10)
{
P2=_crol_(P2,1);
count=0;
}
}
6.继电器
原理图:
程序:
#include <reg51.h>
}
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
while(1);
}
8.乐曲
原理图:
程序:
#include <reg51.h>
sbit beep=P3^7;
unsigned char code song_tone[]={212,212,190,212,159,169,212,212,190,212,142,159,
break;
case 4: if(++time_count!=8) return;
time_count=0;
yellow_b=!yellow_b;green_b=0;
if(++flash_count!=10) return;
flash_count=0;
operation_type=1;
break;
}
case 0x7d:min++;
if(min>59)
min=0;
break;
case 0x7e:min--;
if(min<0)
min=59;
break;
case 0xb7:sec=0;
break;
}
}
}
}
4.定时器中断0
原理图:
程序:
#include <reg52.h>
sbit led=P1^0;
unsigned char count=0;
212,212,106,126,159,169,190,119,119,126,159,142,159,0};
unsigned char code song_long[]={10,10,22,22,26,37,10,10,22,22,26,37,
10,10,22,22,22,24,30,10,10,22,22,26,27,0};
{
while(*s!='\0')
{
transmit(*s);
s++;
delay(10);
}
}
void main()
{
unsigned char c=0;
SCON=0x40;
TMOD=0x20;
PCON=0x00;
TH1=0xfd;
TL1=0xfd;
TI=0;
TR1=1;
delay(1000);
transmit_string("Receiving From 8051 ...\r\n");
{
for(j=0;j<song_long[i]*20;j++)
{
beep=~beep;
for(k=0;k<song_tone[i]/3;k++);
}
delay(30);
i++;
}
}
void main()
{
while(1)
{
playmusic();
delay(500);
}
}
9.门铃
原理图:
程序:
#include <reg51.h>
transmit_string("-------------------------------\r\n");
delay(1000);
while(1)
{
transmit(c+'A');
delay(100);
transmit(' ');
delay(100);
if(c==25)
{
transmit_string("\r\n-------------------------------\r\n");
{
while(dly--);
}
void transmit(unsigned char c)
{
TI=0;
SBUF=c;
while(TI==0);
TI=1;
}
void main()
{
unsigned char operation=0xff;
P0=0x00;
SCON=0x50;
TMOD=0x20;
PCON=0x00;
min++;
if(min>59)
{
min=0;
hour++;
if(hour>23)
hour=0;
}
}
}
void get_clock()
{
buffer[0]=hour/10;
buffer[1]=hour%10;
buffer[2]=10;
buffer[3]=min/10;
buffer[4]=min%10;
if(++time_count!=100) return;
time_count=0;
operation_type=2;
break;
case 2: if(++time_count!=8) return;
time_count=0;
yellow_a=!yellow_a;green_a=0;
if(++flash_count!=10) return;
flash_count=0;
operation_type=3;
break;
case 3: red_a=1;yellow_a=0;green_a=0;
red_b=0;yellow_b=0;green_b=1;
if(++time_count!=100) return;
time_count=0;
operation_type=4;
}
7.交通灯
原理图:
程序:
#include <reg51.h>
sbit red_a=P0^0;
sbit yellow_a=P0^1;
sbit green_a=P0^2;
sbit red_b=P0^3;
sbit yellow_b=P0^4;
sbit green_b=P0^5;
unsigned char time_count=0,flash_count=0,operation_type=1;
unsigned int ms=0;
unsigned char count=0;
unsigned char key_code;
bit key_press=0;
void delay(long dly)
{
while(dly--);
}
void clock()
{
sec++;
if(sec>59)
{
sec=0;
P2=wei[count];
P0=duan[buffer[count]];
count++;
if(count>7)
count=0;
}
void main()
{
EA=1;
ET0=1;
TR0=1;
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
while(1)
相关文档
最新文档