51单片机c语言程序

#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar
sec,min,hour,day,month,year,week,
next,aa,bb,cc,dd,mm,temp0,tmp,irtime,
u1,u2,u3,counter1;
uint u0,counter2,PinLv;
float u;
int temp;
uchar IRcord[4];
uchar irdata[33];
float dis[6];
bit w,irpro_ok,irok;
#define LCD_Data P0
sbit LCD_E=P1^0;
sbit LCD_RW=P1^1;
sbit LCD_RS=P1^2;
sbit SetKey=P1^3;
sbit SureKey=P1^4;
sbit PlusKey=P1^5;
sbit ReduceKey=P1^6;
sbit AD_CS=P2^0;
sbit AD_DIO=P2^1;
sbit AD_CLK=P2^2;
sbit DS1302_CLK=P2^3;
sbit DS1302_DAT=P2^4;
sbit DS1302_RST=P2^5;
sbit DS18B20=P2^6;
sbit WT_SCL=P3^4;
sbit WT_CS=P3^5;
sbit WT_SDA=P3^6;

void EX0_ISR (void) interrupt 0
{
static uchar i;
static bit startflag;
if(startflag)
{
if(irtime<63&&irtime>=33)
i=0;
irdata[i]=irtime;
irtime=0;
i++;
if(i==33)
{
irok=1;
i=0;
}
}
else
{
irtime=0;
startflag=1;
}
}

void time0_isr (void) interrupt 1
{
irtime++;
}

void EX1_PL(void) interrupt 2
{
PinLv++;
}

void time1_PL() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
counter1++;
if(counter1==80)
{
counter2=PinLv;
counter2=(counter2*10)/4;
PinLv=0;
counter1=0;
}
}

void TIM0init(void)
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
TR0=1;
}

void EX0init(void)
{
IT0 = 1;
EX0 = 1;
EA = 1;
}
void PLinit()
{
EA=1;
EX1=1;
ET1=1;
IT1=1;
TMOD=0x12;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR1=1;
}

void Ir_work(void)
{
switch(IRcord[2])
{
case 0x0c:SetKey=0;break;
case 0x18:SureKey=0;break;
case 0x5e:PlusKey=0;break;
case 0x08:ReduceKey=0;break;
default:break;
}
irpro_ok=0;
}

void Ircordpro(void)
{
uchar i, j, k;
uchar cord,value;
k=1;
for(i=0;i<4;i++)
{
for(j=1;j<=8;j++)
{
cord=irdata[k];
if(cord>7)
value|=0x80;
if(j<8)
value>>=1;
k++;
}
IRcord[i]=value;
value=0;
}
irpro_ok=1;
}

void delay(uint a)
{
uchar i;
while(--a)
{
for(i=0;i<125;i++) ;
}
}
void delay1(uint count)
{
while(count--);
}
void delay2(uchar count)
{
uchar i,j,k;
for(k=count;k>0;k--)
for(i=2;i>0;i--)
for(j=248;j>0;j--);
}
void delay3(void)
{
uchar j;
for(j=50;j>0;j--);
}

void yuyin(uchar addr)
{
uchar i;
WT_CS=0;
delay2(5);
for(i=0;i<8;i++)
{
WT_SCL=0;
if(addr & 1)WT_SDA=1;
else WT_SDA=0;
addr>>=1;
delay3();
delay3();
delay3();
WT_SCL=1;
delay3();
delay3();
delay3();
}
WT_CS=1;
}
uchar DS18B20Init()
{
uchar x;
DS18B20=1;
delay1(2);
DS18B20=0;
delay1(80);
DS18B20=1;
delay1(5);
x=DS18B20;
delay1(20);
return x;
}
uchar TempRead(void)
{
uchar i,dat;
DS18B20=1;
delay1(1);
for(i=0;i<8;i++)
{
DS18B20=0;
dat=dat>>1;
DS18B20=1;
if(DS18B20)
dat=dat|0x80;
delay1(4);
}
return dat;
}
voi

