此模板包括锁存器选择,数码管显示,独立按键,矩阵按键,ne555,pcf8591,eeprom,ds18b20,ds1302。每个模块都能使用,记住此模板轻松拿省奖。
全部文件:提取码:azxy
#include "stc15f2k60s2.h"
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
typedef unsigned char u8;
typedef unsigned int u16;
u8 code duan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
u16 dat;
u16 vv;
u16 count;
u16 count1;
u16 pl;
void delay(u16 i)
{
while(i--);
}
void Chan_hc573(u8 chose)
{
switch(chose)
{
case 4:
P2 = (P2 & 0x1f) | 0x80; break;
case 5:
P2 = (P2 & 0x1f) | 0xa0; break;
case 6:
P2 = (P2 & 0x1f) | 0xc0; break;
case 7:
P2 = (P2 & 0x1f) | 0xe0; break;
}
P2 = (P2 & 0x1f) | 0x00;
}
void Init_system()
{
P0=0x00;
Chan_hc573(5);
P0=0xff;
Chan_hc573(4);
}
void Smg_display(u8 com,u8 dat)
{
P0=0xff;
Chan_hc573(7);
P0=0x01<<com;
Chan_hc573(6);
P0=dat;
Chan_hc573(7);
}
void Close_smg()
{
P0=0x00;
Chan_hc573(6);
P0=0xff;
Chan_hc573(7);
}
void Display_all()
{
Smg_display(0,duan[vv/100%10]); delay(500);
Smg_display(1,duan[vv/10%10]); delay(500);
Smg_display(2,duan[vv%10]); delay(500);
Smg_display(3,duan[10]); delay(500);
Smg_display(4,duan[10]); delay(500);
Smg_display(5,duan[dat/100%10]); delay(500);
Smg_display(6,duan[dat/10%10]+0x80); delay(500);
Smg_display(7,duan[dat%10]); delay(500);
Close_smg();
}
void Display_pl()
{
Smg_display(0,duan[10]); delay(500);
Smg_display(1,duan[10]); delay(500);
Smg_display(2,duan[10]); delay(500);
Smg_display(3,duan[pl/10000%10]); delay(500);
Smg_display(4,duan[pl/1000%10]); delay(500);
Smg_display(5,duan[pl/100%10]); delay(500);
Smg_display(6,duan[pl/10%10]); delay(500);
Smg_display(7,duan[pl%10]); delay(500);
Close_smg();
}
void Key_BTN()
{
if(P30==0)
{
delay(1000);
if(P30==0)
{
while(P30==0) {}
}
}
else if(P31==0)
{
delay(1000);
if(P31==0)
{
while(P31==0) {}
}
}
else if(P32==0)
{
delay(1000);
if(P32==0)
{
while(P32==0) {}
}
}
else if(P33==0)
{
delay(1000);
if(P33==0)
{
while(P33==0) {}
}
}
}
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
void Key_KBD()
{
R1=0;
R2=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
delay(1000);
if(C1==0)
{
while(C1==0) {}
}
}
else if(C2==0)
{
delay(1000);
if(C2==0)
{
while(C2==0) {}
}
}
else if(C3==0)
{
delay(1000);
if(C3==0)
{
while(C3==0) {}
}
}
else if(C4==0)
{
delay(1000);
if(C4==0)
{
while(C4==0) {}
}
}
R2=0;
R1=R3=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
delay(1000);
if(C1==0)
{
while(C1==0) {}
}
}
else if(C2==0)
{
delay(1000);
if(C2==0)
{
while(C2==0) {}
}
}
else if(C3==0)
{
delay(1000);
if(C3==0)
{
while(C3==0) {}
}
}
else if(C4==0)
{
delay(1000);
if(C4==0)
{
while(C4==0) {}
}
}
R3=0;
R2=R1=R4=1;
C1=C2=C3=C4=1;
if(C1==0)
{
delay(1000);
if(C1==0)
{
while(C1==0) {}
}
}
else if(C2==0)
{
delay(1000);
if(C2==0)
{
while(C2==0) {}
}
}
else if(C3==0)
{
delay(1000);
if(C3==0)
{
while(C3==0) {}
}
}
else if(C4==0)
{
delay(1000);
if(C4==0)
{
while(C4==0) {}
}
}
R4=0;
R2=R3=R1=1;
C1=C2=C3=C4=1;
if(C1==0)
{
delay(1000);
if(C1==0)
{
while(C1==0) {}
}
}
else if(C2==0)
{
delay(1000);
if(C2==0)
{
while(C2==0) {}
}
}
else if(C3==0)
{
delay(1000);
if(C3==0)
{
while(C3==0) {}
}
}
else if(C4==0)
{
delay(1000);
if(C4==0)
{
while(C4==0) {}
}
}
}
void Timer0_1Init(void)
{
AUXR |= 0x40;
TMOD = 0x16;
TL0 = 0xff;
TH0 = 0xff;
TL1 = 0xA0;
TH1 = 0x15;
TR0 = 1;
ET0 = 1;
TR1 = 1;
ET1 = 1;
EA = 1;
}
void Service_t0() interrupt 1
{
count++;
}
void Service_t1() interrupt 3
{
count1++;
if(count1==200)
{
pl=count;
count=0;
count1=0;
}
}
u8 Read_PCF8691(u8 addr)
{
u8 dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
void Wire_PCF8691(u8 dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
u8 Read_EEPROM(u8 addr)
{
u8 dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
void Wire_EEPROM(u8 addr,u8 dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
float Read_temp()
{
u8 low,hig;
float temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(300);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
hig=Read_DS18B20();
temp=(hig<<8) | low;
temp=temp*0.625;
return temp;
}
u8 time[3]={0x13,0x14,0x20};
u8 wire_time[3]={0x84,0x82,0x80};
u8 read_time[3]={0x85,0x83,0x81};
void Wire_ds1302()
{
u8 i;
for(i=0;i<3;i++)
{
Write_Ds1302_Byte(wire_time[i],time[i]);
}
}
void Read_ds1302()
{
u8 i;
for(i=0;i<3;i++)
{
time[i]=Read_Ds1302_Byte(wire_time[i]);
}
}
void main()
{
Init_system();
Wire_ds1302();
Wire_EEPROM(0x00,13);
Timer0_1Init();
while(1)
{
Display_pl();
}
}
|