📄 单片机.c
字号:
#include <reg51.h>
sbit scl=P3^4; //24c02 SCL
sbit sda=P3^5; //24c02 SDA
sbit DOG=P3^3; //24c02 MODE
sbit Min_10=P1^0;
sbit Min_1=P1^1;
sbit Sec_10=P1^2;
sbit Sec_1=P1^3;
sbit Add=P1^6;
sbit Sub=P1^4;
sbit Mode_Btn=P3^2;
sbit MINSEC=P1^5;
sbit ALARM=P1^7;
sbit ALARM1=P3^0;
unsigned char delaycount1=40;
unsigned char delaycount2=40;
unsigned char alarm_time=0;
int Tdata[10]={0,0,0,0,0,0,0,0,0,0};
unsigned char words[11]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xC6};
//unsigned char words[11]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x39};
unsigned char flags[4]={0xFF,0xF7,0xF6,0xB6};
unsigned char flagcount=0;
unsigned char mode=1; //mode=0设置,mode=1记时
unsigned char is_sec=1; //is_sec=0设置秒 ,is_sec=1设置分钟
int min=0;
//unsigned char hour=0;
unsigned char dot1=1;
unsigned char dot2=0;
//unsigned char dot=1;
unsigned char sec=0;
unsigned char totle_min=0;
unsigned char totle_sec=0;
unsigned char last_totle_min=0;
unsigned char last_totle_sec=0;
int count=0;
int tcount=0;
unsigned char enable_btn;
int last_count=0;
unsigned char maohao=0;
int ALARM_TEM=30;
int REAL_TEM=0;
int alarmcount=0;
void Set_Mode();
unsigned char delaytime=0;
delay1(unsigned char x)
{
int i;
for(i=0;i<x;i++);
DOG=~DOG;
}
void flash()
{
unsigned char u;
DOG=~DOG;
}
void x24c02_init()
{
scl=1; flash(); sda=1; flash();
}
void start()
{
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
void stop()
{
sda=0; flash(); scl=1; flash(); sda=1; flash();
}
void writex(unsigned char j)
{
unsigned char i,temp;
temp=j;
for (i=0;i<8;i++){
temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();
}
scl=0; flash(); sda=1; flash();
}
unsigned char readx()
{
unsigned char i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++){
flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j; scl=0;
}
flash(); return(k);
}
void clock()
{
unsigned char i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
unsigned char x24c02_read(unsigned char address)
{
unsigned char i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(15);
return(i);
}
void x24c02_write(unsigned char address,unsigned char info)
{
//EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
//EA=1;
delay1(60);
}
void check_key()
{
unsigned char i;
if(enable_btn){
if(mode==0)
{
if(Add==0){
last_count=count;
for(i=0;i<100;i++);
if(Add==0){
enable_btn=0;
if (is_sec)
{
if(totle_sec<59)
totle_sec++;
}
else
{
if(totle_min<99)
totle_min++;
}
}
}
if(Sub==0){
last_count=count;
for(i=0;i<100;i++);
if(Sub==0){
enable_btn=0;
if (is_sec)
{
if (totle_sec>0)
totle_sec--;
}
else
{
if (totle_min>0)
totle_min--;
}
}
}
if(MINSEC==0){
last_count=count;
for(i=0;i<100;i++);
if(MINSEC==0){
enable_btn=0;
if (is_sec==1)
{
is_sec=0;
dot2=1;
dot1=0;
}
else
{
is_sec=1;
dot1=1;
dot2=0;
}
}
}
}
{
if(Mode_Btn==0){
last_count=count;
for(i=0;i<100;i++);
if(Mode_Btn==0){
enable_btn=0;
mode++;
mode=mode%2;
Set_Mode();
}
}
}
}
if(enable_btn==0){
if((last_count<count)&&(count-last_count>30)) enable_btn=1;
if((last_count>count)&&(count+2000-last_count>30)) enable_btn=1;
}
}
void displayhmset()
{
int temp,i;
temp=totle_min/10;
//Min_10=0;
P0=words[temp];
if(dot2)
{
//delaycount1--;
if(delaycount1<20)
{
Min_10=0;
//if(delaycount1==0)delaycount1=40;
}
}
else Min_10=0;
for (i=0;i<150;i++);
Min_10=1;
temp=totle_min%10;
P0=words[temp];
if(dot2)
{
delaycount1--;
if(delaycount1<20)
{
Min_1=0;
if(delaycount1==0)delaycount1=40;
}
}
else Min_1=0;
for (i=0;i<150;i++);
Min_1=1;
temp=totle_sec/10;
P0=words[temp];
if(dot1)
{
//delaycount2--;
if(delaycount2<20)
{
Sec_10=0;
//if(delaycount2==0)delaycount2=40;
}
}
else Sec_10=0;
//Sec_10=0;
for (i=0;i<150;i++);
Sec_10=1;
temp=totle_sec%10;
P0=words[temp];
if(dot1)
{
delaycount2--;
if(delaycount2<20)
{
Sec_1=0;
if(delaycount2==0)delaycount2=40;
}
}
else Sec_1=0;
for (i=0;i<150;i++);
Sec_1=1;
Sec_10=0;
Sec_1=0;
P0=0x7F;
for (i=0;i<150;i++);
Sec_10=1;
Sec_1=1;
}
void display_timehm()
{
int temp,i;
temp=min/10;
P0=words[temp];
Min_10=0;
for (i=0;i<150;i++);
Min_10=1;
temp=min%10;
P0=words[temp];
Min_1=0;
for (i=0;i<150;i++);
Min_1=1;
temp=sec/10;
P0=words[temp];
Sec_10=0;
for (i=0;i<150;i++);
Sec_10=1;
temp=sec%10;
P0=words[temp];
Sec_1=0;
for (i=0;i<150;i++);
Sec_1=1;
if(maohao){
Sec_10=0;
Sec_1=0;
P0=0x7F;
for (i=0;i<150;i++);
Sec_10=1;
Sec_1=1;
if(ALARM==0){
alarm_time++;
if(alarm_time>6){
ALARM1=~ALARM1;
alarm_time=0;
}
}
}
else for (i=0;i<150;i++);
}
void Set_Mode()
{
int i;
if(mode==0)
{
ALARM=1;
ALARM1=1;
ET1=0;
TR1=0;
displayhmset();
}
else if(mode==1)
{
ALARM=1;
ALARM1=1;
if(last_totle_sec!=totle_sec) {
last_totle_sec=totle_sec;
x24c02_write(1,totle_sec) ;
}
if(last_totle_min!=totle_min){
for (i=0;i<150;i++);
last_totle_min=totle_min;
x24c02_write(2,totle_min) ;
}
sec=totle_sec;
min=totle_min;
ET1=1;
TR1=1;
EA=1;
display_timehm();
}
}
void OnTimer() interrupt 3
{
EA=0;
tcount++;
if(tcount==3245)maohao=1;
else if(tcount>=6491)
{
tcount=0;
/*if(ALARM==0)
delaytime++;
if(delaytime==30){
delaytime=0;
ALARM=1;
ALARM1=1;
}*/
maohao=0;
if(min==0)
{
if (sec==0)
{
ALARM=0;
//min=totle_min;
//sec=totle_sec;
}
else
{
sec--;
}
}
else
{
if (sec>0)
sec--;
else
{
sec=59;
min--;
}
}
}
EA=1;
}
main()
{
unsigned char temp;
mode=0;
P2=0;
P0=0xFF;
P1=0xFF;
//P1=0x00;
P2=0xFF;
DOG=1;
x24c02_init();
temp= x24c02_read(10);
if(temp!=0xEE){
//x24c02_write(10,0xEE);
while(1){;
}
}
totle_sec= x24c02_read(1);
if(totle_sec>59) totle_sec=59;
last_totle_sec=totle_sec;
totle_min= x24c02_read(2);
if(totle_min>99)totle_min=99;
last_totle_min=totle_min;
TMOD=0x20; //计数器0,方式1,需要软件重装初值
TH1=0x72;
TL1=0x72; //6M,110 HZ
TCON=0x80;
IE=0x8D;
PT1=1;
//IT0=1;
//IT1=1;
EA=1;
ALARM=1;
ALARM1=1;
while(1)
{
count++;
if(count>2000) count=0;
check_key();
if (mode==1)
{
display_timehm();
}
else if (mode==0)
{
displayhmset();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -