题目:双路场馆入场人数统计系统
- 要求:一片单片机采集两路计数数据并显示,将计数值发送给另外一片单片机并LED、LCD显示。单路计数值最大10000。
- 提示:增加显示电路方面两者之间的调试。计数可以采用按键扫描(这里用按键代替传感器)、外部中断、计数器均可。
- 仿真原理图如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200620103419341.png)
- 左侧单片机A: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200620104043821.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zhbmd3ZWlqaWV4,size_16,color_FFFFFF,t_70)
-右侧单片机B: ![在这里插入图片描述](https://img-blog.csdnimg.cn/2020062010464982.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zhbmd3ZWlqaWV4,size_16,color_FFFFFF,t_70)
-
单片机A在统计两个地方的人数并将数据同步到单片机B
单片机A程序:
#include <reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
volatile unsigned char dat=0,flag=0;
uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar Num_Buffer[]={0,0,0,0,0};
uint Key_Code,Key_Counts=0;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit C = P1^2;
void DelayMS(uint x){
uchar i;
while(x--)
for(i=0;i<120;i++);
}
void Show_Counts_ON_DSY(){
uint i,j=0x01;
Num_Buffer[4]=Key_Counts/10000;
Num_Buffer[3]=Key_Counts/1000%10;
Num_Buffer[2]=Key_Counts/100%10;
Num_Buffer[1]=Key_Counts/10%10;
Num_Buffer[0]=Key_Counts%10;
for(i=0;i<5;i++){
j=cror_(j,1);
P0=0xff;
P0=DSY_CODE[Num_Buffer[i]];
P2=j;
DelayMS(1);
}
}
void main(){
while(1){
Show_Counts_ON_DSY();
if(key1==0||key2==0){
DelayMS(10);
if(key1==0||key2==0){
Key_Counts++;
C = 0;
if(Key_Counts==1000)
Key_Counts=0;
}
}
while(!key1);
while(!key2);
C=1;
}
while(1){
if(flag==1){
flag=0;
SBUF=Key_Counts;
while(!TI);
TI=0;
}
else;
}
}
void uart (void)interrupt 4{
if(RI)
{
RI=0;
flag=1;
dat=SBUF;
}
}
单片机B的程序:
#include <reg51.h>
#include<intrins.h>
#include <reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
volatile unsigned char dat=0,flag=0;
uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar Num_Buffer[]={0,0,0,0,0};
uint Key_Code,Key_Counts=0;
sbit key1 = P1^0;
void DelayMS(uint x){
uchar i;
while(x--)
for(i=0;i<120;i++);
}
void Show_Counts_ON_DSY(){
uint i,j=0x01;
Num_Buffer[4]=Key_Counts/10000;
Num_Buffer[3]=Key_Counts/1000%10;
Num_Buffer[2]=Key_Counts/100%10;
Num_Buffer[1]=Key_Counts/10%10;
Num_Buffer[0]=Key_Counts%10;
for(i=0;i<5;i++){
j=cror_(j,1);
P0=0xff;
P0=DSY_CODE[Num_Buffer[i]];
P2=j;
DelayMS(1);
}
}
void main(){
while(1){
Show_Counts_ON_DSY();
if(key1==0){
DelayMS(10);
if(key1==0){
Key_Counts++;
if(Key_Counts==1000)
Key_Counts=0;
}
}
while(!key1);
}
while(1){
if(flag==1){
flag=0;
SBUF=Key_Counts;
while(!TI);
TI=0;
}
else;
}
}
void uart (void)interrupt 4{
if(RI)
{
RI=0;
flag=1;
dat=SBUF;
}
}