#include <reg52.h> unsigned char Table[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; unsigned char Table_F[] = {0x8e}; sbit SEG1 = P3^7; sbit SEG2 = P3^6; sbit SEG3 = P3^5; sbit SEG4 = P3^4; sbit Irin = P3^2; sbit Irout = P3^3; sbit Key = P2^0; sbit SPK = P1^0; sbit LED = P2^7; unsigned char People = 0; unsigned char ALL = 0; #define uchar unsigned char #define uint unsigned int uint Num = 0; void Timer0Init(void); //50??@11.0592MHz void delay(unsigned int i) { char j; for(i; i > 0; i--) //??6000*200? for(j = 200; j > 0; j--); } void Delay_ms_89xx(unsigned int n_ms) //STC89Cxx ?? @11.0592MHz { unsigned char i, j; for(;n_ms>0;n_ms--) { i = 2; j = 176; do { while (--j); } while (--i); } } void main() { unsigned char count_sta = 0; unsigned char delay_time = 0; // IT0 = 1; //set INT0 int type (1:Falling 0:Low level) // EX0 = 1; //enable INT0 interrupt // EA = 1; //open global interrupt switch LED = 0; while(1) { //???? if(Key == 0) { People = 0; ALL = 0; } //???? if(Irin == 0) { Delay_ms_89xx(50); if(Irin == 0) { People++; ALL++; LED = 1; SPK = 0; while(Irin == 0); Delay_ms_89xx(500);Delay_ms_89xx(500); SPK = 1; LED = 0; } } if(Irout == 0) { Delay_ms_89xx(50); if(Irout == 0) { if(People > 0) { People --; } while(Irout == 0); } } //?? P0 = 0xff;//?? SEG1 = 1; SEG2 = 1; SEG3 = 1; SEG4 = 1; delay(2); SEG1 = 0; P0 = ~Table[ALL/10]; delay(2); P0 = 0xff;//?? SEG1 = 1; SEG2 = 1; SEG3 = 1; SEG4 = 1; delay(2); SEG2 = 0; P0 = ~Table[ALL%10]; delay(2); P0 = 0xff;//?? SEG1 = 1; SEG2 = 1; SEG3 = 1; SEG4 = 1; delay(2); SEG3 = 0; P0 = ~Table[People/10]; delay(2); P0 = 0xff;//?? SEG1 = 1; SEG2 = 1; SEG3 = 1; SEG4 = 1; delay(2); SEG4 = 0; P0 = ~Table[People%10]; delay(2); } } //External interrupt0 service routine void exint0() interrupt 0 //(location at 0003H) { Delay_ms_89xx(50); Num++; }程序逐步分析
时间: 2023-11-04 13:04:34 浏览: 182
[详细完整版]单片机列扫描.txt
这是一个基于STC89C52单片机的人数计数器代码。主要使用了按键、红外传感器、数码管、蜂鸣器、LED灯等元件。通过红外传感器检测人的进出,然后通过数码管显示总人数和进入人数,同时蜂鸣器发出短促的声音,LED灯闪烁表示有人进入。
具体实现:
1. 定义了数码管显示的编码表,分别对应0~9、A~F等字符;
2. 定义了按键、红外传感器、蜂鸣器、LED灯等元件的引脚,以及总人数和进入人数的变量;
3. 实现了延时函数,用于延时等待信号的稳定;
4. 在主函数中,通过检测按键和红外传感器的状态,对总人数和进入人数进行更新;
5. 在主函数中,通过数码管显示函数和延时函数,将总人数和进入人数显示在数码管上;
6. 实现了外部中断服务函数,用于对计数器进行计数。
值得注意的是,这段代码并不完整,其中缺少了一些函数的实现,如Timer0Init()函数,需要自行补充。同时,由于该代码是基于STC89C52单片机的,如果要在其他平台上使用,需要进行相应的修改。
阅读全文