初中生
- 性别
- 女
- 学校
- 山东交通学院
|
//晶振11.0592M 定时器0定时,方式1,25ms常数a600,10ms常数dc00,5ms常数ee00,1ms常数fc66,
//2011年3月7日 应网友要求而写,作者:徐学军
//投票计票系统
//以拨动开关K0~K7作一个8人的投票器,拨成ON的为投了赞成票,
//准备阶段(等待投票)4只数码管循环轮流显示P(跑马),
//投票后,以按中断键完成读票,4个数码管
//齐亮齐灭闪烁亮5次赞成票的票数。再次按中断键,恢复等待投票状态。
#include <absacc.h>
#include <intrins.h>
#include <reg52.h>
sbit P1M1 = 0x91 ;
sbit P1M0 = 0x92 ;
sfr smdis = 0x80;
// sfr P4 =0xC0; //定义P4口,
//sfr P4SW =0xBB; //P4口的设置寄存器
sbit dq=P2^0; //DS18B20的数据接口
//sbit led = P3^7;
sbit led = P1^0;
sbit weishuang0 = P2^6; //数码管第一个位选
sbit weishuang1 = P2^3; //数码管第二个位选
sbit weishuang2 = P2^4; //数码管第三个位选
sbit weishuang3 = P2^5; //数码管第三个位选
sbit pp0 = P1^0; //第一个投票按键
sbit pp1 = P1^1; //第二个投票按键
sbit pp2 = P1^2; //第三个投票按键
sbit pp3 = P1^3; //第四个投票按键
sbit pp4 = P1^4; //第五个投票按键
sbit pp5 = P1^5; //第六个投票按键
sbit pp6 = P1^6; //第七个投票按键
sbit pp7 = P1^7; //第八个投票按键
sbit speaker = P3^6; //第八个投票按键
unsigned char shup ,w;
unsigned char time,time5;
bit flagstart; //投票开始标志
bit flagflash; //投票后闪标志
unsigned char code md[]={0xaf,0xa0,0xcd,0xe9,0xe2,0x6b,0x6f,0xa1,0xef,0xe3,
0xbf,0xb0,0xdd,0xf9,0xf2,0x7b,0x7f,0xb1,0xff,0xf3,0x40,0x00,0xc7};
//0,1,2,3,4,5,6,7,8,9,1.,2.,3.,4.,5.,6.,7.,8.,9., -,灭 ,P;
unsigned char shu0,shu1,shu2,shu3,tc=0;
unsigned char ab[8]={0,0,0,0,0,0,0,0};
//-----变量定义-----
void delayt(unsigned int t) //延时约11us
{
unsigned int i;
unsigned int j;
for(i=t;i>0;i--)
{
for(j=0;j<5;j++);
}
}
/*
void delayta(unsigned char t) //延时约11us
{
unsigned char i;
unsigned int j;
for(i=t;i>0;i--)
{
for(j=0;j<5;j++);
}
}
void delayt(unsigned char t) //延时约7us
{
unsigned char i;
for(i=t;i>0;i--)
{
if(tc==0) { smdis=md[shu0]; weishuang0 = 0; weishuang1 = 1; weishuang2 = 1; }
if(tc==1) { smdis=md[shu1]; weishuang0 = 1; weishuang1 = 0; weishuang2 = 1; }
if(tc==2) { smdis=md[shu2]; weishuang0 = 1; weishuang1 = 1; weishuang2 = 0; }
}
}
*/
void int_t0() interrupt 1
{
TR0 = 0;
time++;
if(time>=200)
{
flagflash=!flagflash;
time=0;
}
tc++;
if(tc>=4) {tc=0;}
TH0 = 0xee;
TL0 = 0x00;
if((!flagstart)&&flagflash&&(time5<5))
{shu0=21; shu1=21; shu2=21; shu3=21;time5++;speaker = 1;}
if(tc==0) { smdis=md[shu0]; weishuang0 = 0; weishuang1 = 1; weishuang2 = 1; weishuang3 = 1;}
else if(tc==1) { smdis=md[shu1]; weishuang0 = 1; weishuang1 = 0; weishuang2 = 1; weishuang3 = 1; }
else if(tc==2) { smdis=md[shu2]; weishuang0 = 1; weishuang1 = 1; weishuang2 = 0; weishuang3 = 1; }
else if(tc==3) { smdis=md[shu3]; weishuang0 = 1; weishuang1 = 1; weishuang2 = 1; weishuang3 = 0;}
TR0 = 1;
}
void int_x0() interrupt 0
{
EX0 = 0;
//delayt(20000);
flagstart = !flagstart;
if(!flagstart)
{ time5 = 0;
shup = 0;
for(w=0;w<8;w++)
{
if(ab[w]>0)
{
shup++;
ab[w]=0 ;
}
}
}
delayt(20000);
EX0 = 1; IE0 = 0;
}
void init_mcu()
{
TMOD=0x01; //定时器0方式1
TH0 = 0xee;//0xa6; //25ms定时常数
TL0 = 0x00;
EA = 1;
ET0 = 1;
EX0 = 1;
IT0 = 1;
P1M1 = 0xff ;
P1M0 = 0x00 ;
PT0=1X0=0;
TR0 = 1;
tc = 0;
// P4SW=0xff; //把P4口设置成I/O口
shu0=shu1=shu2=shu3=0;
}
void main() //主函数
{
unsigned int num;
init_mcu();
flagstart = 1; //投票开始标志
speaker = 0;
while(1)
{
if(flagstart)
{
P1 = 0xff;
if(!pp0) ab[0] = 1;
if(!pp1) ab[1] = 1;
if(!pp2) ab[2] = 1;
if(!pp3) ab[3] = 1;
if(!pp4) ab[4] = 1;
if(!pp5) ab[5] = 1;
if(!pp6) ab[6] = 1;
if(!pp7) ab[7] = 1;
num++;
if(num<=10000)
{
shu0=21; shu1=21; shu2=21; shu3=22;
}
else if((num>10000)&&(num<=20000))
{
shu0=21; shu1=21; shu2=22; shu3=21;
}
else if((num>20000)&&(num<=30000))
{
shu0=21; shu1=22; shu2=21; shu3=21;
}
else if((num>30000)&&(num<=40000))
{
shu0=22; shu1=21; shu2=21; shu3=21;
}
else if((num>40000)&&(num<=50000))
{
shu0=21; shu1=21; shu2=21; shu3=21;
}
else {num = 0;}
}
else
{
//ab[0] = 0; ab[1] = 0; ab[2] = 0; ab[3] = 0; ab[4] = 0; ab[5] = 0; ab[6] = 0; ab[7] = 0;
shu0=21; shu1=21; shu2=21; shu3=shup; speaker = 0;
}
////// ////shu0=0; shu1=1; shu2=2; shu3=3;
}
}
|
|