d TempWriteByte(uchar dat)
{
uint i;
DS18B20=1;
delay1(2);
for(i=0;i<8;i++)
{
DS18B20=0;
DS18B20=dat&0x01;
delay1(8);
DS18B20=1;
dat=dat>>1;
delay1(2);
}
}
int GetTemp()
{
float tt;
uchar a,b;
DS18B20Init();
TempWriteByte(0xcc);
TempWriteByte(0x44);
delay1(100);
DS18B20Init();
TempWriteByte(0xcc);
TempWriteByte(0xbe);
delay1(200);
a=TempRead();
b=TempRead();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void WriteDs1302Byte(uchar temp)
{
uchar i;
for (i=8;i>0;i--)
{
DS1302_DAT=temp&0x01;
DS1302_CLK=0;
DS1302_CLK=1;
temp>>=1;
}
}
void WriteDs1302( uchar address,uchar dat )
{
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
delay(1);
WriteDs1302Byte(address);
WriteDs1302Byte(dat);
DS1302_RST=0;
}
uchar read_byte()
{
uchar i;
for(i=8;i>0;i--)
{
if(DS1302_DAT)
temp0=temp0|0x80;
DS1302_CLK=1;
DS1302_CLK=0;
temp0=temp0>>1;
}
return temp0;
}
uchar ReadDs1302(uchar address)
{
uchar temp;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
WriteDs1302Byte(address);
temp=read_byte();
DS1302_RST=0;
DS1302_CLK=1;
return temp;
}
void InitDS1302()
{
uchar
Second=ReadDs1302(0x80);
if(Second&0x80)
{
WriteDs1302(0x8e,0x00);
delay(5);
WriteDs1302(0x8c,0x10);
delay(5);
WriteDs1302(0x8a,0x06);
delay(5);
WriteDs1302(0x88,0x11);
delay(5);
WriteDs1302(0x86,0x6);
delay(5);
WriteDs1302(0x84,0x17);
delay(5);
WriteDs1302(0x82,0x43);
delay(5);
WriteDs1302(0x80,0x00);
delay(5);
WriteDs1302(0x8e,0x80);
}
}
void WriteDataLCD(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
delay(2);
LCD_Data=dat;
delay(2);
LCD_E=1;
delay(5);
LCD_E=0;
delay(5);
}
void WriteCommandLCD(uchar udat)
{
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
delay(2);
LCD_Data=udat;
delay(2);
LCD_E=1;
delay(5);
LCD_E=0;
delay(5);
}
void LCDInit(void)
{
WriteCommandLCD(0x30);
WriteCommandLCD(0x01);
WriteCommandLCD(0x06);
WriteCommandLCD(0x0C);
}
void LCDClear(void)
{
WriteCommandLCD(0x01);
WriteCommandLCD(0x34);
WriteCommandLCD(0x30);
}
uchar A_D()
{
uchar i ,dat;
AD_CS=1;
AD_CLK=0;
AD_CS=0;
AD_DIO=1;
AD_CLK=1; AD_CLK=1;
AD_CLK=0;
AD_DIO=1;
AD_CLK=1; AD_CLK=1;
AD_CLK=0;
AD_DIO=0;
AD_CLK=1; AD_CLK=1;
AD_CLK=0;
AD_DIO=1;
for(i=0;i<8;i++)
{
AD_CLK=1; AD_CLK=1;
AD_CLK=0;
dat<<=1;
dat|=(uchar)AD_DIO;
}
AD_CS=1;
return dat;
}
void LCDSendWord(uchar *p)
{
while(*p>0)
{
WriteDataLCD(*p);
p++;
}
}
void LCDTestWord(bit i,uchar word)
{
if(i==0)
{
WriteCommandLCD(word);
}
else
{
WriteDataLCD(word);
}
}
void DisplayYear(void)
{
year=ReadDs1302(0x8d);
LCDTestWord(0,0x81);
LCDTestWord(1,(year/16)+0x30);
LCDTestWord(1,year%16+0x30);
LCDTestWord(0,0x82);
LCDSendWord("年");
}
void DisplayMont

h(void)
{
month=ReadDs1302(0x89);
LCDTestWord(0,0x83);
if(month/16!=0)
{
LCDTestWord(1,(month/16)+0x30);
}
else
{
LCDTestWord(1,0x20);
}
LCDTestWord(1,month%16+0x30);
LCDTestWord(0,0x84);
LCDSendWord("月");
}
void DisplayWeek(void)
{
week=ReadDs1302(0x8b);
LCDTestWord(0,0x90);
LCDSendWord("星期");
LCDTestWord(0,0x92);// LCDTestWord(1,week+0x30);
if(week==7) {LCDSendWord("日");}
if(week==6) {LCDSendWord("六");}
if(week==5) {LCDSendWord("五");}
if(week==4) {LCDSendWord("四");}
if(week==3) {LCDSendWord("叁");}
if(week==2) {LCDSendWord("二");}
if(week==1) {LCDSendWord("一");}
}
void DisplayDay(void)
{
day=ReadDs1302(0x87);
LCDTestWord(0,0x85);
if(day/16!=0)
{ LCDTestWord(1,(day/16)+0x30); }
else
{ LCDTestWord(1,0x20);}
LCDTestWord(1,day%16+0x30);
LCDTestWord(0,0x86);
LCDSendWord("日");
}
void DisplayHour(void)
{
hour=ReadDs1302(0x85);
LCDTestWord(0,0x88);
LCDTestWord(1,(hour/16)+0x30);
LCDTestWord(1,hour%16+0x30);
}
void DisplayMin(void)
{
min=ReadDs1302(0x83);
LCDTestWord(0,0x89);
LCDSendWord("点");
LCDTestWord(1,(min/16)+0x30);
LCDTestWord(1,min%16+0x30);
LCDSendWord("分");
}
void DisplaySec(void)
{
uchar i=0;
uint a=0,b=0,c=0;
sec=ReadDs1302(0x81);
LCDTestWord(0,0x8c);
LCDTestWord(1,(sec/16)+0x30);
LCDTestWord(1,sec%16+0x30);
LCDSendWord("秒");
}
void DisplayTemp(void)
{
uint i;
uchar a,b,c;
WriteCommandLCD(0x8c);
LCDTestWord(0,0x93);
LCDSendWord(" ");
i=GetTemp();
a=i/100;
LCDTestWord(1,a+0x30);
b=i%100/10;
LCDTestWord(1,b+0x30);
LCDTestWord(1,0x2e);
c=i-a*100-b*10;
LCDTestWord(1,c+0x30);
LCDSendWord("℃");
}

void UpDate(void)
{
DisplayYear();
DisplayMonth();
DisplayDay();
DisplayWeek();
DisplayHour();
DisplayMin();
DisplaySec();
}
void SetTime(uchar count)
{
char address,item;
uchar max,mini;
if(count<8)
{
LCDTestWord(0,0x98);
if(count==7)
{
LCDSendWord("调整秒钟请按+ - ");
address=0x81;max=59;mini=0;
}
if(count==2)
{
LCDSendWord("调整分钟请按+ - ");
address=0x83;max=59;mini=0;
}
if(count==1)
{
LCDSendWord("调整小时请按+ - ");
address=0x85;max=23;mini=0;
}
if(count==5)
{
LCDSendWord("调整星期请按+ - ");
address=0x8b;max=7;mini=1;
}
if(count==3)
{
LCDSendWord("调整日期请按+ - ");
address=0x87; max=31;mini=1;
}
if(count==4)
{
LCDSendWord("调整月份请按+ - ");
address=0x89;max=12;mini=1;
}
if(count==6)
{
LCDSendWord("调整年份请按+ - ");
address=0x8d;max=99;mini=0;
}
item=ReadDs1302(address);
item=(item/16)*10+item%16;
if(PlusKey==0)
item++;
if(ReduceKey==0)
item--;
if(item>max)
item=mini;
if(itemitem=max;
WriteDs1302(0x8e,0x00);
item=(item/10)*16+item%10;
WriteDs1302(address-1,item);
UpDate();
}
else
{
if(count==8)
{

LCDTestWord(0,0x01);
WriteCommandLCD(0x0c);
aa=ReadDs1302(0xc1);
max=99;mini=0;
if(PlusKey==0)
aa++;
if(ReduceKey==0)
aa--;
if(aa>max)aa=mini;
if(aaWriteDs1302(0x8e,0x00);
WriteDs1302(0xc0,aa);
WriteDs1302(0x8e,0x80);
LCDTestWord(0,0x80) ;
LCDSendWord("请输入温度报警上");
LCDTestWord(0,0x90);
LCDSendWord("限:");
LCDTestWord(0,0x92);
LCDTestWord(1,aa/10+0x30);
LCDTestWord(1,aa%10+0x30);
LCDSendWord("度");
}
if(count==9)
{
WriteCommandLCD(0x0c);
bb=ReadDs1302(0xc3);
mini=0;max=99;
if(PlusKey==0)
bb++;
if(ReduceKey==0)
bb--;
if(bb>max)
bb=mini;
if(bbbb=max;
WriteDs1302(0x8e,0x00);
WriteDs1302(0xc2,bb);
WriteDs1302(0x8e,0x80);
LCDTestWord(0,0x88);
LCDSendWord("请输入温度报警下限:");
LCDTestWord(0,0x98);
LCDSendWord("限:");
LCDTestWord(0,0x9a);
LCDTestWord(1,bb/10+0x30);
LCDTestWord(1,bb%10+0x30);
LCDSendWord("度");
}
if(count==10)
{
cc=ReadDs1302(0xc5);
LCDTestWord(0,0x01);
WriteCommandLCD(0x0c);
LCDTestWord(0,0x92);
if(PlusKey==0)
cc++;
if(ReduceKey==0)
cc--;
if(cc>max)
cc=mini;
if(cccc=max;
WriteDs1302(0x8e,0x00);
WriteDs1302(0xc4,cc);
WriteDs1302(0x8e,0x80);
LCDTestWord(0,0x80);
LCDSendWord("请设置闹钟");
LCDTestWord(0,0x93);
LCDSendWord("点");
LCDTestWord(0,0x95);
LCDSendWord("分");
LCDTestWord(0,0x92);
LCDTestWord(1,cc/10+0x30);
LCDTestWord(1,cc%10+0x30);
LCDTestWord(0,0x94);
LCDTestWord(1,dd/10+0x30);
LCDTestWord(1,dd%10+0x30);
LCDTestWord(0,0x88);
LCDSendWord("闹钟:");
max=23;mini=0;
LCDTestWord(0,0x98);
LCDSendWord("设置闹钟小时位");
}
if(count==11)
{
dd=ReadDs1302(0xc7);
if(PlusKey==0)
dd++;
if(ReduceKey==0)
dd--;
if(dd>max)
dd=mini;
if(dddd=max;
WriteDs1302(0x8e,0x00);
WriteDs1302(0xc6,dd);
WriteDs1302(0x8e,0x80);
LCDTestWord(0,0x94);
LCDTestWord(1,dd/10+0x30);
LCDTestWord(1,dd%10+0x30);
LCDTestWord(0,0x98);
max=59;mini=0;
LCDSendWord("设置闹钟分钟位");
}
if(count==12)
{
LCDTestWord(0,0x98);
LCDSendWord("设置闹钟状态");
if(PlusKey==0)
mm=1;
if(ReduceKey==0)
mm=0;
WriteDs1302(0x8e,0x00);
WriteDs1302(0xc8,mm);
WriteDs1302(0x8e,0x80);
if(mm==1)
{LCDTestWord(0,0x8b);LCDSendWord("开");}
if(mm==0)
{LCDTestWord(0,0x8b);LCDSendWord("关");}
}
}
if(count==13)
{
LCDTestWord(0,0x01);
WriteCommandLCD(0x0c);
LCDInit();
LCDClear();
LCDTestWord(0,0x80);
LCDSendWord("测电压:");
LCDTestWord(0,0x95);
LCDSendWord("V");
u=A_D()*5.00/256;
u0=u*100;
u1=u0/100;
u2=(u0-u1*100)/10;
u3=(u0-u1*100)%10;
LCDTestWord(0,0x93);
LCDTestWord(1,u

1+0x30);
LCDSendWord (".");
LCDTestWord(0,0x94);
LCDTestWord(1,u2+0x30);
LCDTestWord(1,u3+0x30);
dis[2]=counter2/100;
dis[1]=(counter2%100)/10;
dis[0]=(counter2%100)%10;
LCDTestWord(0,0x88);
LCDSendWord("测频率:");
LCDTestWord(0,0x9b);
LCDTestWord(1,dis[2]+0x30);
LCDTestWord(1,dis[1]+0x30);
LCDSendWord(".");
LCDTestWord(1,dis[0]+0x30);
LCDSendWord("Hz");
}
}
void Key(void)
{
if(irok)
{
Ircordpro();
irok=0;
}
if(irpro_ok)
Ir_work();
if (SetKey==0)
{
delay(50);
if(SetKey==0&&w==0)
{
w=1;
SetTime(next);
}
if(SetKey==0&&w==1)
{
next++;
if(next==14)
{
next=0;
WriteCommandLCD(0x0c);
LCDClear();
}
SetTime(next);
}
SetKey=1;
while(SetKey==0);
}
switch(next)
{
case 1: WriteCommandLCD(0x0f);
LCDTestWord(0,0x88);break;
case 2: WriteCommandLCD(0x0f);
LCDTestWord(0,0x8a);break;
case 3: WriteCommandLCD(0x0f);
LCDTestWord(0,0x85);break;
case 4: WriteCommandLCD(0x0f);
LCDTestWord(0,0x83);break;
case 5: WriteCommandLCD(0x0f);
LCDTestWord(0,0x92);break;
case 6: WriteCommandLCD(0x0f);
LCDTestWord(0,0x81);break;
case 7: WriteCommandLCD(0x0f);
LCDTestWord(0,0x8c);break;
case 8: WriteCommandLCD(0x0f);
LCDTestWord(0,0x92);break;
case 9: WriteCommandLCD(0x0f);
LCDTestWord(0,0x9a);break;
case 10: WriteCommandLCD(0x0f);
LCDTestWord(0,0x92);break;
case 11: WriteCommandLCD(0x0f);
LCDTestWord(0,0x94);break;
case 12: WriteCommandLCD(0x0f);
LCDTestWord(0,0x8b);break;
case 13: WriteCommandLCD(0x0c);
LCDTestWord(0,0x90);break;
}
if(SureKey==0)
{
delay(50);
if(SureKey==0&&w==1)
{
w=0;
next=0;
LCDClear();
}
SureKey=1;
while(SureKey==0);
}
if (PlusKey==0)
{
delay(50);
if(PlusKey==0&&w==1)
SetTime(next);
PlusKey=1;
while(PlusKey==0);
}
if (ReduceKey==0)
{
delay(50);
if(ReduceKey==0&&w==1)
SetTime(next);
ReduceKey=1;
while(ReduceKey==0);
}
}
void beep()
{
int i;
uchar hour1,min1,high,low;
hour1=ReadDs1302(0xc5);
min1=ReadDs1302(0xc7);
high=ReadDs1302(0xc1);
low=ReadDs1302(0xc3);
mm=ReadDs1302(0xc9);
hour=ReadDs1302(0x85)/16*10+ReadDs1302(0x85)%16;
min=ReadDs1302(0x83)/16*10+ReadDs1302(0x83)%16;
i=GetTemp()/10.0;//tt=1;
if(mm==1)
{
LCDTestWord(0,0x87);
WriteDataLCD(0x0e);
WriteDataLCD(0x0e);
}
if(mm==0)
{
LCDTestWord(0,0x87);
WriteDataLCD(0x01);
WriteDataLCD(0x01);
}
if((hour==hour1&&min==min1)&&mm==1)
{
yuyin(0x01);
delay2(1000);
delay2(1000);
delay2(1000);
}
if((i+1>high||i{
yuyin(0x02);
delay2(1000);
delay2(1000);
delay2(1000);
delay2(1000);
}
}
void main()
{
EX0init();
TIM0init();
PLinit();
delay1(10);
SetKey=1;
SureKey=1;
PlusKey=1;
ReduceKey=1;
next=0;
delay1(100);
LCDInit();
LCDClear();
InitDS1302();
LCDTest

Word(0,0x98);
LCDSendWord("欢迎您来指导工作");
yuyin(0x00);
delay2(1000);
while(1)
{
while(!next)
{
Key();
WriteCommandLCD(0x0c);
DisplayYear();
DisplayMonth();
DisplayDay();
DisplayWeek();
DisplayHour();
DisplayMin();
DisplaySec();zs
DisplayTemp();
beep();
}
Key();
}
}

相关文档
最新文档